Skip to content

chore: custom changesets plugin #3694

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": [
"@changesets/changelog-github",
"@spectrum-tools/changesets-changelog-github",
{
"repo": "adobe/spectrum-css"
}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ jobs:
## --- Run plugins test suites --- ##
- name: Run plugin tests
run: yarn test:plugins
env:
GITHUB_TOKEN: ${{ secrets.GH_ACCESS_FOR_CHANGESETS }}

# -------------------------------------------------------------
# RUN VISUAL REGRESSION TESTS --- #
Expand Down
119 changes: 119 additions & 0 deletions plugins/changesets-changelog-github/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { getInfo, getInfoFromPullRequest } from "@changesets/get-github-info";
import { config } from "dotenv";

config();

/**
* @type {import("@changesets/types").ChangelogFunctions}
*/
const changelogFunctions = {
getDependencyReleaseLine: async (
changesets,
dependenciesUpdated,
options,
) => {
if (!options.repo) {
throw new Error(
"Please provide a repo to this changelog generator like this:\n\"changelog\": [\"@changesets/changelog-github\", { \"repo\": \"org/repo\" }]",
);
}
if (dependenciesUpdated.length === 0) return "";

const changesetLink = `Updated dependencies [${(
await Promise.all(
changesets.map(async (cs) => {
if (cs.commit) {
let { links } = await getInfo({
repo: options.repo,
commit: cs.commit,
});
return links.commit;
}
}),
)
)
.filter((_) => _)
.join(", ")}]:`;

const updatedDepenenciesList = dependenciesUpdated.map(
(dependency) => ` - ${dependency.name}@${dependency.newVersion}`,
);

return [changesetLink, ...updatedDepenenciesList].join("\n");
},
getReleaseLine: async (changeset, type, options) => {
if (!options || !options.repo) {
throw new Error(
"Please provide a repo to this changelog generator like this:\n\"changelog\": [\"@changesets/changelog-github\", { \"repo\": \"org/repo\" }]",
);
}

/** @type {number | undefined} */
let prFromSummary;
/** @type {string | undefined} */
let commitFromSummary;
/** @type {string[]} */
let usersFromSummary = [];

const replacedChangelog = changeset.summary
.replace(/^\s*(?:pr|pull|pull\s+request):\s*#?(\d+)/im, (_, pr) => {
let num = Number(pr);
if (!isNaN(num)) prFromSummary = num;
return "";
})
.replace(/^\s*commit:\s*([^\s]+)/im, (_, commit) => {
commitFromSummary = commit;
return "";
})
.replace(/^\s*(?:author|user):\s*@?([^\s]+)/gim, (_, user) => {
usersFromSummary.push(user);
return "";
})
.trim();

const changelogLines = replacedChangelog
.split("\n")
.map((l) => l.trimRight());

const links = await (async () => {
if (prFromSummary !== undefined) {
let { links } = await getInfoFromPullRequest({
repo: options.repo,
pull: prFromSummary,
});
if (commitFromSummary) {
links.commit = `[\`${commitFromSummary.slice(0, 7)}\`](https://github.com/${options.repo}/commit/${commitFromSummary})`;
}
return links;
}
const commitToFetchFrom = commitFromSummary || changeset.commit;
if (commitToFetchFrom) {
let { links } = await getInfo({
repo: options.repo,
commit: commitToFetchFrom,
});
return links;
}
return { commit: null, pull: null, user: null };
})();

const users = usersFromSummary.length
? usersFromSummary
.map(
(userFromSummary) =>
`[@${userFromSummary}](https://github.com/${userFromSummary})`,
)
.join(", ")
: links.user;

const prefix = [
links.pull === null ? "" : ` ${links.pull}`,
links.commit === null ? "" : ` ${links.commit}`,
users === null ? "" : ` Thanks ${users}!`,
].join("");

return `${prefix ? `\n\n📝 ${prefix}` : ""}\n\n${changelogLines.join("\n")}\n`;
},
};

export default changelogFunctions;
35 changes: 35 additions & 0 deletions plugins/changesets-changelog-github/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@spectrum-tools/changesets-changelog-github",
"version": "0.0.0",
"description": "A changelog entry generator for GitHub that links to commits, PRs and users",
"license": "Apache-2.0",
"author": "Adobe",
"homepage": "https://opensource.adobe.com/spectrum-css/",
"repository": {
"type": "git",
"url": "https://github.com/adobe/spectrum-css.git",
"directory": "plugins/changesets-changelog-github"
},
"bugs": {
"url": "https://github.com/adobe/spectrum-css/issues"
},
"type": "module",
"module": "index.js",
"dependencies": {
"@changesets/get-github-info": "^0.6.0",
"@changesets/types": "^6.1.0",
"dotenv": "^16.5.0"
},
"devDependencies": {
"@changesets/parse": "^0.4.1",
"ava": "^6.4.0",
"sinon": "^20.0.0"
},
"keywords": [
"design-system",
"spectrum",
"spectrum-css",
"adobe",
"adobe-spectrum"
]
}
10 changes: 10 additions & 0 deletions plugins/changesets-changelog-github/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"name": "changesets-changelog-github",
"tags": ["tooling", "changesets", "plugin"],
"targets": {
"format": { "defaultConfiguration": "plugins" },
"lint": { "defaultConfiguration": "plugins" },
"test": { "defaultConfiguration": "plugins" }
}
}
144 changes: 144 additions & 0 deletions plugins/changesets-changelog-github/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import parse from "@changesets/parse";
import test from "ava";
import sinon from "sinon";
import changelogFunctions from "./index.js";

