Skip to content

Commit

Permalink
use github API URL
Browse files Browse the repository at this point in the history
* use the given URL to get tokens
* use the variable `github.api_url` as default value (https://docs.github.com/en/actions/learn-github-actions/contexts)
  • Loading branch information
mzuehlke committed Jul 11, 2023
1 parent 82607ff commit 64d64df
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion action.yml
Expand Up @@ -49,7 +49,7 @@ inputs:
description: |
The URL of the GitHub API, only use this input if
you are using GitHub Enterprise.
default: https://api.github.com
default: ${{ github.api_url }}
required: false
github-app-auth-only:
description: |
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -35,6 +35,7 @@
"@actions/io": "^1.1.3",
"@actions/tool-cache": "^2.0.1",
"@octokit/auth-app": "^4.0.9",
"@octokit/request": "^6.0.0",
"@types/node-fetch": "^2.6.4",
"@types/sinon": "^10.0.13",
"jssha": "^3.3.0",
Expand Down
16 changes: 12 additions & 4 deletions src/action/main.ts
Expand Up @@ -6,6 +6,7 @@ import * as core from '@actions/core'
import {getOctokit} from '@actions/github'
import * as io from '@actions/io'
import {createAppAuth} from '@octokit/auth-app'
import {request} from '@octokit/request'
import {type Files} from '../core/files'
import {type Logger} from '../core/logger'
import {nonEmpty, NonEmptyString} from '../core/types'
Expand All @@ -26,13 +27,13 @@ async function run(): Promise<void> {
const logger: Logger = core
const files: Files = {...fs, ...io}
const inputs = Input.from(core, files, logger).all()
const gitHubToken = await gitHubAppToken(inputs.github.app, 'installation') ?? inputs.github.token.value
const gitHubApiUrl = inputs.github.apiUrl.value
const gitHubToken = await gitHubAppToken(inputs.github.app, gitHubApiUrl, 'installation') ?? inputs.github.token.value
const octokit = getOctokit(gitHubToken, {baseUrl: gitHubApiUrl})
const github = GitHub.from(logger, octokit)
const workspace = Workspace.from(logger, files, os, cache)

const user = await gitHubAppToken(inputs.github.app, 'app')
const user = await gitHubAppToken(inputs.github.app, gitHubApiUrl, 'app')
.then(appToken => appToken ? getOctokit(appToken, {baseUrl: gitHubApiUrl}) : undefined)
.then(async octokit => octokit ? octokit.rest.apps.getAuthenticated() : undefined)
.then(async response => response ? github.getAppUser(response.data.slug) : github.getAuthUser())
Expand Down Expand Up @@ -91,15 +92,22 @@ async function run(): Promise<void> {
* Returns a GitHub App Token.
*
* @param app The GitHub App information.
* @param gitHubApiUrl The GitHub API URL.
* @param type The type of token to retrieve, either `app` or `installation`.
* @returns the GitHub App Token for the provided installation.
*/
async function gitHubAppToken(app: GitHubAppInfo | undefined, type: 'app' | 'installation') {
async function gitHubAppToken(app: GitHubAppInfo | undefined, gitHubApiUrl: string, type: 'app' | 'installation') {
if (!app) {
return undefined
}

const auth = createAppAuth({appId: app.id.value, privateKey: app.key.value})
const auth = createAppAuth({
appId: app.id.value,
privateKey: app.key.value,
request: request.defaults({
baseUrl: gitHubApiUrl,
}),
})

const response = type === 'app'
? await auth({type: 'app'})
Expand Down

0 comments on commit 64d64df

Please sign in to comment.