@nzpr/kb 0.1.7 → 0.1.9
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 +4 -1
- package/lib/cli.js +4 -3
- package/lib/repo-init.js +46 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -176,15 +176,18 @@ export KB_GITHUB_REPO=owner/repo
|
|
|
176
176
|
export GITHUB_TOKEN=...
|
|
177
177
|
```
|
|
178
178
|
|
|
179
|
-
Publishing is allowed when the provided `GITHUB_TOKEN`
|
|
179
|
+
Publishing is allowed when the provided `GITHUB_TOKEN` can read `KB_GITHUB_REPO`. Normal readers do not need GitHub credentials.
|
|
180
180
|
|
|
181
181
|
When `kb init-repo` is given `--repo`, it uses the same token to:
|
|
182
182
|
|
|
183
183
|
- enable issues in the target repo
|
|
184
|
+
- set GitHub Actions workflow permissions to write and allow Actions to create PRs
|
|
184
185
|
- create or update the `kb-entry` and `kb-approved` labels
|
|
185
186
|
- write repository secrets and variables
|
|
186
187
|
- verify and initialize the target database schema if `--database-url` is provided
|
|
187
188
|
|
|
189
|
+
For the GitHub setup step, that token must have repository admin access. A plain write token is not enough for Actions settings and repo secrets.
|
|
190
|
+
|
|
188
191
|
If you run `kb init-repo` without the repo or database inputs, it still scaffolds the knowledge repo and prints exactly which remote bootstrap inputs are still pending.
|
|
189
192
|
|
|
190
193
|
## Quick Start
|
package/lib/cli.js
CHANGED
|
@@ -322,10 +322,10 @@ async function requirePublishAccess({ repo, token, apiBaseUrl }) {
|
|
|
322
322
|
if (!permissions) {
|
|
323
323
|
return;
|
|
324
324
|
}
|
|
325
|
-
if (permissions.admin || permissions.maintain || permissions.push) {
|
|
325
|
+
if (permissions.admin || permissions.maintain || permissions.push || permissions.triage || permissions.pull) {
|
|
326
326
|
return;
|
|
327
327
|
}
|
|
328
|
-
throw new Error(`GITHUB_TOKEN does not have
|
|
328
|
+
throw new Error(`GITHUB_TOKEN does not have repository access to ${repo}`);
|
|
329
329
|
}
|
|
330
330
|
|
|
331
331
|
function printRepoConfiguration(configuration) {
|
|
@@ -349,7 +349,8 @@ function printRepoConfiguration(configuration) {
|
|
|
349
349
|
console.log("");
|
|
350
350
|
console.log("publish workflow auth:");
|
|
351
351
|
console.log(" KB_GITHUB_REPO is set automatically to github.repository in the scaffolded workflow");
|
|
352
|
-
console.log(" GITHUB_TOKEN is provided automatically by GitHub Actions and
|
|
352
|
+
console.log(" GITHUB_TOKEN is provided automatically by GitHub Actions and only needs repository read access for publish");
|
|
353
|
+
console.log(" the token used for kb init-repo itself must have admin access if you want the CLI to configure repo Actions settings")
|
|
353
354
|
}
|
|
354
355
|
|
|
355
356
|
function printInitRepoStatus(result) {
|
package/lib/repo-init.js
CHANGED
|
@@ -226,6 +226,19 @@ async function configureKnowledgeRepo({
|
|
|
226
226
|
variables,
|
|
227
227
|
runGitHubCommand
|
|
228
228
|
}) {
|
|
229
|
+
const repository = await fetchGitHubRepositoryMetadata({
|
|
230
|
+
repo,
|
|
231
|
+
githubToken,
|
|
232
|
+
runGitHubCommand
|
|
233
|
+
});
|
|
234
|
+
const permissions = repository.permissions ?? null;
|
|
235
|
+
|
|
236
|
+
if (!permissions?.admin) {
|
|
237
|
+
throw new Error(
|
|
238
|
+
`GITHUB_TOKEN must have admin access to ${repo} so init-repo can configure Actions workflow permissions, PR creation, labels, and repo secrets`
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
229
242
|
await runGitHubCommand(["repo", "edit", repo, "--enable-issues"], { githubToken });
|
|
230
243
|
const actions = await ensureGitHubActionsPermissions({
|
|
231
244
|
repo,
|
|
@@ -275,7 +288,11 @@ async function configureKnowledgeRepo({
|
|
|
275
288
|
async function ensureGitHubActionsPermissions({ repo, githubToken, runGitHubCommand }) {
|
|
276
289
|
const actionsPermissions = await runGitHubJson(
|
|
277
290
|
["api", `repos/${repo}/actions/permissions`],
|
|
278
|
-
{
|
|
291
|
+
{
|
|
292
|
+
githubToken,
|
|
293
|
+
runGitHubCommand,
|
|
294
|
+
notFoundMessage: buildActionsAdminError(repo)
|
|
295
|
+
}
|
|
279
296
|
);
|
|
280
297
|
|
|
281
298
|
if (actionsPermissions.enabled === false) {
|
|
@@ -296,7 +313,11 @@ async function ensureGitHubActionsPermissions({ repo, githubToken, runGitHubComm
|
|
|
296
313
|
|
|
297
314
|
const workflowPermissions = await runGitHubJson(
|
|
298
315
|
["api", `repos/${repo}/actions/permissions/workflow`],
|
|
299
|
-
{
|
|
316
|
+
{
|
|
317
|
+
githubToken,
|
|
318
|
+
runGitHubCommand,
|
|
319
|
+
notFoundMessage: buildActionsAdminError(repo)
|
|
320
|
+
}
|
|
300
321
|
);
|
|
301
322
|
|
|
302
323
|
if (
|
|
@@ -326,9 +347,28 @@ async function ensureGitHubActionsPermissions({ repo, githubToken, runGitHubComm
|
|
|
326
347
|
};
|
|
327
348
|
}
|
|
328
349
|
|
|
329
|
-
async function
|
|
330
|
-
|
|
331
|
-
|
|
350
|
+
async function fetchGitHubRepositoryMetadata({ repo, githubToken, runGitHubCommand }) {
|
|
351
|
+
return runGitHubJson(["api", `repos/${repo}`], {
|
|
352
|
+
githubToken,
|
|
353
|
+
runGitHubCommand
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function buildActionsAdminError(repo) {
|
|
358
|
+
return `GitHub token could not read or update Actions settings for ${repo}; use a token with repository admin access and rerun init-repo`;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
async function runGitHubJson(args, { githubToken, runGitHubCommand, notFoundMessage = null }) {
|
|
362
|
+
try {
|
|
363
|
+
const output = await runGitHubCommand(args, { githubToken });
|
|
364
|
+
return JSON.parse(output);
|
|
365
|
+
} catch (error) {
|
|
366
|
+
const message = String(error?.message ?? error);
|
|
367
|
+
if (notFoundMessage && /404|Not Found/i.test(message)) {
|
|
368
|
+
throw new Error(notFoundMessage);
|
|
369
|
+
}
|
|
370
|
+
throw error;
|
|
371
|
+
}
|
|
332
372
|
}
|
|
333
373
|
|
|
334
374
|
async function defaultVerifyDatabaseReady({ databaseUrl }) {
|
|
@@ -475,7 +515,7 @@ function renderPublishWorkflow({ docsRootRelative }) {
|
|
|
475
515
|
" publish:",
|
|
476
516
|
" runs-on: ubuntu-latest",
|
|
477
517
|
" permissions:",
|
|
478
|
-
" contents:
|
|
518
|
+
" contents: read",
|
|
479
519
|
" concurrency:",
|
|
480
520
|
" group: kb-publish",
|
|
481
521
|
" cancel-in-progress: false",
|