-
Notifications
You must be signed in to change notification settings - Fork 317
/
commits.js
197 lines (181 loc) · 5.37 KB
/
commits.js
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
const _ = require('lodash')
const { log } = require('./log')
const { paginate } = require('./pagination')
const findCommitsWithPathChangesQuery = /* GraphQL */ `
query findCommitsWithPathChangesQuery(
$name: String!
$owner: String!
$targetCommitish: String!
$since: GitTimestamp
$after: String
$path: String
) {
repository(name: $name, owner: $owner) {
object(expression: $targetCommitish) {
... on Commit {
history(path: $path, since: $since, after: $after) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
}
}
}
}
}
}
`
const findCommitsWithAssociatedPullRequestsQuery = /* GraphQL */ `
query findCommitsWithAssociatedPullRequests(
$name: String!
$owner: String!
$targetCommitish: String!
$withPullRequestBody: Boolean!
$withPullRequestURL: Boolean!
$since: GitTimestamp
$after: String
$withBaseRefName: Boolean!
$withHeadRefName: Boolean!
) {
repository(name: $name, owner: $owner) {
object(expression: $targetCommitish) {
... on Commit {
history(first: 100, since: $since, after: $after) {
totalCount
pageInfo {
hasNextPage
endCursor
}
nodes {
id
committedDate
message
author {
name
user {
login
}
}
associatedPullRequests(first: 5) {
nodes {
title
number
url @include(if: $withPullRequestURL)
body @include(if: $withPullRequestBody)
author {
login
}
baseRepository {
nameWithOwner
}
mergedAt
isCrossRepository
labels(first: 10) {
nodes {
name
}
}
merged
baseRefName @include(if: $withBaseRefName)
headRefName @include(if: $withHeadRefName)
}
}
}
}
}
}
}
}
`
const findCommitsWithAssociatedPullRequests = async ({
context,
targetCommitish,
lastRelease,
config,
}) => {
const { owner, repo } = context.repo()
const variables = {
name: repo,
owner,
targetCommitish,
withPullRequestBody: config['change-template'].includes('$BODY'),
withPullRequestURL: config['change-template'].includes('$URL'),
withBaseRefName: config['change-template'].includes('$BASE_REF_NAME'),
withHeadRefName: config['change-template'].includes('$HEAD_REF_NAME'),
}
const includePaths = config['include-paths']
const dataPath = ['repository', 'object', 'history']
const repoNameWithOwner = `${owner}/${repo}`
let data,
allCommits,
includedIds = {}
if (includePaths.length > 0) {
var anyChanges = false
for (const path of includePaths) {
const pathData = await paginate(
context.octokit.graphql,
findCommitsWithPathChangesQuery,
lastRelease
? { ...variables, since: lastRelease.created_at, path }
: { ...variables, path },
dataPath
)
const commitsWithPathChanges = _.get(pathData, [...dataPath, 'nodes'])
includedIds[path] = includedIds[path] || new Set([])
for (const { id } of commitsWithPathChanges) {
anyChanges = true
includedIds[path].add(id)
}
}
if (!anyChanges) {
// Short circuit to avoid blowing GraphQL budget
return { commits: [], pullRequests: [] }
}
}
if (lastRelease) {
log({
context,
message: `Fetching parent commits of ${targetCommitish} since ${lastRelease.created_at}`,
})
data = await paginate(
context.octokit.graphql,
findCommitsWithAssociatedPullRequestsQuery,
{ ...variables, since: lastRelease.created_at },
dataPath
)
// GraphQL call is inclusive of commits from the specified dates. This means the final
// commit from the last tag is included, so we remove this here.
allCommits = _.get(data, [...dataPath, 'nodes']).filter(
(commit) => commit.committedDate != lastRelease.created_at
)
} else {
log({ context, message: `Fetching parent commits of ${targetCommitish}` })
data = await paginate(
context.octokit.graphql,
findCommitsWithAssociatedPullRequestsQuery,
variables,
dataPath
)
allCommits = _.get(data, [...dataPath, 'nodes'])
}
const commits =
includePaths.length > 0
? allCommits.filter((commit) =>
includePaths.some((path) => includedIds[path].has(commit.id))
)
: allCommits
const pullRequests = _.uniqBy(
commits.flatMap((commit) => commit.associatedPullRequests.nodes),
'number'
).filter(
(pr) => pr.baseRepository.nameWithOwner === repoNameWithOwner && pr.merged
)
return { commits, pullRequests }
}
exports.findCommitsWithAssociatedPullRequestsQuery =
findCommitsWithAssociatedPullRequestsQuery
exports.findCommitsWithPathChangesQuery = findCommitsWithPathChangesQuery
exports.findCommitsWithAssociatedPullRequests =
findCommitsWithAssociatedPullRequests