willfire 0.1.22 → 0.1.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,11 +17,12 @@ pnpm add willfire
17
17
  ## Library
18
18
 
19
19
  ```ts
20
- import { predict } from "willfire";
21
- import { getOctokit } from "@actions/github"; // or new Octokit({ auth: token })
20
+ import { makeGithubClient, predict } from "willfire";
22
21
 
22
+ // makeGithubClient reads GH_TOKEN or GITHUB_TOKEN from the environment; any
23
+ // object satisfying the exported GithubClient interface works in its place.
23
24
  const { entries, checkNames, skip, sources } = await predict(
24
- getOctokit(token),
25
+ makeGithubClient(),
25
26
  "owner/repo",
26
27
  123,
27
28
  { action: context.payload.action }, // "opened" | "synchronize" | "reopened"
package/dist/cli.js CHANGED
@@ -6,12 +6,12 @@
6
6
  // GITHUB_TOKEN.
7
7
  import { parseArgs } from "./cli/parseArgs.js";
8
8
  import { isWorkflowEntry } from "./entries/isWorkflowEntry.js";
9
- import { makeOctokit } from "./predict/makeOctokit.js";
9
+ import { makeGithubClient } from "./predict/makeGithubClient.js";
10
10
  import { predict } from "./predict/predict.js";
11
11
  const isMain = /cli\.(ts|js)$|\/willfire$/.test(process.argv[1] ?? "");
12
12
  if (isMain) {
13
13
  const args = parseArgs(process.argv.slice(2));
14
- const prediction = await predict(makeOctokit(), args.repo, args.pr, {
14
+ const prediction = await predict(makeGithubClient(), args.repo, args.pr, {
15
15
  action: args.action,
16
16
  });
17
17
  const { entries, skip, sources } = prediction;
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export { patternToRegex, matchFilters } from "./filters/index.js";
3
3
  export { expandMatrix } from "./matrix/index.js";
4
4
  export { evalIf, expandWorkflowJobs } from "./jobs/index.js";
5
5
  export { parseUses } from "./uses/index.js";
6
- export { makeOctokit, predict } from "./predict/index.js";
6
+ export { makeGithubClient, predict } from "./predict/index.js";
7
+ export type { GithubClient } from "./predict/index.js";
7
8
  export type { ExecOutcome, JobExecutor } from "./execute.js";
8
9
  export type { JobName, WorkflowEntry, JobEntry, Entry, Prediction, PrEventAction, PredictOptions, Ctx, ExpandedJob, SourceRef, WorkflowSource, FetchWorkflow, ResolveRef, WorkflowReader, UsesTarget, } from "./types.js";
package/dist/index.js CHANGED
@@ -3,4 +3,4 @@ export { patternToRegex, matchFilters } from "./filters/index.js";
3
3
  export { expandMatrix } from "./matrix/index.js";
4
4
  export { evalIf, expandWorkflowJobs } from "./jobs/index.js";
5
5
  export { parseUses } from "./uses/index.js";
6
- export { makeOctokit, predict } from "./predict/index.js";
6
+ export { makeGithubClient, predict } from "./predict/index.js";
@@ -1,4 +1,5 @@
1
- export { makeOctokit } from "./makeOctokit.js";
1
+ export { makeGithubClient } from "./makeGithubClient.js";
2
+ export type { GithubClient } from "./makeGithubClient.js";
2
3
  export { sourceKey } from "./sourceKey.js";
3
4
  export { stackTargetRef } from "./stackTargetRef.js";
4
5
  export { finalizePrediction } from "./finalizePrediction.js";
@@ -1,4 +1,4 @@
1
- export { makeOctokit } from "./makeOctokit.js";
1
+ export { makeGithubClient } from "./makeGithubClient.js";
2
2
  export { sourceKey } from "./sourceKey.js";
3
3
  export { stackTargetRef } from "./stackTargetRef.js";
4
4
  export { finalizePrediction } from "./finalizePrediction.js";
@@ -0,0 +1,108 @@
1
+ interface RepoParams {
2
+ owner: string;
3
+ repo: string;
4
+ }
5
+ interface PageParams {
6
+ per_page?: number;
7
+ page?: number;
8
+ }
9
+ /** Response types carry only the fields willfire actually reads. */
10
+ export interface GithubPullSummary {
11
+ base: {
12
+ ref: string;
13
+ };
14
+ merge_commit_sha: string | null;
15
+ }
16
+ export interface GithubPull extends GithubPullSummary {
17
+ commits: number;
18
+ head: {
19
+ sha: string;
20
+ };
21
+ }
22
+ export interface GithubPullFile {
23
+ filename: string;
24
+ }
25
+ export interface GithubCommit {
26
+ sha: string;
27
+ commit: {
28
+ message: string;
29
+ };
30
+ parents: {
31
+ sha: string;
32
+ }[];
33
+ }
34
+ export interface GithubWorkflow {
35
+ path: string;
36
+ state: string;
37
+ }
38
+ export interface GithubWorkflowRun {
39
+ id: number;
40
+ path: string;
41
+ status: string | null;
42
+ }
43
+ export interface GithubJob {
44
+ name: string;
45
+ conclusion: string | null;
46
+ }
47
+ export interface GithubClient {
48
+ rest: {
49
+ pulls: {
50
+ get(params: RepoParams & {
51
+ pull_number: number;
52
+ }): Promise<{
53
+ data: GithubPull;
54
+ }>;
55
+ list(params: RepoParams & PageParams & {
56
+ state: string;
57
+ head: string;
58
+ }): Promise<{
59
+ data: GithubPullSummary[];
60
+ }>;
61
+ listFiles(params: RepoParams & PageParams & {
62
+ pull_number: number;
63
+ }): Promise<{
64
+ data: GithubPullFile[];
65
+ }>;
66
+ };
67
+ repos: {
68
+ getCommit(params: RepoParams & {
69
+ ref: string;
70
+ }): Promise<{
71
+ data: GithubCommit;
72
+ }>;
73
+ /** The file at `path` as text — always the `raw` media type. */
74
+ getContent(params: RepoParams & {
75
+ path: string;
76
+ ref: string;
77
+ }): Promise<{
78
+ data: string;
79
+ }>;
80
+ downloadTarballArchive(params: RepoParams & {
81
+ ref: string;
82
+ }): Promise<{
83
+ data: ArrayBuffer;
84
+ }>;
85
+ };
86
+ actions: {
87
+ listRepoWorkflows(params: RepoParams & PageParams): Promise<{
88
+ data: GithubWorkflow[];
89
+ }>;
90
+ listWorkflowRunsForRepo(params: RepoParams & PageParams & {
91
+ head_sha: string;
92
+ event: string;
93
+ }): Promise<{
94
+ data: GithubWorkflowRun[];
95
+ }>;
96
+ listJobsForWorkflowRun(params: RepoParams & PageParams & {
97
+ run_id: number;
98
+ }): Promise<{
99
+ data: GithubJob[];
100
+ }>;
101
+ };
102
+ };
103
+ paginate<P extends PageParams, T>(route: (params: P) => Promise<{
104
+ data: T[];
105
+ }>, params: P): Promise<T[]>;
106
+ }
107
+ export declare function makeGithubClient(): GithubClient;
108
+ export {};
@@ -0,0 +1,96 @@
1
+ // The GitHub REST surface willfire uses: a handful of GET endpoints plus the
2
+ // tarball download, over plain `fetch`. The shape mirrors the octokit subset
3
+ // it replaced (#75) so call sites and their fakes carry over unchanged.
4
+ export function makeGithubClient() {
5
+ const token = process.env.GH_TOKEN ?? process.env.GITHUB_TOKEN;
6
+ if (!token) {
7
+ throw new Error("GH_TOKEN or GITHUB_TOKEN must be set");
8
+ }
9
+ const request = async (path, query, accept) => {
10
+ const url = new URL(`https://api.github.com${path}`);
11
+ for (const [key, value] of Object.entries(query)) {
12
+ if (value !== undefined) {
13
+ url.searchParams.set(key, String(value));
14
+ }
15
+ }
16
+ const res = await fetch(url, {
17
+ headers: {
18
+ accept,
19
+ authorization: `Bearer ${token}`,
20
+ // GitHub rejects requests without a User-Agent.
21
+ "user-agent": "willfire",
22
+ "x-github-api-version": "2022-11-28",
23
+ },
24
+ });
25
+ if (!res.ok) {
26
+ throw new Error(`GitHub API ${res.status} for ${path}`);
27
+ }
28
+ return res;
29
+ };
30
+ const json = async (path, query = {}) => {
31
+ const res = await request(path, query, "application/vnd.github+json");
32
+ return { data: (await res.json()) };
33
+ };
34
+ return {
35
+ rest: {
36
+ pulls: {
37
+ get: ({ owner, repo, pull_number }) => json(`/repos/${owner}/${repo}/pulls/${pull_number}`),
38
+ list: ({ owner, repo, state, head, per_page, page }) => json(`/repos/${owner}/${repo}/pulls`, {
39
+ state,
40
+ head,
41
+ per_page,
42
+ page,
43
+ }),
44
+ listFiles: ({ owner, repo, pull_number, per_page, page }) => json(`/repos/${owner}/${repo}/pulls/${pull_number}/files`, {
45
+ per_page,
46
+ page,
47
+ }),
48
+ },
49
+ repos: {
50
+ getCommit: ({ owner, repo, ref }) => json(`/repos/${owner}/${repo}/commits/${ref}`),
51
+ getContent: async ({ owner, repo, path, ref }) => {
52
+ const res = await request(`/repos/${owner}/${repo}/contents/${path}`, { ref }, "application/vnd.github.raw+json");
53
+ return { data: await res.text() };
54
+ },
55
+ // 302 to a short-lived codeload URL; fetch follows it, and undici
56
+ // drops the authorization header on the cross-origin hop.
57
+ downloadTarballArchive: async ({ owner, repo, ref }) => {
58
+ const res = await request(`/repos/${owner}/${repo}/tarball/${ref}`, {}, "application/vnd.github+json");
59
+ return { data: await res.arrayBuffer() };
60
+ },
61
+ },
62
+ actions: {
63
+ // The actions list endpoints wrap their arrays in an envelope; unwrap
64
+ // here so `paginate` sees one shape everywhere.
65
+ listRepoWorkflows: async ({ owner, repo, per_page, page }) => {
66
+ const { data } = await json(`/repos/${owner}/${repo}/actions/workflows`, { per_page, page });
67
+ return { data: data.workflows };
68
+ },
69
+ listWorkflowRunsForRepo: async ({ owner, repo, head_sha, event, per_page, page }) => {
70
+ const { data } = await json(`/repos/${owner}/${repo}/actions/runs`, { head_sha, event, per_page, page });
71
+ return { data: data.workflow_runs };
72
+ },
73
+ listJobsForWorkflowRun: async ({ owner, repo, run_id, per_page, page }) => {
74
+ const { data } = await json(`/repos/${owner}/${repo}/actions/runs/${run_id}/jobs`, { per_page, page });
75
+ return { data: data.jobs };
76
+ },
77
+ },
78
+ },
79
+ // A short page ends the walk. A count landing exactly on a page boundary
80
+ // costs one extra empty-page request, which keeps this free of Link-header
81
+ // parsing.
82
+ paginate: async (route, params) => {
83
+ const perPage = params.per_page ?? 30;
84
+ const all = [];
85
+ let page = 1;
86
+ let full = true;
87
+ while (full) {
88
+ const { data } = await route({ ...params, page });
89
+ all.push(...data);
90
+ full = data.length === perPage;
91
+ page += 1;
92
+ }
93
+ return all;
94
+ },
95
+ };
96
+ }
@@ -1,5 +1,5 @@
1
- import type { Octokit } from "@octokit/rest";
2
1
  import { type JobExecutor, type RunCommand } from "../execute.js";
