Skip to content

Commit 8e0bc6f

Browse files
Jason3Sautofix-ci[bot]Copilot
authored
fix: drop dependency upon @actions/github to get the context. (#2594)
Signed-off-by: Jason Dent <Jason3S@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent f599087 commit 8e0bc6f

File tree

12 files changed

+302
-221
lines changed

12 files changed

+302
-221
lines changed

action-src/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"@cspell/cspell-bundled-dicts"
2222
],
2323
"devDependencies": {
24-
"@actions/github": "^8.0.0",
2524
"@cspell/cspell-types": "^9.6.2",
2625
"@octokit/webhooks-types": "^7.6.1",
2726
"@types/node": "^24.10.9",

action-src/src/action.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Context } from '@actions/github/lib/context.js';
2-
import * as path from 'path';
3-
import * as process from 'process';
1+
import * as path from 'node:path';
2+
import * as process from 'node:process';
3+
44
import { beforeEach, describe, expect, test, vi } from 'vitest';
55

66
import { action } from './action.js';
7+
import { Context } from './actions/github/index.js';
78
import { AppError } from './error.js';
89
import { fetchGithubActionFixture, root, sourceDir } from './test/helper.js';
910

action-src/src/action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import path from 'node:path';
22

3-
import type { Context as GitHubContext } from '@actions/github/lib/context.js';
43
import type { RunResult } from 'cspell';
54

