Use `GITHUB_REPOSITORY` as the default repo when no `repo` option is configured in `changelog-github` by Andarist · Pull Request #2120 · changesets/changesets · GitHub
Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/lucky-repos-jump.md
2 changes: 2 additions & 0 deletions docs/config-file-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ You would specify our github changelog generator with:
}
```

When running in GitHub Actions, `repo` is optional because it defaults to the `GITHUB_REPOSITORY` environment variable.

If you want to disable thank you messages, add `"disableThanks": true` to the options.

For more details on these functions and information on how to write your own see [changelog-functions](./modifying-changelog-format.md)
Expand Down
61 changes: 60 additions & 1 deletion packages/changelog-github/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { parseChangesetFile as parse } from "@changesets/parse";
import { describe, expect, it, test, vi } from "vitest";
import type { ModCompWithPackage } from "@changesets/types";
import { afterEach, describe, expect, it, test, vi } from "vitest";
import changelogFunctions from "./index.ts";

const getReleaseLine = changelogFunctions.getReleaseLine;
const getDependencyReleaseLine = changelogFunctions.getDependencyReleaseLine;

afterEach(() => {
vi.unstubAllEnvs();
});

vi.mock(
"@changesets/get-github-info",
Expand Down Expand Up @@ -99,6 +105,59 @@ const data = {
repo: "emotion-js/emotion",
};

it("uses GITHUB_REPOSITORY when repo option is absent", async () => {
vi.stubEnv("GITHUB_REPOSITORY", data.repo);
const [changeset, releaseType] = getChangeset("", data.commit);

expect(await getReleaseLine(changeset, releaseType, null)).toEqual(
`\n\n- [#1613](https://github.com/emotion-js/emotion/pull/1613) [\`a085003\`](https://github.com/emotion-js/emotion/commit/a085003) Thanks [@Andarist](https://github.com/Andarist)! - something\n`,
);
});

it("uses explicit repo option before GITHUB_REPOSITORY", async () => {
vi.stubEnv("GITHUB_REPOSITORY", "other/repo");

expect(await getReleaseLine(...getChangeset("", data.commit))).toEqual(
`\n\n- [#1613](https://github.com/emotion-js/emotion/pull/1613) [\`a085003\`](https://github.com/emotion-js/emotion/commit/a085003) Thanks [@Andarist](https://github.com/Andarist)! - something\n`,
);
});

it("uses GITHUB_REPOSITORY for dependency release lines", async () => {
vi.stubEnv("GITHUB_REPOSITORY", data.repo);

const changeset = {
...parse(
`---
pkg: "minor"
---

something
`,
),
id: "some-id",
commit: data.commit,
};

const dependency: ModCompWithPackage = {
name: "pkg",
type: "patch",
oldVersion: "0.0.1",
newVersion: "1.0.0",
changesets: [],
dir: "/repo/pkg",
packageJson: {
name: "pkg",
version: "0.0.1",
},
};

expect(await getDependencyReleaseLine([changeset], [dependency], null))
.toMatchInlineSnapshot(`
"- Updated dependencies [[\`a085003\`](https://github.com/emotion-js/emotion/commit/a085003)]:
- pkg@1.0.0"
`);
});

describe.each([data.commit, "wrongcommit", undefined])(
"with commit from changeset of %s",
(commitFromChangeset) => {
Expand Down
40 changes: 26 additions & 14 deletions packages/changelog-github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,29 @@ async function readEnv() {
process.env.GITHUB_SERVER_URL ||
(await readEnvFileCached()).GITHUB_SERVER_URL ||
"https://github.com";
return { GITHUB_SERVER_URL };
const GITHUB_REPOSITORY =
process.env.GITHUB_REPOSITORY ||
(await readEnvFileCached()).GITHUB_REPOSITORY;

return { GITHUB_SERVER_URL, GITHUB_REPOSITORY };
}

async function getRepo(options: null | Record<string, unknown>) {
let repo: string | undefined;
if (options && "repo" in options) {
repo =
typeof options.repo === "string" && options.repo
? options.repo
: undefined;
} else {
repo = (await readEnv()).GITHUB_REPOSITORY;
}
if (!repo) {
throw new Error(
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]\nor set the GITHUB_REPOSITORY environment variable.',
);
}
return repo;
}

const changelogFunctions: ChangelogFunctions = {
Expand All @@ -48,12 +70,7 @@ const changelogFunctions: ChangelogFunctions = {
dependenciesUpdated,
options,
) => {
const repo = options?.repo;
if (!repo || typeof repo !== "string") {
throw new Error(
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]',
);
}
const repo = await getRepo(options);
if (dependenciesUpdated.length === 0) return "";

const changesetLink = `- Updated dependencies [${(
Expand All @@ -76,12 +93,7 @@ const changelogFunctions: ChangelogFunctions = {
return [changesetLink, ...updatedDepenenciesList].join("\n");
},
getReleaseLine: async (changeset, type, options) => {
const repo = options?.repo;
if (!repo || typeof repo !== "string") {
throw new Error(
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]',
);
}
const repo = await getRepo(options);

const { GITHUB_SERVER_URL } = await readEnv();

Expand Down Expand Up @@ -133,7 +145,7 @@ const changelogFunctions: ChangelogFunctions = {
links.user = info?.author?.markdownLink;
}

const users = options.disableThanks
const users = options?.disableThanks
? null
: usersFromSummary.length
? usersFromSummary
Expand Down
87 changes: 41 additions & 46 deletions packages/get-github-info/src/get-commit-info.test.ts