@artieeez/cf-plugin-github 0.0.0-stage → 0.2.0
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 +144 -2
- package/dist/ci.d.ts +93 -0
- package/dist/ci.d.ts.map +1 -0
- package/dist/ci.js +141 -0
- package/dist/ci.js.map +1 -0
- package/dist/diff.d.ts +7 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +39 -0
- package/dist/diff.js.map +1 -0
- package/dist/index.d.ts +539 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1021 -0
- package/dist/index.js.map +1 -0
- package/dist/setup.d.ts +40 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +73 -0
- package/dist/setup.js.map +1 -0
- package/package.json +42 -3
- package/src/ci.ts +228 -0
- package/src/diff.ts +35 -0
- package/src/index.ts +1102 -0
- package/src/setup.ts +99 -0
package/README.md
CHANGED
|
@@ -1,3 +1,145 @@
|
|
|
1
|
-
#
|
|
1
|
+
# github (official plugin)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@artieeez/cf-plugin-github`, the `forge` slot. Connection: a GitHub App installation scoped
|
|
4
|
+
to a list of repos (`config: { appId, installationId, appSlug, repos }`, secrets
|
|
5
|
+
`GITHUB_APP_PRIVATE_KEY` and `GITHUB_WEBHOOK_SECRET`, both on the server only). The App
|
|
6
|
+
needs `contents: write`, `pull_requests: write` and `issues: write`, and the events
|
|
7
|
+
`check_suite`, `pull_request`, `pull_request_review`, `issue_comment` and
|
|
8
|
+
`pull_request_review_comment`.
|
|
9
|
+
|
|
10
|
+
## Set up on the dashboard
|
|
11
|
+
|
|
12
|
+
An admin opens **Integrations > GitHub > Set up** (AD-091). **Create on GitHub** posts the
|
|
13
|
+
App's manifest (the permissions and events above, the webhook URL with a new token) to
|
|
14
|
+
GitHub's new-App page for your account or an organisation. GitHub then makes the App and
|
|
15
|
+
hands its key and webhook secret back to the server, which seals them with `CF_SECRETS_KEY`.
|
|
16
|
+
Next you install the App on the repos you pick, and the instance is active with those repos,
|
|
17
|
+
with no restart. `CF_PUBLIC_URL` must be set so GitHub can reach the factory. **Connect an
|
|
18
|
+
existing App** takes its App id, installation id, private key (pasted or a `.pem` file) and
|
|
19
|
+
webhook secret, and shows the webhook URL to paste into the App.
|
|
20
|
+
|
|
21
|
+
On the instance's page, **Edit repos** chooses which of the installation's repos it covers
|
|
22
|
+
(webhooks, tokens and comments). **Remove** drops it; the App stays on GitHub until you
|
|
23
|
+
delete it in its settings.
|
|
24
|
+
|
|
25
|
+
## Instance env
|
|
26
|
+
|
|
27
|
+
There is no config file (AD-089). Env instances show read-only on the dashboard. When any of
|
|
28
|
+
these is set, the server makes one instance, named `CF_GITHUB_INSTANCE` (default `github`);
|
|
29
|
+
a missing or bad one stops it, naming the variable:
|
|
30
|
+
|
|
31
|
+
| env | config | e.g. |
|
|
32
|
+
| --- | --- | --- |
|
|
33
|
+
| `GITHUB_APP_ID` | `appId` | `1001` |
|
|
34
|
+
| `GITHUB_INSTALLATION_ID` | `installationId` | `42` |
|
|
35
|
+
| `GITHUB_APP_SLUG` | `appSlug` | `cf-factory` |
|
|
36
|
+
| `GITHUB_REPOS` | `repos`, comma-separated | `org/api,org/web` |
|
|
37
|
+
|
|
38
|
+
`CF_GITHUB_HOOK_TOKEN` (24 chars or more) is the `<token>` of the App's webhook URL
|
|
39
|
+
`/hooks/<instance>/<token>`; unset, the instance has no webhook. A workflow's `forge` slot
|
|
40
|
+
binds to the only GitHub instance by itself; its settings are set on the dashboard.
|
|
41
|
+
|
|
42
|
+
Ingress, run in-process on the server, verifies `X-Hub-Signature-256`, deduplicates on
|
|
43
|
+
`X-GitHub-Delivery`, and gives, only for the installation and its repos:
|
|
44
|
+
|
|
45
|
+
- `github.checks.completed` (`check_suite` `completed`), `github.pr.opened`,
|
|
46
|
+
`github.pr.closed` and `github.pr.merged` (`pull_request`);
|
|
47
|
+
- `github.pr.review.submitted` (`pull_request_review` `submitted`): the PR's `repo`,
|
|
48
|
+
`number`, `url`, `headSha` and `author`, the `review` (`id`, `state` `approved` \|
|
|
49
|
+
`changes_requested` \| `commented`, `body`, `commitId`, `url`, `stale` when it reviewed
|
|
50
|
+
another commit than the head), the `user` (`login`, `association`, `bot`) and `byAuthor`;
|
|
51
|
+
- `github.pr.comment.created` (`issue_comment` on a PR, `pull_request_review_comment`): the
|
|
52
|
+
same PR fields, the `comment` (`kind` `issue` \| `review`, `body`, `url`, and for review
|
|
53
|
+
comments `path`, `line`, `reviewId`, `inReplyTo`), `user`, `byAuthor`, and
|
|
54
|
+
`command`/`text` as Linear's comment commands read them.
|
|
55
|
+
|
|
56
|
+
A delivery whose sender is `<appSlug>[bot]`, the App itself, gives no event (AD-075); the
|
|
57
|
+
server's check fails, fatally, when `appSlug` is not the App's slug.
|
|
58
|
+
|
|
59
|
+
Workers hold no GitHub key (AD-072). The server runs two actions as the App:
|
|
60
|
+
`ctx.plugins.<slot>.comment({ repo, number, body })`, and
|
|
61
|
+
`ctx.plugins.<slot>.token({ repo, permissions? })`, which mints an installation token for
|
|
62
|
+
that one repo (default `contents: write`, `pull_requests: write`; at most `contents`,
|
|
63
|
+
`pull_requests`, `checks`, `actions`, `statuses`, `issues`, each `read` or `write`) and
|
|
64
|
+
returns `{ token, expiresAt }`. The token lasts an hour and appears in no event. Git and PR
|
|
65
|
+
work done with it is the App's bot's, so the repo's owner can approve the PR.
|
|
66
|
+
|
|
67
|
+
Task code helpers take a minted token, or an Octokit built from one:
|
|
68
|
+
`github.git.clone(ctx, { token, repo, dir, branch?, depth? })` clones `repo` into `dir`
|
|
69
|
+
under `ctx.wd` through `ctx.exec` and returns `{ dir, sha, branch }` (no token in URLs,
|
|
70
|
+
argv, `.git/config` or logs, no hooks, no global git config; fails if `dir` already exists
|
|
71
|
+
and is non-empty, AD-067), `github.git.push(ctx, { token, repo, dir, sha, branch })` pushes
|
|
72
|
+
a commit through `ctx.exec` (never forced, no hooks, the token only in a credential helper's
|
|
73
|
+
env), and `github.ci.check(ctx, { octokit, repo, sha })` classifies a sha's checks (AD-021)
|
|
74
|
+
and re-runs failed Actions jobs through the workflow's Octokit (`@octokit/rest`, a peer
|
|
75
|
+
dependency). `github.pr.open(ctx, { octokit, repo, head, base, title, body, draft? })` opens
|
|
76
|
+
a PR through that Octokit and returns `{ number, url, created }`: a PR already open for
|
|
77
|
+
`head` on `base` comes back with `created: false`, and one open on another base is an error
|
|
78
|
+
(AD-062); its token needs `contents: read` as well as `pull_requests: write`, or GitHub
|
|
79
|
+
answers 422 `not all refs are readable`.
|
|
80
|
+
`github.pr.feedback(ctx, { octokit, repo, number, reviewId })` returns a review
|
|
81
|
+
(`id`, `state`, `body`, `user`) and every inline comment of it (`path`, `line`, `body`,
|
|
82
|
+
`diffHunk`, `url`, `inReplyTo`) for a rework step.
|
|
83
|
+
`github.pr.review(ctx, { octokit, repo, number, commitId, body, comments?, key? })` posts
|
|
84
|
+
one review on `commitId` and returns `{ id, url, created, inline, inBody }` (AD-079). It
|
|
85
|
+
only comments: the event is always `COMMENT`, and approving stays a person's act. A comment
|
|
86
|
+
(`{ path, line, body }`) goes inline when GitHub's compare of the PR's base and `commitId`
|
|
87
|
+
shows that head line, else in the body under `### Outside the diff`; if GitHub still answers
|
|
88
|
+
`422`, the review is posted again with every comment in the body. The body's first line is
|
|
89
|
+
`<!-- code-factory-review:<key> -->` (default key `<runId>/<step>@<visit>`): a retried step
|
|
90
|
+
finds that review and returns it with `created: false` instead of posting twice. Its token
|
|
91
|
+
needs `contents: read` as well as `pull_requests: write`, or GitHub answers 403 to the
|
|
92
|
+
compare. `commentableLines(diff)` is exported for workflows that parse a diff themselves.
|
|
93
|
+
Anything else is a plain Octokit call.
|
|
94
|
+
|
|
95
|
+
A review reaches a workflow two ways. A `wait` on `github.match.reviewSubmitted({ slot,
|
|
96
|
+
repo, number, states?, reviewers?, sha? })` (or `commentCreated`) fires on the PR's next
|
|
97
|
+
review. Or the run claims the PR's URL as one more run key (`ctx.claimKey(pr.url)`, AD-074),
|
|
98
|
+
and `github.on.reviewAnswers({ slot, step, outcomes, data? })` or
|
|
99
|
+
`github.on.commentCommand(...)` answer its human step, keyed on `$.data.url`. Every review
|
|
100
|
+
helper counts a reviewer with write access (`OWNER`, `MEMBER`, `COLLABORATOR`), or only the
|
|
101
|
+
`reviewers` given, never a bot or the PR's author (AD-073); `reviewAnswers` skips stale
|
|
102
|
+
reviews. Merging stays a person's act on GitHub.
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
triggers: [
|
|
106
|
+
...github.on.reviewAnswers({
|
|
107
|
+
slot: 'forge',
|
|
108
|
+
step: 'review',
|
|
109
|
+
outcomes: { approved: 'approved', changes_requested: 'changes' },
|
|
110
|
+
data: { reviewId: '$.data.review.id', body: '$.data.review.body' },
|
|
111
|
+
}),
|
|
112
|
+
],
|
|
113
|
+
// deliver: const { token } = await ctx.plugins.forge.token({ repo });
|
|
114
|
+
// await github.git.push(ctx, { token, repo, dir, sha, branch });
|
|
115
|
+
// const pr = await github.pr.open(ctx, { octokit: new Octokit({ auth: token }), repo, head: branch, base: 'main', title, body });
|
|
116
|
+
// await ctx.claimKey(pr.url);
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Workflows may also declare typed GitHub settings — `github.repo()`, `github.branch()`, and
|
|
120
|
+
`github.label()` (referenced in triggers and waits with `github.setting('<name>')`) — which
|
|
121
|
+
the server validates at worker register time against the bound GitHub App installation
|
|
122
|
+
(`github.repo` must be in `config.repos` and readable by the App; `github.branch` and
|
|
123
|
+
`github.label` are checked across the workflow's declared `github.repo` settings when any
|
|
124
|
+
exist, else every repo in `config.repos`, AD-067). A CI watch is a `ci-check` task and a
|
|
125
|
+
`wait` on `github.match.checksCompleted` with a fallback timeout, in a loop:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import { github } from '@artieeez/cf-plugin-github';
|
|
129
|
+
|
|
130
|
+
plugins: { forge: github.slot() },
|
|
131
|
+
// ci-check: const { token } = await ctx.plugins.forge.token({ repo, permissions: { checks: 'read', actions: 'write' } });
|
|
132
|
+
// const octokit = new Octokit({ auth: token });
|
|
133
|
+
// const r = await github.ci.check(ctx, { octokit, repo, sha });
|
|
134
|
+
// pending / retrying -> ci-wait; success -> on; anything else -> the workflow's choice
|
|
135
|
+
'ci-wait': wait({
|
|
136
|
+
event: github.match.checksCompleted({ slot: 'forge', repo: ref('input.repo'), sha: ref('deliver.output.sha') }),
|
|
137
|
+
timeout: '2m',
|
|
138
|
+
next: { fired: 'ci-check', timeout: 'ci-check' },
|
|
139
|
+
}),
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`CF_GITHUB_API_URL` (default `https://api.github.com`) points the server's action elsewhere,
|
|
143
|
+
and `CF_GITHUB_GIT_URL` (default `https://github.com`) the push. See
|
|
144
|
+
[docs/design/plugins.md](../../docs/design/plugins.md) and
|
|
145
|
+
`.specs/features/github-plugin/plan.md`.
|
package/dist/ci.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CI classes over a sha's checks (github-plugin door 4, AD-021), boss's `watchCi`
|
|
3
|
+
* (`src/orchestrator/ci.ts`) mapped to GitHub check runs and Actions workflow runs. Pure: the
|
|
4
|
+
* caller reads the checks and starts the re-runs.
|
|
5
|
+
*/
|
|
6
|
+
/** One latest check run on the sha. */
|
|
7
|
+
export interface CheckRun {
|
|
8
|
+
id: number;
|
|
9
|
+
name: string;
|
|
10
|
+
status: string;
|
|
11
|
+
conclusion: string | null;
|
|
12
|
+
/** the app slug that owns it, e.g. `github-actions` */
|
|
13
|
+
app: string;
|
|
14
|
+
suiteId: number;
|
|
15
|
+
}
|
|
16
|
+
/** One Actions workflow run for the sha. */
|
|
17
|
+
export interface WorkflowRun {
|
|
18
|
+
id: number;
|
|
19
|
+
name: string;
|
|
20
|
+
path: string;
|
|
21
|
+
status: string;
|
|
22
|
+
conclusion: string | null;
|
|
23
|
+
suiteId: number;
|
|
24
|
+
url: string;
|
|
25
|
+
/** jobs in the run, counted only when it concluded `failure` or `startup_failure`; else `null` */
|
|
26
|
+
jobs: number | null;
|
|
27
|
+
}
|
|
28
|
+
/** What `getChecks` returns. */
|
|
29
|
+
export interface CiSnapshot {
|
|
30
|
+
sha: string;
|
|
31
|
+
checkRuns: CheckRun[];
|
|
32
|
+
workflowRuns: WorkflowRun[];
|
|
33
|
+
}
|
|
34
|
+
export type CiStatus = 'pending' | 'retrying' | 'success' | 'failed' | 'config_error' | 'canceled' | 'action_required' | 'no_checks' | 'timeout';
|
|
35
|
+
export interface CiRetry {
|
|
36
|
+
at: string;
|
|
37
|
+
workflowRunId: number;
|
|
38
|
+
jobs: string[];
|
|
39
|
+
}
|
|
40
|
+
export interface CiResult {
|
|
41
|
+
status: CiStatus;
|
|
42
|
+
sha: string;
|
|
43
|
+
startedAt: string;
|
|
44
|
+
checkedAt: string;
|
|
45
|
+
retries: CiRetry[];
|
|
46
|
+
failed?: string[];
|
|
47
|
+
url?: string;
|
|
48
|
+
note?: string;
|
|
49
|
+
}
|
|
50
|
+
export interface CiPolicy {
|
|
51
|
+
/** how long the sha may have no check run (ms) */
|
|
52
|
+
appearMs?: number;
|
|
53
|
+
/** how long, from the first check, the checks may take (ms) */
|
|
54
|
+
timeoutMs?: number;
|
|
55
|
+
/** retry rounds of failed jobs */
|
|
56
|
+
maxRetries?: number;
|
|
57
|
+
/** a failed `<workflow name> / <job name>` matching this (case-insensitive) is never re-run */
|
|
58
|
+
neverRetry?: string;
|
|
59
|
+
/** check runs whose name matches this are not counted */
|
|
60
|
+
ignore?: string;
|
|
61
|
+
}
|
|
62
|
+
/** boss `DEFAULT_CI_WATCH`. */
|
|
63
|
+
export declare const DEFAULT_CI_POLICY: {
|
|
64
|
+
readonly appearMs: 300000;
|
|
65
|
+
readonly timeoutMs: 3600000;
|
|
66
|
+
readonly maxRetries: 2;
|
|
67
|
+
readonly neverRetry: "deploy|release|publish|migrat|rollback|prod";
|
|
68
|
+
};
|
|
69
|
+
/** Most names in `failed` (door 4). */
|
|
70
|
+
export declare const CI_FAILED_MAX = 50;
|
|
71
|
+
/** Longest name in `failed` and `retries[].jobs`, and longest `note`. */
|
|
72
|
+
export declare const CI_TEXT_MAX = 200;
|
|
73
|
+
/** Most workflow runs re-run in one round; more failed runs than this is `failed`. */
|
|
74
|
+
export declare const CI_RERUN_MAX = 100;
|
|
75
|
+
/** Most retry rounds a policy may ask for. */
|
|
76
|
+
export declare const CI_RETRIES_MAX = 5;
|
|
77
|
+
/** Largest `CiResult` JSON (AD-012's output cap). */
|
|
78
|
+
export declare const CI_RESULT_MAX_BYTES = 65536;
|
|
79
|
+
/** Whether `v` reads as a `CiResult` (a step's previous output). */
|
|
80
|
+
export declare function isCiResult(v: unknown): v is CiResult;
|
|
81
|
+
/** Rounds in `retries`: entries of one round share their `at`. */
|
|
82
|
+
export declare function retryRounds(retries: CiRetry[]): number;
|
|
83
|
+
/**
|
|
84
|
+
* The class of `snapshot` at `now` (door 4). A `previous` result of the same sha carries
|
|
85
|
+
* `startedAt` and `retries`. For `retrying`, the entries after `previous`'s are the workflow runs
|
|
86
|
+
* to re-run.
|
|
87
|
+
*/
|
|
88
|
+
export declare function classifyCi(snapshot: CiSnapshot, opts: {
|
|
89
|
+
policy?: CiPolicy;
|
|
90
|
+
previous?: CiResult | undefined;
|
|
91
|
+
now: number;
|
|
92
|
+
}): CiResult;
|
|
93
|
+
//# sourceMappingURL=ci.d.ts.map
|
package/dist/ci.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ci.d.ts","sourceRoot":"","sources":["../src/ci.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AAEH,uCAAuC;AACvC,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,uDAAu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AAkC;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,+FAA+F;IAC/F,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,yDAAyD;IACzD,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,+BAA+B;AAC/B,eAAO,MAAM,iBAAiB;;;;;CAKpB,CAAC;AAEX,uCAAuC;AACvC,eAAO,MAAM,aAAa,KAAK,CAAC;AAChC,yEAAyE;AACzE,eAAO,MAAM,WAAW,MAAM,CAAC;AAC/B,sFAAsF;AACtF,eAAO,MAAM,YAAY,MAAM,CAAC;AAChC,8CAA8C;AAC9C,eAAO,MAAM,cAAc,IAAI,CAAC;AAChC,qDAAqD;AACrD,eAAO,MAAM,mBAAmB,QAAS,CAAC;AA0B1C,oEAAoE;AACpE,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,QAAQ,CAIpD;AAED,kEAAkE;AAClE,wBAAgB,WAAW,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,MAAM,CAEtD;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CACxB,QAAQ,EAAE,UAAU,EACpB,IAAI,EAAE;IAAE,MAAM,CAAC,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GACxE,QAAQ,CAoFV"}
|
package/dist/ci.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { cut } from '@artieeez/cf-core';
|
|
2
|
+
/** boss `DEFAULT_CI_WATCH`. */
|
|
3
|
+
export const DEFAULT_CI_POLICY = {
|
|
4
|
+
appearMs: 300_000,
|
|
5
|
+
timeoutMs: 3_600_000,
|
|
6
|
+
maxRetries: 2,
|
|
7
|
+
neverRetry: 'deploy|release|publish|migrat|rollback|prod',
|
|
8
|
+
};
|
|
9
|
+
/** Most names in `failed` (door 4). */
|
|
10
|
+
export const CI_FAILED_MAX = 50;
|
|
11
|
+
/** Longest name in `failed` and `retries[].jobs`, and longest `note`. */
|
|
12
|
+
export const CI_TEXT_MAX = 200;
|
|
13
|
+
/** Most workflow runs re-run in one round; more failed runs than this is `failed`. */
|
|
14
|
+
export const CI_RERUN_MAX = 100;
|
|
15
|
+
/** Most retry rounds a policy may ask for. */
|
|
16
|
+
export const CI_RETRIES_MAX = 5;
|
|
17
|
+
/** Largest `CiResult` JSON (AD-012's output cap). */
|
|
18
|
+
export const CI_RESULT_MAX_BYTES = 65_536;
|
|
19
|
+
/**
|
|
20
|
+
* Keeps `r` within `CI_RESULT_MAX_BYTES`: the job names of each retry entry shrink to 10, 1,
|
|
21
|
+
* then none, and then `failed` does the same. Retry entries themselves stay: they are what the
|
|
22
|
+
* next round counts and which workflow runs were re-run.
|
|
23
|
+
*/
|
|
24
|
+
function fit(r) {
|
|
25
|
+
const size = () => Buffer.byteLength(JSON.stringify(r));
|
|
26
|
+
for (const keep of [10, 1, 0]) {
|
|
27
|
+
if (size() <= CI_RESULT_MAX_BYTES)
|
|
28
|
+
return r;
|
|
29
|
+
r.retries = r.retries.map((e) => ({ ...e, jobs: e.jobs.slice(0, keep) }));
|
|
30
|
+
}
|
|
31
|
+
for (const keep of [10, 1, 0]) {
|
|
32
|
+
if (size() <= CI_RESULT_MAX_BYTES || r.failed === undefined)
|
|
33
|
+
return r;
|
|
34
|
+
r.failed = r.failed.slice(0, keep);
|
|
35
|
+
}
|
|
36
|
+
return r;
|
|
37
|
+
}
|
|
38
|
+
const GREEN = new Set(['success', 'neutral', 'skipped']);
|
|
39
|
+
const CANCELED = new Set(['cancelled', 'stale']);
|
|
40
|
+
const iso = (t) => new Date(t).toISOString();
|
|
41
|
+
const name = (s) => cut(s, CI_TEXT_MAX);
|
|
42
|
+
/** Whether `v` reads as a `CiResult` (a step's previous output). */
|
|
43
|
+
export function isCiResult(v) {
|
|
44
|
+
if (v === null || typeof v !== 'object')
|
|
45
|
+
return false;
|
|
46
|
+
const r = v;
|
|
47
|
+
return typeof r['sha'] === 'string' && typeof r['startedAt'] === 'string' && Array.isArray(r['retries']);
|
|
48
|
+
}
|
|
49
|
+
/** Rounds in `retries`: entries of one round share their `at`. */
|
|
50
|
+
export function retryRounds(retries) {
|
|
51
|
+
return new Set(retries.map((r) => r.at)).size;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The class of `snapshot` at `now` (door 4). A `previous` result of the same sha carries
|
|
55
|
+
* `startedAt` and `retries`. For `retrying`, the entries after `previous`'s are the workflow runs
|
|
56
|
+
* to re-run.
|
|
57
|
+
*/
|
|
58
|
+
export function classifyCi(snapshot, opts) {
|
|
59
|
+
const policy = { ...DEFAULT_CI_POLICY, ...(opts.policy ?? {}) };
|
|
60
|
+
policy.maxRetries = Math.max(0, Math.min(CI_RETRIES_MAX, Math.floor(policy.maxRetries)));
|
|
61
|
+
const same = opts.previous !== undefined && opts.previous.sha === snapshot.sha ? opts.previous : undefined;
|
|
62
|
+
const startedAt = same?.startedAt ?? iso(opts.now);
|
|
63
|
+
const retries = [...(same?.retries ?? [])];
|
|
64
|
+
const elapsed = opts.now - Date.parse(startedAt);
|
|
65
|
+
const base = { sha: snapshot.sha, startedAt, checkedAt: iso(opts.now), retries };
|
|
66
|
+
const done = (status, extra = {}) => {
|
|
67
|
+
const r = { status, ...base, ...extra };
|
|
68
|
+
if (r.note !== undefined)
|
|
69
|
+
r.note = cut(r.note, CI_TEXT_MAX);
|
|
70
|
+
if (r.failed !== undefined)
|
|
71
|
+
r.failed = r.failed.slice(0, CI_FAILED_MAX).map(name);
|
|
72
|
+
return fit(r);
|
|
73
|
+
};
|
|
74
|
+
// a workflow run that cannot start is a config error whatever the check runs say (AC 30)
|
|
75
|
+
const broken = snapshot.workflowRuns.find((w) => w.conclusion === 'startup_failure' || (w.conclusion === 'failure' && w.jobs === 0));
|
|
76
|
+
if (broken !== undefined) {
|
|
77
|
+
return done('config_error', {
|
|
78
|
+
url: broken.url,
|
|
79
|
+
note: `workflow ${broken.path} could not start (${broken.conclusion}); fix the workflow file, then run again`,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
const ignore = policy.ignore !== undefined && policy.ignore !== '' ? new RegExp(policy.ignore) : undefined;
|
|
83
|
+
const counted = snapshot.checkRuns.filter((c) => ignore === undefined || !ignore.test(c.name));
|
|
84
|
+
if (counted.length === 0) {
|
|
85
|
+
return elapsed > policy.appearMs
|
|
86
|
+
? done('no_checks', { note: `no check run on ${snapshot.sha.slice(0, 10)} in ${policy.appearMs / 1000} s` })
|
|
87
|
+
: done('pending');
|
|
88
|
+
}
|
|
89
|
+
if (counted.some((c) => c.status !== 'completed')) {
|
|
90
|
+
return elapsed > policy.timeoutMs
|
|
91
|
+
? done('timeout', { note: `checks did not complete in ${policy.timeoutMs / 1000} s` })
|
|
92
|
+
: done('pending');
|
|
93
|
+
}
|
|
94
|
+
if (counted.some((c) => c.conclusion === 'action_required')) {
|
|
95
|
+
return done('action_required', {
|
|
96
|
+
failed: counted.filter((c) => c.conclusion === 'action_required').map((c) => c.name),
|
|
97
|
+
note: 'a check asks for action on GitHub',
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
const failed = counted.filter((c) => !GREEN.has(c.conclusion ?? '') && !CANCELED.has(c.conclusion ?? ''));
|
|
101
|
+
if (failed.length === 0) {
|
|
102
|
+
if (counted.some((c) => CANCELED.has(c.conclusion ?? ''))) {
|
|
103
|
+
return done('canceled', { note: 'a check was cancelled outside the factory' });
|
|
104
|
+
}
|
|
105
|
+
return done('success');
|
|
106
|
+
}
|
|
107
|
+
const failedNames = failed.map((c) => c.name);
|
|
108
|
+
const bySuite = new Map(snapshot.workflowRuns.map((w) => [w.suiteId, w]));
|
|
109
|
+
const neverRetry = new RegExp(policy.neverRetry, 'i');
|
|
110
|
+
const blocked = failed.filter((c) => {
|
|
111
|
+
const w = bySuite.get(c.suiteId);
|
|
112
|
+
const retryable = c.conclusion === 'failure' || c.conclusion === 'timed_out';
|
|
113
|
+
return w === undefined || !retryable || neverRetry.test(`${w.name} / ${c.name}`);
|
|
114
|
+
});
|
|
115
|
+
if (blocked.length > 0) {
|
|
116
|
+
return done('failed', {
|
|
117
|
+
failed: failedNames,
|
|
118
|
+
note: `checks the factory does not re-run (${blocked.map((c) => c.name).join(', ')}): a person acts`,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
const rounds = retryRounds(retries);
|
|
122
|
+
if (rounds >= policy.maxRetries) {
|
|
123
|
+
return done('failed', { failed: failedNames, note: `jobs still failed after ${rounds} retry round(s)` });
|
|
124
|
+
}
|
|
125
|
+
const at = iso(opts.now);
|
|
126
|
+
const runs = new Map();
|
|
127
|
+
for (const c of failed) {
|
|
128
|
+
const id = bySuite.get(c.suiteId).id;
|
|
129
|
+
const jobs = runs.get(id) ?? [];
|
|
130
|
+
if (jobs.length < CI_FAILED_MAX)
|
|
131
|
+
jobs.push(name(c.name));
|
|
132
|
+
runs.set(id, jobs);
|
|
133
|
+
}
|
|
134
|
+
if (runs.size > CI_RERUN_MAX) {
|
|
135
|
+
return done('failed', { failed: failedNames, note: `${runs.size} workflow runs failed; the factory re-runs at most ${CI_RERUN_MAX}` });
|
|
136
|
+
}
|
|
137
|
+
for (const [workflowRunId, jobs] of runs)
|
|
138
|
+
retries.push({ at, workflowRunId, jobs });
|
|
139
|
+
return done('retrying', { failed: failedNames, note: `re-running failed jobs, round ${rounds + 1} of ${policy.maxRetries}` });
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=ci.js.map
|
package/dist/ci.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ci.js","sourceRoot":"","sources":["../src/ci.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAgFxC,+BAA+B;AAC/B,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,QAAQ,EAAE,OAAO;IACjB,SAAS,EAAE,SAAS;IACpB,UAAU,EAAE,CAAC;IACb,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G,CAAC;IACD,MAAM,EAAE,GAAG,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,MAAM,IAAI,GAAG,IAAI,GAAG,EAAoB,CAAC;IACzC,KAAK,MAAM,CAAC,IAAI,MAAM,EAAE,CAAC;QACvB,MAAM,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAE,CAAC,EAAE,CAAC;QACtC,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC;QAChC,IAAI,IAAI,CAAC,MAAM,GAAG,aAAa;YAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QACzD,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IACrB,CAAC;IACD,IAAI,IAAI,CAAC,IAAI,GAAG,YAAY,EAAE,CAAC;QAC7B,OAAO,IAAI,CAAC,QAAQ,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,IAAI,sDAAsD,YAAY,EAAE,EAAE,CAAC,CAAC;IACzI,CAAC;IACD,KAAK,MAAM,CAAC,aAAa,EAAE,IAAI,CAAC,IAAI,IAAI;QAAE,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;IACpF,OAAO,IAAI,CAAC,UAAU,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,IAAI,EAAE,iCAAiC,MAAM,GAAG,CAAC,OAAO,MAAM,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;AAChI,CAAC"}
|
package/dist/diff.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lines of each file's head version that a unified diff shows (added and context lines):
|
|
3
|
+
* the only lines GitHub takes an inline review comment on (side `RIGHT`). Keys are head paths;
|
|
4
|
+
* a deleted file (`+++ /dev/null`) has none (github-review-helper door 1).
|
|
5
|
+
*/
|
|
6
|
+
export declare function commentableLines(diff: string): Map<string, Set<number>>;
|
|
7
|
+
//# sourceMappingURL=diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CA6BvE"}
|
package/dist/diff.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lines of each file's head version that a unified diff shows (added and context lines):
|
|
3
|
+
* the only lines GitHub takes an inline review comment on (side `RIGHT`). Keys are head paths;
|
|
4
|
+
* a deleted file (`+++ /dev/null`) has none (github-review-helper door 1).
|
|
5
|
+
*/
|
|
6
|
+
export function commentableLines(diff) {
|
|
7
|
+
const out = new Map();
|
|
8
|
+
let lines;
|
|
9
|
+
// between `diff --git` and the file's first hunk: `--- `/`+++ ` are headers, not content
|
|
10
|
+
let header = false;
|
|
11
|
+
let next = 0;
|
|
12
|
+
for (const raw of diff.split('\n')) {
|
|
13
|
+
if (raw.startsWith('diff --git ')) {
|
|
14
|
+
lines = undefined;
|
|
15
|
+
header = true;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (header && raw.startsWith('+++ ')) {
|
|
19
|
+
const path = raw.slice(4);
|
|
20
|
+
lines = path === '/dev/null' ? undefined : new Set();
|
|
21
|
+
if (lines !== undefined)
|
|
22
|
+
out.set(path.replace(/^b\//, ''), lines);
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const hunk = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(raw);
|
|
26
|
+
if (hunk !== null) {
|
|
27
|
+
header = false;
|
|
28
|
+
next = Number(hunk[1]);
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (header || lines === undefined || raw.startsWith('\\'))
|
|
32
|
+
continue;
|
|
33
|
+
if (raw.startsWith('+') || raw.startsWith(' '))
|
|
34
|
+
lines.add(next++);
|
|
35
|
+
// a removed line (`-`) has no head line
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=diff.js.map
|
package/dist/diff.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diff.js","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,MAAM,GAAG,GAAG,IAAI,GAAG,EAAuB,CAAC;IAC3C,IAAI,KAA8B,CAAC;IACnC,yFAAyF;IACzF,IAAI,MAAM,GAAG,KAAK,CAAC;IACnB,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;QACnC,IAAI,GAAG,CAAC,UAAU,CAAC,aAAa,CAAC,EAAE,CAAC;YAClC,KAAK,GAAG,SAAS,CAAC;YAClB,MAAM,GAAG,IAAI,CAAC;YACd,SAAS;QACX,CAAC;QACD,IAAI,MAAM,IAAI,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;YACrC,MAAM,IAAI,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAC1B,KAAK,GAAG,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,CAAC;YACrD,IAAI,KAAK,KAAK,SAAS;gBAAE,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;YAClE,SAAS;QACX,CAAC;QACD,MAAM,IAAI,GAAG,u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