@aspruyt/xfg 7.2.1 → 7.4.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/PR.md +2 -0
- package/dist/ai/anthropic-client.d.ts +16 -0
- package/dist/ai/anthropic-client.js +35 -0
- package/dist/ai/change-describer.d.ts +16 -0
- package/dist/ai/change-describer.js +170 -0
- package/dist/ai/client-factory.d.ts +2 -0
- package/dist/ai/client-factory.js +33 -0
- package/dist/ai/http.d.ts +3 -0
- package/dist/ai/http.js +33 -0
- package/dist/ai/index.d.ts +5 -0
- package/dist/ai/index.js +4 -0
- package/dist/ai/openai-compatible-client.d.ts +16 -0
- package/dist/ai/openai-compatible-client.js +36 -0
- package/dist/ai/types.d.ts +22 -0
- package/dist/ai/types.js +1 -0
- package/dist/cli/program.d.ts +7 -0
- package/dist/cli/program.js +15 -11
- package/dist/cli/repo-sync-runner.js +1 -0
- package/dist/cli/secrets-command.js +1 -0
- package/dist/cli/settings-factories.d.ts +2 -1
- package/dist/cli/settings-factories.js +7 -1
- package/dist/cli/settings-report-builder.d.ts +6 -1
- package/dist/cli/settings-report-builder.js +15 -0
- package/dist/cli/settings-runner.js +12 -0
- package/dist/cli/sync-command.js +2 -0
- package/dist/cli/types.d.ts +5 -2
- package/dist/cli/unified-summary.d.ts +3 -1
- package/dist/cli/unified-summary.js +14 -26
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.js +0 -2
- package/dist/config/normalizer.js +49 -27
- package/dist/config/types.d.ts +30 -1
- package/dist/config/validator.js +11 -4
- package/dist/config/validators/ai-validator.d.ts +1 -0
- package/dist/config/validators/ai-validator.js +39 -0
- package/dist/config/validators/group-validator.js +3 -0
- package/dist/config/validators/repo-entry-validator.js +2 -0
- package/dist/config/validators/shared.js +33 -0
- package/dist/lifecycle/formatter.d.ts +0 -4
- package/dist/lifecycle/formatter.js +2 -6
- package/dist/lifecycle/github-lifecycle-provider.d.ts +1 -0
- package/dist/lifecycle/github-lifecycle-provider.js +30 -14
- package/dist/output/github-summary.d.ts +4 -3
- package/dist/output/github-summary.js +22 -5
- package/dist/output/lifecycle-report.d.ts +3 -2
- package/dist/output/lifecycle-report.js +20 -26
- package/dist/output/settings-report.d.ts +12 -2
- package/dist/output/settings-report.js +49 -47
- package/dist/output/summary-budget.d.ts +12 -0
- package/dist/output/summary-budget.js +125 -0
- package/dist/output/sync-report.d.ts +6 -3
- package/dist/output/sync-report.js +33 -34
- package/dist/settings/collaborators/diff.d.ts +17 -0
- package/dist/settings/collaborators/diff.js +64 -0
- package/dist/settings/collaborators/formatter.d.ts +14 -0
- package/dist/settings/collaborators/formatter.js +41 -0
- package/dist/settings/collaborators/github-collaborators-strategy.d.ts +18 -0
- package/dist/settings/collaborators/github-collaborators-strategy.js +43 -0
- package/dist/settings/collaborators/index.d.ts +4 -0
- package/dist/settings/collaborators/index.js +2 -0
- package/dist/settings/collaborators/processor.d.ts +21 -0
- package/dist/settings/collaborators/processor.js +77 -0
- package/dist/settings/collaborators/types.d.ts +19 -0
- package/dist/settings/collaborators/types.js +1 -0
- package/dist/settings/index.d.ts +1 -0
- package/dist/settings/index.js +1 -0
- package/dist/settings/labels/formatter.d.ts +0 -3
- package/dist/settings/labels/formatter.js +10 -17
- package/dist/settings/rulesets/formatter.d.ts +0 -6
- package/dist/settings/rulesets/formatter.js +4 -32
- package/dist/settings/secrets/formatter.js +4 -3
- package/dist/settings/variables/formatter.js +6 -5
- package/dist/shared/markdown-fence.d.ts +3 -0
- package/dist/shared/markdown-fence.js +32 -0
- package/dist/shared/string-utils.d.ts +1 -3
- package/dist/shared/string-utils.js +5 -4
- package/dist/sync/commit-push-manager.js +2 -2
- package/dist/sync/index.d.ts +1 -1
- package/dist/sync/pr-merge-handler.js +3 -1
- package/dist/sync/repository-processor.d.ts +5 -0
- package/dist/sync/repository-processor.js +4 -1
- package/dist/sync/sync-workflow.d.ts +4 -1
- package/dist/sync/sync-workflow.js +29 -2
- package/dist/sync/types.d.ts +14 -3
- package/dist/vcs/ado-pr-strategy.js +43 -1
- package/dist/vcs/git-commit-strategy.js +3 -5
- package/dist/vcs/graphql-commit-strategy.js +3 -6
- package/dist/vcs/pr-creator.d.ts +5 -10
- package/dist/vcs/pr-creator.js +34 -20
- package/dist/vcs/types.d.ts +1 -0
- package/package.json +4 -2
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { appendDiffBlock, diffFence, inlineCode, } from "../shared/markdown-fence.js";
|
|
2
|
+
const CUT_NOTE = "... cut to fit GitHub's 1 MiB summary limit";
|
|
3
|
+
// Room kept for the "more repos not shown" line when something is cut.
|
|
4
|
+
const NOTE_RESERVE_BYTES = 1000;
|
|
5
|
+
// Shorter leftovers of a cut line are not worth showing.
|
|
6
|
+
const MIN_CUT_LINE_BYTES = 40;
|
|
7
|
+
export function summaryHeader(dryRun, title = dryRun ? "## xfg Plan" : "## xfg Apply") {
|
|
8
|
+
const lines = [title, ""];
|
|
9
|
+
if (dryRun) {
|
|
10
|
+
lines.push("> [!WARNING]", "> This was a dry run — no changes were applied", "");
|
|
11
|
+
}
|
|
12
|
+
return lines;
|
|
13
|
+
}
|
|
14
|
+
function lineBytes(line) {
|
|
15
|
+
return Buffer.byteLength(line) + 1;
|
|
16
|
+
}
|
|
17
|
+
function linesBytes(lines) {
|
|
18
|
+
let total = 0;
|
|
19
|
+
for (const line of lines)
|
|
20
|
+
total += lineBytes(line);
|
|
21
|
+
return total;
|
|
22
|
+
}
|
|
23
|
+
function blockOverhead(block) {
|
|
24
|
+
const heading = block.heading === undefined ? 0 : lineBytes(block.heading) + 1;
|
|
25
|
+
if (block.diffLines.length === 0)
|
|
26
|
+
return heading;
|
|
27
|
+
const fence = diffFence(block.diffLines).length;
|
|
28
|
+
return heading + fence + 5 + fence + 1 + 1;
|
|
29
|
+
}
|
|
30
|
+
function truncateBytes(text, maxBytes) {
|
|
31
|
+
const buf = Buffer.from(text);
|
|
32
|
+
if (buf.length <= maxBytes)
|
|
33
|
+
return text;
|
|
34
|
+
let end = Math.max(0, maxBytes);
|
|
35
|
+
while (end > 0 && (buf[end] & 0xc0) === 0x80)
|
|
36
|
+
end--;
|
|
37
|
+
return buf.subarray(0, end).toString();
|
|
38
|
+
}
|
|
39
|
+
function cutToFit(diffLines, room) {
|
|
40
|
+
const kept = [];
|
|
41
|
+
let used = lineBytes(CUT_NOTE);
|
|
42
|
+
for (const line of diffLines) {
|
|
43
|
+
const left = room - used;
|
|
44
|
+
if (lineBytes(line) > left) {
|
|
45
|
+
const part = truncateBytes(line, left - 1 - Buffer.byteLength("…"));
|
|
46
|
+
if (Buffer.byteLength(part) >= MIN_CUT_LINE_BYTES)
|
|
47
|
+
kept.push(part + "…");
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
kept.push(line);
|
|
51
|
+
used += lineBytes(line);
|
|
52
|
+
}
|
|
53
|
+
if (kept.length > 0)
|
|
54
|
+
kept.push(CUT_NOTE);
|
|
55
|
+
return kept;
|
|
56
|
+
}
|
|
57
|
+
function hiddenNote(repos, maxBytes) {
|
|
58
|
+
const prefix = `_${repos.length} more ${repos.length === 1 ? "repo" : "repos"} not shown, see the job log:_`;
|
|
59
|
+
const more = ", …";
|
|
60
|
+
let note = prefix;
|
|
61
|
+
for (let i = 0; i < repos.length; i++) {
|
|
62
|
+
const name = `${i === 0 ? " " : ", "}${inlineCode(repos[i])}`;
|
|
63
|
+
const tail = i < repos.length - 1 ? Buffer.byteLength(more) : 0;
|
|
64
|
+
if (Buffer.byteLength(note + name) + tail > maxBytes) {
|
|
65
|
+
return i === 0 ? prefix : note + more;
|
|
66
|
+
}
|
|
67
|
+
note += name;
|
|
68
|
+
}
|
|
69
|
+
return note;
|
|
70
|
+
}
|
|
71
|
+
// Priority: footer, then header, then whole blocks, then cut blocks with what is left.
|
|
72
|
+
export function fitSummary(parts, maxBytes) {
|
|
73
|
+
let used = Buffer.byteLength(parts.footer);
|
|
74
|
+
if (used > maxBytes)
|
|
75
|
+
return "";
|
|
76
|
+
const lines = [];
|
|
77
|
+
const headerBytes = linesBytes(parts.header);
|
|
78
|
+
if (used + headerBytes <= maxBytes) {
|
|
79
|
+
for (const line of parts.header)
|
|
80
|
+
lines.push(line);
|
|
81
|
+
used += headerBytes;
|
|
82
|
+
}
|
|
83
|
+
const overheads = parts.blocks.map(blockOverhead);
|
|
84
|
+
const sizes = parts.blocks.map((block, i) => overheads[i] + linesBytes(block.diffLines));
|
|
85
|
+
const allFit = used + sizes.reduce((a, b) => a + b, 0) <= maxBytes;
|
|
86
|
+
const reserve = allFit
|
|
87
|
+
? 0
|
|
88
|
+
: Math.min(NOTE_RESERVE_BYTES, Math.floor((maxBytes - used) / 2));
|
|
89
|
+
const room = () => maxBytes - used - reserve;
|
|
90
|
+
const shown = [];
|
|
91
|
+
parts.blocks.forEach((block, i) => {
|
|
92
|
+
if (sizes[i] <= room()) {
|
|
93
|
+
shown[i] = block.diffLines;
|
|
94
|
+
used += sizes[i];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
parts.blocks.forEach((block, i) => {
|
|
98
|
+
if (shown[i])
|
|
99
|
+
return;
|
|
100
|
+
const kept = cutToFit(block.diffLines, room() - overheads[i]);
|
|
101
|
+
if (kept.length === 0)
|
|
102
|
+
return;
|
|
103
|
+
shown[i] = kept;
|
|
104
|
+
used += overheads[i] + linesBytes(kept);
|
|
105
|
+
});
|
|
106
|
+
const hidden = [];
|
|
107
|
+
parts.blocks.forEach((block, i) => {
|
|
108
|
+
const diffLines = shown[i];
|
|
109
|
+
if (!diffLines) {
|
|
110
|
+
for (const repo of block.repos)
|
|
111
|
+
hidden.push(repo);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (block.heading !== undefined)
|
|
115
|
+
lines.push(block.heading, "");
|
|
116
|
+
appendDiffBlock(lines, diffLines);
|
|
117
|
+
});
|
|
118
|
+
if (hidden.length > 0) {
|
|
119
|
+
const note = hiddenNote(hidden, maxBytes - used - 2);
|
|
120
|
+
if (lineBytes(note) + 1 <= maxBytes - used)
|
|
121
|
+
lines.push(note, "");
|
|
122
|
+
}
|
|
123
|
+
lines.push(parts.footer);
|
|
124
|
+
return lines.join("\n");
|
|
125
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { MergeMode } from "../config/index.js";
|
|
2
2
|
import type { ActiveAction } from "../settings/index.js";
|
|
3
|
+
import type { DebugLog } from "../shared/logger.js";
|
|
3
4
|
export interface ReportFileChange {
|
|
4
5
|
path: string;
|
|
5
6
|
action: ActiveAction;
|
|
@@ -23,6 +24,8 @@ export interface RepoFileChanges {
|
|
|
23
24
|
error?: string;
|
|
24
25
|
}
|
|
25
26
|
export declare function formatSyncReportCLI(report: SyncReport): string[];
|
|
26
|
-
export declare function formatSyncReportMarkdown(report: SyncReport, dryRun: boolean): string;
|
|
27
|
-
export declare
|
|
28
|
-
export declare
|
|
27
|
+
export declare function formatSyncReportMarkdown(report: SyncReport, dryRun: boolean, maxBytes?: number): string;
|
|
28
|
+
export declare const SUMMARY_DIFF_LINE_LIMIT = 500;
|
|
29
|
+
export declare const SUMMARY_LINE_MAX_CHARS = 1000;
|
|
30
|
+
export declare function renderSyncLines(syncRepo: RepoFileChanges, lines?: string[]): string[];
|
|
31
|
+
export declare function writeSyncReportSummary(report: SyncReport, dryRun: boolean, summaryPath: string | undefined, log?: DebugLog): void;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import chalk from "chalk";
|
|
2
|
-
import { writeGitHubStepSummary } from "./github-summary.js";
|
|
3
2
|
import { formatCountEntry } from "../shared/count-format.js";
|
|
4
3
|
import { formatDiffLine } from "../shared/diff-format.js";
|
|
4
|
+
import { STEP_SUMMARY_MAX_BYTES, writeGitHubStepSummary, } from "./github-summary.js";
|
|
5
|
+
import { fitSummary, summaryHeader } from "./summary-budget.js";
|
|
5
6
|
function formatSyncSummary(totals) {
|
|
6
7
|
const entry = formatCountEntry("file", "files", [
|
|
7
8
|
{ label: "to create", value: totals.files.create },
|
|
@@ -41,35 +42,30 @@ export function formatSyncReportCLI(report) {
|
|
|
41
42
|
lines.push(formatSyncSummary(report.totals));
|
|
42
43
|
return lines;
|
|
43
44
|
}
|
|
44
|
-
export function formatSyncReportMarkdown(report, dryRun) {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
lines.push(`### ${repo.repoName}`);
|
|
59
|
-
lines.push("");
|
|
60
|
-
const diffLines = renderSyncLines(repo);
|
|
61
|
-
if (diffLines.length > 0) {
|
|
62
|
-
lines.push("```diff");
|
|
63
|
-
lines.push(...diffLines);
|
|
64
|
-
lines.push("```");
|
|
65
|
-
lines.push("");
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
lines.push(`**${formatSyncSummary(report.totals)}**`);
|
|
69
|
-
return lines.join("\n");
|
|
45
|
+
export function formatSyncReportMarkdown(report, dryRun, maxBytes = STEP_SUMMARY_MAX_BYTES) {
|
|
46
|
+
const blocks = report.repos
|
|
47
|
+
.filter((repo) => repo.files.length > 0 || repo.error)
|
|
48
|
+
.map((repo) => ({
|
|
49
|
+
heading: `### ${repo.repoName}`,
|
|
50
|
+
diffLines: renderSyncLines(repo),
|
|
51
|
+
repos: [repo.repoName],
|
|
52
|
+
}));
|
|
53
|
+
return fitSummary({
|
|
54
|
+
header: summaryHeader(dryRun),
|
|
55
|
+
blocks,
|
|
56
|
+
footer: `**${formatSyncSummary(report.totals)}**`,
|
|
57
|
+
}, maxBytes);
|
|
70
58
|
}
|
|
71
|
-
|
|
72
|
-
|
|
59
|
+
// Stops one huge file from using up the summary space the other files need.
|
|
60
|
+
export const SUMMARY_DIFF_LINE_LIMIT = 500;
|
|
61
|
+
// Same idea for single long lines, e.g. minified JSON.
|
|
62
|
+
export const SUMMARY_LINE_MAX_CHARS = 1000;
|
|
63
|
+
function shortenLine(line) {
|
|
64
|
+
return line.length > SUMMARY_LINE_MAX_CHARS
|
|
65
|
+
? line.slice(0, SUMMARY_LINE_MAX_CHARS) + "…"
|
|
66
|
+
: line;
|
|
67
|
+
}
|
|
68
|
+
export function renderSyncLines(syncRepo, lines = []) {
|
|
73
69
|
for (let i = 0; i < syncRepo.files.length; i++) {
|
|
74
70
|
const file = syncRepo.files[i];
|
|
75
71
|
if (i > 0)
|
|
@@ -83,8 +79,12 @@ export function renderSyncLines(syncRepo) {
|
|
|
83
79
|
else if (file.action === "delete") {
|
|
84
80
|
lines.push(`- ${file.path}`);
|
|
85
81
|
}
|
|
86
|
-
|
|
87
|
-
|
|
82
|
+
const diffLines = file.diffLines ?? [];
|
|
83
|
+
const shown = Math.min(diffLines.length, SUMMARY_DIFF_LINE_LIMIT);
|
|
84
|
+
for (let j = 0; j < shown; j++)
|
|
85
|
+
lines.push(shortenLine(diffLines[j]));
|
|
86
|
+
if (diffLines.length > shown) {
|
|
87
|
+
lines.push(`... ${diffLines.length - shown} more lines not shown`);
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
if (syncRepo.error) {
|
|
@@ -92,7 +92,6 @@ export function renderSyncLines(syncRepo) {
|
|
|
92
92
|
}
|
|
93
93
|
return lines;
|
|
94
94
|
}
|
|
95
|
-
export function writeSyncReportSummary(report, dryRun, summaryPath) {
|
|
96
|
-
|
|
97
|
-
writeGitHubStepSummary(markdown, summaryPath);
|
|
95
|
+
export function writeSyncReportSummary(report, dryRun, summaryPath, log) {
|
|
96
|
+
writeGitHubStepSummary((maxBytes) => formatSyncReportMarkdown(report, dryRun, maxBytes), summaryPath, log);
|
|
98
97
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { GitHubCollaborator, GitHubRepoInvitation } from "./types.js";
|
|
2
|
+
import type { SettingsAction } from "../base-processor.js";
|
|
3
|
+
export type CollaboratorAction = Exclude<SettingsAction, "update">;
|
|
4
|
+
export interface CollaboratorChange {
|
|
5
|
+
action: CollaboratorAction;
|
|
6
|
+
username: string;
|
|
7
|
+
pending?: boolean;
|
|
8
|
+
invitationId?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface CollaboratorsDiffInput {
|
|
11
|
+
owner: string;
|
|
12
|
+
collaborators: GitHubCollaborator[];
|
|
13
|
+
invitations: GitHubRepoInvitation[];
|
|
14
|
+
desired: string[];
|
|
15
|
+
deleteOrphaned: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare function diffCollaborators(input: CollaboratorsDiffInput): CollaboratorChange[];
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export function diffCollaborators(input) {
|
|
2
|
+
const owner = input.owner.toLowerCase();
|
|
3
|
+
const collaborators = new Map(input.collaborators.map((c) => [c.login.toLowerCase(), c]));
|
|
4
|
+
const invitations = new Map();
|
|
5
|
+
for (const inv of input.invitations) {
|
|
6
|
+
if (inv.invitee) {
|
|
7
|
+
invitations.set(inv.invitee.login.toLowerCase(), {
|
|
8
|
+
id: inv.id,
|
|
9
|
+
login: inv.invitee.login,
|
|
10
|
+
expired: inv.expired === true,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const desired = new Set(input.desired.map((u) => u.toLowerCase()));
|
|
15
|
+
const changes = [];
|
|
16
|
+
if (input.deleteOrphaned) {
|
|
17
|
+
for (const [key, collaborator] of collaborators) {
|
|
18
|
+
if (desired.has(key) || key === owner)
|
|
19
|
+
continue;
|
|
20
|
+
changes.push({ action: "delete", username: collaborator.login });
|
|
21
|
+
}
|
|
22
|
+
for (const [key, invitation] of invitations) {
|
|
23
|
+
if (desired.has(key) || collaborators.has(key))
|
|
24
|
+
continue;
|
|
25
|
+
changes.push({
|
|
26
|
+
action: "delete",
|
|
27
|
+
username: invitation.login,
|
|
28
|
+
pending: true,
|
|
29
|
+
invitationId: invitation.id,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const unchanged = [];
|
|
34
|
+
for (const user of input.desired) {
|
|
35
|
+
const key = user.toLowerCase();
|
|
36
|
+
const collaborator = collaborators.get(key);
|
|
37
|
+
const invitation = invitations.get(key);
|
|
38
|
+
if (collaborator) {
|
|
39
|
+
unchanged.push({ action: "unchanged", username: collaborator.login });
|
|
40
|
+
}
|
|
41
|
+
else if (invitation && !invitation.expired) {
|
|
42
|
+
unchanged.push({
|
|
43
|
+
action: "unchanged",
|
|
44
|
+
username: invitation.login,
|
|
45
|
+
pending: true,
|
|
46
|
+
invitationId: invitation.id,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
else if (key === owner) {
|
|
50
|
+
unchanged.push({ action: "unchanged", username: user });
|
|
51
|
+
}
|
|
52
|
+
else if (invitation) {
|
|
53
|
+
changes.push({
|
|
54
|
+
action: "create",
|
|
55
|
+
username: user,
|
|
56
|
+
invitationId: invitation.id,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
changes.push({ action: "create", username: user });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return [...changes, ...unchanged];
|
|
64
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { CollaboratorChange, CollaboratorAction } from "./diff.js";
|
|
2
|
+
export interface CollaboratorsPlanEntry {
|
|
3
|
+
name: string;
|
|
4
|
+
action: CollaboratorAction;
|
|
5
|
+
pending?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface CollaboratorsPlanResult {
|
|
8
|
+
lines: string[];
|
|
9
|
+
creates: number;
|
|
10
|
+
deletes: number;
|
|
11
|
+
unchanged: number;
|
|
12
|
+
entries: CollaboratorsPlanEntry[];
|
|
13
|
+
}
|
|
14
|
+
export declare function formatCollaboratorsPlan(changes: CollaboratorChange[]): CollaboratorsPlanResult;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import { quoted } from "../../shared/string-utils.js";
|
|
3
|
+
import { countActions } from "../base-processor.js";
|
|
4
|
+
export function formatCollaboratorsPlan(changes) {
|
|
5
|
+
const lines = [];
|
|
6
|
+
const entries = changes.map((c) => c.pending
|
|
7
|
+
? { name: c.username, action: c.action, pending: true }
|
|
8
|
+
: { name: c.username, action: c.action });
|
|
9
|
+
const { create: creates, delete: deletes, unchanged } = countActions(changes);
|
|
10
|
+
const creating = changes.filter((c) => c.action === "create");
|
|
11
|
+
const deleting = changes.filter((c) => c.action === "delete");
|
|
12
|
+
const pending = changes.filter((c) => c.action === "unchanged" && c.pending);
|
|
13
|
+
if (creating.length > 0) {
|
|
14
|
+
lines.push(chalk.bold(" Invite:"));
|
|
15
|
+
for (const c of creating) {
|
|
16
|
+
lines.push(chalk.green(` + collaborator ${quoted(c.username)} (invite)`));
|
|
17
|
+
}
|
|
18
|
+
lines.push("");
|
|
19
|
+
}
|
|
20
|
+
if (deleting.length > 0) {
|
|
21
|
+
lines.push(chalk.bold(" Remove:"));
|
|
22
|
+
for (const c of deleting) {
|
|
23
|
+
const suffix = c.pending ? " (cancel invite)" : "";
|
|
24
|
+
lines.push(chalk.red(` - collaborator ${quoted(c.username)}${suffix}`));
|
|
25
|
+
}
|
|
26
|
+
lines.push("");
|
|
27
|
+
}
|
|
28
|
+
for (const c of pending) {
|
|
29
|
+
lines.push(chalk.dim(` collaborator ${quoted(c.username)}: invite pending`));
|
|
30
|
+
}
|
|
31
|
+
const total = creates + deletes;
|
|
32
|
+
if (total > 0) {
|
|
33
|
+
const parts = [];
|
|
34
|
+
if (creates > 0)
|
|
35
|
+
parts.push(`${creates} to invite`);
|
|
36
|
+
if (deletes > 0)
|
|
37
|
+
parts.push(`${deletes} to remove`);
|
|
38
|
+
lines.push(` Plan: ${total} collaborators (${parts.join(", ")})`);
|
|
39
|
+
}
|
|
40
|
+
return { lines, creates, deletes, unchanged, entries };
|
|
41
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ICommandExecutor } from "../../shared/command-executor.js";
|
|
2
|
+
import { type RepoInfo } from "../../repo/index.js";
|
|
3
|
+
import { type GhApiOptions } from "../../shared/gh-api-utils.js";
|
|
4
|
+
import type { ICollaboratorsStrategy, GitHubCollaborator, GitHubRepoInvitation } from "./types.js";
|
|
5
|
+
interface GitHubCollaboratorsStrategyOptions {
|
|
6
|
+
retries?: number;
|
|
7
|
+
cwd: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class GitHubCollaboratorsStrategy implements ICollaboratorsStrategy {
|
|
10
|
+
private api;
|
|
11
|
+
constructor(executor: ICommandExecutor, options: GitHubCollaboratorsStrategyOptions);
|
|
12
|
+
listCollaborators(repoInfo: RepoInfo, options?: GhApiOptions): Promise<GitHubCollaborator[]>;
|
|
13
|
+
listInvitations(repoInfo: RepoInfo, options?: GhApiOptions): Promise<GitHubRepoInvitation[]>;
|
|
14
|
+
add(repoInfo: RepoInfo, username: string, options?: GhApiOptions): Promise<void>;
|
|
15
|
+
remove(repoInfo: RepoInfo, username: string, options?: GhApiOptions): Promise<void>;
|
|
16
|
+
cancelInvitation(repoInfo: RepoInfo, invitationId: number, options?: GhApiOptions): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { assertGitHubRepo } from "../../repo/index.js";
|
|
2
|
+
import { GhApiClient } from "../../shared/gh-api-utils.js";
|
|
3
|
+
import { parseApiJson } from "../../shared/json-utils.js";
|
|
4
|
+
const CONTEXT = "GitHub Collaborators strategy";
|
|
5
|
+
export class GitHubCollaboratorsStrategy {
|
|
6
|
+
api;
|
|
7
|
+
constructor(executor, options) {
|
|
8
|
+
this.api = new GhApiClient(executor, options.retries ?? 3, options.cwd);
|
|
9
|
+
}
|
|
10
|
+
async listCollaborators(repoInfo, options) {
|
|
11
|
+
assertGitHubRepo(repoInfo, CONTEXT);
|
|
12
|
+
const endpoint = `/repos/${repoInfo.owner}/${repoInfo.repo}/collaborators?affiliation=direct`;
|
|
13
|
+
const result = await this.api.call("GET", endpoint, {
|
|
14
|
+
options,
|
|
15
|
+
paginate: true,
|
|
16
|
+
});
|
|
17
|
+
return parseApiJson(result, "collaborators response");
|
|
18
|
+
}
|
|
19
|
+
async listInvitations(repoInfo, options) {
|
|
20
|
+
assertGitHubRepo(repoInfo, CONTEXT);
|
|
21
|
+
const endpoint = `/repos/${repoInfo.owner}/${repoInfo.repo}/invitations`;
|
|
22
|
+
const result = await this.api.call("GET", endpoint, {
|
|
23
|
+
options,
|
|
24
|
+
paginate: true,
|
|
25
|
+
});
|
|
26
|
+
return parseApiJson(result, "invitations response");
|
|
27
|
+
}
|
|
28
|
+
async add(repoInfo, username, options) {
|
|
29
|
+
assertGitHubRepo(repoInfo, CONTEXT);
|
|
30
|
+
const endpoint = `/repos/${repoInfo.owner}/${repoInfo.repo}/collaborators/${encodeURIComponent(username)}`;
|
|
31
|
+
await this.api.call("PUT", endpoint, { options });
|
|
32
|
+
}
|
|
33
|
+
async remove(repoInfo, username, options) {
|
|
34
|
+
assertGitHubRepo(repoInfo, CONTEXT);
|
|
35
|
+
const endpoint = `/repos/${repoInfo.owner}/${repoInfo.repo}/collaborators/${encodeURIComponent(username)}`;
|
|
36
|
+
await this.api.call("DELETE", endpoint, { options });
|
|
37
|
+
}
|
|
38
|
+
async cancelInvitation(repoInfo, invitationId, options) {
|
|
39
|
+
assertGitHubRepo(repoInfo, CONTEXT);
|
|
40
|
+
const endpoint = `/repos/${repoInfo.owner}/${repoInfo.repo}/invitations/${invitationId}`;
|
|
41
|
+
await this.api.call("DELETE", endpoint, { options });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { type CollaboratorChange, type CollaboratorAction } from "./diff.js";
|
|
2
|
+
export { type CollaboratorsPlanEntry } from "./formatter.js";
|
|
3
|
+
export { CollaboratorsProcessor, type ICollaboratorsProcessor, } from "./processor.js";
|
|
4
|
+
export { GitHubCollaboratorsStrategy } from "./github-collaborators-strategy.js";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { RepoConfig } from "../../config/index.js";
|
|
2
|
+
import type { IRepoMetadataProvider, RepoInfo } from "../../repo/index.js";
|
|
3
|
+
import { type CollaboratorsPlanResult } from "./formatter.js";
|
|
4
|
+
import type { ICollaboratorsStrategy } from "./types.js";
|
|
5
|
+
import { type BaseProcessorOptions, type BaseProcessorResult, type ISettingsProcessor, type ChangeCounts } from "../base-processor.js";
|
|
6
|
+
export type ICollaboratorsProcessor = ISettingsProcessor<CollaboratorsProcessorOptions, CollaboratorsProcessorResult>;
|
|
7
|
+
export interface CollaboratorsProcessorOptions extends BaseProcessorOptions {
|
|
8
|
+
noDelete?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface CollaboratorsProcessorResult extends BaseProcessorResult {
|
|
11
|
+
changes?: ChangeCounts;
|
|
12
|
+
planOutput?: CollaboratorsPlanResult;
|
|
13
|
+
warnings?: string[];
|
|
14
|
+
}
|
|
15
|
+
export declare class CollaboratorsProcessor implements ICollaboratorsProcessor {
|
|
16
|
+
private readonly strategy;
|
|
17
|
+
private readonly metadataProvider;
|
|
18
|
+
constructor(strategy: ICollaboratorsStrategy, metadataProvider: IRepoMetadataProvider);
|
|
19
|
+
process(repoConfig: RepoConfig, repoInfo: RepoInfo, options: CollaboratorsProcessorOptions): Promise<CollaboratorsProcessorResult>;
|
|
20
|
+
private applySettings;
|
|
21
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { diffCollaborators } from "./diff.js";
|
|
2
|
+
import { formatCollaboratorsPlan, } from "./formatter.js";
|
|
3
|
+
import { withGitHubGuards, countActions, buildDryRunResult, buildApplyResult, } from "../base-processor.js";
|
|
4
|
+
export class CollaboratorsProcessor {
|
|
5
|
+
strategy;
|
|
6
|
+
metadataProvider;
|
|
7
|
+
constructor(strategy, metadataProvider) {
|
|
8
|
+
this.strategy = strategy;
|
|
9
|
+
this.metadataProvider = metadataProvider;
|
|
10
|
+
}
|
|
11
|
+
async process(repoConfig, repoInfo, options) {
|
|
12
|
+
return withGitHubGuards(repoConfig, repoInfo, options, {
|
|
13
|
+
hasDesiredSettings: (rc) => {
|
|
14
|
+
const c = rc.settings?.collaborators;
|
|
15
|
+
return (c?.users ?? []).length > 0 || c?.deleteOrphaned === true;
|
|
16
|
+
},
|
|
17
|
+
emptySettingsMessage: "No collaborators configured",
|
|
18
|
+
applySettings: (githubRepo, rc, opts, token, repoName) => this.applySettings(githubRepo, rc, opts, token, repoName),
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
async applySettings(githubRepo, repoConfig, options, effectiveToken, repoName) {
|
|
22
|
+
const { dryRun, noDelete } = options;
|
|
23
|
+
const desired = repoConfig.settings?.collaborators ?? {};
|
|
24
|
+
const strategyOptions = { token: effectiveToken, host: githubRepo.host };
|
|
25
|
+
const metadata = await this.metadataProvider.getMetadata(githubRepo, strategyOptions);
|
|
26
|
+
if (metadata.ownerType !== "User") {
|
|
27
|
+
return {
|
|
28
|
+
success: true,
|
|
29
|
+
repoName,
|
|
30
|
+
message: "Skipped: collaborators only apply to personal repos",
|
|
31
|
+
skipped: true,
|
|
32
|
+
warnings: [
|
|
33
|
+
`${repoName}: collaborators only apply to personal repos (owner is an organization) - skipped`,
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const deleteOrphaned = desired.deleteOrphaned === true && !noDelete;
|
|
38
|
+
const [collaborators, invitations] = await Promise.all([
|
|
39
|
+
this.strategy.listCollaborators(githubRepo, strategyOptions),
|
|
40
|
+
this.strategy.listInvitations(githubRepo, strategyOptions),
|
|
41
|
+
]);
|
|
42
|
+
const changes = diffCollaborators({
|
|
43
|
+
owner: githubRepo.owner,
|
|
44
|
+
collaborators,
|
|
45
|
+
invitations,
|
|
46
|
+
desired: desired.users ?? [],
|
|
47
|
+
deleteOrphaned,
|
|
48
|
+
});
|
|
49
|
+
const changeCounts = countActions(changes);
|
|
50
|
+
const planOutput = formatCollaboratorsPlan(changes);
|
|
51
|
+
if (dryRun) {
|
|
52
|
+
return buildDryRunResult(repoName, changeCounts, { planOutput });
|
|
53
|
+
}
|
|
54
|
+
let appliedCount = 0;
|
|
55
|
+
for (const change of changes) {
|
|
56
|
+
if (change.action === "create") {
|
|
57
|
+
if (change.invitationId !== undefined) {
|
|
58
|
+
await this.strategy.cancelInvitation(githubRepo, change.invitationId, strategyOptions);
|
|
59
|
+
}
|
|
60
|
+
await this.strategy.add(githubRepo, change.username, strategyOptions);
|
|
61
|
+
appliedCount++;
|
|
62
|
+
}
|
|
63
|
+
else if (change.action === "delete") {
|
|
64
|
+
if (change.pending && change.invitationId !== undefined) {
|
|
65
|
+
await this.strategy.cancelInvitation(githubRepo, change.invitationId, strategyOptions);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
await this.strategy.remove(githubRepo, change.username, strategyOptions);
|
|
69
|
+
}
|
|
70
|
+
appliedCount++;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return buildApplyResult(repoName, changeCounts, appliedCount, {
|
|
74
|
+
planOutput,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { RepoInfo } from "../../repo/index.js";
|
|
2
|
+
import type { GhApiOptions } from "../../shared/gh-api-utils.js";
|
|
3
|
+
export interface GitHubCollaborator {
|
|
4
|
+
login: string;
|
|
5
|
+
}
|
|
6
|
+
export interface GitHubRepoInvitation {
|
|
7
|
+
id: number;
|
|
8
|
+
invitee: {
|
|
9
|
+
login: string;
|
|
10
|
+
} | null;
|
|
11
|
+
expired?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export interface ICollaboratorsStrategy {
|
|
14
|
+
listCollaborators(repoInfo: RepoInfo, options?: GhApiOptions): Promise<GitHubCollaborator[]>;
|
|
15
|
+
listInvitations(repoInfo: RepoInfo, options?: GhApiOptions): Promise<GitHubRepoInvitation[]>;
|
|
16
|
+
add(repoInfo: RepoInfo, username: string, options?: GhApiOptions): Promise<void>;
|
|
17
|
+
remove(repoInfo: RepoInfo, username: string, options?: GhApiOptions): Promise<void>;
|
|
18
|
+
cancelInvitation(repoInfo: RepoInfo, invitationId: number, options?: GhApiOptions): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/settings/index.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export { RepoSettingsProcessor, type IRepoSettingsProcessor, type RepoSettingsPl
|
|
|
4
4
|
export { type LabelsPlanEntry, LabelsProcessor, type ILabelsProcessor, GitHubLabelsStrategy, } from "./labels/index.js";
|
|
5
5
|
export { type CodeScanningPlanEntry, CodeScanningProcessor, type ICodeScanningProcessor, GitHubCodeScanningStrategy, } from "./code-scanning/index.js";
|
|
6
6
|
export { type VariablesPlanEntry, VariablesProcessor, type IVariablesProcessor, GitHubVariablesStrategy, } from "./variables/index.js";
|
|
7
|
+
export { type CollaboratorsPlanEntry, CollaboratorsProcessor, type ICollaboratorsProcessor, GitHubCollaboratorsStrategy, } from "./collaborators/index.js";
|
|
7
8
|
export { SecretsProcessor, type SecretsPlanEntry, type ISecretsProcessor, type SecretsProcessorOptions, type SecretsProcessorResult, GitHubSecretsStrategy, SodiumEncryptor, type ISecretEncryptor, } from "./secrets/index.js";
|
package/dist/settings/index.js
CHANGED
|
@@ -4,5 +4,6 @@ export { RepoSettingsProcessor, GitHubRepoSettingsStrategy, } from "./repo-setti
|
|
|
4
4
|
export { LabelsProcessor, GitHubLabelsStrategy, } from "./labels/index.js";
|
|
5
5
|
export { CodeScanningProcessor, GitHubCodeScanningStrategy, } from "./code-scanning/index.js";
|
|
6
6
|
export { VariablesProcessor, GitHubVariablesStrategy, } from "./variables/index.js";
|
|
7
|
+
export { CollaboratorsProcessor, GitHubCollaboratorsStrategy, } from "./collaborators/index.js";
|
|
7
8
|
// Secrets — not wired into `xfg sync`; driven by `xfg secrets sync` only.
|
|
8
9
|
export { SecretsProcessor, GitHubSecretsStrategy, SodiumEncryptor, } from "./secrets/index.js";
|