Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: github-api-url #88

Merged
merged 16 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ In order to use this action, you need to:
### Create a token for the current repository

```yaml
on: [issues]
name: Run tests on staging
on:
push:
branches:
- main

jobs:
hello-world:
Expand All @@ -26,11 +30,10 @@ jobs:
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.PRIVATE_KEY }}
- uses: peter-evans/create-or-update-comment@v3
github-api-url: "https://github.acme-inc.com/api/v3"
- uses: ./actions/staging-tests
with:
token: ${{ steps.app-token.outputs.token }}
issue-number: ${{ github.event.issue.number }}
body: "Hello, World!"
```

### Use app token with `actions/checkout`
Expand Down Expand Up @@ -146,7 +149,7 @@ jobs:
run: echo 'matrix=[{"owner":"owner1"},{"owner":"owner2","repos":["repo1"]}]' >>"$GITHUB_OUTPUT"

use-matrix:
name: '@${{ matrix.owners-and-repos.owner }} installation'
name: "@${{ matrix.owners-and-repos.owner }} installation"
needs: [set-matrix]
runs-on: ubuntu-latest
strategy:
Expand All @@ -172,6 +175,12 @@ jobs:
MULTILINE_JSON_STRING: ${{ steps.get-installation-repositories.outputs.data }}
```

### Run the workflow in a github.com repository against an organization in Github Enterprise.
gr2m marked this conversation as resolved.
Show resolved Hide resolved

```yaml
on: [push]
```

## Inputs

### `app-id`
Expand All @@ -197,6 +206,10 @@ jobs:

**Optional:** If truthy, the token will not be revoked when the current job is complete.

### `github-api-url`

**Optional:** GitHub API URL. Defaults to `https://api.github.com`.
gr2m marked this conversation as resolved.
Show resolved Hide resolved

## Outputs

### `token`
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ inputs:
description: "If truthy, the token will not be revoked when the current job is complete"
required: false
deprecationMessage: "'skip_token_revoke' is deprecated and will be removed in a future version. Use 'skip-token-revoke' instead."
# Make GitHub API consigurable to support GitHub Enterprise server usecases
gr2m marked this conversation as resolved.
Show resolved Hide resolved
# see https://github.com/actions/create-github-app-token/issues/77
github-api-url:
description: The API URL of the GitHub server.
gr2m marked this conversation as resolved.
Show resolved Hide resolved
default: ${{ github.api_url }}
outputs:
token:
description: "GitHub installation access token"
Expand Down
6 changes: 3 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ const skipTokenRevoke = Boolean(
core.getInput("skip-token-revoke") || core.getInput("skip_token_revoke")
);

const baseUrl = core.getInput("github-api-url").replace(/\/$/, "");

main(
appId,
privateKey,
owner,
repositories,
core,
createAppAuth,
request.defaults({
baseUrl: process.env["GITHUB_API_URL"],
}),
request.defaults({ baseUrl }),
skipTokenRevoke
).catch((error) => {
/* c8 ignore next 3 */
Expand Down
9 changes: 3 additions & 6 deletions post.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import core from "@actions/core";
import { post } from "./lib/post.js";
import request from "./lib/request.js";

post(
core,
request.defaults({
baseUrl: process.env["GITHUB_API_URL"],
})
).catch((error) => {
const baseUrl = core.getInput("github-api-url").replace(/\/$/, "");

post(core, request.defaults({ baseUrl })).catch((error) => {
/* c8 ignore next 3 */
console.error(error);
core.setFailed(error.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { test } from "./main.js";

// Verify `main` successfully obtains a token when the `owner` input is set (to a user), but the `repositories` input isn’t set.
await test((mockPool) => {
process.env.INPUT_REPOSITORIES = "smockle";
process.env.INPUT_OWNER = "smockle";
delete process.env.INPUT_REPOSITORIES;

// Mock installation id request
const mockInstallationId = "123456";
Expand All @@ -17,8 +17,8 @@ await test((mockPool) => {
// Intentionally omitting the `authorization` header, since JWT creation is not idempotent.
},
})
.reply(500, 'GitHub API not available')
mockPool
.reply(500, "GitHub API not available");
mockPool
.intercept({
path: `/orgs/${process.env.INPUT_OWNER}/installation`,
method: "GET",
Expand Down
2 changes: 2 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export async function test(cb = (_mockPool) => {}) {
// Set required environment variables and inputs
process.env.GITHUB_REPOSITORY_OWNER = "actions";
process.env.GITHUB_REPOSITORY = "actions/create-github-app-token";

// inputs are set as environment variables with the prefix INPUT_
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
process.env["INPUT_GITHUB-API-URL"] = "https://api.github.com";
process.env["INPUT_APP-ID"] = "123456";
process.env["INPUT_PRIVATE-KEY"] = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA280nfuUM9w00Ib9E2rvZJ6Qu3Ua3IqR34ZlK53vn/Iobn2EL
Expand Down
Loading