-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgithubHelper.ts
104 lines (93 loc) · 3.03 KB
/
githubHelper.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
import * as core from '@actions/core'
import {context, getOctokit} from '@actions/github'
import {IParams} from './paramsHelper'
export interface IGithubHelper {
isFork(): Promise<boolean>
setStatus(params: IParams): Promise<void>
addComment(token: string, comment: string): Promise<void>
isPullRequest(): Promise<boolean>
}
export async function CreateGithubHelper(): Promise<IGithubHelper> {
return await GithubHelper.createGithubHelper()
}
class GithubHelper {
private owner
private repo
private sha
private issueNumber
private octokit
private isPR
private baseRepo
private headRepo
private baseOwner
private baseRepoName
private constructor() {}
static async createGithubHelper(): Promise<GithubHelper> {
const result = new GithubHelper()
await result.initialize()
return result
}
private async initialize(): Promise<void> {
if (['pull_request', 'pull_request_target'].includes(context.eventName)) {
this.isPR = true
this.owner = context.payload?.pull_request?.head?.repo?.owner?.login
this.repo = context.payload?.pull_request?.head?.repo?.name
this.sha = context.payload?.pull_request?.head?.sha
this.issueNumber = context.payload?.pull_request?.number
this.baseRepo = context.payload?.pull_request?.base?.repo?.full_name
this.headRepo = context.payload?.pull_request?.head?.repo?.full_name
this.baseOwner = context.payload?.pull_request?.base?.repo?.owner?.login
this.baseRepoName = context.payload?.pull_request?.base?.repo?.name
}
if (context.eventName === 'push') {
this.isPR = false
this.owner = context.payload?.repository?.owner?.login
this.repo = context.payload?.repository?.name
this.sha = context.sha
}
}
async isPullRequest(): Promise<boolean> {
return this.isPR
}
async isFork(): Promise<boolean> {
return this.baseRepo !== this.headRepo
}
async setStatus(params: IParams): Promise<void> {
try {
const octokit = getOctokit(params.token)
await octokit.rest.repos.createCommitStatus({
context: params.name,
description: params.description,
owner: this.owner,
repo: this.repo,
sha: this.sha,
state: params.status,
target_url: params.url
})
core.info(`Updated build status: ${params.status}`)
} catch (error) {
throw new Error(
`error while setting context status: ${(error as Error).message}`
)
}
}
async addComment(token: string, comment: string): Promise<void> {
// if we support forks, then we need to use the base, cause head will be the fork
try {
const octokit = getOctokit(token)
await octokit.rest.issues.createComment({
owner: this.baseOwner,
repo: this.baseRepoName,
issue_number: this.issueNumber,
body: comment
})
core.info(`Comment added to pull request`)
} catch (error) {
throw new Error(
`error while adding comment to pull request: ${
(error as Error).message
}`
)
}
}
}