This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
/
pr_all.coffee
74 lines (63 loc) · 2.08 KB
/
pr_all.coffee
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
# Description:
# This module handles the `pr orgname/repo all` command. The two main
# functions are fetching the PR information for all open PRs, and then
# listing out open PRs against each user.
Octokat = require 'octokat'
_ = require 'underscore'
Q = require 'q'
class PrAll
# For some reason, calling @fetchAllPrs() in the constructor doesn't seem to
# work, where fetchAllPrs()'s functionality is to populate the @allPrs
# variable
constructor: (@org, @repo) ->
github = new Octokat(token: process.env.GH_AUTH_TOKEN)
repo = github.repos(@org, @repo)
@allPrs =
repo.pulls.fetch({status: "open"}).then (prs) =>
Q.all _.map(prs, (pr) => repo.pulls(pr.number).fetch())
mergeablePrs: (prs) ->
_.filter(prs, (pr) -> pr.mergeable == true)
unMergeablePrs: (prs) ->
_.filter(prs, (pr) -> pr.mergeable == false)
prsGroupedByUser: (prs) ->
_.groupBy(prs, (pr) -> pr.user.login)
# Returns a string in the following format:
#
# Summary of all open PRs:
#
# 11 open PRs
#
# 6 by user1
# 2 by user2
# 3 by user3
#
# 10 mergeable
# 1 unmergeable
#
# Run `pr conflicts` to know details about unmergeable pulls
generateSummary: ->
@allPrs.then (prs) =>
if prs.length > 0
mergeablePrCount = @mergeablePrs(prs).length
unMergeablePrCount = @unMergeablePrs(prs).length
stats = "Summary of all open PRs\n\n"
stats += "#{prs.length} open PRs\n"
stats += "\n"
_.each(
@prsGroupedByUser(prs),
(prs, user) =>
linksToPrs = _.map(
prs,
(pr) -> "<#{pr.Links.html.href}|##{pr.number}>"
)
stats += "#{prs.length} by #{user}: #{linksToPrs.join(", ")}\n"
)
stats += "\n"
stats += "#{mergeablePrCount} mergeable\n"
stats += "#{unMergeablePrCount} unmergeable\n"
stats += "\n"
stats += "Run `@bot pr conflicts` to know details about unmergeable pulls"
stats += "\n"
else
stats = "No open PRs :tada:"
module.exports = PrAll