65
import { validateActionParams } from './ActionParams.js';
76
import { debug, error, info, setFailed, setOutput, warning } from './actions/core/index.js';
7+
import type { Context as GitHubContext } from './actions/github/index.js';
88
import { checkDotMap } from './checkDotMap.js';
99
import { checkSpellingForContext, type Context } from './checkSpelling.js';
1010
import { getActionParams } from './getActionParams.js';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright 2019 GitHub
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @action/github
2+
3+
This is a pared down version of GitHub's [@actions/github](https://github.com/actions/toolkit/tree/dfc20ac/packages/github)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import path from 'node:path';
2+
3+
import { describe, expect, test, vi } from 'vitest';
4+
5+
import { fetchGithubActionFixture, root } from '../../test/helper.js';
6+
7+
const configFile = path.resolve(root, 'cspell.json');
8+
9+
import { Context } from './context.js';
10+
11+
describe('Context', () => {
12+
test('Context', () => {
13+
createContextEnvFromFile('pull_request.json');
14+
const context = new Context();
15+
expect(context.eventName).toBe('pull_request');
16+
expect(context.sha).toBe('fac78ee45538f198c00ae651db5aedc7336f7ccc');
17+
});
18+
19+
test('Context no event path', () => {
20+
createContextEnvFromFile('pull_request.json');
21+
process.env.GITHUB_EVENT_PATH = '';
22+
const context = new Context();
23+
expect(context.eventName).toBe('pull_request');
24+
expect(context.sha).toBe('fac78ee45538f198c00ae651db5aedc7336f7ccc');
25+
});
26+
27+
test('Context bad event path', () => {
28+
process.env.GITHUB_EVENT_PATH = './non/existent/path.json';
29+
const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
30+
expect(() => new Context()).not.toThrow();
31+
expect(stdoutSpy).toHaveBeenCalledWith('GITHUB_EVENT_PATH ./non/existent/path.json does not exist\n');
32+
});
33+
34+
test('Context.issue', () => {
35+
createContextEnvFromFile('pull_request.json');
36+
const context = new Context();
37+
expect(context.issue).toEqual({ number: 3, owner: 'streetsidesoftware', repo: 'cspell-action' });
38+
});
39+
40+
test('Context.issue with non-issue', () => {
41+
process.env.GITHUB_EVENT_PATH = '';
42+
process.env.GITHUB_REPOSITORY = 'some-owner/some-repo';
43+
const context = new Context();
44+
expect(context.issue).toEqual({ number: undefined, owner: 'some-owner', repo: 'some-repo' });
45+
});
46+
47+
test('Context.repo', () => {
48+
createContextEnvFromFile('pull_request.json');
49+
process.env.GITHUB_REPOSITORY = '';
50+
const context = new Context();
51+
expect(context.repo).toEqual({ owner: 'streetsidesoftware', repo: 'cspell-action' });
52+
53+
process.env.GITHUB_REPOSITORY = 'some-owner/some-repo';
54+
expect(context.repo).toEqual({ owner: 'some-owner', repo: 'some-repo' });
55+
});
56+
57+
test('Context.repo error', () => {
58+
process.env.GITHUB_EVENT_PATH = '';
59+
process.env.GITHUB_REPOSITORY = '';
60+
const context = new Context();
61+
expect(() => context.repo).toThrow(
62+
"context.repo requires a GITHUB_REPOSITORY environment variable like 'owner/repo'",
63+
);
64+
});
65+
});
66+
67+
function createContextEnvFromFile(filename: string, ...params: Record<string, string>[]): void {
68+
return createContext(fetchGithubActionFixture(filename), ...params);
69+
}
70+
71+
function createContext(...params: Record<string, string>[]): void {
72+
Object.assign(process.env, ...params);
73+
setEnvIfNotExist('INPUT_ROOT', root);
74+
setEnvIfNotExist('INPUT_CONFIG', configFile);
75+
process.env.INPUT_CONFIG = path.resolve(root, process.env.INPUT_CONFIG || configFile);
76+
}
77+
78+
function setEnvIfNotExist(key: string, value: string) {
79+
if (process.env[key] === undefined) {
80+
process.env[key] = value;
81+
}
82+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copied from:
2+
// https://github.com/actions/toolkit/blob/ecdfc18bf2ccf0651026d5a34ede173cf2c85e9d/packages/github/src/context.ts
3+
import { existsSync, readFileSync } from 'fs';
4+
import { EOL } from 'os';
5+
6+
import { WebhookPayload } from './interfaces.js';
7+
8+
export class Context {
9+
/**
10+
* Webhook payload object that triggered the workflow
11+
*/
12+
payload: WebhookPayload;
13+
14+
eventName: string;
15+
sha: string;
16+
ref: string;
17+
workflow: string;
18+
action: string;
19+
actor: string;
20+
job: string;
21+
runAttempt: number;
22+
runNumber: number;
23+
runId: number;
24+
apiUrl: string;
25+
serverUrl: string;
26+
graphqlUrl: string;
27+
28+
/**
29+
* Hydrate the context from the environment
30+
*/
31+
constructor() {
32+
this.payload = {};
33+
if (process.env.GITHUB_EVENT_PATH) {
34+
if (existsSync(process.env.GITHUB_EVENT_PATH)) {
35+
this.payload = JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: 'utf8' }));
36+
} else {
37+
const path = process.env.GITHUB_EVENT_PATH;
38+
process.stdout.write(`GITHUB_EVENT_PATH ${path} does not exist${EOL}`);
39+
}
40+
}
41+
this.eventName = process.env.GITHUB_EVENT_NAME as string;
42+
this.sha = process.env.GITHUB_SHA as string;
43+
this.ref = process.env.GITHUB_REF as string;
44+
this.workflow = process.env.GITHUB_WORKFLOW as string;
45+
this.action = process.env.GITHUB_ACTION as string;
46+
this.actor = process.env.GITHUB_ACTOR as string;
47+
this.job = process.env.GITHUB_JOB as string;
48+
this.runAttempt = parseInt(process.env.GITHUB_RUN_ATTEMPT as string, 10);
49+
this.runNumber = parseInt(process.env.GITHUB_RUN_NUMBER as string, 10);
50+
this.runId = parseInt(process.env.GITHUB_RUN_ID as string, 10);
51+
this.apiUrl = process.env.GITHUB_API_URL ?? `https://api.github.com`;
52+
this.serverUrl = process.env.GITHUB_SERVER_URL ?? `https://github.com`;
53+
this.graphqlUrl = process.env.GITHUB_GRAPHQL_URL ?? `https://api.github.com/graphql`;
54+
}
55+
56+
get issue(): { owner: string; repo: string; number: number } {
57+
const payload = this.payload;
58+
59+
return {
60+
...this.repo,
61+
number: (payload.issue || payload.pull_request || payload).number,
62+
};
63+
}
64+
65+
get repo(): { owner: string; repo: string } {
66+
if (process.env.GITHUB_REPOSITORY) {
67+
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
68+
return { owner, repo };
69+
}
70+
71+
if (this.payload.repository) {
72+
return {
73+
owner: this.payload.repository.owner.login,
74+
repo: this.payload.repository.name,
75+
};
76+
}
77+
78+
throw new Error("context.repo requires a GITHUB_REPOSITORY environment variable like 'owner/repo'");
79+
}
80+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { Context } from './context.js';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copied from:
2+
// https://github.com/actions/toolkit/blob/ecdfc18bf2ccf0651026d5a34ede173cf2c85e9d/packages/github/src/interfaces.ts
3+
4+
/* eslint-disable @typescript-eslint/no-explicit-any */
5+
6+
export interface PayloadRepository {
7+
[key: string]: any;
8+
full_name?: string;
9+
name: string;
10+
owner: {
11+
[key: string]: any;
12+
login: string;
13+
name?: string;
14+
};
15+
html_url?: string;
16+
}
17+
18+
export interface WebhookPayload {
19+
[key: string]: any;
20+
repository?: PayloadRepository;
21+
issue?: {
22+
[key: string]: any;
23+
number: number;
24+
html_url?: string;
25+
body?: string;
26+
};
27+
pull_request?: {
28+
[key: string]: any;
29+
number: number;
30+
html_url?: string;
31+
body?: string;
32+
};
33+
sender?: {
34+
[key: string]: any;
35+
type: string;
36+
};
37+
action?: string;
38+
installation?: {
39+
id: number;
40+
[key: string]: any;
41+
};
42+
comment?: {
43+
id: number;
44+
[key: string]: any;
45+
};
46+
}

action-src/src/main.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Context } from '@actions/github/lib/context.js';
2-
31
import { action } from './action.js';
42
import { info, setFailed } from './actions/core/index.js';
3+
import { Context } from './actions/github/index.js';
54
import { toError } from './error.js';
65

76
export async function run(): Promise<undefined | Error> {

0 commit comments

Comments
 (0)