Skip to content

Commit 686cacf

Browse files
committed
ci(refactor): splitting the JS code on the notify-user module to make it more readable
1 parent 84cdfd9 commit 686cacf

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

.github/workflows/deploy-chatbot.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ jobs:
6969
id: notify-user
7070
with:
7171
script: |
72-
const script = require('./actions_scripts/notify_user.js')
72+
const environment = `${{ steps.parse-command.outputs.environment }}`;
73+
const project = `${{ steps.parse-command.outputs.project }}`;
74+
const infra = `${{ steps.parse-command.outputs.infra }}`;
7375
74-
const environment = `${{ steps.parse-command.outputs.result.environment }}`;
75-
const project = `${{ steps.parse-command.outputs.result.project }}`;
76-
const infra = `${{ steps.parse-command.outputs.result.infra }}`;
76+
const script = require('./actions_scripts/notify_user.js')
7777
7878
return await script({github, context, environment, project, infra})
7979
@@ -149,14 +149,11 @@ jobs:
149149
with:
150150
script: |
151151
const workflowLocalRun = `${{ github.event.act }}` === 'true';
152-
console.log("Is local run: " + workflowLocalRun);
153152
154-
const commentOnPr = JSON.parse(`${{ steps.notify-user.outputs.result }}`);
153+
const commentOnPr = ${{ steps.notify-user.outputs.result }};
155154
const commentId = commentOnPr.comment_id;
156-
console.log(`Updating comment with id ${commentId} for workflow status`);
157155
158156
const workflowDetails = ${{ steps.trigger-deployment-workflow.outputs.result }};
159-
console.log(`Trigger deployment status: ${JSON.stringify(workflowDetails, null, 2)}`);
160157
161158
const owner = `${{ github.event.repository.owner.login }}`;
162159
const project = `${{ steps.parse-command.outputs.project }}`;

actions_scripts/notify_user.js

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,61 @@
11
module.exports = async ({github, context, environment, project, infra}) => {
2-
const localRun = context.payload.act;
3-
const isLocalRun = (localRun !== undefined) ? localRun : false;
2+
const isLocalRun = ciLocalRun(context);
43

5-
const actor = context.actor;
6-
const username = (actor !== undefined && actor !== "") ? actor : 'Unknown';
4+
const username = getUsername(context);
5+
const prNumber = context.payload.issue.number;
6+
const message = generatePrCommentMsg(username, environment, project, infra);
77

8-
const prNumber = context.payload.issue.number;
9-
10-
let message = `🚀 Deployment action request received from user: ${username}\n`;
11-
if (project) {
12-
message += `- Project: \`${project}\`\n`;
13-
}
14-
if (environment) {
15-
message += `- Environment: \`${environment}\`\n`;
16-
}
17-
if (infra) {
18-
message += `- Infrastructure: \`${infra}\`\n`;
8+
if (isLocalRun) {
9+
logMessageOnLocalEnv(message);
10+
return { comment_id: 10, message: message } // arbitraty mocked comment number;
11+
} else {
12+
try {
13+
const comment = createPrComment(github, context, prNumber, message);
14+
return { comment_id: comment.data.id, message: message };
15+
} catch (ex) {
16+
console.log("Failed to POST the comment on the PR to notify the user due to =[> " + ex + "]");
17+
return { comment_id: null };
1918
}
19+
}
20+
}
2021

21-
if (isLocalRun) {
22-
console.log(`Action is being runned locally by 'ACT'.
23-
Skipping the REST request to post a message for notify the user on PR, but output would have been:
24-
${message}`);
25-
return { comment_id: 10, message: message } // arbitraty mocked comment number;
26-
} else {
27-
let comment = {};
28-
try {
29-
comment = await github.rest.issues.createComment({
30-
owner: context.repo.owner,
31-
repo: context.repo.repo,
32-
issue_number: prNumber,
33-
body: message,
34-
});
35-
return { comment_id: comment.data.id, message: message };
36-
} catch (ex) {
37-
console.log("Failed to POST the comment on the PR to notify the user due to =[> " + ex + "]");
38-
return { comment_id: null };
39-
}
40-
}
22+
function ciLocalRun(context) {
23+
const localRun = context.payload.act;
24+
return(localRun !== undefined) ? localRun : false;
25+
}
26+
27+
function getUsername(context) {
28+
const actor = context.actor;
29+
return (actor !== undefined && actor !== "") ? actor : 'Unknown';
30+
// TODO: throw ex when undefined or non valid actor
31+
}
32+
33+
function generatePrCommentMsg(username, environment, project, infra) {
34+
let message = `🚀 Deployment action request received from user: ${username}\n`;
35+
if (project) {
36+
message += `- Project: \`${project}\`\n`;
37+
}
38+
if (environment) {
39+
message += `- Environment: \`${environment}\`\n`;
40+
}
41+
if (infra) {
42+
message += `- Infrastructure: \`${infra}\`\n`;
43+
}
44+
45+
return message;
46+
}
47+
48+
async function createPrComment(github, context, prNumber, message) {
49+
return await github.rest.issues.createComment({
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
issue_number: prNumber,
53+
body: message,
54+
});
55+
}
56+
57+
function logMessageOnLocalEnv(message) {
58+
console.log(`Action is being runned locally by 'ACT'.
59+
Skipping the REST request to post a message for notify the user on PR, but output would have been:
60+
${message}`);
4161
}

0 commit comments

Comments
 (0)