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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
123 changes: 123 additions & 0 deletions plugins/changesets-changelog-github/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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) {
const shortCommitId = commitFromSummary.slice(0, 7);
links = {
...links,
commit: `[\`${shortCommitId}\`](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 `\n\n📝 ${prefix ? `${prefix}` : ""}\n\n${changelogLines.join("\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.4.7"
},
"devDependencies": {
"@changesets/parse": "^0.4.1",
"ava": "^6.2.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" }
}
}
143 changes: 143 additions & 0 deletions plugins/changesets-changelog-github/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import parse from "@changesets/parse";
import test from "ava";
import sinon from "sinon";
import changelogFunctions from "./index.js";

/** @type {sinon.SinonSandbox} */
let sandbox;

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

test.beforeEach((t) => {
sandbox = sinon.createSandbox();
const stub = sandbox.stub("@changesets/get-github-info");
stub.getInfo.returns({
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}\`](https://github.com/${data.repo}/commit/${data.commit})`,
},
});

stub.getInfoFromPullRequest.returns({
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}\`](https://github.com/${data.repo}/commit/${data.commit})`,
},
});
});

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/a085003) Thanks [@Andarist](https://github.com/Andarist)! - something\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/a085003) Thanks [@Andarist](https://github.com/Andarist)! - something\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/a085003) Thanks [@Andarist](https://github.com/Andarist)! - something\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/a085003) 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/a085003) 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
)
),
`
- [#1613](https://github.com/emotion-js/emotion/pull/1613) [\`a085003\`](https://github.com/emotion-js/emotion/commit/a085003) Thanks [@Andarist](https://github.com/Andarist), [@mitchellhamilton](https://github.com/mitchellhamilton)!\n\nsomething`);
});
Loading
Loading