-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
pulls-helper.ts
153 lines (143 loc) · 4.02 KB
/
pulls-helper.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
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
import * as core from '@actions/core'
import {graphql} from '@octokit/graphql'
import * as OctokitTypes from '@octokit/types'
import {inspect} from 'util'
export class PullsHelper {
graphqlClient: typeof graphql
constructor(token: string) {
this.graphqlClient = graphql.defaults({
headers: {
authorization: `token ${token}`
}
})
}
async get(
repository: string,
head: string,
headOwner: string,
base: string,
includeLabels: string[],
excludeLabels: string[],
excludeDrafts: boolean
): Promise<Pull[]> {
const [owner, repo] = repository.split('/')
const params: OctokitTypes.RequestParameters = {
owner: owner,
repo: repo
}
if (head.length > 0) params.head = head
if (base.length > 0) params.base = base
const query = `query Pulls($owner: String!, $repo: String!, $head: String, $base: String) {
repository(owner:$owner, name:$repo) {
pullRequests(first: 100, states: OPEN, headRefName: $head, baseRefName: $base) {
edges {
node {
baseRefName
headRefName
headRepository {
nameWithOwner
url
}
headRepositoryOwner {
login
}
isDraft
labels(first: 100) {
nodes {
name
}
}
maintainerCanModify
}
}
}
}
}`
const pulls = await this.graphqlClient<Pulls>(query, params)
core.debug(`Pulls: ${inspect(pulls.repository.pullRequests.edges)}`)
const filteredPulls = pulls.repository.pullRequests.edges
.map(p => {
if (
// Filter out pull requests where the head repo and/or branch has been deleted prematurely
p.node.headRepository &&
// Filter on head owner since the query only filters on head ref
(headOwner.length == 0 ||
p.node.headRepositoryOwner.login == headOwner) &&
// Filter heads from forks where 'maintainer can modify' is false
(p.node.headRepositoryOwner.login == owner ||
p.node.maintainerCanModify) &&
// Filter out pull requests that do not have labels in the include list
(includeLabels.length == 0 ||
p.node.labels.nodes.some(function (value: Label): boolean {
// At least one label is in the include list
return includeLabels.includes(value.name)
})) &&
// Filter out pull requests with labels in the exclude list
p.node.labels.nodes.every(function (value: Label): boolean {
// Every label is not in the exclude list
return !excludeLabels.includes(value.name)
}) &&
// Filter out drafts if set to exclude
(!excludeDrafts || p.node.isDraft == false)
) {
return new Pull(
p.node.baseRefName,
p.node.headRepository.url,
p.node.headRepository.nameWithOwner,
p.node.headRefName
)
}
})
.filter(notUndefined)
core.debug(`filteredPulls: ${inspect(filteredPulls)}`)
return filteredPulls
}
}
type Label = {
name: string
}
type Edge = {
node: {
baseRefName: string
headRefName: string
headRepository: {
nameWithOwner: string
url: string
}
headRepositoryOwner: {
login: string
}
isDraft: boolean
labels: {
nodes: Label[]
}
maintainerCanModify: boolean
}
}
type Pulls = {
repository: {
pullRequests: {
edges: Edge[]
}
}
}
export class Pull {
baseRef: string
headRepoUrl: string
headRepoName: string
headRef: string
constructor(
baseRef: string,
headRepoUrl: string,
headRepoName: string,
headRef: string
) {
this.baseRef = baseRef
this.headRepoUrl = headRepoUrl
this.headRepoName = headRepoName
this.headRef = headRef
}
}
function notUndefined<T>(x: T | undefined): x is T {
return x !== undefined
}