@rmartz/pr-policy 1.5.1 → 1.7.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 +1 -0
- package/dist/bin/pr-policy.js +9 -3
- package/dist/{chunk-KWZP3U4O.js → chunk-JJ5AKEQ3.js} +181 -173
- package/dist/index.d.ts +128 -10
- package/dist/index.js +142 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ new required-status name.
|
|
|
19
19
|
| **Base branch** ([docs](docs/checks/base-branch.md)) | The PR targets a branch other than the default and it isn't a non-epic PR on an open epic's branch. A stacked PR waits for its base PR (shows as pending); a base no open PR heads blocks. |
|
|
20
20
|
| **Title-type rules** ([docs](docs/checks/title.md)) | The squash title isn't a valid Conventional Commit, puts `!` on a non-functional type, disagrees with the `breaking change` label, types a linter/formatter bump as a non-release, non-`ci` type, or changes paths its type rules out (`docs`, `test`, `refactor`, `ci`). |
|
|
21
21
|
| **UAT sign-off** ([docs](docs/checks/uat.md)) | pr-lifecycle's sign-off status says the head needs UAT (or, before a repo cuts over, the PR isn't docs, tests, CI, dependency-only, or repo metadata), or the status can't be trusted, and no trusted user has applied `no UAT needed`, or `UAT passed` (or `tested`) since the last push (shows as pending). |
|
|
22
|
+
| **UAT label presence** ([docs](docs/checks/uat-label.md)), opt-in | `require-uat-label` is on and the PR carries none of `no UAT needed`, `UAT pending`, `UAT ready`, `UAT passed` (shows as pending). |
|
|
22
23
|
| **Dependabot claim verification** ([docs](docs/checks/dependabot.md)) | A Dependabot PR touches a non-dependency path, changes a dependency its description doesn't claim, pins a version other than the claimed one, or edits anything Dependabot never edits (a `package.json` script, a workflow step). |
|
|
23
24
|
| Domain labels (backlog) | Never blocks. Additive path-glob → domain-label map. |
|
|
24
25
|
| Milestone inheritance (backlog) | Never blocks. Issue → PR milestone. |
|
package/dist/bin/pr-policy.js
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
postVerdict,
|
|
9
9
|
resolveRepoTarget,
|
|
10
10
|
selectChecks
|
|
11
|
-
} from "../chunk-
|
|
11
|
+
} from "../chunk-JJ5AKEQ3.js";
|
|
12
12
|
|
|
13
13
|
// src/bin/pr-policy.ts
|
|
14
14
|
import { readFile } from "fs/promises";
|
|
@@ -31,7 +31,11 @@ var USAGE = `Usage:
|
|
|
31
31
|
--sign-off-status-creators <login,...>
|
|
32
32
|
GitHub App logins trusted to post the pr-lifecycle/sign-off status,
|
|
33
33
|
e.g. rmartz-merge-bot[bot]. Exact matches only; github-actions[bot]
|
|
34
|
-
is refused. A person who could merge is always trusted
|
|
34
|
+
is refused. A person who could merge is always trusted.
|
|
35
|
+
--require-uat-label
|
|
36
|
+
Hold any PR that carries no UAT label (no UAT needed, UAT pending,
|
|
37
|
+
UAT ready, UAT passed). Off by default; turn it on once pr-lifecycle,
|
|
38
|
+
which applies the labels, runs on the default branch.`;
|
|
35
39
|
async function readInput(path) {
|
|
36
40
|
if (path !== "-") return readFile(path, "utf8");
|
|
37
41
|
const chunks = [];
|
|
@@ -50,6 +54,7 @@ async function main(argv) {
|
|
|
50
54
|
json: { type: "boolean" },
|
|
51
55
|
"skip-uat": { type: "boolean" },
|
|
52
56
|
"sign-off-status-creators": { type: "string" },
|
|
57
|
+
"require-uat-label": { type: "boolean" },
|
|
53
58
|
help: { type: "boolean", short: "h" }
|
|
54
59
|
}
|
|
55
60
|
});
|
|
@@ -63,7 +68,8 @@ async function main(argv) {
|
|
|
63
68
|
}
|
|
64
69
|
const checks = selectChecks({
|
|
65
70
|
skipUat: values["skip-uat"],
|
|
66
|
-
signOffStatusCreators: (values["sign-off-status-creators"] ?? "").split(",")
|
|
71
|
+
signOffStatusCreators: (values["sign-off-status-creators"] ?? "").split(","),
|
|
72
|
+
requireUatLabel: values["require-uat-label"]
|
|
67
73
|
});
|
|
68
74
|
if (values.facts !== void 0) {
|
|
69
75
|
const evaluation = await evaluatePolicy(parseFacts(await readInput(values.facts)), checks);
|
|
@@ -6,6 +6,17 @@ var CI_CHANGE_APPROVED_LABEL = "CI change approved";
|
|
|
6
6
|
var UAT_PASSED_LABEL = "UAT passed";
|
|
7
7
|
var LEGACY_UAT_PASSED_LABEL = "tested";
|
|
8
8
|
var NO_UAT_NEEDED_LABEL = "no UAT needed";
|
|
9
|
+
var UAT_PENDING_LABEL = "UAT pending";
|
|
10
|
+
var UAT_READY_LABEL = "UAT ready";
|
|
11
|
+
var LEGACY_UAT_READY_LABEL = "ready for UAT";
|
|
12
|
+
var UAT_LABELS = [
|
|
13
|
+
NO_UAT_NEEDED_LABEL,
|
|
14
|
+
UAT_PENDING_LABEL,
|
|
15
|
+
UAT_READY_LABEL,
|
|
16
|
+
UAT_PASSED_LABEL,
|
|
17
|
+
LEGACY_UAT_PASSED_LABEL,
|
|
18
|
+
LEGACY_UAT_READY_LABEL
|
|
19
|
+
];
|
|
9
20
|
var CONTAINED_BREAK_LABEL = "contained break";
|
|
10
21
|
var SIGN_OFF_LABELS = [
|
|
11
22
|
CI_CHANGE_APPROVED_LABEL,
|
|
@@ -1550,6 +1561,38 @@ var titleCheck = {
|
|
|
1550
1561
|
evaluate: async (pr) => decideTitle(pr)
|
|
1551
1562
|
};
|
|
1552
1563
|
|
|
1564
|
+
// src/checks/uat-label.ts
|
|
1565
|
+
var UAT_LABEL_CHECK = "uat-label";
|
|
1566
|
+
var UAT = new Set(UAT_LABELS.map((label) => label.toLowerCase()));
|
|
1567
|
+
function decideUatLabel(pr) {
|
|
1568
|
+
const present = pr.labels.filter((label) => UAT.has(label.toLowerCase()));
|
|
1569
|
+
if (present.length > 0) {
|
|
1570
|
+
const names = present.map((label) => `\`${label}\``).join(", ");
|
|
1571
|
+
return {
|
|
1572
|
+
findings: [
|
|
1573
|
+
{
|
|
1574
|
+
check: UAT_LABEL_CHECK,
|
|
1575
|
+
message: `UAT determination recorded: ${names}.`,
|
|
1576
|
+
effect: "info"
|
|
1577
|
+
}
|
|
1578
|
+
]
|
|
1579
|
+
};
|
|
1580
|
+
}
|
|
1581
|
+
return {
|
|
1582
|
+
findings: [
|
|
1583
|
+
{
|
|
1584
|
+
check: UAT_LABEL_CHECK,
|
|
1585
|
+
message: "Waiting for a UAT determination: no UAT label is on the PR. pr-lifecycle applies one of `no UAT needed`, `UAT pending`, `UAT ready`, or `UAT passed` when it runs; a person can apply one too.",
|
|
1586
|
+
effect: "hold"
|
|
1587
|
+
}
|
|
1588
|
+
]
|
|
1589
|
+
};
|
|
1590
|
+
}
|
|
1591
|
+
var uatLabelCheck = {
|
|
1592
|
+
name: UAT_LABEL_CHECK,
|
|
1593
|
+
evaluate: async (pr) => decideUatLabel(pr)
|
|
1594
|
+
};
|
|
1595
|
+
|
|
1553
1596
|
// src/checks/base-branch.ts
|
|
1554
1597
|
var BASE_BRANCH_CHECK = "base-branch";
|
|
1555
1598
|
var isEpic = (labels) => labels.some((label) => label.trim().toLowerCase() === EPIC_LABEL);
|
|
@@ -1727,7 +1770,11 @@ function selectChecks(options = {}) {
|
|
|
1727
1770
|
if (check === ciChangeCheck) return ciChangeCheckFor(trustedBots);
|
|
1728
1771
|
return check;
|
|
1729
1772
|
});
|
|
1730
|
-
|
|
1773
|
+
const selected = options.skipUat ? configured.filter((check) => check.name !== uatCheck.name) : configured;
|
|
1774
|
+
if (!options.requireUatLabel) return selected;
|
|
1775
|
+
return selected.flatMap(
|
|
1776
|
+
(check) => check === dependabotCheck ? [uatLabelCheck, check] : [check]
|
|
1777
|
+
);
|
|
1731
1778
|
}
|
|
1732
1779
|
|
|
1733
1780
|
// src/report.ts
|
|
@@ -1749,10 +1796,10 @@ function buildReport(findings) {
|
|
|
1749
1796
|
outcome = "pending";
|
|
1750
1797
|
title = `Waiting on ${plural(holds.length, "human sign-off")}`;
|
|
1751
1798
|
}
|
|
1752
|
-
const
|
|
1799
|
+
const lines = findings.map(
|
|
1753
1800
|
(finding) => `- ${ICONS[finding.effect]} **${finding.check}**: ${finding.message}`
|
|
1754
1801
|
);
|
|
1755
|
-
const summary =
|
|
1802
|
+
const summary = lines.length > 0 ? lines.join("\n") : "No policy findings.";
|
|
1756
1803
|
return { outcome, title, summary, findings };
|
|
1757
1804
|
}
|
|
1758
1805
|
|
|
@@ -2044,6 +2091,48 @@ async function removeLabel(repo, issue, label, call = {}) {
|
|
|
2044
2091
|
return out !== null ? out.trim() : null;
|
|
2045
2092
|
}
|
|
2046
2093
|
|
|
2094
|
+
// src/github/client.ts
|
|
2095
|
+
function createGhClient(options = {}) {
|
|
2096
|
+
const call = { cwd: options.cwd };
|
|
2097
|
+
const lines = (out) => out.split("\n").filter((line) => line.trim() !== "").map((line) => JSON.parse(line));
|
|
2098
|
+
return {
|
|
2099
|
+
async getJson(path) {
|
|
2100
|
+
const out = await ghCall({ argv: ["gh", "api", path] }, null, call);
|
|
2101
|
+
return out === null ? null : JSON.parse(out);
|
|
2102
|
+
},
|
|
2103
|
+
async list(path, key) {
|
|
2104
|
+
const jq = key === void 0 ? ".[]" : `.${key}[]`;
|
|
2105
|
+
const out = await ghCall({ argv: ["gh", "api", "--paginate", path, "--jq", jq] }, null, call);
|
|
2106
|
+
return out === null ? null : lines(out);
|
|
2107
|
+
},
|
|
2108
|
+
async getRaw(path) {
|
|
2109
|
+
return ghCall(
|
|
2110
|
+
{ argv: ["gh", "api", "-H", "Accept: application/vnd.github.raw", path] },
|
|
2111
|
+
null,
|
|
2112
|
+
call
|
|
2113
|
+
);
|
|
2114
|
+
},
|
|
2115
|
+
async send(method, path, body) {
|
|
2116
|
+
const out = await ghCall(
|
|
2117
|
+
{ argv: ["gh", "api", "-X", method, path, "--input", "-"], stdin: JSON.stringify(body) },
|
|
2118
|
+
null,
|
|
2119
|
+
call
|
|
2120
|
+
);
|
|
2121
|
+
if (out === null) return null;
|
|
2122
|
+
return out.trim() === "" ? {} : JSON.parse(out);
|
|
2123
|
+
},
|
|
2124
|
+
async addLabels(repo, issue, labels) {
|
|
2125
|
+
await addLabels(repo, issue, [...labels], call);
|
|
2126
|
+
},
|
|
2127
|
+
async removeLabel(repo, issue, label) {
|
|
2128
|
+
await removeLabel(repo, issue, label, call);
|
|
2129
|
+
}
|
|
2130
|
+
};
|
|
2131
|
+
}
|
|
2132
|
+
function clientOf(target) {
|
|
2133
|
+
return target.client ?? createGhClient({ cwd: target.cwd });
|
|
2134
|
+
}
|
|
2135
|
+
|
|
2047
2136
|
// src/github/check-run.ts
|
|
2048
2137
|
function checkRunBody(evaluation, now) {
|
|
2049
2138
|
const output = { title: evaluation.title, summary: evaluation.summary };
|
|
@@ -2055,89 +2144,75 @@ function checkRunBody(evaluation, now) {
|
|
|
2055
2144
|
output
|
|
2056
2145
|
};
|
|
2057
2146
|
}
|
|
2058
|
-
async function openRun(target, headSha) {
|
|
2059
|
-
const out = await
|
|
2060
|
-
{
|
|
2061
|
-
argv: [
|
|
2062
|
-
"gh",
|
|
2063
|
-
"api",
|
|
2064
|
-
`repos/${target.repo}/commits/${headSha}/check-runs?check_name=${PR_POLICY_CHECK_NAME}&filter=latest`,
|
|
2065
|
-
"--jq",
|
|
2066
|
-
".check_runs"
|
|
2067
|
-
]
|
|
2068
|
-
},
|
|
2069
|
-
null,
|
|
2070
|
-
{ cwd: target.cwd }
|
|
2147
|
+
async function openRun(target, headSha, name) {
|
|
2148
|
+
const out = await clientOf(target).getJson(
|
|
2149
|
+
`repos/${target.repo}/commits/${headSha}/check-runs?check_name=${encodeURIComponent(name)}&filter=latest`
|
|
2071
2150
|
);
|
|
2072
|
-
if (out === null) throw new Error(`could not list ${
|
|
2073
|
-
const runs =
|
|
2151
|
+
if (out === null) throw new Error(`could not list ${name} check-runs`);
|
|
2152
|
+
const runs = out.check_runs;
|
|
2074
2153
|
return runs.find((run) => run.status !== "completed")?.id ?? null;
|
|
2075
2154
|
}
|
|
2076
|
-
async function postCheckRun(target, headSha, evaluation) {
|
|
2155
|
+
async function postCheckRun(target, headSha, evaluation, options = {}) {
|
|
2156
|
+
const name = options.name ?? PR_POLICY_CHECK_NAME;
|
|
2077
2157
|
const body = checkRunBody(evaluation, /* @__PURE__ */ new Date());
|
|
2078
|
-
const existing = await openRun(target, headSha);
|
|
2079
|
-
const
|
|
2080
|
-
const
|
|
2081
|
-
|
|
2082
|
-
|
|
2158
|
+
const existing = await openRun(target, headSha, name);
|
|
2159
|
+
const client = clientOf(target);
|
|
2160
|
+
const out = existing === null ? await client.send("POST", `repos/${target.repo}/check-runs`, {
|
|
2161
|
+
name,
|
|
2162
|
+
head_sha: headSha,
|
|
2163
|
+
...body
|
|
2164
|
+
}) : await client.send("PATCH", `repos/${target.repo}/check-runs/${existing}`, body);
|
|
2165
|
+
if (out === null) throw new Error(`could not post the ${name} check-run`);
|
|
2083
2166
|
}
|
|
2084
2167
|
var STATUS_DESCRIPTION_MAX = 140;
|
|
2085
|
-
function statusBody(evaluation, runUrl) {
|
|
2168
|
+
function statusBody(evaluation, runUrl, name = PR_POLICY_CHECK_NAME) {
|
|
2086
2169
|
return {
|
|
2087
2170
|
state: evaluation.outcome,
|
|
2088
|
-
context:
|
|
2171
|
+
context: name,
|
|
2089
2172
|
description: evaluation.title.slice(0, STATUS_DESCRIPTION_MAX),
|
|
2090
2173
|
...runUrl ? { target_url: runUrl } : {}
|
|
2091
2174
|
};
|
|
2092
2175
|
}
|
|
2093
|
-
async function postCommitStatus(target, headSha, evaluation) {
|
|
2176
|
+
async function postCommitStatus(target, headSha, evaluation, name) {
|
|
2094
2177
|
const { GITHUB_SERVER_URL, GITHUB_REPOSITORY, GITHUB_RUN_ID } = process.env;
|
|
2095
2178
|
const runUrl = GITHUB_SERVER_URL && GITHUB_REPOSITORY && GITHUB_RUN_ID ? `${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}` : void 0;
|
|
2096
|
-
const out = await
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
},
|
|
2101
|
-
null,
|
|
2102
|
-
{ cwd: target.cwd }
|
|
2179
|
+
const out = await clientOf(target).send(
|
|
2180
|
+
"POST",
|
|
2181
|
+
`repos/${target.repo}/statuses/${headSha}`,
|
|
2182
|
+
statusBody(evaluation, runUrl, name)
|
|
2103
2183
|
);
|
|
2104
2184
|
if (out === null) {
|
|
2105
2185
|
console.warn(
|
|
2106
|
-
`::warning::could not set the ${
|
|
2186
|
+
`::warning::could not set the ${name} commit status on ${headSha} \u2014 does the caller workflow grant \`statuses: write\`? (rmartz/pr-policy#24)`
|
|
2107
2187
|
);
|
|
2108
2188
|
}
|
|
2109
2189
|
}
|
|
2110
|
-
async function postVerdict(target, headSha, evaluation) {
|
|
2111
|
-
|
|
2112
|
-
await
|
|
2190
|
+
async function postVerdict(target, headSha, evaluation, options = {}) {
|
|
2191
|
+
const name = options.name ?? PR_POLICY_CHECK_NAME;
|
|
2192
|
+
await postCheckRun(target, headSha, evaluation, { name });
|
|
2193
|
+
await postCommitStatus(target, headSha, evaluation, name);
|
|
2113
2194
|
}
|
|
2114
2195
|
|
|
2115
2196
|
// src/github/head-push.ts
|
|
2116
|
-
async function
|
|
2117
|
-
const out = await
|
|
2118
|
-
cwd: target.cwd
|
|
2119
|
-
});
|
|
2197
|
+
async function list(target, endpoint, key) {
|
|
2198
|
+
const out = await clientOf(target).list(endpoint, key);
|
|
2120
2199
|
if (out === null) throw new Error(`could not read ${endpoint}`);
|
|
2121
|
-
return out
|
|
2200
|
+
return out;
|
|
2122
2201
|
}
|
|
2123
2202
|
function times(stamps) {
|
|
2124
2203
|
return stamps.map((stamp) => Date.parse(stamp)).filter((ms) => !Number.isNaN(ms));
|
|
2125
2204
|
}
|
|
2126
2205
|
async function gatherHeadPushedAt(target, headSha) {
|
|
2206
|
+
const events = await list(target, `repos/${target.repo}/issues/${target.pr}/events`);
|
|
2127
2207
|
const forcePushes = times(
|
|
2128
|
-
|
|
2129
|
-
target,
|
|
2130
|
-
`repos/${target.repo}/issues/${target.pr}/events`,
|
|
2131
|
-
'.[] | select(.event == "head_ref_force_pushed") | .created_at'
|
|
2132
|
-
)
|
|
2208
|
+
events.filter((event) => event.event === "head_ref_force_pushed").map((e) => e.created_at)
|
|
2133
2209
|
);
|
|
2134
|
-
const
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
".check_suites[].created_at"
|
|
2139
|
-
)
|
|
2210
|
+
const checkSuites = await list(
|
|
2211
|
+
target,
|
|
2212
|
+
`repos/${target.repo}/commits/${headSha}/check-suites`,
|
|
2213
|
+
"check_suites"
|
|
2140
2214
|
);
|
|
2215
|
+
const suites = times(checkSuites.map((suite) => suite.created_at));
|
|
2141
2216
|
const candidates = [...forcePushes];
|
|
2142
2217
|
if (suites.length > 0) candidates.push(Math.min(...suites));
|
|
2143
2218
|
if (candidates.length === 0) return void 0;
|
|
@@ -2146,39 +2221,21 @@ async function gatherHeadPushedAt(target, headSha) {
|
|
|
2146
2221
|
|
|
2147
2222
|
// src/github/sign-offs.ts
|
|
2148
2223
|
async function listLabelEvents(target) {
|
|
2149
|
-
const out = await
|
|
2150
|
-
{
|
|
2151
|
-
argv: [
|
|
2152
|
-
"gh",
|
|
2153
|
-
"api",
|
|
2154
|
-
"--paginate",
|
|
2155
|
-
`repos/${target.repo}/issues/${target.pr}/events`,
|
|
2156
|
-
"--jq",
|
|
2157
|
-
'.[] | select(.event == "labeled") | {label: .label.name, login: .actor.login, type: .actor.type, at: .created_at}'
|
|
2158
|
-
]
|
|
2159
|
-
},
|
|
2160
|
-
null,
|
|
2161
|
-
{ cwd: target.cwd }
|
|
2162
|
-
);
|
|
2224
|
+
const out = await clientOf(target).list(`repos/${target.repo}/issues/${target.pr}/events`);
|
|
2163
2225
|
if (out === null) throw new Error(`could not list label events of ${target.repo}#${target.pr}`);
|
|
2164
|
-
return out.
|
|
2226
|
+
return out.filter((event) => event.event === "labeled").map((event) => ({
|
|
2227
|
+
label: event.label?.name ?? "",
|
|
2228
|
+
login: event.actor?.login ?? null,
|
|
2229
|
+
type: event.actor?.type ?? null,
|
|
2230
|
+
at: event.created_at
|
|
2231
|
+
}));
|
|
2165
2232
|
}
|
|
2166
2233
|
async function lookupPermission(target, login) {
|
|
2167
|
-
const out = await
|
|
2168
|
-
{
|
|
2169
|
-
argv: [
|
|
2170
|
-
"gh",
|
|
2171
|
-
"api",
|
|
2172
|
-
`repos/${target.repo}/collaborators/${encodeURIComponent(login)}/permission`,
|
|
2173
|
-
"--jq",
|
|
2174
|
-
"{permission, role_name}"
|
|
2175
|
-
]
|
|
2176
|
-
},
|
|
2177
|
-
null,
|
|
2178
|
-
{ cwd: target.cwd }
|
|
2234
|
+
const out = await clientOf(target).getJson(
|
|
2235
|
+
`repos/${target.repo}/collaborators/${encodeURIComponent(login)}/permission`
|
|
2179
2236
|
);
|
|
2180
2237
|
if (out === null) return "none";
|
|
2181
|
-
const { permission, role_name: roleName } =
|
|
2238
|
+
const { permission, role_name: roleName } = out;
|
|
2182
2239
|
if (isRepoPermission(roleName)) return roleName;
|
|
2183
2240
|
return isRepoPermission(permission) ? permission : "none";
|
|
2184
2241
|
}
|
|
@@ -2202,100 +2259,57 @@ async function gatherSignOffs(target, labels) {
|
|
|
2202
2259
|
// src/github/sign-off-status.ts
|
|
2203
2260
|
async function gatherSignOffStatus(target, sha) {
|
|
2204
2261
|
const endpoint = `repos/${target.repo}/commits/${sha}/statuses`;
|
|
2205
|
-
const out = await
|
|
2206
|
-
{
|
|
2207
|
-
argv: [
|
|
2208
|
-
"gh",
|
|
2209
|
-
"api",
|
|
2210
|
-
"--paginate",
|
|
2211
|
-
endpoint,
|
|
2212
|
-
"--jq",
|
|
2213
|
-
".[] | {context, state, description, login: .creator.login, type: .creator.type}"
|
|
2214
|
-
]
|
|
2215
|
-
},
|
|
2216
|
-
null,
|
|
2217
|
-
{ cwd: target.cwd }
|
|
2218
|
-
);
|
|
2262
|
+
const out = await clientOf(target).list(endpoint);
|
|
2219
2263
|
if (out === null) throw new Error(`could not read ${endpoint}`);
|
|
2220
|
-
const latest = out.
|
|
2264
|
+
const latest = out.find(
|
|
2265
|
+
(status2) => status2.context === SIGN_OFF_STATUS_CONTEXT
|
|
2266
|
+
);
|
|
2221
2267
|
if (latest === void 0) return void 0;
|
|
2222
2268
|
const status = { state: latest.state, description: latest.description ?? "" };
|
|
2223
|
-
if (latest.
|
|
2224
|
-
const type = latest.
|
|
2225
|
-
const permission = type === "User" ? await lookupPermission(target,
|
|
2226
|
-
return { ...status, creator: { login
|
|
2269
|
+
if (latest.creator === null) return status;
|
|
2270
|
+
const { login, type } = latest.creator;
|
|
2271
|
+
const permission = type === "User" ? await lookupPermission(target, login) : "none";
|
|
2272
|
+
return { ...status, creator: { login, type, permission } };
|
|
2227
2273
|
}
|
|
2228
2274
|
|
|
2229
2275
|
// src/github/pull-request.ts
|
|
2230
|
-
async function
|
|
2231
|
-
const out = await
|
|
2232
|
-
if (out === null) throw new Error(`
|
|
2233
|
-
return
|
|
2276
|
+
async function getJson(target, path) {
|
|
2277
|
+
const out = await clientOf(target).getJson(path);
|
|
2278
|
+
if (out === null) throw new Error(`GitHub call failed: GET ${path}`);
|
|
2279
|
+
return out;
|
|
2234
2280
|
}
|
|
2235
2281
|
async function blob(target, path, ref) {
|
|
2236
|
-
const out = await
|
|
2237
|
-
{
|
|
2238
|
-
argv: [
|
|
2239
|
-
"gh",
|
|
2240
|
-
"api",
|
|
2241
|
-
"-H",
|
|
2242
|
-
"Accept: application/vnd.github.raw",
|
|
2243
|
-
`repos/${target.repo}/contents/${path}?ref=${encodeURIComponent(ref)}`
|
|
2244
|
-
]
|
|
2245
|
-
},
|
|
2246
|
-
null,
|
|
2247
|
-
{ cwd: target.cwd }
|
|
2282
|
+
const out = await clientOf(target).getRaw(
|
|
2283
|
+
`repos/${target.repo}/contents/${path}?ref=${encodeURIComponent(ref)}`
|
|
2248
2284
|
);
|
|
2249
2285
|
if (out === null) throw new Error(`could not read ${path} at ${ref}`);
|
|
2250
2286
|
return out;
|
|
2251
2287
|
}
|
|
2252
2288
|
async function listFiles(target) {
|
|
2253
|
-
const out = await
|
|
2254
|
-
{
|
|
2255
|
-
argv: [
|
|
2256
|
-
"gh",
|
|
2257
|
-
"api",
|
|
2258
|
-
"--paginate",
|
|
2259
|
-
`repos/${target.repo}/pulls/${target.pr}/files`,
|
|
2260
|
-
"--jq",
|
|
2261
|
-
".[] | {filename, status, previous_filename}"
|
|
2262
|
-
]
|
|
2263
|
-
},
|
|
2264
|
-
null,
|
|
2265
|
-
{ cwd: target.cwd }
|
|
2266
|
-
);
|
|
2289
|
+
const out = await clientOf(target).list(`repos/${target.repo}/pulls/${target.pr}/files`);
|
|
2267
2290
|
if (out === null) throw new Error(`could not list files of ${target.repo}#${target.pr}`);
|
|
2268
|
-
return out
|
|
2291
|
+
return out;
|
|
2269
2292
|
}
|
|
2270
2293
|
async function gatherBase(target, view) {
|
|
2271
2294
|
const branch = view.base.ref;
|
|
2272
2295
|
const defaultBranch = view.base.repo.default_branch;
|
|
2273
2296
|
if (branch === defaultBranch) return { branch, defaultBranch, headOf: [] };
|
|
2274
2297
|
const head = encodeURIComponent(`${target.repo.split("/")[0] ?? ""}:${branch}`);
|
|
2275
|
-
const out = await
|
|
2276
|
-
{
|
|
2277
|
-
argv: [
|
|
2278
|
-
"gh",
|
|
2279
|
-
"api",
|
|
2280
|
-
"--paginate",
|
|
2281
|
-
`repos/${target.repo}/pulls?state=open&head=${head}&per_page=100`,
|
|
2282
|
-
"--jq",
|
|
2283
|
-
".[] | {number, labels: [.labels[].name]}"
|
|
2284
|
-
]
|
|
2285
|
-
},
|
|
2286
|
-
null,
|
|
2287
|
-
{ cwd: target.cwd }
|
|
2298
|
+
const out = await clientOf(target).list(
|
|
2299
|
+
`repos/${target.repo}/pulls?state=open&head=${head}&per_page=100`
|
|
2288
2300
|
);
|
|
2289
2301
|
if (out === null) throw new Error(`could not list the open PRs heading ${branch}`);
|
|
2290
|
-
const headOf = out.
|
|
2302
|
+
const headOf = out.map(
|
|
2303
|
+
(pull) => ({
|
|
2304
|
+
number: pull.number,
|
|
2305
|
+
labels: pull.labels.map((label) => label.name)
|
|
2306
|
+
})
|
|
2307
|
+
);
|
|
2291
2308
|
return { branch, defaultBranch, headOf };
|
|
2292
2309
|
}
|
|
2293
|
-
async function gatherFacts(
|
|
2294
|
-
const
|
|
2295
|
-
|
|
2296
|
-
"api",
|
|
2297
|
-
`repos/${target.repo}/pulls/${target.pr}`
|
|
2298
|
-
]);
|
|
2310
|
+
async function gatherFacts(pullRequest) {
|
|
2311
|
+
const target = { ...pullRequest, client: clientOf(pullRequest) };
|
|
2312
|
+
const view = await getJson(target, `repos/${target.repo}/pulls/${target.pr}`);
|
|
2299
2313
|
const headSha = view.head.sha;
|
|
2300
2314
|
const files = await listFiles(target);
|
|
2301
2315
|
const touches = (predicate) => files.filter((file) => predicate(file.filename) || predicate(file.previous_filename ?? ""));
|
|
@@ -2304,11 +2318,10 @@ async function gatherFacts(target) {
|
|
|
2304
2318
|
let workflowChanges = [];
|
|
2305
2319
|
let manifestChanges = [];
|
|
2306
2320
|
if (workflowFiles.length > 0 || manifestFiles.length > 0) {
|
|
2307
|
-
const comparison = await
|
|
2308
|
-
|
|
2309
|
-
"api",
|
|
2321
|
+
const comparison = await getJson(
|
|
2322
|
+
target,
|
|
2310
2323
|
`repos/${target.repo}/compare/${view.base.ref}...${headSha}`
|
|
2311
|
-
|
|
2324
|
+
);
|
|
2312
2325
|
const mergeBase = comparison.merge_base_commit.sha;
|
|
2313
2326
|
const read = async (file) => ({
|
|
2314
2327
|
path: file.filename,
|
|
@@ -2344,29 +2357,17 @@ async function gatherFacts(target) {
|
|
|
2344
2357
|
}
|
|
2345
2358
|
async function openPullRequestsAtHead(target, sha) {
|
|
2346
2359
|
const endpoint = `repos/${target.repo}/commits/${sha}/pulls`;
|
|
2347
|
-
const out = await
|
|
2348
|
-
{
|
|
2349
|
-
argv: [
|
|
2350
|
-
"gh",
|
|
2351
|
-
"api",
|
|
2352
|
-
"--paginate",
|
|
2353
|
-
endpoint,
|
|
2354
|
-
"--jq",
|
|
2355
|
-
'.[] | select(.state == "open") | {number, head: .head.sha}'
|
|
2356
|
-
]
|
|
2357
|
-
},
|
|
2358
|
-
null,
|
|
2359
|
-
{ cwd: target.cwd }
|
|
2360
|
-
);
|
|
2360
|
+
const out = await clientOf(target).list(endpoint);
|
|
2361
2361
|
if (out === null) throw new Error(`could not read ${endpoint}`);
|
|
2362
|
-
return out.
|
|
2362
|
+
return out.filter((pull) => pull.state === "open" && pull.head.sha === sha).map((pull) => pull.number);
|
|
2363
2363
|
}
|
|
2364
2364
|
async function applyLabelEdits(target, evaluation) {
|
|
2365
|
+
const client = clientOf(target);
|
|
2365
2366
|
if (evaluation.labelsToAdd.length > 0) {
|
|
2366
|
-
await addLabels(target.repo, target.pr, evaluation.labelsToAdd
|
|
2367
|
+
await client.addLabels(target.repo, target.pr, evaluation.labelsToAdd);
|
|
2367
2368
|
}
|
|
2368
2369
|
for (const label of evaluation.labelsToRemove) {
|
|
2369
|
-
await removeLabel(target.repo, target.pr, label
|
|
2370
|
+
await client.removeLabel(target.repo, target.pr, label);
|
|
2370
2371
|
}
|
|
2371
2372
|
}
|
|
2372
2373
|
|
|
@@ -2375,6 +2376,7 @@ export {
|
|
|
2375
2376
|
SIGN_OFF_STATUS_CONTEXT,
|
|
2376
2377
|
CI_APPROVAL_NEEDED_LABEL,
|
|
2377
2378
|
CI_CHANGE_APPROVED_LABEL,
|
|
2379
|
+
UAT_LABELS,
|
|
2378
2380
|
DO_NOT_MERGE_LABEL,
|
|
2379
2381
|
DNM_LABEL,
|
|
2380
2382
|
BLOCKED_LABEL,
|
|
@@ -2413,9 +2415,13 @@ export {
|
|
|
2413
2415
|
COMMIT_TYPES,
|
|
2414
2416
|
FUNCTIONAL_TYPES,
|
|
2415
2417
|
parseTitle,
|
|
2418
|
+
isFunctionalType,
|
|
2416
2419
|
TITLE_CHECK,
|
|
2417
2420
|
decideTitle,
|
|
2418
2421
|
titleCheck,
|
|
2422
|
+
UAT_LABEL_CHECK,
|
|
2423
|
+
decideUatLabel,
|
|
2424
|
+
uatLabelCheck,
|
|
2419
2425
|
CHECKS,
|
|
2420
2426
|
selectChecks,
|
|
2421
2427
|
POLICY_OUTCOMES,
|
|
@@ -2423,6 +2429,8 @@ export {
|
|
|
2423
2429
|
evaluatePolicy,
|
|
2424
2430
|
parseFacts,
|
|
2425
2431
|
resolveRepoTarget,
|
|
2432
|
+
createGhClient,
|
|
2433
|
+
clientOf,
|
|
2426
2434
|
checkRunBody,
|
|
2427
2435
|
postCheckRun,
|
|
2428
2436
|
statusBody,
|
package/dist/index.d.ts
CHANGED
|
@@ -173,10 +173,18 @@ interface PolicyOptions {
|
|
|
173
173
|
* unaffected: they still need a trusted person.
|
|
174
174
|
*/
|
|
175
175
|
signOffStatusCreators?: readonly string[];
|
|
176
|
+
/**
|
|
177
|
+
* Hold every PR that carries no UAT label (`uat-label`, rmartz/pr-policy#103).
|
|
178
|
+
* Off by default: turn it on once the repo's pr-lifecycle caller, which
|
|
179
|
+
* applies the labels, is on the default branch. It runs even with `skipUat`.
|
|
180
|
+
*/
|
|
181
|
+
requireUatLabel?: boolean;
|
|
176
182
|
}
|
|
177
183
|
/**
|
|
178
184
|
* The checks to run for a repo configured with `options`, in report order.
|
|
179
|
-
*
|
|
185
|
+
* The opt-in `uat-label` check is not in `CHECKS`; it runs just before
|
|
186
|
+
* `dependabot` when `requireUatLabel` is set. Throws on an invalid
|
|
187
|
+
* `signOffStatusCreators` entry.
|
|
180
188
|
*/
|
|
181
189
|
declare function selectChecks(options?: PolicyOptions): readonly PolicyCheck[];
|
|
182
190
|
|
|
@@ -205,6 +213,11 @@ declare const CI_APPROVAL_NEEDED_LABEL = "CI approval needed";
|
|
|
205
213
|
* applies it.
|
|
206
214
|
*/
|
|
207
215
|
declare const CI_CHANGE_APPROVED_LABEL = "CI change approved";
|
|
216
|
+
/**
|
|
217
|
+
* Every label that records a UAT determination on a PR, current and legacy
|
|
218
|
+
* names alike. The `uat-label` check passes when any one is present.
|
|
219
|
+
*/
|
|
220
|
+
declare const UAT_LABELS: readonly ["no UAT needed", "UAT pending", "UAT ready", "UAT passed", "tested", "ready for UAT"];
|
|
208
221
|
/**
|
|
209
222
|
* Labels a person puts on a PR to stop it merging. The merge-block check holds
|
|
210
223
|
* `pr-policy` while any of them is present, and never applies or removes one.
|
|
@@ -441,6 +454,10 @@ declare const MERGE_BLOCK_CHECK = "merge-block";
|
|
|
441
454
|
declare function decideMergeBlock(pr: PullRequestFacts): CheckResult;
|
|
442
455
|
declare const mergeBlockCheck: PolicyCheck;
|
|
443
456
|
|
|
457
|
+
declare const UAT_LABEL_CHECK = "uat-label";
|
|
458
|
+
declare function decideUatLabel(pr: Pick<PullRequestFacts, 'labels'>): CheckResult;
|
|
459
|
+
declare const uatLabelCheck: PolicyCheck;
|
|
460
|
+
|
|
444
461
|
/**
|
|
445
462
|
* Which files this guard considers "a CI workflow".
|
|
446
463
|
*
|
|
@@ -542,17 +559,48 @@ declare function evaluatePolicy(pr: PullRequestFacts, checks?: readonly PolicyCh
|
|
|
542
559
|
*/
|
|
543
560
|
declare function parseFacts(raw: string): PullRequestFacts;
|
|
544
561
|
|
|
545
|
-
interface
|
|
562
|
+
interface GitHubClient {
|
|
563
|
+
/** GET one JSON resource (`repos/o/r/...`), or `null` on failure. */
|
|
564
|
+
getJson(path: string): Promise<unknown>;
|
|
565
|
+
/**
|
|
566
|
+
* GET every page of a list resource, items concatenated, or `null` on
|
|
567
|
+
* failure. Most list endpoints return an array; for one that wraps its items
|
|
568
|
+
* in an object (`{ check_suites: [...] }`), pass that key.
|
|
569
|
+
*/
|
|
570
|
+
list(path: string, key?: string): Promise<unknown[] | null>;
|
|
571
|
+
/** GET a file's raw text from the contents API, or `null` on failure. */
|
|
572
|
+
getRaw(path: string): Promise<string | null>;
|
|
573
|
+
/** POST or PATCH a JSON body, returning the response JSON, or `null` on failure. */
|
|
574
|
+
send(method: 'POST' | 'PATCH', path: string, body: unknown): Promise<unknown>;
|
|
575
|
+
/** Add labels to an issue or PR. Soft-fails, like the vendored helper. */
|
|
576
|
+
addLabels(repo: string, issue: number, labels: readonly string[]): Promise<void>;
|
|
577
|
+
/** Remove one label from an issue or PR. Soft-fails, like the vendored helper. */
|
|
578
|
+
removeLabel(repo: string, issue: number, label: string): Promise<void>;
|
|
579
|
+
}
|
|
580
|
+
/** The `gh` client: the default for the CLI and the action. */
|
|
581
|
+
declare function createGhClient(options?: {
|
|
582
|
+
cwd?: string;
|
|
583
|
+
}): GitHubClient;
|
|
584
|
+
/** Where a GitHub call goes: an explicit client, or `gh` in a working directory. */
|
|
585
|
+
interface ClientTarget {
|
|
586
|
+
/** The working directory for the default `gh` client. */
|
|
587
|
+
cwd?: string;
|
|
588
|
+
/** How to reach GitHub; defaults to the `gh` client in `cwd`. */
|
|
589
|
+
client?: GitHubClient;
|
|
590
|
+
}
|
|
591
|
+
/** The target's client, or the default `gh` client in its working directory. */
|
|
592
|
+
declare function clientOf(target: ClientTarget): GitHubClient;
|
|
593
|
+
|
|
594
|
+
interface PullRequestTarget extends ClientTarget {
|
|
546
595
|
repo: string;
|
|
547
596
|
pr: number;
|
|
548
|
-
cwd?: string;
|
|
549
597
|
}
|
|
550
598
|
/**
|
|
551
599
|
* Gather everything the checks judge. Workflow files and dependency manifests
|
|
552
600
|
* are read at the **merge base**, not the base tip, so a change merged into the
|
|
553
601
|
* base after this PR branched is not attributed to it.
|
|
554
602
|
*/
|
|
555
|
-
declare function gatherFacts(
|
|
603
|
+
declare function gatherFacts(pullRequest: PullRequestTarget): Promise<{
|
|
556
604
|
facts: PullRequestFacts;
|
|
557
605
|
headSha: string;
|
|
558
606
|
}>;
|
|
@@ -562,20 +610,90 @@ declare function gatherFacts(target: PullRequestTarget): Promise<{
|
|
|
562
610
|
* status. A PR whose head has since moved isn't returned: its newer head gets
|
|
563
611
|
* its own status. A failed lookup throws.
|
|
564
612
|
*/
|
|
565
|
-
declare function openPullRequestsAtHead(target: {
|
|
613
|
+
declare function openPullRequestsAtHead(target: ClientTarget & {
|
|
566
614
|
repo: string;
|
|
567
|
-
cwd?: string;
|
|
568
615
|
}, sha: string): Promise<number[]>;
|
|
569
616
|
/** Apply the label edits the checks planned (labels this package owns). */
|
|
570
617
|
declare function applyLabelEdits(target: PullRequestTarget, evaluation: PolicyEvaluation): Promise<void>;
|
|
571
618
|
|
|
619
|
+
/**
|
|
620
|
+
* Where to post the verdict. `name` is the check-run name and status context,
|
|
621
|
+
* `pr-policy` unless a caller shadows under a distinct one (`pr-policy (app)`)
|
|
622
|
+
* while another poster still owns the real name. Two creators posting one
|
|
623
|
+
* context would overwrite each other.
|
|
624
|
+
*/
|
|
625
|
+
interface VerdictOptions {
|
|
626
|
+
name?: string;
|
|
627
|
+
}
|
|
572
628
|
/** The request body for this evaluation's check-run state. */
|
|
573
629
|
declare function checkRunBody(evaluation: PolicyEvaluation, now: Date): Record<string, unknown>;
|
|
574
630
|
/** Post (or complete) the one `pr-policy` check-run on the head commit. */
|
|
575
|
-
declare function postCheckRun(target: PullRequestTarget, headSha: string, evaluation: PolicyEvaluation): Promise<void>;
|
|
631
|
+
declare function postCheckRun(target: PullRequestTarget, headSha: string, evaluation: PolicyEvaluation, options?: VerdictOptions): Promise<void>;
|
|
576
632
|
/** The request body for the `pr-policy` commit status mirroring this evaluation. */
|
|
577
|
-
declare function statusBody(evaluation: PolicyEvaluation, runUrl: string | undefined): Record<string, unknown>;
|
|
633
|
+
declare function statusBody(evaluation: PolicyEvaluation, runUrl: string | undefined, name?: string): Record<string, unknown>;
|
|
578
634
|
/** Post the verdict: the `pr-policy` check-run, then the matching commit status. */
|
|
579
|
-
declare function postVerdict(target: PullRequestTarget, headSha: string, evaluation: PolicyEvaluation): Promise<void>;
|
|
635
|
+
declare function postVerdict(target: PullRequestTarget, headSha: string, evaluation: PolicyEvaluation, options?: VerdictOptions): Promise<void>;
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* One informational commit status per policy check (`pr-policy / title`), so a
|
|
639
|
+
* PR's status list shows which check failed, which waits on a human and which
|
|
640
|
+
* passed, without opening the check-run. Moved here from rmartz/pr-policy-action
|
|
641
|
+
* so every wrapper (the Action, a hosted App) posts the same statuses.
|
|
642
|
+
*
|
|
643
|
+
* These are informational: the `pr-policy` verdict status is the gate, and it is
|
|
644
|
+
* posted by `postVerdict` whatever happens here.
|
|
645
|
+
*/
|
|
646
|
+
|
|
647
|
+
/** One check's status. */
|
|
648
|
+
interface CheckStatus {
|
|
649
|
+
check: string;
|
|
650
|
+
state: 'success' | 'failure' | 'pending';
|
|
651
|
+
description: string;
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Each check's status: red on any `block`, pending on any `hold`, else green.
|
|
655
|
+
* The description leads with the finding that decided the state. A check that
|
|
656
|
+
* didn't run (left out by `selectChecks`) gets no status.
|
|
657
|
+
*/
|
|
658
|
+
declare function checkStatuses(checks: readonly Pick<PolicyCheck, 'name'>[], findings: readonly Finding[]): CheckStatus[];
|
|
659
|
+
interface CheckStatusOptions {
|
|
660
|
+
/** The status names are `<context> / <check>`; default `pr-policy`. */
|
|
661
|
+
context?: string;
|
|
662
|
+
/** Where each status links, e.g. the Actions run. */
|
|
663
|
+
targetUrl?: string;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Post each check's status on `sha`. Returns false, having stopped at the first
|
|
667
|
+
* failure, when one can't be posted (most often a token without
|
|
668
|
+
* `statuses: write`); the caller decides whether to warn.
|
|
669
|
+
*/
|
|
670
|
+
declare function postCheckStatuses(target: ClientTarget & {
|
|
671
|
+
repo: string;
|
|
672
|
+
}, sha: string, statuses: readonly CheckStatus[], options?: CheckStatusOptions): Promise<boolean>;
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* The `ci` title for this PR, or `undefined` to leave the title alone. Only a
|
|
676
|
+
* Dependabot PR with a sensitive bump is retitled, and never one already typed
|
|
677
|
+
* `ci` or a release type: demoting `fix`/`feat`/`perf`/`revert` would cancel a
|
|
678
|
+
* release. Scope, `!` and subject are kept.
|
|
679
|
+
*/
|
|
680
|
+
declare function ciRetitle(facts: Pick<PullRequestFacts, 'author' | 'title' | 'manifestChanges'>): string | undefined;
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* A token-authenticated `GitHubClient` over `fetch`, for a hosted caller with no
|
|
684
|
+
* `gh` binary (a GitHub App's webhook server). It makes the same REST calls the
|
|
685
|
+
* `gh` client does; it has no retry or GraphQL fallback, so a failed call
|
|
686
|
+
* returns `null` (or soft-fails for labels) and the caller's next event retries.
|
|
687
|
+
*/
|
|
688
|
+
|
|
689
|
+
interface FetchClientOptions {
|
|
690
|
+
/** An installation (or other) token, sent as a bearer token. */
|
|
691
|
+
token: string;
|
|
692
|
+
/** The REST API base, default `https://api.github.com`. */
|
|
693
|
+
apiUrl?: string;
|
|
694
|
+
/** Injectable for tests; defaults to the global `fetch`. */
|
|
695
|
+
fetch?: typeof fetch;
|
|
696
|
+
}
|
|
697
|
+
declare function createFetchClient(options: FetchClientOptions): GitHubClient;
|
|
580
698
|
|
|
581
|
-
export { AMBIGUOUS_INDICATORS, type AmbiguousIndicator, BLOCKED_LABEL, BLOCKING_LABELS, BREAKING_CHANGE_LABEL, CHECKS, CI_APPROVAL_NEEDED_LABEL, CI_CHANGE_APPROVED_LABEL, CI_CHANGE_CHECK, CI_CHANGE_VERDICTS, CI_SENSITIVE_PACKAGES, COMMIT_TYPES, type CheckResult, type CheckRunReport, type CiChangeVerdict, type Claim, type Classification, type CommitStatus, type CommitType, DEPENDABOT_CHECK, DEPENDABOT_LOGIN, DNM_LABEL, DO_NOT_MERGE_LABEL, ESCALATION_NEEDED_LABEL, FUNCTIONAL_TYPES, type FileChange, type Finding, GITHUB_ACTIONS_BOT, HOTFIX_LABEL, type Indicator, type IndicatorClass, type IndicatorKind, LOOSENING_INDICATORS, type LooseningIndicator, MERGE_BLOCK_CHECK, POLICY_OUTCOMES, PR_POLICY_CHECK_NAME, type ParsedTitle, type PolicyCheck, type PolicyEvaluation, type PolicyOptions, type PolicyOutcome, type PullRequestAuthor, type PullRequestFacts, type PullRequestTarget, RELEASE_PLEASE_PENDING_LABEL, SIGN_OFF_REQUIREMENTS, SIGN_OFF_STATUS_CONTEXT, SIGN_OFF_STATUS_VERSIONS, type SignOffClassification, type SignOffRequirement, type SignOffStatusReading, TITLE_CHECK, WAIVER_STATES, type WaiverState, type WorkflowFileChange, applyLabelEdits, buildReport, checkRunBody, ciChangeCheck, classifyWorkflowChanges, classifyWorkflowFile, decideCiChange, decideDependabot, decideMergeBlock, decideTitle, dependabotCheck, evaluatePolicy, gatherFacts, isManifestPath, isWorkflowPath, mergeBlockCheck, openPullRequestsAtHead, parseClaims, parseFacts, parseSignOffDescription, parseSignOffStatusCreators, parseTitle, postCheckRun, postVerdict, readSignOffStatus, selectChecks, sensitiveBumps, statusBody, titleCheck };
|
|
699
|
+
export { AMBIGUOUS_INDICATORS, type AmbiguousIndicator, BLOCKED_LABEL, BLOCKING_LABELS, BREAKING_CHANGE_LABEL, CHECKS, CI_APPROVAL_NEEDED_LABEL, CI_CHANGE_APPROVED_LABEL, CI_CHANGE_CHECK, CI_CHANGE_VERDICTS, CI_SENSITIVE_PACKAGES, COMMIT_TYPES, type CheckResult, type CheckRunReport, type CheckStatus, type CheckStatusOptions, type CiChangeVerdict, type Claim, type Classification, type ClientTarget, type CommitStatus, type CommitType, DEPENDABOT_CHECK, DEPENDABOT_LOGIN, DNM_LABEL, DO_NOT_MERGE_LABEL, ESCALATION_NEEDED_LABEL, FUNCTIONAL_TYPES, type FetchClientOptions, type FileChange, type Finding, GITHUB_ACTIONS_BOT, type GitHubClient, HOTFIX_LABEL, type Indicator, type IndicatorClass, type IndicatorKind, LOOSENING_INDICATORS, type LooseningIndicator, MERGE_BLOCK_CHECK, POLICY_OUTCOMES, PR_POLICY_CHECK_NAME, type ParsedTitle, type PolicyCheck, type PolicyEvaluation, type PolicyOptions, type PolicyOutcome, type PullRequestAuthor, type PullRequestFacts, type PullRequestTarget, RELEASE_PLEASE_PENDING_LABEL, SIGN_OFF_REQUIREMENTS, SIGN_OFF_STATUS_CONTEXT, SIGN_OFF_STATUS_VERSIONS, type SignOffClassification, type SignOffRequirement, type SignOffStatusReading, TITLE_CHECK, UAT_LABELS, UAT_LABEL_CHECK, type VerdictOptions, WAIVER_STATES, type WaiverState, type WorkflowFileChange, applyLabelEdits, buildReport, checkRunBody, checkStatuses, ciChangeCheck, ciRetitle, classifyWorkflowChanges, classifyWorkflowFile, clientOf, createFetchClient, createGhClient, decideCiChange, decideDependabot, decideMergeBlock, decideTitle, decideUatLabel, dependabotCheck, evaluatePolicy, gatherFacts, isManifestPath, isWorkflowPath, mergeBlockCheck, openPullRequestsAtHead, parseClaims, parseFacts, parseSignOffDescription, parseSignOffStatusCreators, parseTitle, postCheckRun, postCheckStatuses, postVerdict, readSignOffStatus, selectChecks, sensitiveBumps, statusBody, titleCheck, uatLabelCheck };
|
package/dist/index.js
CHANGED
|
@@ -27,6 +27,8 @@ import {
|
|
|
27
27
|
SIGN_OFF_STATUS_CONTEXT,
|
|
28
28
|
SIGN_OFF_STATUS_VERSIONS,
|
|
29
29
|
TITLE_CHECK,
|
|
30
|
+
UAT_LABELS,
|
|
31
|
+
UAT_LABEL_CHECK,
|
|
30
32
|
WAIVER_STATES,
|
|
31
33
|
applyLabelEdits,
|
|
32
34
|
buildReport,
|
|
@@ -34,13 +36,17 @@ import {
|
|
|
34
36
|
ciChangeCheck,
|
|
35
37
|
classifyWorkflowChanges,
|
|
36
38
|
classifyWorkflowFile,
|
|
39
|
+
clientOf,
|
|
40
|
+
createGhClient,
|
|
37
41
|
decideCiChange,
|
|
38
42
|
decideDependabot,
|
|
39
43
|
decideMergeBlock,
|
|
40
44
|
decideTitle,
|
|
45
|
+
decideUatLabel,
|
|
41
46
|
dependabotCheck,
|
|
42
47
|
evaluatePolicy,
|
|
43
48
|
gatherFacts,
|
|
49
|
+
isFunctionalType,
|
|
44
50
|
isManifestPath,
|
|
45
51
|
isWorkflowPath,
|
|
46
52
|
mergeBlockCheck,
|
|
@@ -56,8 +62,131 @@ import {
|
|
|
56
62
|
selectChecks,
|
|
57
63
|
sensitiveBumps,
|
|
58
64
|
statusBody,
|
|
59
|
-
titleCheck
|
|
60
|
-
|
|
65
|
+
titleCheck,
|
|
66
|
+
uatLabelCheck
|
|
67
|
+
} from "./chunk-JJ5AKEQ3.js";
|
|
68
|
+
|
|
69
|
+
// src/github/check-statuses.ts
|
|
70
|
+
var MAX_DESCRIPTION = 140;
|
|
71
|
+
var STATES = { block: "failure", hold: "pending" };
|
|
72
|
+
function checkStatuses(checks, findings) {
|
|
73
|
+
return checks.map(({ name }) => {
|
|
74
|
+
const own = findings.filter((finding) => finding.check === name);
|
|
75
|
+
const deciding = own.find((finding) => finding.effect === "block") ?? own.find((finding) => finding.effect === "hold");
|
|
76
|
+
const state = deciding?.effect === "block" || deciding?.effect === "hold" ? STATES[deciding.effect] : "success";
|
|
77
|
+
const lead = deciding ?? own.find((finding) => finding.headline);
|
|
78
|
+
const gating = own.filter((finding) => finding.effect !== "info").length;
|
|
79
|
+
let description = lead?.message ?? "Passed";
|
|
80
|
+
if (gating > 1) description += ` (+${gating - 1} more)`;
|
|
81
|
+
if (description.length > MAX_DESCRIPTION) {
|
|
82
|
+
description = `${description.slice(0, MAX_DESCRIPTION - 1)}\u2026`;
|
|
83
|
+
}
|
|
84
|
+
return { check: name, state, description };
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async function postCheckStatuses(target, sha, statuses, options = {}) {
|
|
88
|
+
const client = clientOf(target);
|
|
89
|
+
const context = options.context ?? "pr-policy";
|
|
90
|
+
for (const { check, state, description } of statuses) {
|
|
91
|
+
const out = await client.send("POST", `repos/${target.repo}/statuses/${sha}`, {
|
|
92
|
+
state,
|
|
93
|
+
context: `${context} / ${check}`,
|
|
94
|
+
description,
|
|
95
|
+
...options.targetUrl ? { target_url: options.targetUrl } : {}
|
|
96
|
+
});
|
|
97
|
+
if (out === null) return false;
|
|
98
|
+
}
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/checks/title/ci-retitle.ts
|
|
103
|
+
function ciRetitle(facts) {
|
|
104
|
+
if (facts.author?.login !== DEPENDABOT_LOGIN) return void 0;
|
|
105
|
+
const title = parseTitle(facts.title);
|
|
106
|
+
if (title === null || title.type === "ci" || isFunctionalType(title.type)) {
|
|
107
|
+
return void 0;
|
|
108
|
+
}
|
|
109
|
+
if (sensitiveBumps(facts.manifestChanges).length === 0) return void 0;
|
|
110
|
+
return `ci${facts.title.slice(title.type.length)}`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// src/github/fetch-client.ts
|
|
114
|
+
var JSON_ACCEPT = "application/vnd.github+json";
|
|
115
|
+
var RAW_ACCEPT = "application/vnd.github.raw";
|
|
116
|
+
function nextPageUrl(link) {
|
|
117
|
+
if (link === null) return void 0;
|
|
118
|
+
for (const part of link.split(",")) {
|
|
119
|
+
const match = part.match(/<([^>]+)>\s*;\s*rel="next"/);
|
|
120
|
+
if (match?.[1] !== void 0) return match[1];
|
|
121
|
+
}
|
|
122
|
+
return void 0;
|
|
123
|
+
}
|
|
124
|
+
function createFetchClient(options) {
|
|
125
|
+
const apiUrl = (options.apiUrl ?? "https://api.github.com").replace(/\/$/, "");
|
|
126
|
+
const doFetch = options.fetch ?? fetch;
|
|
127
|
+
const request = async (url, init = {}) => {
|
|
128
|
+
try {
|
|
129
|
+
const headers = {
|
|
130
|
+
accept: init.accept ?? JSON_ACCEPT,
|
|
131
|
+
authorization: `Bearer ${options.token}`,
|
|
132
|
+
"x-github-api-version": "2022-11-28"
|
|
133
|
+
};
|
|
134
|
+
if (init.body !== void 0) headers["content-type"] = "application/json";
|
|
135
|
+
const response = await doFetch(url, {
|
|
136
|
+
method: init.method ?? "GET",
|
|
137
|
+
headers,
|
|
138
|
+
body: init.body === void 0 ? void 0 : JSON.stringify(init.body)
|
|
139
|
+
});
|
|
140
|
+
return response.ok ? response : null;
|
|
141
|
+
} catch {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const urlOf = (path) => `${apiUrl}/${path}`;
|
|
146
|
+
const json = async (response) => {
|
|
147
|
+
if (response === null) return null;
|
|
148
|
+
const text = await response.text();
|
|
149
|
+
return text.trim() === "" ? {} : JSON.parse(text);
|
|
150
|
+
};
|
|
151
|
+
return {
|
|
152
|
+
async getJson(path) {
|
|
153
|
+
return json(await request(urlOf(path)));
|
|
154
|
+
},
|
|
155
|
+
async list(path, key) {
|
|
156
|
+
const items = [];
|
|
157
|
+
let url = urlOf(path);
|
|
158
|
+
while (url !== void 0) {
|
|
159
|
+
const response = await request(url);
|
|
160
|
+
if (response === null) return null;
|
|
161
|
+
const page = await json(response);
|
|
162
|
+
const pageItems = key === void 0 ? page : page?.[key];
|
|
163
|
+
if (!Array.isArray(pageItems)) return null;
|
|
164
|
+
items.push(...pageItems);
|
|
165
|
+
url = nextPageUrl(response.headers.get("link"));
|
|
166
|
+
}
|
|
167
|
+
return items;
|
|
168
|
+
},
|
|
169
|
+
async getRaw(path) {
|
|
170
|
+
const response = await request(urlOf(path), { accept: RAW_ACCEPT });
|
|
171
|
+
return response === null ? null : response.text();
|
|
172
|
+
},
|
|
173
|
+
async send(method, path, body) {
|
|
174
|
+
return json(await request(urlOf(path), { method, body }));
|
|
175
|
+
},
|
|
176
|
+
async addLabels(repo, issue, labels) {
|
|
177
|
+
if (labels.length === 0) return;
|
|
178
|
+
await request(urlOf(`repos/${repo}/issues/${issue}/labels`), {
|
|
179
|
+
method: "POST",
|
|
180
|
+
body: { labels }
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
async removeLabel(repo, issue, label) {
|
|
184
|
+
await request(urlOf(`repos/${repo}/issues/${issue}/labels/${encodeURIComponent(label)}`), {
|
|
185
|
+
method: "DELETE"
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
61
190
|
export {
|
|
62
191
|
AMBIGUOUS_INDICATORS,
|
|
63
192
|
BLOCKED_LABEL,
|
|
@@ -87,17 +216,25 @@ export {
|
|
|
87
216
|
SIGN_OFF_STATUS_CONTEXT,
|
|
88
217
|
SIGN_OFF_STATUS_VERSIONS,
|
|
89
218
|
TITLE_CHECK,
|
|
219
|
+
UAT_LABELS,
|
|
220
|
+
UAT_LABEL_CHECK,
|
|
90
221
|
WAIVER_STATES,
|
|
91
222
|
applyLabelEdits,
|
|
92
223
|
buildReport,
|
|
93
224
|
checkRunBody,
|
|
225
|
+
checkStatuses,
|
|
94
226
|
ciChangeCheck,
|
|
227
|
+
ciRetitle,
|
|
95
228
|
classifyWorkflowChanges,
|
|
96
229
|
classifyWorkflowFile,
|
|
230
|
+
clientOf,
|
|
231
|
+
createFetchClient,
|
|
232
|
+
createGhClient,
|
|
97
233
|
decideCiChange,
|
|
98
234
|
decideDependabot,
|
|
99
235
|
decideMergeBlock,
|
|
100
236
|
decideTitle,
|
|
237
|
+
decideUatLabel,
|
|
101
238
|
dependabotCheck,
|
|
102
239
|
evaluatePolicy,
|
|
103
240
|
gatherFacts,
|
|
@@ -111,10 +248,12 @@ export {
|
|
|
111
248
|
parseSignOffStatusCreators,
|
|
112
249
|
parseTitle,
|
|
113
250
|
postCheckRun,
|
|
251
|
+
postCheckStatuses,
|
|
114
252
|
postVerdict,
|
|
115
253
|
readSignOffStatus,
|
|
116
254
|
selectChecks,
|
|
117
255
|
sensitiveBumps,
|
|
118
256
|
statusBody,
|
|
119
|
-
titleCheck
|
|
257
|
+
titleCheck,
|
|
258
|
+
uatLabelCheck
|
|
120
259
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rmartz/pr-policy",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Read-only PR content classifiers — CI-change classification, title-type rules, and more — reported as one blocking pr-policy check-run. Distributed as a pinned Action kept current by Dependabot.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pr-policy",
|