Skip to content

Commit 5f07b53

Browse files
duckdumEduardo de Paula
and
Eduardo de Paula
authored
Fixes + Review feedback + Tests (#82)
* Add tests and address feedback * Address feedback review * just testing (#81) Co-authored-by: Eduardo de Paula <[email protected]> --------- Co-authored-by: Eduardo de Paula <[email protected]>
1 parent 013b3c4 commit 5f07b53

18 files changed

+4558
-39000
lines changed

.github/workflows/release-notification.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ jobs:
1717
- name: Build the project
1818
run: npm run build
1919

20+
- name: Run tests
21+
run: npm run test
22+
2023
- name: Send Slack notification
2124
uses: ./
2225
with:
2326
slack-bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
2427
slack-channel: ${{ secrets.SLACK_CHANNEL }}
25-
github-token: ${{ secrets.GITHUB_TOKEN }}
28+
github-token: ${{ secrets.GITHUB_TOKEN }}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
uses: actions/checkout@v4
6464
6565
- name: Run Slack notification action
66-
uses: duckdum/[email protected]
66+
uses: thunkable/[email protected]
6767
with:
6868
slack-token: ${{ secrets.SLACK_BOT_TOKEN }}
6969
github-token: ${{ secrets.GITHUB_TOKEN }}

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ inputs:
3131
default: 'Pull request <${prUrl}|${prTitle}> was merged by @${mergedBy}'
3232
runs:
3333
using: 'node20'
34-
main: 'dist/index.js'
34+
main: 'dist/main.js'
3535
branding:
3636
icon: 'bell'
3737
color: 'blue'

dist/handlePRClosed.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
2222
__setModuleDefault(result, mod);
2323
return result;
2424
};
25-
var __importDefault = (this && this.__importDefault) || function (mod) {
26-
return (mod && mod.__esModule) ? mod : { "default": mod };
27-
};
2825
Object.defineProperty(exports, "__esModule", { value: true });
2926
exports.handlePRClosed = void 0;
3027
const github = __importStar(require("@actions/github"));
31-
const axios_1 = __importDefault(require("axios"));
3228
async function handlePRClosed(slackToken, slackChannel, closeMessageTemplate) {
3329
const pr = github.context.payload.pull_request;
3430
if (!pr) {
@@ -51,15 +47,21 @@ async function handlePRClosed(slackToken, slackChannel, closeMessageTemplate) {
5147
.replace('${prUrl}', prUrl)
5248
.replace('${prTitle}', prTitle)
5349
.replace('${mergedBy}', mergedBy);
54-
await axios_1.default.post('https://slack.com/api/chat.postMessage', {
55-
channel: slackChannel,
56-
text: closeMessage,
57-
thread_ts: messageTs
58-
}, {
50+
const response = await fetch('https://slack.com/api/chat.postMessage', {
51+
method: 'POST',
5952
headers: {
60-
'Authorization': `Bearer ${slackToken}`,
61-
'Content-Type': 'application/json'
62-
}
53+
Authorization: `Bearer ${slackToken}`,
54+
'Content-Type': 'application/json',
55+
},
56+
body: JSON.stringify({
57+
channel: slackChannel,
58+
text: closeMessage,
59+
thread_ts: messageTs,
60+
}),
6361
});
62+
if (!response.ok) {
63+
const errorData = await response.json();
64+
throw new Error(`Slack API request failed: ${response.status} ${response.statusText} - ${JSON.stringify(errorData)}`);
65+
}
6466
}
6567
exports.handlePRClosed = handlePRClosed;

dist/handlePROpened.js

+35-29
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
2222
__setModuleDefault(result, mod);
2323
return result;
2424
};
25-
var __importDefault = (this && this.__importDefault) || function (mod) {
26-
return (mod && mod.__esModule) ? mod : { "default": mod };
27-
};
2825
Object.defineProperty(exports, "__esModule", { value: true });
2926
exports.handlePROpened = void 0;
3027
const github = __importStar(require("@actions/github"));
31-
const axios_1 = __importDefault(require("axios"));
3228
async function handlePROpened(slackToken, slackChannel, githubToken, initialMessageTemplate, commitListMessageTemplate, githubToSlackMap) {
3329
const pr = github.context.payload.pull_request;
3430
if (!pr) {
@@ -46,58 +42,68 @@ async function handlePROpened(slackToken, slackChannel, githubToken, initialMess
4642
.replace('${branchName}', branchName)
4743
.replace('${targetBranch}', targetBranch)
4844
.replace(/\\n/g, '\n');
49-
const initialMessageResponse = await axios_1.default.post('https://slack.com/api/chat.postMessage', {
50-
channel: slackChannel,
51-
text: initialMessage
52-
}, {
45+
const initialMessageResponse = await fetch('https://slack.com/api/chat.postMessage', {
46+
method: 'POST',
5347
headers: {
54-
'Authorization': `Bearer ${slackToken}`,
55-
'Content-Type': 'application/json'
56-
}
48+
Authorization: `Bearer ${slackToken}`,
49+
'Content-Type': 'application/json',
50+
},
51+
body: JSON.stringify({
52+
channel: slackChannel,
53+
text: initialMessage,
54+
}),
5755
});
58-
if (!initialMessageResponse.data.ok) {
56+
const initialMessageData = await initialMessageResponse.json();
57+
if (!initialMessageData.ok) {
5958
throw new Error('Failed to send initial Slack message');
6059
}
61-
const messageTs = initialMessageResponse.data.ts;
60+
const messageTs = initialMessageData.ts;
6261
const newPrBody = `Slack message_ts: ${messageTs}\n\n${prBody}`;
6362
const octokit = github.getOctokit(githubToken);
6463
await octokit.rest.pulls.update({
6564
...github.context.repo,
6665
pull_number: prNumber,
67-
body: newPrBody
66+
body: newPrBody,
6867
});
6968
const commitsUrl = pr.commits_url;
70-
const commitsResponse = await axios_1.default.get(commitsUrl, {
69+
const commitsResponse = await fetch(commitsUrl, {
7170
headers: {
72-
'Authorization': `token ${githubToken}`
73-
}
71+
Authorization: `token ${githubToken}`,
72+
},
7473
});
74+
const commitsData = await commitsResponse.json();
7575
const repoUrl = `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}`;
76-
const commitMessages = commitsResponse.data.map((commit) => {
77-
const commitMessage = commit.commit.message;
76+
const commitMessages = commitsData
77+
.map((commit) => {
78+
const commitMessage = commit.commit.message.split('\n')[0]; // Extract only the first line
7879
const commitSha = commit.sha;
7980
const commitUrl = `${repoUrl}/commit/${commitSha}`;
8081
const githubUser = commit.author?.login || commit.commit.author.name;
81-
const slackUserId = githubToSlackMap ? githubToSlackMap[githubUser] : githubUser;
82+
const slackUserId = githubToSlackMap
83+
? githubToSlackMap[githubUser]
84+
: null;
8285
const userDisplay = slackUserId ? `<@${slackUserId}>` : `@${githubUser}`;
8386
return `- <${commitUrl}|${commitMessage}> by ${userDisplay}`;
84-
}).join('\n');
87+
})
88+
.join('\n');
8589
const changelogUrl = `${repoUrl}/compare/${targetBranch}...${branchName}`;
8690
const commitListMessage = commitListMessageTemplate
8791
.replace('${commitListMessage}', commitMessages)
8892
.replace('${changelogUrl}', changelogUrl)
8993
.replace('${branchName}', branchName)
9094
.replace('${targetBranch}', targetBranch)
9195
.replace(/\\n/g, '\n'); // Replace escaped newline characters with actual newline characters
92-
await axios_1.default.post('https://slack.com/api/chat.postMessage', {
93-
channel: slackChannel,
94-
text: commitListMessage,
95-
thread_ts: messageTs
96-
}, {
96+
await fetch('https://slack.com/api/chat.postMessage', {
97+
method: 'POST',
9798
headers: {
98-
'Authorization': `Bearer ${slackToken}`,
99-
'Content-Type': 'application/json'
100-
}
99+
Authorization: `Bearer ${slackToken}`,
100+
'Content-Type': 'application/json',
101+
},
102+
body: JSON.stringify({
103+
channel: slackChannel,
104+
text: commitListMessage,
105+
thread_ts: messageTs,
106+
}),
101107
});
102108
}
103109
exports.handlePROpened = handlePROpened;

dist/handlePRUpdated.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
2222
__setModuleDefault(result, mod);
2323
return result;
2424
};
25-
var __importDefault = (this && this.__importDefault) || function (mod) {
26-
return (mod && mod.__esModule) ? mod : { "default": mod };
27-
};
2825
Object.defineProperty(exports, "__esModule", { value: true });
2926
exports.handlePRUpdated = void 0;
3027
const github = __importStar(require("@actions/github"));
31-
const axios_1 = __importDefault(require("axios"));
3228
async function handlePRUpdated(slackToken, slackChannel, githubToken, updateMessageTemplate) {
3329
const pr = github.context.payload.pull_request;
3430
if (!pr) {
@@ -41,13 +37,18 @@ async function handlePRUpdated(slackToken, slackChannel, githubToken, updateMess
4137
throw new Error('No Slack message_ts found in pull request description');
4238
}
4339
const commitsUrl = pr.commits_url;
44-
const commitsResponse = await axios_1.default.get(commitsUrl, {
40+
const commitsResponse = await fetch(commitsUrl, {
4541
headers: {
46-
'Authorization': `token ${githubToken}`
47-
}
42+
Authorization: `token ${githubToken}`,
43+
},
4844
});
45+
if (!commitsResponse.ok) {
46+
const errorData = await commitsResponse.json();
47+
throw new Error(`GitHub API request failed: ${commitsResponse.status} ${commitsResponse.statusText} - ${JSON.stringify(errorData)}`);
48+
}
49+
const commitsData = await commitsResponse.json();
4950
const repoUrl = `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}`;
50-
const latestCommit = commitsResponse.data[commitsResponse.data.length - 1];
51+
const latestCommit = commitsData[commitsData.length - 1];
5152
if (!latestCommit) {
5253
throw new Error('No commits found');
5354
}
@@ -59,15 +60,21 @@ async function handlePRUpdated(slackToken, slackChannel, githubToken, updateMess
5960
.replace('${commitUrl}', commitUrl)
6061
.replace('${commitMessage}', commitMessage)
6162
.replace('${githubUser}', githubUser);
62-
await axios_1.default.post('https://slack.com/api/chat.postMessage', {
63-
channel: slackChannel,
64-
text: updateMessage,
65-
thread_ts: messageTs
66-
}, {
63+
const slackResponse = await fetch('https://slack.com/api/chat.postMessage', {
64+
method: 'POST',
6765
headers: {
68-
'Authorization': `Bearer ${slackToken}`,
69-
'Content-Type': 'application/json'
70-
}
66+
Authorization: `Bearer ${slackToken}`,
67+
'Content-Type': 'application/json',
68+
},
69+
body: JSON.stringify({
70+
channel: slackChannel,
71+
text: updateMessage,
72+
thread_ts: messageTs,
73+
}),
7174
});
75+
if (!slackResponse.ok) {
76+
const errorData = await slackResponse.json();
77+
throw new Error(`Slack API request failed: ${slackResponse.status} ${slackResponse.statusText} - ${JSON.stringify(errorData)}`);
78+
}
7279
}
7380
exports.handlePRUpdated = handlePRUpdated;

0 commit comments

Comments
 (0)