-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for stubbing @actions/github (#150)
This PR adds support for the `@actions/github` package. Specifically, the goal is to ensure all the needed `GITHUB_*` environment variables are loaded before the package is imported. Otherwise, creation of the `Context` class instance happens before the environment variables are loaded by `dotenv`, causing the `github.context` object to be mostly empty. Closes #149
- Loading branch information
Showing
17 changed files
with
477 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"action": "opened", | ||
"issue": { | ||
"assignee": { | ||
"login": "ncalteen" | ||
}, | ||
"assignees": [ | ||
{ | ||
"login": "ncalteen" | ||
} | ||
], | ||
"body": "This is an issue!", | ||
"number": 1, | ||
"state": "open", | ||
"title": "New Issue", | ||
"user": { | ||
"login": "ncalteen" | ||
} | ||
}, | ||
"organization": { | ||
"login": "github" | ||
}, | ||
"repository": { | ||
"full_name": "github/local-action", | ||
"name": "local-action", | ||
"owner": { | ||
"login": "github" | ||
} | ||
}, | ||
"sender": { | ||
"login": "ncalteen" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { jest } from '@jest/globals' | ||
import path from 'path' | ||
import { Context } from '../../../src/stubs/github/context.js' | ||
|
||
let envBackup: NodeJS.ProcessEnv | ||
|
||
describe('github/context', () => { | ||
beforeEach(() => { | ||
envBackup = process.env | ||
process.env.GITHUB_REPOSITORY = 'github/local-action' | ||
}) | ||
|
||
afterEach(() => { | ||
process.env = envBackup | ||
jest.resetAllMocks() | ||
}) | ||
|
||
describe('Context', () => { | ||
it('Creates a Context object', () => { | ||
const context = new Context() | ||
expect(context).toBeInstanceOf(Context) | ||
}) | ||
|
||
it('Gets the event payload', () => { | ||
process.env.GITHUB_EVENT_PATH = path.join( | ||
process.cwd(), | ||
'__fixtures__', | ||
'payloads', | ||
'issue_opened.json' | ||
) | ||
|
||
const context = new Context() | ||
expect(context.payload.action).toBe('opened') | ||
}) | ||
|
||
it('Does not get the event payload if the path does not exist', () => { | ||
process.env.GITHUB_EVENT_PATH = path.join( | ||
process.cwd(), | ||
'__fixtures__', | ||
'payloads', | ||
'does_not_exist.json' | ||
) | ||
|
||
const context = new Context() | ||
expect(context.payload.action).toBeUndefined() | ||
}) | ||
|
||
it('Gets the issue payload', () => { | ||
process.env.GITHUB_EVENT_PATH = path.join( | ||
process.cwd(), | ||
'__fixtures__', | ||
'payloads', | ||
'issue_opened.json' | ||
) | ||
|
||
const context = new Context() | ||
expect(context.issue.number).toBe(1) | ||
}) | ||
|
||
it('Gets the repo payload', () => { | ||
process.env.GITHUB_EVENT_PATH = path.join( | ||
process.cwd(), | ||
'__fixtures__', | ||
'payloads', | ||
'issue_opened.json' | ||
) | ||
|
||
const context = new Context() | ||
expect(context.repo.owner).toBe('github') | ||
expect(context.repo.repo).toBe('local-action') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { jest } from '@jest/globals' | ||
import * as github from '../../../src/stubs/github/github.js' | ||
import { GitHub } from '../../../src/stubs/github/utils.js' | ||
|
||
describe('github/github', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
describe('getOctokit', () => { | ||
it('Returns the options', () => { | ||
expect(github.getOctokit('test')).toBeInstanceOf(GitHub) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { jest } from '@jest/globals' | ||
import * as utils from '../../../src/stubs/github/utils.js' | ||
|
||
describe('github/utils', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
describe('getOctokitOptions', () => { | ||
it('Returns the options', () => { | ||
expect(utils.getOctokitOptions('test')).toEqual({ | ||
auth: 'token test' | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@github/local-action", | ||
"description": "Local Debugging for GitHub Actions", | ||
"version": "2.5.1", | ||
"version": "2.6.0", | ||
"type": "module", | ||
"author": "Nick Alteen <[email protected]>", | ||
"private": false, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { existsSync, readFileSync } from 'fs' | ||
import { EOL } from 'os' | ||
import { WebhookPayload } from './interfaces.js' | ||
|
||
export class Context { | ||
/** | ||
* Webhook payload object that triggered the workflow | ||
*/ | ||
payload: WebhookPayload | ||
|
||
eventName: string | ||
sha: string | ||
ref: string | ||
workflow: string | ||
action: string | ||
actor: string | ||
job: string | ||
runAttempt: number | ||
runNumber: number | ||
runId: number | ||
apiUrl: string | ||
serverUrl: string | ||
graphqlUrl: string | ||
|
||
/** | ||
* Hydrate the context from the environment | ||
*/ | ||
constructor() { | ||
this.payload = {} | ||
|
||
if (process.env.GITHUB_EVENT_PATH) { | ||
console.log(process.env.GITHUB_EVENT_PATH) | ||
if (existsSync(process.env.GITHUB_EVENT_PATH)) { | ||
this.payload = JSON.parse( | ||
readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: 'utf8' }) | ||
) | ||
} else { | ||
const path = process.env.GITHUB_EVENT_PATH | ||
process.stdout.write(`GITHUB_EVENT_PATH ${path} does not exist${EOL}`) | ||
} | ||
} | ||
|
||
this.eventName = process.env.GITHUB_EVENT_NAME as string | ||
this.sha = process.env.GITHUB_SHA as string | ||
this.ref = process.env.GITHUB_REF as string | ||
this.workflow = process.env.GITHUB_WORKFLOW as string | ||
this.action = process.env.GITHUB_ACTION as string | ||
this.actor = process.env.GITHUB_ACTOR as string | ||
this.job = process.env.GITHUB_JOB as string | ||
this.runAttempt = parseInt(process.env.GITHUB_RUN_ATTEMPT as string, 10) | ||
this.runNumber = parseInt(process.env.GITHUB_RUN_NUMBER as string, 10) | ||
this.runId = parseInt(process.env.GITHUB_RUN_ID as string, 10) | ||
/* istanbul ignore next */ | ||
this.apiUrl = process.env.GITHUB_API_URL ?? 'https://api.github.com' | ||
/* istanbul ignore next */ | ||
this.serverUrl = process.env.GITHUB_SERVER_URL ?? 'https://github.com' | ||
/* istanbul ignore next */ | ||
this.graphqlUrl = | ||
process.env.GITHUB_GRAPHQL_URL ?? 'https://api.github.com/graphql' | ||
} | ||
|
||
get issue(): { owner: string; repo: string; number: number } { | ||
const payload = this.payload | ||
|
||
/* istanbul ignore next */ | ||
return { | ||
...this.repo, | ||
number: (payload.issue || payload.pull_request || payload).number | ||
} | ||
} | ||
|
||
get repo(): { owner: string; repo: string } { | ||
if (process.env.GITHUB_REPOSITORY) { | ||
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/') | ||
return { owner, repo } | ||
} | ||
|
||
/* istanbul ignore next */ | ||
if (this.payload.repository) { | ||
return { | ||
owner: this.payload.repository.owner.login, | ||
repo: this.payload.repository.name | ||
} | ||
} | ||
|
||
/* istanbul ignore next */ | ||
throw new Error( | ||
"context.repo requires a GITHUB_REPOSITORY environment variable like 'owner/repo'" | ||
) | ||
} | ||
} |
Oops, something went wrong.