/
commits.js
131 lines (122 loc) · 3.71 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
const _ = require('lodash')
const { log } = require('./log')
const { paginate } = require('./pagination')
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 dataPath = ['repository', 'object', 'history']
const repoNameWithOwner = `${owner}/${repo}`
let data, commits
if (lastRelease) {
log({
context,
message: `Fetching parent commits ${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.
commits = _.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
)
commits = _.get(data, [...dataPath, 'nodes'])
}
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.findCommitsWithAssociatedPullRequests =
findCommitsWithAssociatedPullRequests