-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
github-helper.ts
189 lines (173 loc) · 5.05 KB
/
github-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
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
import * as core from '@actions/core'
import {Octokit, PullsGetResponseData} from './octokit-client'
import {Command, SlashCommandPayload} from './command-helper'
import {inspect} from 'util'
type ReposCreateDispatchEventParamsClientPayload = {
[key: string]: ReposCreateDispatchEventParamsClientPayloadKeyString
}
// eslint-disable-next-line
type ReposCreateDispatchEventParamsClientPayloadKeyString = {}
export interface ClientPayload
extends ReposCreateDispatchEventParamsClientPayload {
// eslint-disable-next-line
github: any
// eslint-disable-next-line
pull_request?: any
// eslint-disable-next-line
slash_command?: SlashCommandPayload | any
}
interface Repository {
owner: string
repo: string
}
type CollaboratorPermission = {
repository: {
collaborators: {
edges: [
{
permission: string
}
]
}
}
}
export class GitHubHelper {
private octokit: InstanceType<typeof Octokit>
constructor(token: string) {
this.octokit = new Octokit({
auth: token,
baseUrl: process.env['GITHUB_API_URL'] || 'https://api.github.com'
})
}
private parseRepository(repository: string): Repository {
const [owner, repo] = repository.split('/')
return {
owner: owner,
repo: repo
}
}
async getActorPermission(repo: Repository, actor: string): Promise<string> {
// https://docs.github.com/en/graphql/reference/enums#repositorypermission
// https://docs.github.com/en/graphql/reference/objects#repositorycollaboratoredge
// Returns 'READ', 'TRIAGE', 'WRITE', 'MAINTAIN', 'ADMIN'
const query = `query CollaboratorPermission($owner: String!, $repo: String!, $collaborator: String) {
repository(owner:$owner, name:$repo) {
collaborators(query: $collaborator) {
edges {
permission
}
}
}
}`
const collaboratorPermission =
await this.octokit.graphql<CollaboratorPermission>(query, {
...repo,
collaborator: actor
})
core.debug(
`CollaboratorPermission: ${inspect(
collaboratorPermission.repository.collaborators.edges
)}`
)
return collaboratorPermission.repository.collaborators.edges.length > 0
? collaboratorPermission.repository.collaborators.edges[0].permission.toLowerCase()
: 'none'
}
async tryAddReaction(
repo: Repository,
commentId: number,
reaction:
| '+1'
| '-1'
| 'laugh'
| 'confused'
| 'heart'
| 'hooray'
| 'rocket'
| 'eyes'
): Promise<void> {
try {
await this.octokit.rest.reactions.createForIssueComment({
...repo,
comment_id: commentId,
content: reaction
})
} catch (error) {
core.debug(error)
core.warning(`Failed to set reaction on comment ID ${commentId}.`)
}
}
async getPull(
repo: Repository,
pullNumber: number
): Promise<PullsGetResponseData> {
const {data: pullRequest} = await this.octokit.rest.pulls.get({
...repo,
pull_number: pullNumber
})
return pullRequest
}
async createDispatch(
cmd: Command,
clientPayload: ClientPayload
): Promise<void> {
if (cmd.dispatch_type == 'repository') {
await this.createRepositoryDispatch(cmd, clientPayload)
} else {
await this.createWorkflowDispatch(cmd, clientPayload)
}
}
private async createRepositoryDispatch(
cmd: Command,
clientPayload: ClientPayload
): Promise<void> {
const eventType = `${cmd.command}${cmd.event_type_suffix}`
await this.octokit.rest.repos.createDispatchEvent({
...this.parseRepository(cmd.repository),
event_type: `${cmd.command}${cmd.event_type_suffix}`,
client_payload: clientPayload
})
core.info(
`Command '${cmd.command}' dispatched to '${cmd.repository}' ` +
`with event type '${eventType}'.`
)
}
async createWorkflowDispatch(
cmd: Command,
clientPayload: ClientPayload
): Promise<void> {
const workflow = `${cmd.command}${cmd.event_type_suffix}.yml`
const slashCommand: SlashCommandPayload = clientPayload.slash_command
const ref = slashCommand.args.named.ref
? slashCommand.args.named.ref
: await this.getDefaultBranch(cmd.repository)
// Take max 10 named arguments, excluding 'ref'.
const inputs = {}
let count = 0
for (const key in slashCommand.args.named) {
if (key != 'ref') {
inputs[key] = slashCommand.args.named[key]
count++
}
if (count == 10) break
}
await this.octokit.request(
'POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches',
{
...this.parseRepository(cmd.repository),
workflow_id: workflow,
ref: ref,
inputs: inputs
}
)
core.info(
`Command '${cmd.command}' dispatched to workflow '${workflow}' in '${cmd.repository}'`
)
}
private async getDefaultBranch(repository: string): Promise<string> {
const {data: repo} = await this.octokit.rest.repos.get({
...this.parseRepository(repository)
})
return repo.default_branch
}
}