/** @type {sinon.SinonSandbox} */
let sandbox = sinon.createSandbox();

const data = {
commit: "a085003d4c8ca284c116668d7217fb747802ed85",
user: "Andarist",
pull: 1613,
repo: "emotion-js/emotion",
};

test.beforeEach((t) => {
sandbox.stub({
getInfo: () => ({
pull: data.pull,
user: data.user,
links: {
user: `[@${data.user}](https://github.com/${data.user})`,
pull: `[#${data.pull}](https://github.com/${data.repo}/pull/${data.pull})`,
commit: `[\`${data.commit.slice(0, 7)}\`](https://github.com/${data.repo}/commit/${data.commit})`,
},
})
}, "getInfo");
sandbox.stub({
getInfoFromPullRequest: () => ({
commit: data.commit,
user: data.user,
links: {
user: `[@${data.user}](https://github.com/${data.user})`,
pull: `[#${data.pull}](https://github.com/${data.repo}/pull/${data.pull})`,
commit: `[\`${data.commit.slice(0, 7)}\`](https://github.com/${data.repo}/commit/${data.commit})`,
},
}),
}, "getInfoFromPullRequest");
});

test.afterEach.always(() => {
sandbox.restore();
});

/**
*
* @param {string} content
* @param {string|undefined} commit
* @returns
*/
const getChangeset = (content, commit) => {
return [
{
...parse(
`---
pkg: "minor"
---

something
${content}
`
),
id: "some-id",
commit,
},
"minor",
{ repo: data.repo },
];
};

[data.commit, "wrongcommit", undefined].forEach((commitFromChangeset) => {
["pr", "pull request", "pull"].forEach((keyword) => {
test(`with commit from changeset of ${commitFromChangeset} override pr with ${keyword} keyword with #`, async (t) => {
t.is(
await changelogFunctions.getReleaseLine(
...getChangeset(
`${keyword}: #${data.pull}`,
commitFromChangeset
)
),
"\n\n📝 [#1613](https://github.com/emotion-js/emotion/pull/1613) [`a085003`](https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85) Thanks [@Andarist](https://github.com/Andarist)!\n\nsomething\n"
);
});

test(`with commit from changeset of ${commitFromChangeset} override pr with pr ${keyword} without #`, async (t) => {
t.is(
await changelogFunctions.getReleaseLine(
...getChangeset(
`pr: ${data.pull}`,
commitFromChangeset
)
),
"\n\n📝 [#1613](https://github.com/emotion-js/emotion/pull/1613) [`a085003`](https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85) Thanks [@Andarist](https://github.com/Andarist)!\n\nsomething\n"
);
});
});

test(`override commit ${commitFromChangeset} with commit keyword`, async (t) => {
t.is(
await changelogFunctions.getReleaseLine(
...getChangeset(`commit: ${data.commit}`, commitFromChangeset)
),
"\n\n📝 [#1613](https://github.com/emotion-js/emotion/pull/1613) [`a085003`](https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85) Thanks [@Andarist](https://github.com/Andarist)!\n\nsomething\n"
);
});
});

["author", "user"].forEach((keyword) => {
test(`override author with ${keyword} keyword with @`, async (t) => {
t.is(
await changelogFunctions.getReleaseLine(
...getChangeset(
`${keyword}: @other`,
data.commit
)
),
"\n\n📝 [#1613](https://github.com/emotion-js/emotion/pull/1613) [`a085003`](https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85) Thanks [@other](https://github.com/other)!\n\nsomething\n"
);
});

test(`override author with ${keyword} keyword without @`, async (t) => {
t.is(
await changelogFunctions.getReleaseLine(
...getChangeset(
`${keyword}: other`,
data.commit
)
),
"\n\n📝 [#1613](https://github.com/emotion-js/emotion/pull/1613) [`a085003`](https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85) Thanks [@other](https://github.com/other)!\n\nsomething\n"
);
});
});

test("with multiple authors", async (t) => {
t.is(
await changelogFunctions.getReleaseLine(
...getChangeset(
["author: @Andarist", "author: @mitchellhamilton"].join("\n"),
data.commit
)
),
`\n\n📝 [#1613](https://github.com/emotion-js/emotion/pull/1613) [\`a085003\`](https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85) Thanks [@Andarist](https://github.com/Andarist), [@mitchellhamilton](https://github.com/mitchellhamilton)!\n\nsomething\n`
);
});
Loading
Loading