@davidvornholt/standards 0.9.0 → 0.10.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 +3 -3
- package/package.json +2 -1
- package/src/github-api.ts +47 -0
- package/src/github-apply.ts +20 -4
- package/src/github-command-shared.ts +36 -2
- package/src/github-commands.ts +12 -78
- package/src/github-diff.ts +5 -5
- package/src/github-live-drift.ts +99 -0
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ standards github --check
|
|
|
12
12
|
standards github --apply
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
`github --check` compares the live GitHub repository (merge settings and rulesets) against the merged declaration in `.github/settings.json` + `.github/settings.local.json`; `github --apply` converges the live repository to exactly the declared state. `check` runs the same comparison whenever `.github/settings.json` is present. A repository whose GitHub plan cannot enforce rulesets can declare that in the seam (see `rulesetEnforcement` below); both commands then skip rulesets
|
|
15
|
+
`github --check` compares the live GitHub repository (merge settings and rulesets) against the merged declaration in `.github/settings.json` + `.github/settings.local.json`; `github --apply` converges the live repository to exactly the declared state. `check` runs the same comparison whenever `.github/settings.json` is present, and fails closed: declared state the token cannot see fails the gate with a message naming the token fix instead of passing unverified. `--apply` re-diffs the PATCH response, so a setting GitHub accepts with HTTP 200 but silently ignores (plan-unavailable features) is a reported failure, not a false success. A repository whose GitHub plan cannot enforce rulesets can declare that in the seam (see `rulesetEnforcement` below); both commands then skip rulesets and plan-gated repository settings, converge the rest, and print an unprotected-branch notice on every run.
|
|
16
16
|
|
|
17
17
|
`structure` enforces the canonical monorepo structure contract: workspace scripts (`check-types`, `lint`, `lint:fix`, `test`), root gate scripts, filtered Turbo convenience aliases, internal `0.0.0`/`workspace:*` versioning, package `exports`, shared tsconfig inheritance, and browser a11y wiring. Only a workspace containing an explicit `*.a11y.ts` suite must provide a `test:a11y` script and direct `@axe-core/playwright` and `@playwright/test` dependencies. `check` includes `structure`, so these rules gate every consumer PR.
|
|
18
18
|
|
|
@@ -21,7 +21,7 @@ standards github --apply
|
|
|
21
21
|
## Configuration
|
|
22
22
|
|
|
23
23
|
- **`sync-standards.local.json`** (optional) — consumer-owned sync policy at the repo root, validated by `doctor`/`check` and every `init`/`sync` even when an explicit ref or local source makes its `"ref"` irrelevant. All fields optional; a missing file means the defaults. `"ref"` is a non-empty single-line string that pins a tag, branch, or full commit sha to sync from instead of `main` (an explicit `--ref` overrides it; a local-path `--from` source is used as-is and ignores only the validated pin). `"autoSync": false` is read by the standards-sync workflow, not the CLI, and skips the weekly scheduled run. Version 0.7.0 removes the legacy `STANDARDS_AUTO_SYNC` and `STANDARDS_SYNC_REF` variable behavior; consumers must upgrade the package and lockfile before adopting a policy file.
|
|
24
|
-
- **`.github/settings.local.json` `"rulesetEnforcement": "unavailable-on-plan"`** (optional) — declares that the repository's GitHub plan cannot enforce rulesets (private repositories on GitHub Free, both personal accounts and organizations). `github --check` and `--apply` then skip rulesets instead of comparing them, because a comparison cannot be trusted there: personal accounts answer ruleset reads with HTTP 403, and free-plan organizations report declared rulesets as active while silently not enforcing them.
|
|
25
|
-
- **`GH_TOKEN` / `GITHUB_TOKEN`** (optional) — GitHub API token for the `github` command and the GitHub portion of `check`. When neither is set, the token from the local `gh` CLI is used; with no token at all, reads still work on public repositories. `--apply`
|
|
24
|
+
- **`.github/settings.local.json` `"rulesetEnforcement": "unavailable-on-plan"`** (optional) — declares that the repository's GitHub plan cannot enforce rulesets (private repositories on GitHub Free, both personal accounts and organizations). `github --check` and `--apply` then skip rulesets instead of comparing them, because a comparison cannot be trusted there: personal accounts answer ruleset reads with HTTP 403, and free-plan organizations report declared rulesets as active while silently not enforcing them. Plan-gated repository settings that only function alongside branch protection (`allow_auto_merge`) are skipped for the same reason — GitHub accepts a PATCH for them with HTTP 200 and silently keeps the old value. The remaining repository merge settings are still checked and converged, and both commands print an unprotected-branch notice on every run. The only accepted value is `"unavailable-on-plan"` (enforcement is the default; omit the key on paid plans), and combining the opt-out with additional local rulesets is rejected. After upgrading the plan, remove the declaration and run `bun standards github --apply` to restore enforcement.
|
|
25
|
+
- **`GH_TOKEN` / `GITHUB_TOKEN`** (optional) — GitHub API token for the `github` command and the GitHub portion of `check`. When neither is set, the token from the local `gh` CLI is used; with no token at all, reads still work on public repositories. `--apply` needs an admin token. GitHub's REST API reveals repo merge settings only to write-capable viewers — `check` retries REST-hidden keys over GraphQL, which serves them to read-only tokens — and ruleset `bypass_actors` requires repository Administration read; a token that still cannot see declared state fails the check rather than passing unverified. In CI, where the workflow token can never hold that permission, the canonical workflow decrypts `ci.github_settings_read_token` from the SOPS-encrypted `secrets/ci.yaml` — a fine-grained PAT with read-only "Administration" access to the repository.
|
|
26
26
|
|
|
27
27
|
See the standards repository README for the ownership model and adoption workflow.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@davidvornholt/standards",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Bootstrap, synchronize, and validate davidvornholt/standards consumers",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"src/github-command-shared.ts",
|
|
26
26
|
"src/github-commands.ts",
|
|
27
27
|
"src/github-diff.ts",
|
|
28
|
+
"src/github-live-drift.ts",
|
|
28
29
|
"src/github-settings-parse.ts",
|
|
29
30
|
"src/github-settings.ts",
|
|
30
31
|
"src/structure-check.ts",
|
package/src/github-api.ts
CHANGED
|
@@ -111,6 +111,53 @@ export const loadDeclared = async (
|
|
|
111
111
|
);
|
|
112
112
|
};
|
|
113
113
|
|
|
114
|
+
const GRAPHQL_MERGE_FIELDS: ReadonlyMap<string, string> = new Map([
|
|
115
|
+
['allow_auto_merge', 'autoMergeAllowed'],
|
|
116
|
+
['allow_merge_commit', 'mergeCommitAllowed'],
|
|
117
|
+
['allow_rebase_merge', 'rebaseMergeAllowed'],
|
|
118
|
+
['allow_squash_merge', 'squashMergeAllowed'],
|
|
119
|
+
['delete_branch_on_merge', 'deleteBranchOnMerge'],
|
|
120
|
+
['merge_commit_message', 'mergeCommitMessage'],
|
|
121
|
+
['merge_commit_title', 'mergeCommitTitle'],
|
|
122
|
+
['squash_merge_commit_message', 'squashMergeCommitMessage'],
|
|
123
|
+
['squash_merge_commit_title', 'squashMergeCommitTitle'],
|
|
124
|
+
]);
|
|
125
|
+
|
|
126
|
+
// REST omits repository merge settings for read-only viewers (they surface
|
|
127
|
+
// only with write access — community discussion #153258), but GraphQL serves
|
|
128
|
+
// the same values, with identical enum spellings, to any token that can read
|
|
129
|
+
// the repository. This keeps a read-only PAT sufficient for the check.
|
|
130
|
+
// Returns only the keys GraphQL answered; the rest stay unverifiable.
|
|
131
|
+
export const fetchMergeSettingsViaGraphql = async (
|
|
132
|
+
token: string | null,
|
|
133
|
+
repo: string,
|
|
134
|
+
keys: ReadonlyArray<string>,
|
|
135
|
+
): Promise<Readonly<Record<string, unknown>>> => {
|
|
136
|
+
const mapped = keys.filter((key) => GRAPHQL_MERGE_FIELDS.has(key));
|
|
137
|
+
const [owner, name] = repo.split('/');
|
|
138
|
+
if (mapped.length === 0 || owner === undefined || name === undefined) {
|
|
139
|
+
return {};
|
|
140
|
+
}
|
|
141
|
+
const fields = mapped.map((key) => GRAPHQL_MERGE_FIELDS.get(key)).join(' ');
|
|
142
|
+
const response = await request(token, 'POST', '/graphql', {
|
|
143
|
+
query: `query { repository(owner: ${JSON.stringify(owner)}, name: ${JSON.stringify(name)}) { ${fields} } }`,
|
|
144
|
+
});
|
|
145
|
+
if (response.status !== HTTP_OK || !isRecord(response.body)) {
|
|
146
|
+
return {};
|
|
147
|
+
}
|
|
148
|
+
const data = isRecord(response.body.data) ? response.body.data : null;
|
|
149
|
+
const repository =
|
|
150
|
+
data !== null && isRecord(data.repository) ? data.repository : null;
|
|
151
|
+
if (repository === null) {
|
|
152
|
+
return {};
|
|
153
|
+
}
|
|
154
|
+
return Object.fromEntries(
|
|
155
|
+
mapped
|
|
156
|
+
.map((key) => [key, repository[GRAPHQL_MERGE_FIELDS.get(key) ?? '']])
|
|
157
|
+
.filter(([, value]) => value !== undefined && value !== null),
|
|
158
|
+
);
|
|
159
|
+
};
|
|
160
|
+
|
|
114
161
|
export type LiveRulesets = {
|
|
115
162
|
readonly rulesets: ReadonlyArray<Record<string, unknown>> | null;
|
|
116
163
|
readonly problem: string | null;
|
package/src/github-apply.ts
CHANGED
|
@@ -11,8 +11,11 @@ import {
|
|
|
11
11
|
request,
|
|
12
12
|
} from './github-api';
|
|
13
13
|
import { diffRepositorySettings, diffRuleset } from './github-diff';
|
|
14
|
-
import type
|
|
14
|
+
import { type GithubSettings, isRecord } from './github-settings-parse';
|
|
15
15
|
|
|
16
|
+
// GitHub answers a PATCH containing a plan-unavailable setting with HTTP 200
|
|
17
|
+
// and the old value, so the response body — the updated repository — is
|
|
18
|
+
// diffed again instead of trusting the status code.
|
|
16
19
|
export const applyRepositorySettings = async (
|
|
17
20
|
token: string,
|
|
18
21
|
repo: string,
|
|
@@ -24,9 +27,22 @@ export const applyRepositorySettings = async (
|
|
|
24
27
|
return [];
|
|
25
28
|
}
|
|
26
29
|
const patched = await request(token, 'PATCH', `/repos/${repo}`, repository);
|
|
27
|
-
if (patched.status !== HTTP_OK) {
|
|
30
|
+
if (patched.status !== HTTP_OK || !isRecord(patched.body)) {
|
|
28
31
|
throw new Error(apiError('updating repository settings', patched));
|
|
29
32
|
}
|
|
33
|
+
const persisted = diffRepositorySettings(repository, patched.body);
|
|
34
|
+
const ignored = [
|
|
35
|
+
...persisted.drifted,
|
|
36
|
+
...persisted.unverifiable.map(
|
|
37
|
+
(key) =>
|
|
38
|
+
`repository setting "${key}" is missing from the update response`,
|
|
39
|
+
),
|
|
40
|
+
];
|
|
41
|
+
if (ignored.length > 0) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`GitHub returned HTTP 200 but ignored part of the update: ${ignored.join('; ')}. A silently ignored setting is usually unavailable on this GitHub plan; if this plan cannot protect branches, declare the ruleset-enforcement opt-out instead`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
30
46
|
return ['updated repository merge settings'];
|
|
31
47
|
};
|
|
32
48
|
|
|
@@ -37,8 +53,8 @@ export const applySummary = (
|
|
|
37
53
|
): string => {
|
|
38
54
|
if (rulesetsSkipped) {
|
|
39
55
|
return actions.length === 0
|
|
40
|
-
? `standards github:
|
|
41
|
-
: `standards github:
|
|
56
|
+
? `standards github: enforceable settings already converged for ${repo}; plan-gated settings skipped`
|
|
57
|
+
: `standards github: enforceable settings apply complete for ${repo}; plan-gated settings skipped`;
|
|
42
58
|
}
|
|
43
59
|
return actions.length === 0
|
|
44
60
|
? 'standards github: already converged; no changes'
|
|
@@ -1,11 +1,45 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
ENFORCEMENT_OPT_OUT,
|
|
3
|
+
type GithubSettings,
|
|
4
|
+
} from './github-settings-parse';
|
|
5
|
+
|
|
6
|
+
// Repository settings that only function alongside branch protection, which
|
|
7
|
+
// the ruleset-enforcement opt-out declares unavailable on the plan. GitHub
|
|
8
|
+
// answers a PATCH for them with HTTP 200 and silently keeps the old value, so
|
|
9
|
+
// they must be skipped, never applied-and-trusted.
|
|
10
|
+
export const PLAN_GATED_REPOSITORY_KEYS: ReadonlySet<string> = new Set([
|
|
11
|
+
'allow_auto_merge',
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
export const enforceableRepositorySettings = (
|
|
15
|
+
declared: GithubSettings,
|
|
16
|
+
): Readonly<Record<string, unknown>> =>
|
|
17
|
+
declared.rulesetEnforcement === ENFORCEMENT_OPT_OUT
|
|
18
|
+
? Object.fromEntries(
|
|
19
|
+
Object.entries(declared.repository).filter(
|
|
20
|
+
([key]) => !PLAN_GATED_REPOSITORY_KEYS.has(key),
|
|
21
|
+
),
|
|
22
|
+
)
|
|
23
|
+
: declared.repository;
|
|
2
24
|
|
|
3
25
|
export const optOutEligibilityProblem = (
|
|
4
26
|
repo: string,
|
|
5
27
|
declared: GithubSettings,
|
|
6
28
|
liveRepository: Readonly<Record<string, unknown>>,
|
|
7
29
|
): string | null =>
|
|
8
|
-
declared.rulesetEnforcement ===
|
|
30
|
+
declared.rulesetEnforcement === ENFORCEMENT_OPT_OUT &&
|
|
9
31
|
liveRepository.private !== true
|
|
10
32
|
? `.github/settings.local.json "rulesetEnforcement" may only be declared for a private repository; ${repo} is public`
|
|
11
33
|
: null;
|
|
34
|
+
|
|
35
|
+
// Declared state the token cannot see is a gate failure, not a pass with a
|
|
36
|
+
// log line: a gate that cannot perform its comparison fails closed.
|
|
37
|
+
export const unverifiableProblem = (
|
|
38
|
+
scope: string,
|
|
39
|
+
items: ReadonlyArray<string>,
|
|
40
|
+
): ReadonlyArray<string> =>
|
|
41
|
+
items.length === 0
|
|
42
|
+
? []
|
|
43
|
+
: [
|
|
44
|
+
`${scope} not visible to this token, so the gate cannot verify: ${items.join('; ')}. Use a token with read access to repository administration (in CI: ci.github_settings_read_token in secrets/ci.yaml), or verify locally with admin auth`,
|
|
45
|
+
];
|
package/src/github-commands.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
// `standards github --check` verifies the live GitHub repository against the
|
|
2
|
-
// declared settings and fails closed on any drift
|
|
3
|
-
// github --apply` converges the live
|
|
4
|
-
// it runs locally rather than in CI.
|
|
2
|
+
// declared settings and fails closed on any drift, API error, or declared
|
|
3
|
+
// state the token cannot see. `standards github --apply` converges the live
|
|
4
|
+
// repository; it needs an admin token, so it runs locally rather than in CI.
|
|
5
5
|
|
|
6
6
|
import {
|
|
7
7
|
apiError,
|
|
8
|
-
fetchLiveRulesets,
|
|
9
8
|
HTTP_OK,
|
|
10
9
|
loadDeclared,
|
|
11
10
|
request,
|
|
@@ -17,14 +16,17 @@ import {
|
|
|
17
16
|
applyRulesets,
|
|
18
17
|
applySummary,
|
|
19
18
|
} from './github-apply';
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
import {
|
|
20
|
+
enforceableRepositorySettings,
|
|
21
|
+
optOutEligibilityProblem,
|
|
22
|
+
} from './github-command-shared';
|
|
23
|
+
import { collectLiveDrift } from './github-live-drift';
|
|
24
|
+
import { isRecord } from './github-settings-parse';
|
|
23
25
|
|
|
24
26
|
// Printed on every check and apply while the opt-out is declared: the skip
|
|
25
27
|
// must stay louder than the comfort of a green gate.
|
|
26
28
|
const UNENFORCEABLE_NOTICE =
|
|
27
|
-
'standards github: rulesets are declared unenforceable on this GitHub plan (.github/settings.local.json "rulesetEnforcement"); the default branch is NOT protected. After upgrading the plan, remove the declaration, then run `bun standards github --apply`.';
|
|
29
|
+
'standards github: rulesets are declared unenforceable on this GitHub plan (.github/settings.local.json "rulesetEnforcement"); the default branch is NOT protected, and plan-gated repository settings ("allow_auto_merge") are skipped. After upgrading the plan, remove the declaration, then run `bun standards github --apply`.';
|
|
28
30
|
|
|
29
31
|
const reportProblems = (problems: ReadonlyArray<string>): void => {
|
|
30
32
|
console.error(
|
|
@@ -33,74 +35,6 @@ const reportProblems = (problems: ReadonlyArray<string>): void => {
|
|
|
33
35
|
console.error(problems.map((problem) => ` - ${problem}`).join('\n'));
|
|
34
36
|
};
|
|
35
37
|
|
|
36
|
-
const repositoryDrift = async (
|
|
37
|
-
token: string | null,
|
|
38
|
-
repo: string,
|
|
39
|
-
declared: GithubSettings,
|
|
40
|
-
): Promise<ReadonlyArray<string>> => {
|
|
41
|
-
const repoResponse = await request(token, 'GET', `/repos/${repo}`);
|
|
42
|
-
if (repoResponse.status !== HTTP_OK || !isRecord(repoResponse.body)) {
|
|
43
|
-
return [apiError(`reading repository ${repo}`, repoResponse)];
|
|
44
|
-
}
|
|
45
|
-
const eligibilityProblem = optOutEligibilityProblem(
|
|
46
|
-
repo,
|
|
47
|
-
declared,
|
|
48
|
-
repoResponse.body,
|
|
49
|
-
);
|
|
50
|
-
if (eligibilityProblem !== null) {
|
|
51
|
-
return [eligibilityProblem];
|
|
52
|
-
}
|
|
53
|
-
const diff = diffRepositorySettings(declared.repository, repoResponse.body);
|
|
54
|
-
if (diff.unverifiable.length > 0) {
|
|
55
|
-
console.log(
|
|
56
|
-
`standards github: repository setting(s) not visible to this token, verify with admin auth: ${diff.unverifiable.join(', ')}`,
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
return diff.drifted;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const rulesetDrift = async (
|
|
63
|
-
token: string | null,
|
|
64
|
-
repo: string,
|
|
65
|
-
declared: GithubSettings,
|
|
66
|
-
): Promise<ReadonlyArray<string>> => {
|
|
67
|
-
if (declared.rulesetEnforcement === 'unavailable-on-plan') {
|
|
68
|
-
return [];
|
|
69
|
-
}
|
|
70
|
-
const live = await fetchLiveRulesets(token, repo);
|
|
71
|
-
if (live.rulesets === null) {
|
|
72
|
-
return [live.problem ?? 'unable to read rulesets'];
|
|
73
|
-
}
|
|
74
|
-
const diff = diffRulesets(declared.rulesets, live.rulesets);
|
|
75
|
-
if (diff.unverifiable.length > 0) {
|
|
76
|
-
console.log(
|
|
77
|
-
`standards github: ruleset field(s) not visible to this token, verify with admin auth: ${diff.unverifiable.join('; ')}`,
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
return diff.drifted;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const collectLiveDrift = async (
|
|
84
|
-
consumer: string,
|
|
85
|
-
declared: GithubSettings,
|
|
86
|
-
): Promise<ReadonlyArray<string>> => {
|
|
87
|
-
const repo = resolveGithubRepo(consumer);
|
|
88
|
-
if (repo === null) {
|
|
89
|
-
return ['cannot determine the GitHub repository from the origin remote'];
|
|
90
|
-
}
|
|
91
|
-
const token = resolveToken();
|
|
92
|
-
try {
|
|
93
|
-
return [
|
|
94
|
-
...(await repositoryDrift(token, repo, declared)),
|
|
95
|
-
...(await rulesetDrift(token, repo, declared)),
|
|
96
|
-
];
|
|
97
|
-
} catch (error) {
|
|
98
|
-
return [
|
|
99
|
-
`GitHub API unreachable: ${error instanceof Error ? error.message : String(error)}`,
|
|
100
|
-
];
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
|
|
104
38
|
export const runGithubCheck = async (consumer: string): Promise<boolean> => {
|
|
105
39
|
const declared = await loadDeclared(consumer);
|
|
106
40
|
if (declared.merged?.rulesetEnforcement === 'unavailable-on-plan') {
|
|
@@ -119,7 +53,7 @@ export const runGithubCheck = async (consumer: string): Promise<boolean> => {
|
|
|
119
53
|
}
|
|
120
54
|
console.log(
|
|
121
55
|
declared.merged?.rulesetEnforcement === 'unavailable-on-plan'
|
|
122
|
-
? 'standards github: live repository settings match the declared configuration (
|
|
56
|
+
? 'standards github: live repository settings match the declared configuration (plan-gated settings skipped)'
|
|
123
57
|
: 'standards github: live GitHub settings match the declared configuration',
|
|
124
58
|
);
|
|
125
59
|
return true;
|
|
@@ -165,7 +99,7 @@ export const runGithubApply = async (consumer: string): Promise<boolean> => {
|
|
|
165
99
|
...(await applyRepositorySettings(
|
|
166
100
|
token,
|
|
167
101
|
repo,
|
|
168
|
-
declared.merged
|
|
102
|
+
enforceableRepositorySettings(declared.merged),
|
|
169
103
|
repoResponse.body,
|
|
170
104
|
)),
|
|
171
105
|
];
|
package/src/github-diff.ts
CHANGED
|
@@ -89,8 +89,8 @@ const diffRules = (
|
|
|
89
89
|
// Some ruleset fields — bypass_actors in particular — are only included in
|
|
90
90
|
// API responses for admin viewers. A declared key that is absent on the live
|
|
91
91
|
// side is unverifiable for this token, not drift: the same policy as
|
|
92
|
-
// repository merge settings, so
|
|
93
|
-
//
|
|
92
|
+
// repository merge settings, so callers can fail with a targeted
|
|
93
|
+
// missing-visibility message instead of a bogus value mismatch.
|
|
94
94
|
export const diffRuleset = (
|
|
95
95
|
declared: Readonly<Record<string, unknown>>,
|
|
96
96
|
live: Readonly<Record<string, unknown>>,
|
|
@@ -150,9 +150,9 @@ export const diffRulesets = (
|
|
|
150
150
|
};
|
|
151
151
|
|
|
152
152
|
// Repo merge settings are only visible to admin tokens; report invisible keys
|
|
153
|
-
// as unverifiable instead of drifted so
|
|
154
|
-
//
|
|
155
|
-
//
|
|
153
|
+
// as unverifiable instead of drifted so callers fail the gate with a
|
|
154
|
+
// missing-visibility message that names the token fix, not a bogus value
|
|
155
|
+
// mismatch.
|
|
156
156
|
export const diffRepositorySettings = (
|
|
157
157
|
declared: Readonly<Record<string, unknown>>,
|
|
158
158
|
live: Readonly<Record<string, unknown>>,
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// Drift collection for `standards github --check`: compares the live GitHub
|
|
2
|
+
// repository against the merged declaration and fails closed on state the
|
|
3
|
+
// token cannot see. Command wiring lives in github-commands.ts.
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
apiError,
|
|
7
|
+
fetchLiveRulesets,
|
|
8
|
+
fetchMergeSettingsViaGraphql,
|
|
9
|
+
HTTP_OK,
|
|
10
|
+
request,
|
|
11
|
+
resolveGithubRepo,
|
|
12
|
+
resolveToken,
|
|
13
|
+
} from './github-api';
|
|
14
|
+
import {
|
|
15
|
+
enforceableRepositorySettings,
|
|
16
|
+
optOutEligibilityProblem,
|
|
17
|
+
unverifiableProblem,
|
|
18
|
+
} from './github-command-shared';
|
|
19
|
+
import { diffRepositorySettings, diffRulesets } from './github-diff';
|
|
20
|
+
import { type GithubSettings, isRecord } from './github-settings-parse';
|
|
21
|
+
|
|
22
|
+
const repositoryDrift = async (
|
|
23
|
+
token: string | null,
|
|
24
|
+
repo: string,
|
|
25
|
+
declared: GithubSettings,
|
|
26
|
+
): Promise<ReadonlyArray<string>> => {
|
|
27
|
+
const repoResponse = await request(token, 'GET', `/repos/${repo}`);
|
|
28
|
+
if (repoResponse.status !== HTTP_OK || !isRecord(repoResponse.body)) {
|
|
29
|
+
return [apiError(`reading repository ${repo}`, repoResponse)];
|
|
30
|
+
}
|
|
31
|
+
const eligibilityProblem = optOutEligibilityProblem(
|
|
32
|
+
repo,
|
|
33
|
+
declared,
|
|
34
|
+
repoResponse.body,
|
|
35
|
+
);
|
|
36
|
+
if (eligibilityProblem !== null) {
|
|
37
|
+
return [eligibilityProblem];
|
|
38
|
+
}
|
|
39
|
+
const declaredRepository = enforceableRepositorySettings(declared);
|
|
40
|
+
const diff = diffRepositorySettings(declaredRepository, repoResponse.body);
|
|
41
|
+
if (diff.unverifiable.length === 0) {
|
|
42
|
+
return diff.drifted;
|
|
43
|
+
}
|
|
44
|
+
// REST hides merge settings from read-only tokens; retry the invisible
|
|
45
|
+
// keys over GraphQL before failing the gate.
|
|
46
|
+
const fallback = await fetchMergeSettingsViaGraphql(
|
|
47
|
+
token,
|
|
48
|
+
repo,
|
|
49
|
+
diff.unverifiable,
|
|
50
|
+
);
|
|
51
|
+
const rediff = diffRepositorySettings(declaredRepository, {
|
|
52
|
+
...repoResponse.body,
|
|
53
|
+
...fallback,
|
|
54
|
+
});
|
|
55
|
+
return [
|
|
56
|
+
...rediff.drifted,
|
|
57
|
+
...unverifiableProblem('repository setting(s)', rediff.unverifiable),
|
|
58
|
+
];
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const rulesetDrift = async (
|
|
62
|
+
token: string | null,
|
|
63
|
+
repo: string,
|
|
64
|
+
declared: GithubSettings,
|
|
65
|
+
): Promise<ReadonlyArray<string>> => {
|
|
66
|
+
if (declared.rulesetEnforcement === 'unavailable-on-plan') {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
const live = await fetchLiveRulesets(token, repo);
|
|
70
|
+
if (live.rulesets === null) {
|
|
71
|
+
return [live.problem ?? 'unable to read rulesets'];
|
|
72
|
+
}
|
|
73
|
+
const diff = diffRulesets(declared.rulesets, live.rulesets);
|
|
74
|
+
return [
|
|
75
|
+
...diff.drifted,
|
|
76
|
+
...unverifiableProblem('ruleset field(s)', diff.unverifiable),
|
|
77
|
+
];
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const collectLiveDrift = async (
|
|
81
|
+
consumer: string,
|
|
82
|
+
declared: GithubSettings,
|
|
83
|
+
): Promise<ReadonlyArray<string>> => {
|
|
84
|
+
const repo = resolveGithubRepo(consumer);
|
|
85
|
+
if (repo === null) {
|
|
86
|
+
return ['cannot determine the GitHub repository from the origin remote'];
|
|
87
|
+
}
|
|
88
|
+
const token = resolveToken();
|
|
89
|
+
try {
|
|
90
|
+
return [
|
|
91
|
+
...(await repositoryDrift(token, repo, declared)),
|
|
92
|
+
...(await rulesetDrift(token, repo, declared)),
|
|
93
|
+
];
|
|
94
|
+
} catch (error) {
|
|
95
|
+
return [
|
|
96
|
+
`GitHub API unreachable: ${error instanceof Error ? error.message : String(error)}`,
|
|
97
|
+
];
|
|
98
|
+
}
|
|
99
|
+
};
|