Skip to content

Commit c2c50c7

Browse files
committed
Fix PR notification workflow
1 parent efd19f1 commit c2c50c7

2 files changed

Lines changed: 69 additions & 8 deletions

File tree

.github/workflows/release.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ on:
5656
- 'ubuntu-latest'
5757
- 'windows-latest'
5858
default: 'ubuntu-latest'
59-
notify-prs:
60-
type: boolean
61-
description: Notify Included PRs
59+
notify-pr-months:
60+
type: number
61+
description: Notify PRs (Months)
6262
required: true
63-
default: true
63+
default: 2
6464
sync-desc-modrinth:
6565
type: boolean
6666
description: Sync Modrinth Description
@@ -205,8 +205,10 @@ jobs:
205205

206206
- name: Notify PRs
207207
uses: actions/github-script@v9
208-
if: inputs.notify-prs && steps.github.outcome == 'success'
208+
if: inputs.notify-pr-months > 0 && steps.github.outcome == 'success'
209209
continue-on-error: true
210+
env:
211+
SCAN_MONTHS: ${{ github.event.inputs.notify-pr-months }}
210212
with:
211213
github-token: ${{ secrets.GITHUB_TOKEN }}
212214
script: |

.github/workflows/scripts/src/notify-prs.js

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @typedef {import('@octokit/rest').Octokit} Octokit
33
* @typedef {import('@actions/github').context} Context
44
* @typedef {import('@actions/core')} Core
5+
* @typedef {import('@octokit/openapi-types').components['schemas']['release']} Release
56
* @typedef {import('@octokit/openapi-types').components['schemas']['tag']} Tag
67
* @typedef {import('@octokit/openapi-types').components['schemas']['pull-request']} PullRequest
78
* @typedef {import('@octokit/openapi-types').components['schemas']['commit-comparison']} CommitComparison
@@ -34,6 +35,18 @@ module.exports = async ({github, context, core}) => {
3435
// Functions
3536
//
3637

38+
/**
39+
* @returns {Promise<Release[]>}
40+
*/
41+
async function getAllReleases() {
42+
// https://docs.github.com/en/rest/releases/releases#list-releases
43+
return await github.paginate(github.rest.repos.listReleases, {
44+
owner,
45+
repo,
46+
per_page: 100,
47+
});
48+
}
49+
3750
/**
3851
* @returns {Promise<Tag[]>}
3952
*/
@@ -80,7 +93,7 @@ module.exports = async ({github, context, core}) => {
8093
* @returns {Set<number>}
8194
*/
8295
function getPullNumbers(comparison) {
83-
const mergeRegex = /Merge pull request #(\d+) from/i;
96+
const mergeRegex = /Merge pull request #(\d+)/i;
8497
/** @type {Set<number>} */
8598
const pullNumbers = new Set();
8699

@@ -152,9 +165,49 @@ module.exports = async ({github, context, core}) => {
152165
// Control flow
153166
//
154167

168+
const scanMonths = Number(process.env.SCAN_MONTHS ?? `0`)
169+
if (!Number.isInteger(scanMonths)) {
170+
console.log(`Env SCAN_MONTHS (${process.env.SCAN_MONTHS}) is not an integer: aborting`);
171+
return;
172+
} else if (scanMonths < 1) {
173+
console.log(`Env SCAN_MONTHS (${scanMonths}) is less than one: aborting)`)
174+
return;
175+
}
176+
177+
const allReleases = await getAllReleases();
178+
console.log(`Found ${allReleases.length} total releases`);
179+
if (allReleases.length > 0) {
180+
console.log(`Newest release tag is ${allReleases[0].tag_name}`);
181+
}
182+
155183
const allTags = await getAllTags();
156184
console.log(`Found ${allTags.length} total tags`);
157185

186+
const cutoff = new Date(new Date().setMonth(new Date().getMonth() - scanMonths));
187+
188+
const recentReleases = new Map(allReleases
189+
.filter((release) => release.published_at && new Date(release.published_at).getTime() >= cutoff)
190+
.map((release) => [release.tag_name, release])
191+
);
192+
console.log(`Filtered ${recentReleases.size} recent releases from the last ${scanMonths} months`);
193+
if (recentReleases.length > 0) {
194+
console.log(`Oldest recent release tag is ${recentReleases[recentReleases.length - 1].tag_name}`);
195+
}
196+
197+
let recentTags = allTags
198+
.filter((tag) => recentReleases.has(tag.name))
199+
.sort((a, b) => {
200+
// newest first
201+
const releaseA = recentReleases.get(a.name);
202+
const releaseB = recentReleases.get(b.name);
203+
return (new Date(releaseB.published_at).getTime() - new Date(releaseA.published_at).getTime());
204+
});
205+
console.log(`Filtered ${recentTags.length} recent tags`);
206+
if (recentTags.length > 0) {
207+
console.log(`Newest recent tag is ${recentTags[0].name}`);
208+
console.log(`Oldest recent tag is ${recentTags[recentTags.length - 1].name}`);
209+
}
210+
158211
const releaseTagNames = [];
159212
if (context.payload && context.payload.release) {
160213
// release context; use the release tag
@@ -163,13 +216,19 @@ module.exports = async ({github, context, core}) => {
163216
} else {
164217
// other context; iterate all tags with matching SHA
165218
console.log(`No release context; searching tags`)
166-
for (const tag of allTags) {
219+
for (const tag of recentTags) {
167220
if (tag.commit.sha === process.env.GITHUB_SHA) {
168221
console.log(`Found tag with matching SHA: ${tag.name}`);
169222
releaseTagNames.push(tag.name);
170223
}
171224
}
172225
}
226+
recentTags = recentTags.filter((tag) => !releaseTagNames.includes(tag.name));
227+
console.log(`Refined ${recentTags.length} recent tags`);
228+
if (recentTags.length > 0) {
229+
console.log(`Newest recent tag is ${recentTags[0].name}`);
230+
console.log(`Oldest recent tag is ${recentTags[recentTags.length - 1].name}`);
231+
}
173232

174233
if (releaseTagNames.length === 0) {
175234
console.error(`Failed to find a release tag name`);
@@ -188,7 +247,7 @@ module.exports = async ({github, context, core}) => {
188247
/** @type {Set<number>} */
189248
let pullNumbers;
190249

191-
const previousTag = await getPreviousTag(allTags, loaderName, releaseTagName);
250+
const previousTag = await getPreviousTag(recentTags, loaderName, releaseTagName);
192251
if (previousTag) {
193252
console.log(`Found previous tag: ${previousTag.name}`);
194253

0 commit comments

Comments
 (0)