Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
 - Remove boilerplate code by introducing a utility function
 - Fix a bug related to how the release 'published_at' date is parsed
  • Loading branch information
kaffein committed Nov 1, 2018
1 parent 835ec8e commit abcd880
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions src/yetibot/commands/github.clj
Expand Up @@ -137,36 +137,32 @@
(f/unparse date-formatter datetime))))
(str n " is not a number"))))

(defn format-release
"Displays information about a release"
[release org-name repo]
(if (nil? (:status release))
(let [tag (:tag_name release)
author (get-in release [:author :login])
published-at (-> "YYYY-MM-dd'T'HH:mm:ssZ"
(f/formatter)
(f/parse (:published_at release)))]
(format "Release version, tagged: %s from %s/%s, was published on %s by %s" tag org-name repo
(f/unparse date-hour-formatter published-at) author))
(format "No release version info found for %s/%s" org-name repo)))

(defn show-latest-release-info-cmd
"gh releases show <org>/<repo-name> # retrieve info about the latest release on a Github repository"
{:yb/cat #{:util :info}}
[{[_ org-name repo] :match}]
(let [latest-release (gh/latest-releases org-name repo)
status (:status latest-release)]
(if (nil? status)
(let [tag (:tag_name latest-release)
author (get-in latest-release [:author :login])
published-at (:published_at (-> "YYYY-MM-dd'T'HH:mm:ssZ"
(f/formatter)
(f/parse (:published_at latest-release))))]
(format "%s/%s latest version, tagged: %s, was published on %s by %s" org-name repo tag
(f/unparse date-hour-formatter published-at) author))
(format "No release version info found for %s/%s" org-name repo))))
(if-let [release (gh/latest-releases org-name repo)]
(format-release release org-name repo)))

(defn show-release-info-by-tag-cmd
"gh releases show <org>/<repo-name> <tag> # retrieve info about a specific release tag on a Github repository"
{:yb/cat #{:util :info}}
[{[_ org-name repo tag] :match}]
(let [tagged-release (gh/release-by-tag org-name repo tag)
status (:status tagged-release)]
(if (nil? status)
(let [author (get-in tagged-release [:author :login])
published-at (:published_at (-> "YYYY-MM-dd'T'HH:mm:ssZ"
(f/formatter)
(f/parse (:published_at tagged-release))))]
(format "Release tag: %s from %s/%s, was published on %s by %s" tag org-name repo
(f/unparse date-hour-formatter published-at) author))
(format "No release version info found for tag %s on %s/%s" tag org-name repo))))
(if-let [release (gh/release-by-tag org-name repo tag)]
(format-release release org-name repo)))

(defn list-releases-info-cmd
"gh releases <org>/<repo-name> # list releases for a Github repository"
Expand All @@ -176,9 +172,9 @@
(for [release releases]
(let [tag (:tag_name release)
author (get-in release [:author :login])
published-at (:published_at (-> "YYYY-MM-dd'T'HH:mm:ssZ"
(f/formatter)
(f/parse (:published_at release))))]
published-at (-> "YYYY-MM-dd'T'HH:mm:ssZ"
(f/formatter)
(f/parse (:published_at release)))]
(format "Release version tagged: %s, from %s/%s, was published on %s by %s" tag org-name repo
(f/unparse date-hour-formatter published-at) author)))))

Expand Down

0 comments on commit abcd880

Please sign in to comment.