-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci(refactor): splitting the JS code on the notify-user module to make…
… it more readable
- Loading branch information
1 parent
84cdfd9
commit 686cacf
Showing
2 changed files
with
60 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} |