Skip to content

Commit

Permalink
Add support for GitHub Enterprise
Browse files Browse the repository at this point in the history
Without this support octokit always tries to query api.github.com
instead of GitHub Enterprise where it's running
  • Loading branch information
sochotnicky committed Oct 10, 2024
1 parent 89848c0 commit 6214189
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Full list of the options
| NAME | DESCRIPTION | TYPE | DEFAULT | OPTIONS |
| ----------------------------------- | -------------------------------------------------------------- | -------- | --------------------- | ---------------------------------------- |
| `github-api-url` | The Github API endpoint. Override for Github Enterprise usage. | `string` | `true` | `https://api.github.com` |
| `github-token` | The GITHUB_TOKEN secret. You can use PAT if you want. | `string` | `${{ github.token }}` | |
| `wait-seconds-before-first-polling` | Wait this interval before first polling | `number` | `10` | |
| `min-interval-seconds` | Wait this interval or the multiplied value (and jitter) | `number` | `15` | |
Expand All @@ -90,6 +91,15 @@ permissions:
actions: read
```

## Support for Github Enterprise

To run this action in your Github Enterprise (GHE) instance you need to override `github-api-url`:

```yaml
with:
github-api-url: 'https://ghe-host.acme.net/api/v3'
```

## outputs.<output_id>

- `dump`\
Expand Down
2 changes: 2 additions & 0 deletions __tests__/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { deepStrictEqual } from 'node:assert/strict';
import { checkSync } from 'recheck';

const defaultOptions = Object.freeze({
apiUrl: 'https://api.github.com',
isEarlyExit: true,
attemptLimits: 1000,
waitList: [],
Expand All @@ -21,6 +22,7 @@ const defaultOptions = Object.freeze({

test('Options keep given values', () => {
optionsEqual({
apiUrl: 'https://api.github.com',
isEarlyExit: true,
attemptLimits: 1000,
waitList: [],
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ inputs:
description: 'The GITHUB_TOKEN secret'
required: false
default: ${{ github.token }}
github-api-url:
description: 'Github API URL'
required: true
default: 'https://api.github.com'
wait-seconds-before-first-polling:
description: 'Wait this seconds before first polling'
required: false
Expand Down
9 changes: 6 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32618,6 +32618,7 @@ var WaitList = z2.array(WaitFilterCondition).readonly();
var SkipList = z2.array(SkipFilterCondition).readonly();
var retryMethods = z2.enum(["exponential_backoff", "equal_intervals"]);
var Options = z2.object({
apiUrl: z2.string().url(),
waitList: WaitList,
skipList: SkipList,
initialDuration: Duration,
Expand Down Expand Up @@ -32687,7 +32688,9 @@ function parseInput() {
const isEarlyExit = (0, import_core.getBooleanInput)("early-exit", { required: true, trimWhitespace: true });
const shouldSkipSameWorkflow = (0, import_core.getBooleanInput)("skip-same-workflow", { required: true, trimWhitespace: true });
const isDryRun = (0, import_core.getBooleanInput)("dry-run", { required: true, trimWhitespace: true });
const apiUrl = (0, import_core.getInput)("github-api-url", { required: true, trimWhitespace: true });
const options = Options.parse({
apiUrl,
initialDuration: Durationable.parse({ seconds: waitSecondsBeforeFirstPolling }),
leastInterval: Durationable.parse({ seconds: minIntervalSeconds }),
retryMethod,
Expand Down Expand Up @@ -33808,8 +33811,8 @@ function paginateGraphQL(octokit) {

// src/github-api.ts
var PaginatableOctokit = Octokit.plugin(paginateGraphQL);
async function fetchChecks(token, trigger) {
const octokit = new PaginatableOctokit({ auth: token });
async function fetchChecks(apiUrl, token, trigger) {
const octokit = new PaginatableOctokit({ auth: token, baseUrl: apiUrl });
const { repository: { object: { checkSuites } } } = await octokit.graphql.paginate(
/* GraphQL */
`
Expand Down Expand Up @@ -34167,7 +34170,7 @@ async function run() {
}
const elapsed = mr.Duration.from({ milliseconds: Math.ceil(performance.now() - startedAt) });
(0, import_core3.startGroup)(`Polling ${attempts}: ${(/* @__PURE__ */ new Date()).toISOString()} # total elapsed ${readableDuration(elapsed)}`);
const checks = await fetchChecks(githubToken, trigger);
const checks = await fetchChecks(options.apiUrl, githubToken, trigger);
const report = generateReport(
getSummaries(checks, trigger),
trigger,
Expand Down
3 changes: 2 additions & 1 deletion src/github-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { Check, Trigger } from './schema.ts';
const PaginatableOctokit = Octokit.plugin(paginateGraphQL);

export async function fetchChecks(
apiUrl: string,
token: string,
trigger: Trigger,
): Promise<Check[]> {
const octokit = new PaginatableOctokit({ auth: token });
const octokit = new PaginatableOctokit({ auth: token, baseUrl: apiUrl });
const { repository: { object: { checkSuites } } } = await octokit.graphql.paginate<
{ repository: { object: { checkSuites: Commit['checkSuites'] } } }
>(
Expand Down
2 changes: 2 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ export function parseInput(): { trigger: Trigger; options: Options; githubToken:
const isEarlyExit = getBooleanInput('early-exit', { required: true, trimWhitespace: true });
const shouldSkipSameWorkflow = getBooleanInput('skip-same-workflow', { required: true, trimWhitespace: true });
const isDryRun = getBooleanInput('dry-run', { required: true, trimWhitespace: true });
const apiUrl = getInput('github-api-url', { required: true, trimWhitespace: true });

const options = Options.parse({
apiUrl,
initialDuration: Durationable.parse({ seconds: waitSecondsBeforeFirstPolling }),
leastInterval: Durationable.parse({ seconds: minIntervalSeconds }),
retryMethod,
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async function run(): Promise<void> {
// Put getting elapsed time before of fetchChecks to keep accuracy of the purpose
const elapsed = Temporal.Duration.from({ milliseconds: Math.ceil(performance.now() - startedAt) });
startGroup(`Polling ${attempts}: ${(new Date()).toISOString()} # total elapsed ${readableDuration(elapsed)}`);
const checks = await fetchChecks(githubToken, trigger);
const checks = await fetchChecks(options.apiUrl, githubToken, trigger);

const report = generateReport(
getSummaries(checks, trigger),
Expand Down
1 change: 1 addition & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export type RetryMethod = z.infer<typeof retryMethods>;
// - Do not specify default values with zod. That is an action.yml role
// - Do not include secrets here, for example githubToken. See https://github.com/colinhacks/zod/issues/1783
export const Options = z.object({
apiUrl: z.string().url(),
waitList: WaitList,
skipList: SkipList,
initialDuration: Duration,
Expand Down

0 comments on commit 6214189

Please sign in to comment.