11import * as c from './constants'
22import * as core from '@actions/core'
33import * as github from '@actions/github'
4- import * as httpClient from '@actions/http-client'
54import * as semver from 'semver'
65import * as tc from '@actions/tool-cache'
76import * as fs from 'fs'
87import { ExecOptions , exec as e } from '@actions/exec'
98import { readFileSync , readdirSync } from 'fs'
10- import { Octokit } from '@octokit/core'
119import { createHash } from 'crypto'
1210import { join } from 'path'
1311import { 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'
2313
2414export async function exec ( commandLine : string , args ?: string [ ] , options ?: ExecOptions | undefined ) : Promise < void > {
2515 const exitCode = await e ( commandLine , args , options )
@@ -29,9 +19,7 @@ export async function exec(commandLine: string, args?: string[], options?: ExecO
2919}
3020
3121export 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 ( )
3523 return (
3624 await octokit . request ( 'GET /repos/{owner}/{repo}/releases/latest' , {
3725 owner : c . GRAALVM_GH_USER ,
@@ -41,9 +29,7 @@ export async function getLatestRelease(repo: string): Promise<c.LatestReleaseRes
4129}
4230
4331export 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 ( )
4733 return (
4834 await octokit . request ( 'GET /repos/{owner}/{repo}/contents/{path}' , {
4935 owner : c . GRAALVM_GH_USER ,
@@ -58,9 +44,7 @@ export async function getTaggedRelease(
5844 repo : string ,
5945 tag : string
6046) : 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 ( )
6448 return (
6549 await octokit . request ( 'GET /repos/{owner}/{repo}/releases/tags/{tag}' , {
6650 owner,
@@ -75,9 +59,7 @@ export async function getMatchingTags(
7559 repo : string ,
7660 tagPrefix : string
7761) : 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 ( )
8163 return (
8264 await octokit . request ( 'GET /repos/{owner}/{repo}/git/matching-refs/tags/{tagPrefix}' , {
8365 owner,
@@ -156,8 +138,15 @@ export function isPREvent(): boolean {
156138 return process . env [ c . ENV_GITHUB_EVENT_NAME ] === c . EVENT_NAME_PULL_REQUEST
157139}
158140
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+ }
161150}
162151
163152export async function findExistingPRCommentId ( bodyStartsWith : string ) : Promise < number | undefined > {
@@ -166,7 +155,7 @@ export async function findExistingPRCommentId(bodyStartsWith: string): Promise<n
166155 }
167156
168157 const context = github . context
169- const octokit = github . getOctokit ( getGitHubToken ( ) )
158+ const octokit = getOctokit ( )
170159 try {
171160 const comments = await octokit . paginate ( octokit . rest . issues . listComments , {
172161 ...context . repo ,
@@ -189,7 +178,7 @@ export async function updatePRComment(content: string, commentId: number): Promi
189178 }
190179
191180 try {
192- await github . getOctokit ( getGitHubToken ( ) ) . rest . issues . updateComment ( {
181+ await getOctokit ( ) . rest . issues . updateComment ( {
193182 ...github . context . repo ,
194183 comment_id : commentId ,
195184 body : content
@@ -207,7 +196,7 @@ export async function createPRComment(content: string): Promise<void> {
207196 }
208197 const context = github . context
209198 try {
210- await github . getOctokit ( getGitHubToken ( ) ) . rest . issues . createComment ( {
199+ await getOctokit ( ) . rest . issues . createComment ( {
211200 ...context . repo ,
212201 issue_number : context . payload . pull_request ?. number as number ,
213202 body : content
0 commit comments