-
Notifications
You must be signed in to change notification settings - Fork 3
/
new_tag.ts
82 lines (64 loc) · 2.46 KB
/
new_tag.ts
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
import { schedule, danger, markdown } from "danger"
import { Create } from "github-webhook-event-types"
import * as semverSort from "semver-sort"
export default async (gh: Create) => {
const api = danger.github.api
const tag = gh.ref
const thisRepo = { owner: gh.repository.owner.login, repo: gh.repository.name }
// Grab all tags, should be latest ~20
const allTagsResponse = await api.repos.listTags(thisRepo)
const allTags: Tag[] = allTagsResponse.data
// Sort the tags in semver, so we can know specifically what range to
// work out the commits
const semverStrings: string[] = semverSort.desc(allTags.map(tag => tag.name))
const versionIndex = semverStrings.findIndex(version => version === tag)
const releaseMinusOne = semverStrings[versionIndex + 1]
// Bail if we can't find a release
if (!releaseMinusOne) {
return
}
// Ask for the commits
const compareResults = await api.repos.compareCommits({ ...thisRepo, base: releaseMinusOne, head: tag })
const compareData: CompareResults = compareResults.data
// Pull out all the GH crafted merge commits on a repo
const numberExtractor = /Merge pull request #(\d*)/
const commitMessages = compareData.commits.map(c => c.commit.message)
const prMerges = commitMessages.filter(message => message && message.startsWith("Merge pull request #"))
// This is now a number array of PR ids
// e.g. [ 930, 934, 937, 932, 938 ]
const prs = prMerges
.map(msg => msg.match(numberExtractor) && msg.match(numberExtractor)![1])
.filter(pr => pr)
.map((pr: any) => parseInt(pr))
const changelogURL = gh.repository.html_url + "/blob/master/CHANGELOG.md"
const displayTag = gh.ref.startsWith("v") ? gh.ref : `v${gh.ref}`
const inviteMarkdown = (username: string) => `
Thanks for the PR @${username}.
This PR has been shipped in ${displayTag} - [CHANGELOG][].
[CHANGELOG]: ${changelogURL}
`
for (const prID of prs) {
const prResponse = await api.pulls.get({ ...thisRepo, number: prID })
const prData = prResponse.data
const author = prData.user.login
// If the PR wasn't created by whoever made the tag, send them a comment saying thanks!
if (author !== gh.sender.login) {
await api.issues.createComment({ ...thisRepo, number: prID, body: inviteMarkdown(author) })
}
}
}
interface Tag {
name: string
commit: {
sha: string
url: string
}
}
interface Commit {
commit: {
message: string
}
}
interface CompareResults {
commits: Commit[]
}