1
1
import * as c from './constants'
2
2
import * as core from '@actions/core'
3
3
import * as github from '@actions/github'
4
- import * as httpClient from '@actions/http-client'
5
4
import * as semver from 'semver'
6
5
import * as tc from '@actions/tool-cache'
7
6
import * as fs from 'fs'
8
7
import { ExecOptions , exec as e } from '@actions/exec'
9
8
import { readFileSync , readdirSync } from 'fs'
10
- import { Octokit } from '@octokit/core'
11
9
import { createHash } from 'crypto'
12
10
import { join } from 'path'
13
11
import { tmpdir } from 'os'
14
-
15
- // Set up Octokit for github.com only and in the same way as @actions /github (see https://git.io/Jy9YP)
16
- const baseUrl = 'https://api.github.com'
17
- const GitHubDotCom = Octokit . defaults ( {
18
- baseUrl,
19
- request : {
20
- agent : new httpClient . HttpClient ( ) . getAgent ( baseUrl )
21
- }
22
- } )
12
+ import { GitHub } from '@actions/github/lib/utils'
23
13
24
14
export async function exec ( commandLine : string , args ?: string [ ] , options ?: ExecOptions | undefined ) : Promise < void > {
25
15
const exitCode = await e ( commandLine , args , options )
@@ -29,9 +19,7 @@ export async function exec(commandLine: string, args?: string[], options?: ExecO
29
19
}
30
20
31
21
export async function getLatestRelease ( repo : string ) : Promise < c . LatestReleaseResponse [ 'data' ] > {
32
- const githubToken = getGitHubToken ( )
33
- const options = githubToken . length > 0 ? { auth : githubToken } : { }
34
- const octokit = new GitHubDotCom ( options )
22
+ const octokit = getOctokit ( )
35
23
return (
36
24
await octokit . request ( 'GET /repos/{owner}/{repo}/releases/latest' , {
37
25
owner : c . GRAALVM_GH_USER ,
@@ -41,9 +29,7 @@ export async function getLatestRelease(repo: string): Promise<c.LatestReleaseRes
41
29
}
42
30
43
31
export async function getContents ( repo : string , path : string ) : Promise < c . ContentsResponse [ 'data' ] > {
44
- const githubToken = getGitHubToken ( )
45
- const options = githubToken . length > 0 ? { auth : githubToken } : { }
46
- const octokit = new GitHubDotCom ( options )
32
+ const octokit = getOctokit ( )
47
33
return (
48
34
await octokit . request ( 'GET /repos/{owner}/{repo}/contents/{path}' , {
49
35
owner : c . GRAALVM_GH_USER ,
@@ -58,9 +44,7 @@ export async function getTaggedRelease(
58
44
repo : string ,
59
45
tag : string
60
46
) : Promise < c . LatestReleaseResponse [ 'data' ] > {
61
- const githubToken = getGitHubToken ( )
62
- const options = githubToken . length > 0 ? { auth : githubToken } : { }
63
- const octokit = new GitHubDotCom ( options )
47
+ const octokit = getOctokit ( )
64
48
return (
65
49
await octokit . request ( 'GET /repos/{owner}/{repo}/releases/tags/{tag}' , {
66
50
owner,
@@ -75,9 +59,7 @@ export async function getMatchingTags(
75
59
repo : string ,
76
60
tagPrefix : string
77
61
) : Promise < c . MatchingRefsResponse [ 'data' ] > {
78
- const githubToken = getGitHubToken ( )
79
- const options = githubToken . length > 0 ? { auth : githubToken } : { }
80
- const octokit = new GitHubDotCom ( options )
62
+ const octokit = getOctokit ( )
81
63
return (
82
64
await octokit . request ( 'GET /repos/{owner}/{repo}/git/matching-refs/tags/{tagPrefix}' , {
83
65
owner,
@@ -156,8 +138,15 @@ export function isPREvent(): boolean {
156
138
return process . env [ c . ENV_GITHUB_EVENT_NAME ] === c . EVENT_NAME_PULL_REQUEST
157
139
}
158
140
159
- function getGitHubToken ( ) : string {
160
- return core . getInput ( c . INPUT_GITHUB_TOKEN )
141
+ function getOctokit ( ) : InstanceType < typeof GitHub > {
142
+ /* Set up GitHub instance manually because @actions/github does not allow unauthenticated access */
143
+ const GitHubWithPlugins = GitHub . plugin ( )
144
+ const token = core . getInput ( c . INPUT_GITHUB_TOKEN )
145
+ if ( token ) {
146
+ return new GitHubWithPlugins ( { auth : `token ${ token } ` } )
147
+ } else {
148
+ return new GitHubWithPlugins ( ) /* unauthenticated */
149
+ }
161
150
}
162
151
163
152
export async function findExistingPRCommentId ( bodyStartsWith : string ) : Promise < number | undefined > {
@@ -166,7 +155,7 @@ export async function findExistingPRCommentId(bodyStartsWith: string): Promise<n
166
155
}
167
156
168
157
const context = github . context
169
- const octokit = github . getOctokit ( getGitHubToken ( ) )
158
+ const octokit = getOctokit ( )
170
159
try {
171
160
const comments = await octokit . paginate ( octokit . rest . issues . listComments , {
172
161
...context . repo ,
@@ -189,7 +178,7 @@ export async function updatePRComment(content: string, commentId: number): Promi
189
178
}
190
179
191
180
try {
192
- await github . getOctokit ( getGitHubToken ( ) ) . rest . issues . updateComment ( {
181
+ await getOctokit ( ) . rest . issues . updateComment ( {
193
182
...github . context . repo ,
194
183
comment_id : commentId ,
195
184
body : content
@@ -207,7 +196,7 @@ export async function createPRComment(content: string): Promise<void> {
207
196
}
208
197
const context = github . context
209
198
try {
210
- await github . getOctokit ( getGitHubToken ( ) ) . rest . issues . createComment ( {
199
+ await getOctokit ( ) . rest . issues . createComment ( {
211
200
...context . repo ,
212
201
issue_number : context . payload . pull_request ?. number as number ,
213
202
body : content
0 commit comments