Skip to content

Commit

Permalink
fix: Fix Octokit not using github_token (#51)
Browse files Browse the repository at this point in the history
Signed-off-by: Oğuzhan Durgun <[email protected]>
  • Loading branch information
oguzhand95 authored Feb 17, 2023
1 parent 3ed5633 commit 8fec844
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 51 deletions.
6 changes: 4 additions & 2 deletions __tests__/get-url-to-download.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import {expect, test} from '@jest/globals'
import {Octokit} from '@octokit/core'
import getURLToDownload from './../src/get-url-to-download'
import {createRunningEnvironment} from './test-utils.test'

Expand Down Expand Up @@ -33,11 +34,12 @@ test('test getURLToDownload()', async () => {
}
]

const octokit = new Octokit({})
for (const testCase of testCases) {
const url = await getURLToDownload(
octokit,
testCase.inputRunningEnvironment,
testCase.inputVersion,
''
testCase.inputVersion
)

console.log(
Expand Down
6 changes: 4 additions & 2 deletions __tests__/get-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import {expect, test} from '@jest/globals'
import {Octokit} from '@octokit/core'
import getVersion from './../src/get-version'

test('test getVersion()', async () => {
Expand All @@ -16,8 +17,9 @@ test('test getVersion()', async () => {
{input: 'latest', unexpected: undefined}
]

const octokit = new Octokit({})
for (const testCase of testCasesWithExpected) {
const version = await getVersion(testCase.input)
const version = await getVersion(octokit, testCase.input)

console.log(
`Test Case - Input ${testCase.input} - Expected ${testCase.expected} - Actual ${version}`
Expand All @@ -27,7 +29,7 @@ test('test getVersion()', async () => {
}

for (const testCase of testCasesWithUnexpected) {
const version = await getVersion(testCase.input)
const version = await getVersion(octokit, testCase.input)

console.log(
`Test Case - Input ${testCase.input} - Unexpected ${testCase.unexpected} - Actual ${version}`
Expand Down
40 changes: 19 additions & 21 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

17 changes: 2 additions & 15 deletions src/get-url-to-download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,14 @@

import * as core from '@actions/core'
import {Octokit} from '@octokit/core'
import {HttpsProxyAgent} from 'https-proxy-agent'
import {RunningEnvironment} from './get-running-environment'

async function getURLToDownload(
octokit: Octokit,
runningEnvironment: RunningEnvironment,
version: string,
githubToken: string
version: string
): Promise<string> {
const assetName = `cerbos_${version}_${runningEnvironment.os}_${runningEnvironment.architecture}.tar.gz`
const requestAgent = process.env.http_proxy
? new HttpsProxyAgent(process.env.http_proxy)
: undefined
const octokit = new Octokit({
auth: githubToken,
request: {
agent: requestAgent
},
userAgent: process.env['GITHUB_REPOSITORY']
? process.env['GITHUB_REPOSITORY']
: 'cerbos-setup-action'
})
const {data: releases} = await octokit.request(
'GET /repos/{owner}/{repo}/releases',
{
Expand Down
7 changes: 4 additions & 3 deletions src/get-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

import {Octokit} from '@octokit/core'

async function getVersion(inputVersion: string): Promise<string> {
async function getVersion(
octokit: Octokit,
inputVersion: string
): Promise<string> {
if (inputVersion === '' || inputVersion === 'latest') {
const octokit = new Octokit()

const {data: release} = await octokit.request(
'GET /repos/{owner}/{repo}/releases/latest',
{
Expand Down
24 changes: 17 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

import * as core from '@actions/core'
import {Octokit} from '@octokit/core'
import {HttpsProxyAgent} from 'https-proxy-agent'
import downloadAndCache from './download-and-cache'
import getRunningEnvironment from './get-running-environment'
import getURLToDownload from './get-url-to-download'
Expand All @@ -22,13 +24,21 @@ async function run(): Promise<void> {
const runningEnvironment = await getRunningEnvironment()
await validate(runningEnvironment)

const version = await getVersion(inputVersion)

const url = await getURLToDownload(
runningEnvironment,
version,
inputGitHubToken
)
const requestAgent = process.env.http_proxy
? new HttpsProxyAgent(process.env.http_proxy)
: undefined
const octokit = new Octokit({
auth: inputGitHubToken,
request: {
agent: requestAgent
},
userAgent: process.env['GITHUB_REPOSITORY']
? process.env['GITHUB_REPOSITORY']
: 'cerbos-setup-action'
})

const version = await getVersion(octokit, inputVersion)
const url = await getURLToDownload(octokit, runningEnvironment, version)

await downloadAndCache(url, version)
}
Expand Down

0 comments on commit 8fec844

Please sign in to comment.