2
+ import type { GithubClient } from "./makeGithubClient.js";
3
3
  import type { ResolveRef, WorkflowSource } from "../types.js";
4
4
  export interface LiveExecutorOpts {
5
5
  /** How steps run; the hermetic docker sandbox by default. */
@@ -17,4 +17,4 @@ export interface LiveExecutorOpts {
17
17
  * docker sandbox; infrastructure subprocesses (`tar`, `git`) run on the host,
18
18
  * since the clone needs the network the sandbox denies.
19
19
  */
20
- export declare function makeLiveExecutor(octokit: Octokit, workspace: WorkflowSource, resolveRef: ResolveRef, opts?: LiveExecutorOpts): JobExecutor;
20
+ export declare function makeLiveExecutor(github: GithubClient, workspace: WorkflowSource, resolveRef: ResolveRef, opts?: LiveExecutorOpts): JobExecutor;
@@ -6,10 +6,10 @@ import { SANDBOX_NODE_MAJOR } from "../sandbox/sandboxConfig.js";
6
6
  * docker sandbox; infrastructure subprocesses (`tar`, `git`) run on the host,
7
7
  * since the clone needs the network the sandbox denies.
8
8
  */
9
- export function makeLiveExecutor(octokit, workspace, resolveRef, opts = {}) {
9
+ export function makeLiveExecutor(github, workspace, resolveRef, opts = {}) {
10
10
  const download = async (src) => {
11
11
  try {
12
- const { data } = await octokit.rest.repos.downloadTarballArchive({
12
+ const { data } = await github.rest.repos.downloadTarballArchive({
13
13
  owner: src.owner,
14
14
  repo: src.repo,
15
15
  ref: src.sha,
@@ -1,3 +1,3 @@
1
- import type { Octokit } from "@octokit/rest";
1
+ import type { GithubClient } from "./makeGithubClient.js";
2
2
  import type { Prediction, PredictOptions } from "../types.js";
3
- export declare function predict(octokit: Octokit, repo: string, prNumber: number, opts?: PredictOptions): Promise<Prediction>;
3
+ export declare function predict(github: GithubClient, repo: string, prNumber: number, opts?: PredictOptions): Promise<Prediction>;
@@ -15,16 +15,16 @@ import { sourceKey } from "./sourceKey.js";
15
15
  import { stackTargetRef } from "./stackTargetRef.js";
16
16
  const SKIP_RE = /\[(skip ci|ci skip|no ci|skip actions|actions skip)\]/i;
17
17
  const SKIP_TRAILER_RE = /^skip-checks:\s*true/im;
18
- export async function predict(octokit, repo, prNumber, opts = {}) {
18
+ export async function predict(github, repo, prNumber, opts = {}) {
19
19
  const [owner, name] = repo.split("/");
20
20
  const base = { owner, repo: name };
21
- const { data: pr } = await octokit.rest.pulls.get({ ...base, pull_number: prNumber });
22
- const files = await octokit.paginate(octokit.rest.pulls.listFiles, {
21
+ const { data: pr } = await github.rest.pulls.get({ ...base, pull_number: prNumber });
22
+ const files = await github.paginate(github.rest.pulls.listFiles, {
23
23
  ...base,
24
24
  pull_number: prNumber,
25
25
  per_page: 100,
26
26
  });
27
- const stackTarget = await stackTargetRef(octokit, owner, name, pr);
27
+ const stackTarget = await stackTargetRef(github, owner, name, pr);
28
28
  const ctx = {
29
29
  // The caller's answer wins whenever it has one. The commit-count fallback
30
30
  // is a guess kept only so existing callers keep working.
@@ -43,7 +43,7 @@ export async function predict(octokit, repo, prNumber, opts = {}) {
43
43
  // is in from the start: it is read even on the skip path, where the commit
44
44
  // message is what decides the verdict.
45
45
  const sources = new Map([[sourceKey(headSource), headSource]]);
46
- const { data: headCommit } = await octokit.rest.repos.getCommit({
46
+ const { data: headCommit } = await github.rest.repos.getCommit({
47
47
  ...base,
48
48
  ref: headSha,
49
49
  });
@@ -63,7 +63,7 @@ export async function predict(octokit, repo, prNumber, opts = {}) {
63
63
  }
64
64
  let sha;
65
65
  try {
66
- const { data } = await octokit.rest.repos.getCommit({
66
+ const { data } = await github.rest.repos.getCommit({
67
67
  owner: src.owner,
68
68
  repo: src.repo,
69
69
  ref: src.ref,
@@ -96,12 +96,11 @@ export async function predict(octokit, repo, prNumber, opts = {}) {
96
96
  }
97
97
  let content;
98
98
  try {
99
- const { data } = await octokit.rest.repos.getContent({
99
+ const { data } = await github.rest.repos.getContent({
100
100
  owner: src.owner,
101
101
  repo: src.repo,
102
102
  path,
103
103
  ref: src.sha,
104
- mediaType: { format: "raw" },
105
104
  });
106
105
  content = data;
107
106
  }
@@ -116,9 +115,9 @@ export async function predict(octokit, repo, prNumber, opts = {}) {
116
115
  const reader = { fetchWorkflow, resolveRef };
117
116
  // Execution is on by default and costs nothing until a workflow needs it.
118
117
  const executor = opts.executor === undefined
119
- ? makeLiveExecutor(octokit, headSource, resolveRef)
118
+ ? makeLiveExecutor(github, headSource, resolveRef)
120
119
  : (opts.executor ?? undefined);
121
- const workflows = await octokit.paginate(octokit.rest.actions.listRepoWorkflows, {
120
+ const workflows = await github.paginate(github.rest.actions.listRepoWorkflows, {
122
121
  ...base,
123
122
  per_page: 100,
124
123
  });
@@ -1,4 +1,4 @@
1
- import type { Octokit } from "@octokit/rest";
1
+ import type { GithubClient } from "./makeGithubClient.js";
2
2
  import type { StackNode } from "../types.js";
3
3
  /**
4
4
  * The branch this PR's stack ultimately targets, or null for a plain PR.
@@ -11,4 +11,4 @@ import type { StackNode } from "../types.js";
11
11
  * parent PR's own merge sha in stacked mode. Anything undecidable ends the
12
12
  * walk at the last proven hop; never throws.
13
13
  */
14
- export declare function stackTargetRef(octokit: Octokit, owner: string, repo: string, pr: StackNode): Promise<string | null>;
14
+ export declare function stackTargetRef(github: GithubClient, owner: string, repo: string, pr: StackNode): Promise<string | null>;
@@ -11,7 +11,7 @@ const MAX_STACK_DEPTH = 10;
11
11
  * parent PR's own merge sha in stacked mode. Anything undecidable ends the
12
12
  * walk at the last proven hop; never throws.
13
13
  */
14
- export async function stackTargetRef(octokit, owner, repo, pr) {
14
+ export async function stackTargetRef(github, owner, repo, pr) {
15
15
  let target = null;
16
16
  let cur = pr;
17
17
  try {
@@ -20,7 +20,7 @@ export async function stackTargetRef(octokit, owner, repo, pr) {
20
20
  if (mergeSha === null) {
21
21
  break;
22
22
  }
23
- const { data: preview } = await octokit.rest.repos.getCommit({
23
+ const { data: preview } = await github.rest.repos.getCommit({
24
24
  owner,
25
25
  repo,
26
26
  ref: mergeSha,
@@ -29,7 +29,7 @@ export async function stackTargetRef(octokit, owner, repo, pr) {
29
29
  if (previewParent === undefined) {
30
30
  break;
31
31
  }
32
- const { data: baseTip } = await octokit.rest.repos.getCommit({
32
+ const { data: baseTip } = await github.rest.repos.getCommit({
33
33
  owner,
34
34
  repo,
35
35
  ref: cur.base.ref,
@@ -40,7 +40,7 @@ export async function stackTargetRef(octokit, owner, repo, pr) {
40
40
  }
41
41
  // Otherwise only an exact match against an open PR whose head is the
42
42
  // base branch proves stacked mode; a stale preview matches nothing.
43
- const { data: candidates } = await octokit.rest.pulls.list({
43
+ const { data: candidates } = await github.rest.pulls.list({
44
44
  owner,
45
45
  repo,
46
46
  state: "open",
package/dist/verify.js CHANGED
@@ -4,7 +4,7 @@
4
4
  //
5
5
  // Ground truth: workflow runs for the PR head SHA with a pull_request event,
6
6
  // and the job entries inside each run (skipped jobs included).
7
- import { isJobEntry, makeOctokit, predict } from "./index.js";
7
+ import { isJobEntry, makeGithubClient, predict } from "./index.js";
8
8
  async function actualEntries(octokit, repo, prNumber) {
9
9
  const [owner, name] = repo.split("/");
10
10
  const base = { owner, repo: name };
@@ -43,7 +43,7 @@ if (!repo || !prArg) {
43
43
  process.exit(2);
44
44
  }
45
45
  const pr = Number(prArg);
46
- const octokit = makeOctokit();
46
+ const octokit = makeGithubClient();
47
47
  const { entries: predictedRaw } = await predict(octokit, repo, pr);
48
48
  // Compare on the resolved check name — that is the string GitHub actually
49
49
  // puts on the job. Entries whose name could not be resolved statically have
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "willfire",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
4
4
  "description": "Predict the set of CI check entries GitHub Actions will create for a pull request",
5
5
  "license": "MIT",
6
6
  "packageManager": "pnpm@10.33.0",
@@ -46,10 +46,11 @@
46
46
  "verify": "tsx src/verify.ts"
47
47
  },
48
48
  "dependencies": {
49
- "@octokit/rest": "^22.0.0",
50
49
  "yaml": "^2.6.0"
51
50
  },
52
51
  "devDependencies": {
52
+ "@stryker-mutator/core": "^10.0.0",
53
+ "@stryker-mutator/vitest-runner": "^10.0.0",
53
54
  "@types/node": "^22.0.0",
54
55
  "@vitest/coverage-v8": "^4.1.10",
55
56
  "eslint": "^10.9.1",
@@ -1,2 +0,0 @@
1
- import { Octokit } from "@octokit/rest";
2
- export declare function makeOctokit(): Octokit;
@@ -1,8 +0,0 @@
1
- import { Octokit } from "@octokit/rest";
2
- export function makeOctokit() {
3
- const token = process.env.GH_TOKEN ?? process.env.GITHUB_TOKEN;
4
- if (!token) {
5
- throw new Error("GH_TOKEN or GITHUB_TOKEN must be set");
6
- }
7
- return new Octokit({ auth: token });
8
- }