UNPKG

3.79 kBtext/coffeescriptView Raw
1Configuration = require './configuration'
2Aliases = Configuration.Nconf.get('aliases')
3Changelog = Configuration.Nconf.get('changelog')
4
5_ = require('underscore')._
6_.str = require 'underscore.string'
7_.mixin _.str.exports()
8
9Github = Configuration.Github
10Async = require 'async'
11Moment = require 'moment'
12
13Utils = require './utils'
14GhHelpers = require './gh_helpers'
15
16changelog = (fallback, repo, keyword, filter, period, save) ->
17 repo = Aliases[repo] || repo
18 org = Github.Org
19
20 # First we verify if the first argument is an URL
21 match = GhHelpers.githubPRUrlMatching repo
22 if (match?)
23 org = match[0].org
24 repo = match[0].repo
25 filter = match[0].number
26 keyword = 'pr'
27
28 if (keyword == 'pr')
29 forPullRequests(fallback, org, repo, filter, period, save)
30
31 if (keyword == 'since')
32 forSince(fallback, org, repo, filter, period, save)
33
34 if (keyword == 'between')
35 forBetween(fallback, org, repo, filter, period, save)
36
37forPullRequests = (fallback, org, repo, filter, period, save) ->
38 Github.Api.pullRequests.getCommits {
39 user: org
40 repo: repo
41 number: filter
42 }, (error, commits) ->
43 if (error?)
44 Utils.fallback_printError(fallback, error)
45 else
46 display(fallback, org, repo, commits, period)
47
48forSince = (fallback, org, repo, filter, period, save) ->
49 Github.Api.repos.getCommits {
50 user: org
51 repo: repo
52 since: Moment().subtract(period, filter).format()
53 }, (error, commits) ->
54 if (error?)
55 Utils.fallback_printError(fallback, error)
56 else
57 display(fallback, org, repo, commits, save)
58
59forBetween = (fallback, org, repo, filter, period, save) ->
60 if (_.str.include filter, "...")
61 first = _.first filter.split("...")
62 last = _.last filter.split("...")
63 save = period
64 else if (_.str.include filter, "..")
65 first = _.first filter.split("..")
66 last = _.last filter.split("..")
67 save = period
68 else
69 first = filter
70 last = period
71
72 Github.Api.repos.compareCommits {
73 user: org
74 repo: repo
75 base: first
76 head: last
77 }, (error, diff) ->
78 if (error?)
79 Utils.fallback_printError(fallback, error)
80 else
81 display(fallback, org, repo, diff.commits, save)
82
83display = (fallback, org, repo, commits, save) ->
84 Async.map commits, (commit, callback) ->
85 Github.Api.statuses.get {
86 user: org,
87 repo: repo,
88 sha: commit.sha
89 }, (error, statuses) ->
90 callback null, {
91 title: commit.commit.message
92 url: "https://github.com/#{org}/#{repo}/commit/#{commit.sha}"
93 comments: Moment(commit.commit.committer.date).fromNow()
94 avatar: commit.committer.gravatar_id
95 order: commit.commit.committer.date
96 status: GhHelpers.buildStatus(statuses)
97 }
98 , (err, list) ->
99 list = _.filter list, (object) ->
100 not object.title.match(new RegExp '^Merge')
101
102 if (save == "save")
103 saving(fallback, list)
104 else
105 Utils.fallback_printList fallback, list
106
107saving = (fallback, list) ->
108 gist list, (error, url) ->
109 if (error?)
110 Utils.fallback_printError(fallback, error)
111 else
112 Utils.fallback_print(fallback) {
113 title: "View the changelog"
114 url: url
115 comments: url
116 status: true
117 }
118
119gist = (list, callback) ->
120 data = _.reduce list, (memo, o) ->
121 memo += '- '
122 memo += o.title
123 memo += '\n'
124 , ""
125
126 Github.Api.gists.edit { id: Changelog.gistId, files: {"history.md": { content: data } }}, (err, gist) ->
127 if (err?) then callback(err) else callback(null, gist.html_url + "/" + gist.history[0].version)
128
129module.exports = {
130 name: "Changelog",
131 description: "-project- [ | -alias-] -pr- -number-
132 | -since- -number- -period- | -between- -tag-range-
133 [save] List changelog for a given PR, period, range ",
134 action: changelog
135}