Skip to content

Commit 37410a1

Browse files
committed
refacored
1 parent 1c601d1 commit 37410a1

File tree

1 file changed

+72
-50
lines changed

1 file changed

+72
-50
lines changed

.github/workflows/comment-on-release.yml

Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,51 @@ jobs:
1717
with:
1818
fetch-depth: 0
1919

20-
- name: Get previous release tag
21-
id: previous_tag
22-
run: |
23-
# Get all tags sorted by version
24-
CURRENT_TAG="${{ github.event.release.tag_name }}"
25-
PREVIOUS_TAG=$(git tag --sort=-version:refname | grep -A 1 "^${CURRENT_TAG}$" | tail -n 1)
26-
27-
# If there's no previous tag, use the first commit
28-
if [ -z "$PREVIOUS_TAG" ] || [ "$PREVIOUS_TAG" = "$CURRENT_TAG" ]; then
29-
PREVIOUS_TAG=$(git rev-list --max-parents=0 HEAD)
30-
fi
31-
32-
echo "previous_tag=${PREVIOUS_TAG}" >> $GITHUB_OUTPUT
33-
echo "Found previous tag/commit: ${PREVIOUS_TAG}"
20+
- name: Get previous release
21+
id: previous_release
22+
uses: actions/github-script@v7
23+
with:
24+
script: |
25+
const currentTag = '${{ github.event.release.tag_name }}';
26+
27+
// Get all releases
28+
const { data: releases } = await github.rest.repos.listReleases({
29+
owner: context.repo.owner,
30+
repo: context.repo.repo,
31+
per_page: 100
32+
});
33+
34+
// Find current release index
35+
const currentIndex = releases.findIndex(r => r.tag_name === currentTag);
36+
37+
if (currentIndex === -1) {
38+
console.log('Current release not found in list');
39+
return null;
40+
}
41+
42+
// Get previous release (next in the list since they're sorted by date desc)
43+
const previousRelease = releases[currentIndex + 1];
44+
45+
if (!previousRelease) {
46+
console.log('No previous release found, this might be the first release');
47+
return null;
48+
}
49+
50+
console.log(`Found previous release: ${previousRelease.tag_name}`);
51+
return previousRelease.tag_name;
3452
3553
- name: Get merged PRs between releases
3654
id: get_prs
3755
uses: actions/github-script@v7
3856
with:
3957
script: |
4058
const currentTag = '${{ github.event.release.tag_name }}';
41-
const previousTag = '${{ steps.previous_tag.outputs.previous_tag }}';
59+
const previousTag = ${{ steps.previous_release.outputs.result }};
60+
61+
if (!previousTag) {
62+
console.log('No previous release found, skipping');
63+
return [];
64+
}
4265
4366
console.log(`Finding PRs between ${previousTag} and ${currentTag}`);
4467
@@ -53,51 +76,30 @@ jobs:
5376
const commits = comparison.data.commits;
5477
console.log(`Found ${commits.length} commits`);
5578
56-
// Extract PR numbers from commit messages
79+
// Get PRs associated with each commit using GitHub API
5780
const prNumbers = new Set();
58-
const prPattern = /#(\d+)/g;
5981
6082
for (const commit of commits) {
61-
const message = commit.commit.message;
62-
const matches = message.matchAll(prPattern);
63-
64-
for (const match of matches) {
65-
const prNumber = parseInt(match[1]);
66-
prNumbers.add(prNumber);
67-
}
68-
69-
// Also check if the commit is associated with a PR
70-
if (commit.commit.message.includes('Merge pull request #')) {
71-
const match = commit.commit.message.match(/Merge pull request #(\d+)/);
72-
if (match) {
73-
prNumbers.add(parseInt(match[1]));
74-
}
75-
}
76-
}
77-
78-
// Verify these are actually PRs (not issues)
79-
const validPRs = [];
80-
81-
for (const prNumber of prNumbers) {
8283
try {
83-
const { data: pr } = await github.rest.pulls.get({
84+
const { data: prs } = await github.rest.repos.listPullRequestsAssociatedWithCommit({
8485
owner: context.repo.owner,
8586
repo: context.repo.repo,
86-
pull_number: prNumber
87+
commit_sha: commit.sha
8788
});
88-
89-
// Only include merged PRs
90-
if (pr.merged_at) {
91-
validPRs.push(prNumber);
92-
console.log(`Found valid merged PR: #${prNumber}`);
89+
90+
for (const pr of prs) {
91+
if (pr.merged_at) {
92+
prNumbers.add(pr.number);
93+
console.log(`Found merged PR: #${pr.number}`);
94+
}
9395
}
9496
} catch (error) {
95-
console.log(`#${prNumber} is not a valid PR or was not found`);
97+
console.log(`Failed to get PRs for commit ${commit.sha}: ${error.message}`);
9698
}
9799
}
98100
99-
console.log(`Found ${validPRs.length} valid merged PRs`);
100-
return validPRs;
101+
console.log(`Found ${prNumbers.size} merged PRs`);
102+
return Array.from(prNumbers);
101103
102104
- name: Comment on PRs
103105
uses: actions/github-script@v7
@@ -107,20 +109,40 @@ jobs:
107109
const releaseTag = '${{ github.event.release.tag_name }}';
108110
const releaseUrl = '${{ github.event.release.html_url }}';
109111
110-
const comment = `🎉 This pull request is included in [${releaseTag}](${releaseUrl})!`;
112+
const comment = `This pull request is included in [${releaseTag}](${releaseUrl})`;
113+
114+
let commentedCount = 0;
111115
112116
for (const prNumber of prNumbers) {
113117
try {
118+
// Check if we've already commented on this PR for this release
119+
const { data: comments } = await github.rest.issues.listComments({
120+
owner: context.repo.owner,
121+
repo: context.repo.repo,
122+
issue_number: prNumber,
123+
per_page: 100
124+
});
125+
126+
const alreadyCommented = comments.some(c =>
127+
c.user.type === 'Bot' && c.body.includes(releaseTag)
128+
);
129+
130+
if (alreadyCommented) {
131+
console.log(`Skipping PR #${prNumber} - already commented for ${releaseTag}`);
132+
continue;
133+
}
134+
114135
await github.rest.issues.createComment({
115136
owner: context.repo.owner,
116137
repo: context.repo.repo,
117138
issue_number: prNumber,
118139
body: comment
119140
});
141+
commentedCount++;
120142
console.log(`Successfully commented on PR #${prNumber}`);
121143
} catch (error) {
122144
console.error(`Failed to comment on PR #${prNumber}:`, error.message);
123145
}
124146
}
125147
126-
console.log(`Commented on ${prNumbers.length} PRs`);
148+
console.log(`Commented on ${commentedCount} of ${prNumbers.length} PRs`);

0 commit comments

Comments
 (0)