-
Notifications
You must be signed in to change notification settings - Fork 65
/
github.clj
201 lines (179 loc) 路 7.65 KB
/
github.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
(ns yetibot.commands.github
(:require
[taoensso.timbre :refer [info warn error]]
[yetibot.api.github :as gh]
[clojure.string :as s]
[yetibot.core.util.http :refer [get-json]]
[yetibot.core.hooks :refer [cmd-hook]]
[taoensso.timbre :refer [info]]
[inflections.core :refer [plural]]
[clj-time [core :refer [ago minutes hours days weeks years months]]]
[clj-time.coerce :as c]
[clj-time.format :as f]
[robert.bruce :refer [try-try-again] :as rb]))
(def date-formatter (f/formatters :date))
(def date-hour-formatter (f/formatter "MMM d, yyyy 'at' hh:mm"))
(defn feed
"gh feed <org-name> # list recent activity for <org-name>"
[{[_ org-name] :match}] (gh/formatted-events org-name))
(defn repos
"gh repos # list all known repos
gh repos <org-name> # list repos under <org-name>"
[{match :match}]
(if (sequential? match)
(let [[_ org-name] match]
(map #(format "%s/%s" org-name (:name %)) (gh/repos org-name)))
(mapcat
(fn [[org-name repos]]
(map #(format "%s/%s" org-name (:name %)) repos))
(gh/repos-by-org))))
(defn repos-urls
"gh repos urls # list the ssh urls of all repos"
[{match :match}]
(if (sequential? match)
(let [[_ org-name] match]
(map :ssh_url (gh/repos org-name)))
(mapcat
(fn [[org-name repos]]
(map :ssh_url repos))
(gh/repos-by-org))))
(defn orgs
"gh orgs # show configured orgs"
[_] (gh/org-names))
(defn tags
"gh tags <org-name>/<repo> # list the tags for <org-name>/<repo>"
[{[_ org-name repo] :match}]
(map :name (gh/tags org-name repo)))
(defn branches
"gh branches <org-name>/<repo> # list branches for <org-name>/<repo>"
[{[_ org-name repo] :match}]
(map :name (gh/branches org-name repo)))
(defn- fmt-status [st] ((juxt :status :body :created_on) st))
(defn status
"gh status # show GitHub's current system status"
[_] (fmt-status (get-json "https://status.github.com/api/last-message.json")))
(defn statuses
"gh statuses # show all recent GitHub system status messages"
[_] (interleave
(map fmt-status (get-json "https://status.github.com/api/messages.json"))
(repeat ["--"])))
(defn pull-requests
"gh pr <org-name> # list open pull requests for <org-name>"
[{[_ org-name] :match}]
(let [prs (gh/search-pull-requests org-name "" {:state "open"})]
(->> prs
:items
(map (fn [pr]
(s/join " "
(remove nil?
[(format "[%s]" (-> pr :user :login))
(when-let [a (:assignee pr)]
(format "[assignee: %s]" (:login a)))
(:title pr)
(-> pr :pull_request :html_url)])))))))
(defn notify-add-cmd
"gh notify add <org>/<repo-name> # sets up notifications to this channel on pushes to <repo-name>"
[{[_ repo] :match chat-source :chat-source}]
"Not yet implemented")
(defn notify-list-cmd
"gh notify list # lists repos which are configured to post to this channel on push"
[{:keys [chat-source]}]
"Not yet implemented")
(defn notify-remove-cmd
"gh notify remove <org>/<repo-name> # removes notifications to this channel for <repo-name>"
[{:keys [chat-source]}]
"Not yet implemented")
(defn stats-cmd
"gh stats <org>/<repo-name> # commits, additions, deletions"
[{[_ org-name repo] :match}]
;; github might need some time to crunch the stats,
;; in which case the result will simply be polled
(try-try-again
{:decay 1.5 :sleep 2000 :tries 4 :return? :truthy?}
(fn []
(let [stats (gh/sum-stats org-name repo)]
(cond (map? stats)
(let [{:keys [a d c con]} stats]
(format
"%s/%s: %s commits, %s additions, %s deletions, %s contributors"
org-name repo c a d con))
rb/*last-try*
(format
"Crunching the latest data for `%s/%s`, try again in a few moments 馃悓"
org-name repo))))))
(defn contributors-since-cmd
"gh contributors <org>/<repo-name> since <n> <minutes|hours|days|weeks|months> ago # list contributors in order of commits since a given time"
{:yb/cat #{:util :info}}
[{[_ org-name repo n unit] :match chat-source :chat-source}]
(let [unit (plural unit) ; pluralize if singular
unit-fn (ns-resolve 'clj-time.core (symbol unit))
n (read-string n)]
(if (number? n)
(let [datetime (-> n unit-fn ago)
ts (c/to-long datetime)
sorted-contribs (gh/contributors-since-ts org-name repo ts)]
(conj
(map #(format "%s: %s commits, %s additions, %s deletions"
(:author %) (:c %) (:a %) (:d %))
sorted-contribs)
(format "Contributions on %s/%s since %s" org-name repo
(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}]
(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}]
(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"
{:yb/cat #{:util :info}}
[{[_ org-name repo] :match}]
(let [releases (gh/releases org-name repo)]
(for [release releases]
(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)))))
(when (gh/configured?)
(cmd-hook ["gh" #"^gh|github$"]
#"feed\s+(\S+)" feed
#"repos urls\s+(\S+)" repos-urls
#"repos urls" repos-urls
#"repos\s+(\S+)" repos
#"repos" repos
#"notify\s+list" notify-list-cmd
#"notify\s+add\s+(\S+)" notify-add-cmd
#"notify\s+remove\s+(\S+)" notify-remove-cmd
#"orgs" orgs
#"statuses" statuses
#"status$" status
#"pr\s+(\S+)" pull-requests
#"stats\s+(\S+)\/(\S+)" stats-cmd
#"contributors\s+(\S+)\/(\S+)\s+since\s+(\d+)\s+(minutes*|hours*|days*|weeks*|months*)" contributors-since-cmd
#"tags\s+(\S+)\/(\S+)" tags
#"branches\s+(\S+)\/(\S+)" branches
#"releases\s+show\s+(\S+)\/(\S+)\s+(\S+)" show-release-info-by-tag-cmd
#"releases\s+show\s+(\S+)\/(\S+)" show-latest-release-info-cmd
#"releases\s+(\S+)\/(\S+)" list-releases-info-cmd))