Skip to content

Commit

Permalink
Allow replacing the PR/commit/author shown in the changlog line by wr…
Browse files Browse the repository at this point in the history
…iting `pr: some-pr-number` and etc. (#535)

* Allow replacing the PR/commit/author shown in the changlog line by writing `pr: some-pr-number` and etc.

* manypkg fix

* Update changeset

* Fix an error message

* Address comments

* Address comments

* Update patterns
  • Loading branch information
emmatown committed Mar 8, 2021
1 parent 06a41aa commit 91d1ef2
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-hotels-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/changelog-github": minor
---

Allow replacing the PR/commit/author shown in the changlog line by writing `pr: some-pr-number` and similarly for `commit` and `author` in the changlog summary(not the frontmatter).
5 changes: 5 additions & 0 deletions .changeset/tidy-tools-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/get-github-info": minor
---

Added `getInfoFromPullRequest`
3 changes: 3 additions & 0 deletions packages/changelog-github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"@changesets/get-github-info": "^0.4.5",
"@changesets/types": "^3.0.0",
"dotenv": "^8.1.0"
},
"devDependencies": {
"@changesets/parse": "*"
}
}
119 changes: 119 additions & 0 deletions packages/changelog-github/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import changelogFunctions from "./index";
import parse from "@changesets/parse";

const getReleaseLine = changelogFunctions.getReleaseLine;

jest.mock(
"@changesets/get-github-info",
(): typeof import("@changesets/get-github-info") => {
// this is duplicated because jest.mock reordering things
const data = {
commit: "a085003",
user: "Andarist",
pull: 1613,
repo: "emotion-js/emotion"
};
const 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})`
};
return {
async getInfo({ commit, repo }) {
expect(commit).toBe(data.commit);
expect(repo).toBe(data.repo);
return {
pull: data.pull,
user: data.user,
links
};
},
async getInfoFromPullRequest({ pull, repo }) {
expect(pull).toBe(data.pull);
expect(repo).toBe(data.repo);
return {
commit: data.commit,
user: data.user,
links
};
}
};
}
);

const getChangeset = (content: string, commit: string | undefined) => {
return [
{
...parse(
`---
pkg: "minor"
---
something
${content}
`
),
id: "some-id",
commit
},
"minor",
{ repo: data.repo }
] as const;
};

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

describe.each([data.commit, "wrongcommit", undefined])(
"with commit from changeset of %s",
commitFromChangeset => {
describe.each(["pr", "pull request", "pull"])(
"override pr with %s keyword",
keyword => {
test.each(["with #", "without #"] as const)("%s", async kind => {
expect(
await getReleaseLine(
...getChangeset(
`${keyword}: ${kind === "with #" ? "#" : ""}${data.pull}`,
commitFromChangeset
)
)
).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`
);
});
}
);
test("override commit with commit keyword", async () => {
expect(
await getReleaseLine(
...getChangeset(`commit: ${data.commit}`, commitFromChangeset)
)
).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`
);
});
}
);

describe.each(["author", "user"])(
"override author with %s keyword",
keyword => {
test.each(["with @", "without @"] as const)("%s", async kind => {
expect(
await getReleaseLine(
...getChangeset(
`${keyword}: ${kind === "with @" ? "@" : ""}other`,
data.commit
)
)
).toEqual(
`\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)! - something\n`
);
});
}
);
80 changes: 65 additions & 15 deletions packages/changelog-github/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChangelogFunctions } from "@changesets/types";
// @ts-ignore
import { config } from "dotenv";
import { getInfo } from "@changesets/get-github-info";
import { getInfo, getInfoFromPullRequest } from "@changesets/get-github-info";

config();

Expand Down Expand Up @@ -46,23 +46,73 @@ const changelogFunctions: ChangelogFunctions = {
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]'
);
}
const [firstLine, ...futureLines] = changeset.summary

let prFromSummary: number | undefined;
let commitFromSummary: string | undefined;
let userFromSummary: string | undefined;

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]+)/im, (_, user) => {
userFromSummary = user;
return "";
})
.trim();

const [firstLine, ...futureLines] = replacedChangelog
.split("\n")
.map(l => l.trimRight());

if (changeset.commit) {
let { links } = await getInfo({
repo: options.repo,
commit: changeset.commit
});
return `\n\n- ${links.commit}${
links.pull === null ? "" : ` ${links.pull}`
}${
links.user === null ? "" : ` Thanks ${links.user}!`
} - ${firstLine}\n${futureLines.map(l => ` ${l}`).join("\n")}`;
} else {
return `\n\n- ${firstLine}\n${futureLines.map(l => ` ${l}`).join("\n")}`;
}
const links = await (async () => {
if (prFromSummary !== undefined) {
let { links } = await getInfoFromPullRequest({
repo: options.repo,
pull: prFromSummary
});
if (commitFromSummary) {
links = {
...links,
commit: `[\`${commitFromSummary}\`](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 user = userFromSummary
? `[@${userFromSummary}](https://github.com/${userFromSummary})`
: links.user;

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

return `\n\n-${prefix ? `${prefix} -` : ""} ${firstLine}\n${futureLines
.map(l => ` ${l}`)
.join("\n")}`;
}
};

Expand Down
68 changes: 67 additions & 1 deletion packages/get-github-info/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getInfo } from ".";
import { getInfo, getInfoFromPullRequest } from ".";
import nock from "nock";
import prettier from "prettier";

Expand Down Expand Up @@ -331,3 +331,69 @@ test("throws error on invalid repo name", () => {
`"Please pass a valid GitHub repository in the form of userOrOrg/repoName to getInfo (it has to match the \\"^[\\\\w.-]+\\\\/[\\\\w.-]+$\\" pattern)"`
);
});

test("associated with multiple PRs with only one merged", async () => {
nock("https://api.github.com", {
reqheaders: {
Authorization: `Token ${process.env.GITHUB_TOKEN}`
}
})
.post(apiPath, ({ query }) => {
expect(prettier.format(query, { parser: "graphql" }))
.toMatchInlineSnapshot(`
"query {
a0: repository(owner: \\"emotion-js\\", name: \\"emotion\\") {
pr__1613: pullRequest(number: 1613) {
url
author {
login
url
}
mergeCommit {
commitUrl
abbreviatedOid
}
}
}
}
"
`);
return true;
})
.reply(
200,
JSON.stringify({
data: {
a0: {
pr__1613: {
url: "https://github.com/emotion-js/emotion/pull/1613",
author: {
login: "Andarist",
url: "https://github.com/Andarist"
},
mergeCommit: {
commitUrl:
"https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85",
abbreviatedOid: "a085003"
}
}
}
}
})
);
let result = await getInfoFromPullRequest({
pull: 1613,
repo: "emotion-js/emotion"
});
expect(result).toMatchInlineSnapshot(`
Object {
"commit": "a085003",
"links": Object {
"commit": "[\`a085003\`](https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85)",
"pull": "[#1613](https://github.com/emotion-js/emotion/pull/1613)",
"user": "[@Andarist](https://github.com/Andarist)",
},
"user": "Andarist",
}
`);
});

0 comments on commit 91d1ef2

Please sign in to comment.