Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include URLs in GitHub app and secret suggestions #1964

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/blocks/blockGitHubApps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { testBlock } from "bingo-stratum-testers";
import { describe, expect, test } from "vitest";

import { blockGitHubApps } from "./blockGitHubApps.js";
import { optionsBase } from "./options.fakes.js";

describe("blockGitHubApps", () => {
test("without addons", () => {
const creation = testBlock(blockGitHubApps, {
options: optionsBase,
});

expect(creation).toMatchInlineSnapshot(`
{
"suggestions": undefined,
}
`);
});

test("with addons", () => {
const creation = testBlock(blockGitHubApps, {
addons: {
apps: [
{
name: "Secret A.",
url: "https://example.com?a",
},
{
name: "Secret B.",
url: "https://example.com?b",
},
],
},
options: optionsBase,
});

expect(creation).toMatchInlineSnapshot(`
{
"suggestions": [
"- enable the GitHub apps on https://github.com/test-owner/test-repository/settings/installations:
- Secret A. (https://example.com?a)
- Secret B. (https://example.com?b)",
],
}
`);
});
});
35 changes: 15 additions & 20 deletions src/blocks/blockGitHubApps.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
import { z } from "zod";

import { base } from "../base.js";
import { getInstallationSuggestions } from "./getInstallationSuggestions.js";

const zApp = z.object({
name: z.string(),
url: z.string(),
});
export const blockGitHubApps = base.createBlock({
about: {
name: "GitHub Apps",
},
addons: {
apps: z.array(zApp).default([]),
apps: z
.array(
z.object({
name: z.string(),
url: z.string(),
}),
)
.default([]),
},
produce({ addons }) {
produce({ addons, options }) {
return {
suggestions: addons.apps.length
? [
[
`- enable the GitHub app`,
addons.apps.length === 1 ? "" : "s",
`:\n`,
addons.apps.map(printApp).join("\n"),
].join(""),
]
: undefined,
suggestions: getInstallationSuggestions(
"enable the GitHub app",
addons.apps.map((app) => `${app.name} (${app.url})`),
`https://github.com/${options.owner}/${options.repository}/settings/installations`,
),
};
},
});

function printApp(app: z.infer<typeof zApp>) {
return ` - ${app.name} (${app.url})`;
}
47 changes: 47 additions & 0 deletions src/blocks/blockRepositorySecrets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { testBlock } from "bingo-stratum-testers";
import { describe, expect, test } from "vitest";

import { blockRepositorySecrets } from "./blockRepositorySecrets.js";
import { optionsBase } from "./options.fakes.js";

describe("blockRepositorySecrets", () => {
test("without addons", () => {
const creation = testBlock(blockRepositorySecrets, {
options: optionsBase,
});

expect(creation).toMatchInlineSnapshot(`
{
"suggestions": undefined,
}
`);
});

test("with addons", () => {
const creation = testBlock(blockRepositorySecrets, {
addons: {
secrets: [
{
description: "Secret description a.",
name: "Secret Name A",
},
{
description: "Secret description b.",
name: "Secret Name B",
},
],
},
options: optionsBase,
});

expect(creation).toMatchInlineSnapshot(`
{
"suggestions": [
"- populate the secrets on https://github.com/test-owner/test-repository/settings/secrets/actions:
- Secret Name A (Secret description a.)
- Secret Name B (Secret description b.)",
],
}
`);
});
});
37 changes: 17 additions & 20 deletions src/blocks/blockRepositorySecrets.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
import { z } from "zod";

import { base } from "../base.js";
import { getInstallationSuggestions } from "./getInstallationSuggestions.js";

const zSecret = z.object({
description: z.string(),
name: z.string(),
});
export const blockRepositorySecrets = base.createBlock({
about: {
name: "Repository Secrets",
},
addons: {
secrets: z.array(zSecret).default([]),
secrets: z
.array(
z.object({
description: z.string(),
name: z.string(),
}),
)
.default([]),
},
produce({ addons }) {
produce({ addons, options }) {
return {
suggestions: addons.secrets.length
? [
[
`- populate the secret`,
addons.secrets.length === 1 ? "" : "s",
`:\n`,
addons.secrets.map(printSecret).join("\n"),
].join(""),
]
: undefined,
suggestions: getInstallationSuggestions(
"populate the secret",
addons.secrets.map(
(secret) => `${secret.name} (${secret.description})`,
),
`https://github.com/${options.owner}/${options.repository}/settings/secrets/actions`,
),
};
},
});

function printSecret(app: z.infer<typeof zSecret>) {
return ` - ${app.name} (${app.description})`;
}
41 changes: 41 additions & 0 deletions src/blocks/getInstallationSuggestions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it } from "vitest";

import { getInstallationSuggestions } from "./getInstallationSuggestions.js";

const description = "do the action";
const url = "https://example.com";

describe(getInstallationSuggestions, () => {
it("returns undefined when there are no entries", () => {
const actual = getInstallationSuggestions(description, [], url);

expect(actual).toBeUndefined();
});

it("returns a non-plural list when there is one entry", () => {
const actual = getInstallationSuggestions(description, ["entry"], url);

expect(actual).toMatchInlineSnapshot(`
[
"- do the action on https://example.com:
- entry",
]
`);
});

it("returns a plural list when there are multiple entries", () => {
const actual = getInstallationSuggestions(
description,
["entry a", "entry b"],
url,
);

expect(actual).toMatchInlineSnapshot(`
[
"- do the actions on https://example.com:
- entry a
- entry b",
]
`);
});
});
16 changes: 16 additions & 0 deletions src/blocks/getInstallationSuggestions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function getInstallationSuggestions(
description: string,
entries: string[],
url: string,
) {
return entries.length
? [
[
`- ${description}`,
entries.length === 1 ? "" : "s",
` on ${url}:\n`,
entries.map((entry) => ` - ${entry}`).join("\n"),
].join(""),
]
: undefined;
}