Skip to content

Commit

Permalink
Development branch (#484)
Browse files Browse the repository at this point in the history
* Fixes

* Update readme
* Use split/join to avoid weird replace behavior
* Apply nicer typing to issue aggregation from #460

* More tests for replacements

* node 20
  • Loading branch information
apexskier authored Dec 26, 2023
1 parent 7550e2e commit 0741008
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
- uses: actions/checkout@v2

- name: Setup Node.js environment
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: "15"
node-version: "20"

- name: Install dependencies
run: yarn
Expand Down
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
## Release process

Create a new tag, following semver and prefixed with `v`. On push, workflows will create associated tags and releases. After the release is created, edit it through GitHub and make sure "Publish this Action to the GitHub Marketplace" is checked.

## Notes

I use `.split(search).join(replace)` instead of `.replace(search, replace)` to avoid unintentional behavior to to `.replace`'s [replacement string semantics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement).
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ A GitHub personal access token with repo scope, such as [`secrets.GITHUB_TOKEN`]

**comment-template** (optional)

Override the comment posted on Issues and PRs. Set to the empty string to disable commenting. The string `{release_link}` will be replaced with a markdown link to the release. `{release_name}` will be replaced with the release's name. `{release_tag}` will be replaced with the release's tag.
Override the comment posted on Issues and PRs. Set to the empty string to disable commenting. Several variables strings will be automatically replaced:

- `{release_link}` - a markdown link to the release
- `{release_name}` - the release's name
- `{release_tag}` - the release's tag

**label-template** (optional)

Add the given label. Multiple labels can be separated by commas. `{release_name}` will be replaced with the release's name. `{release_tag}` will be replaced with the release's tag.
Add the given label. Multiple labels can be separated by commas. Several variable strings will be automatically replaced:

- `{release_name}` - the release's name
- `{release_tag}` - the release's tag

**skip-label** (optional)

Expand Down
22 changes: 12 additions & 10 deletions dst/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -160,43 +160,43 @@ Object {
"calls": Array [
Array [
Object {
"body": "Included in release [current_tag_name](http://current_release)",
"body": "Included in release [current_tag_name](http://current_release). Replacements: current_tag_name, current_tag_name.",
"issue_number": 3,
},
],
Array [
Object {
"body": "Included in release [current_tag_name](http://current_release)",
"body": "Included in release [current_tag_name](http://current_release). Replacements: current_tag_name, current_tag_name.",
"issue_number": 123,
},
],
Array [
Object {
"body": "Included in release [current_tag_name](http://current_release)",
"body": "Included in release [current_tag_name](http://current_release). Replacements: current_tag_name, current_tag_name.",
"issue_number": 7,
},
],
Array [
Object {
"body": "Included in release [current_tag_name](http://current_release)",
"body": "Included in release [current_tag_name](http://current_release). Replacements: current_tag_name, current_tag_name.",
"issue_number": 4,
},
],
Array [
Object {
"body": "Included in release [current_tag_name](http://current_release)",
"body": "Included in release [current_tag_name](http://current_release). Replacements: current_tag_name, current_tag_name.",
"issue_number": 5,
},
],
Array [
Object {
"body": "Included in release [current_tag_name](http://current_release)",
"body": "Included in release [current_tag_name](http://current_release). Replacements: current_tag_name, current_tag_name.",
"issue_number": 9,
},
],
Array [
Object {
"body": "Included in release [current_tag_name](http://current_release)",
"body": "Included in release [current_tag_name](http://current_release). Replacements: current_tag_name, current_tag_name.",
"issue_number": 2,
},
],
Expand Down
3 changes: 2 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ describe("tests", () => {
fail(`Unexpected input key ${key}`);
});

commentTempate = "Included in release {release_link}";
commentTempate =
"Included in release {release_link}. Replacements: {release_name}, {release_tag}.";
labelTemplate = null;
simpleMockOctokit = {
rest: {
Expand Down
29 changes: 15 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,25 @@ const releaseTagTemplateRegex = /{release_tag}/g;

const comment = commentTemplate
.trim()
.replace(
releaseLinkTemplateRegex,
`[${releaseLabel}](${currentRelease.html_url})`,
)
.replace(releaseNameTemplateRegex, releaseLabel)
.replace(releaseTagTemplateRegex, currentRelease.tag_name);
.split(releaseLinkTemplateRegex)
.join(`[${releaseLabel}](${currentRelease.html_url})`)
.split(releaseNameTemplateRegex)
.join(releaseLabel)
.split(releaseTagTemplateRegex)
.join(currentRelease.tag_name);
const parseLabels = (rawInput: string | null) =>
rawInput
?.replace(releaseNameTemplateRegex, releaseLabel)
?.replace(releaseTagTemplateRegex, currentRelease.tag_name)
?.split(releaseNameTemplateRegex)
.join(releaseLabel)
?.split(releaseTagTemplateRegex)
.join(currentRelease.tag_name)
?.split(",")
?.map((l) => l.trim())
.filter((l) => l);
const labels = parseLabels(labelTemplate);
const skipLabels = parseLabels(skipLabelTemplate);

const linkedIssuesPrs = new Set<string>();
const linkedIssuesPrs = new Set<number>();

await Promise.all(
commits.map((commit) =>
Expand Down Expand Up @@ -178,7 +180,7 @@ const releaseTagTemplateRegex = /{release_tag}/g;
].join(" ");
for (const match of html.matchAll(closesMatcher)) {
const [, num] = match;
linkedIssuesPrs.add(num);
linkedIssuesPrs.add(parseInt(num, 10));
}

if (response.resource.associatedPullRequests.pageInfo.hasNextPage) {
Expand All @@ -202,7 +204,7 @@ const releaseTagTemplateRegex = /{release_tag}/g;
) {
continue;
}
linkedIssuesPrs.add(`${associatedPR.node.number}`);
linkedIssuesPrs.add(associatedPR.node.number);
// these are sorted by creation date in ascending order. The latest event for a given issue/PR is all we need
// ignore links that aren't part of this repo
const links = associatedPR.node.timelineItems.nodes
Expand All @@ -213,7 +215,7 @@ const releaseTagTemplateRegex = /{release_tag}/g;
continue;
}
if (link.__typename == "ConnectedEvent") {
linkedIssuesPrs.add(`${link.subject.number}`);
linkedIssuesPrs.add(link.subject.number);
}
seen.add(link.subject.number);
}
Expand All @@ -223,8 +225,7 @@ const releaseTagTemplateRegex = /{release_tag}/g;
);

const requests: Array<Promise<unknown>> = [];
for (const issueStr of linkedIssuesPrs) {
const issueNumber = parseInt(issueStr);
for (const issueNumber of linkedIssuesPrs) {
const baseRequest = {
...github.context.repo,
issue_number: issueNumber,
Expand Down

0 comments on commit 0741008

Please sign in to comment.