Skip to content

Commit

Permalink
ci(refactor): splitting the JS code on the notify-user module to make…
Browse files Browse the repository at this point in the history
… it more readable
  • Loading branch information
TheRustifyer committed Dec 27, 2024
1 parent 84cdfd9 commit 686cacf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
13 changes: 5 additions & 8 deletions .github/workflows/deploy-chatbot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ jobs:
id: notify-user
with:
script: |
const script = require('./actions_scripts/notify_user.js')
const environment = `${{ steps.parse-command.outputs.environment }}`;
const project = `${{ steps.parse-command.outputs.project }}`;
const infra = `${{ steps.parse-command.outputs.infra }}`;
const environment = `${{ steps.parse-command.outputs.result.environment }}`;
const project = `${{ steps.parse-command.outputs.result.project }}`;
const infra = `${{ steps.parse-command.outputs.result.infra }}`;
const script = require('./actions_scripts/notify_user.js')
return await script({github, context, environment, project, infra})
Expand Down Expand Up @@ -149,14 +149,11 @@ jobs:
with:
script: |
const workflowLocalRun = `${{ github.event.act }}` === 'true';
console.log("Is local run: " + workflowLocalRun);
const commentOnPr = JSON.parse(`${{ steps.notify-user.outputs.result }}`);
const commentOnPr = ${{ steps.notify-user.outputs.result }};
const commentId = commentOnPr.comment_id;
console.log(`Updating comment with id ${commentId} for workflow status`);
const workflowDetails = ${{ steps.trigger-deployment-workflow.outputs.result }};
console.log(`Trigger deployment status: ${JSON.stringify(workflowDetails, null, 2)}`);
const owner = `${{ github.event.repository.owner.login }}`;
const project = `${{ steps.parse-command.outputs.project }}`;
Expand Down
90 changes: 55 additions & 35 deletions actions_scripts/notify_user.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
module.exports = async ({github, context, environment, project, infra}) => {
const localRun = context.payload.act;
const isLocalRun = (localRun !== undefined) ? localRun : false;
const isLocalRun = ciLocalRun(context);

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

const prNumber = context.payload.issue.number;

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

if (isLocalRun) {
console.log(`Action is being runned locally by 'ACT'.
Skipping the REST request to post a message for notify the user on PR, but output would have been:
${message}`);
return { comment_id: 10, message: message } // arbitraty mocked comment number;
} else {
let comment = {};
try {
comment = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: message,
});
return { comment_id: comment.data.id, message: message };
} catch (ex) {
console.log("Failed to POST the comment on the PR to notify the user due to =[> " + ex + "]");
return { comment_id: null };
}
}
function ciLocalRun(context) {
const localRun = context.payload.act;
return(localRun !== undefined) ? localRun : false;
}

function getUsername(context) {
const actor = context.actor;
return (actor !== undefined && actor !== "") ? actor : 'Unknown';
// TODO: throw ex when undefined or non valid actor
}

function generatePrCommentMsg(username, environment, project, infra) {
let message = `🚀 Deployment action request received from user: ${username}\n`;
if (project) {
message += `- Project: \`${project}\`\n`;
}
if (environment) {
message += `- Environment: \`${environment}\`\n`;
}
if (infra) {
message += `- Infrastructure: \`${infra}\`\n`;
}

return message;
}

async function createPrComment(github, context, prNumber, message) {
return await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: message,
});
}

function logMessageOnLocalEnv(message) {
console.log(`Action is being runned locally by 'ACT'.
Skipping the REST request to post a message for notify the user on PR, but output would have been:
${message}`);
}

0 comments on commit 686cacf

Please sign in to comment.