Skip to content

Commit

Permalink
WIP code to implement update-deploy-status on PR
Browse files Browse the repository at this point in the history
  • Loading branch information
hellais committed Feb 6, 2024
1 parent dfb3267 commit f7d3205
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# For docs on this see:
# * https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
# * https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
# * https://docs.github.com/en/webhooks/webhook-events-and-payloads?actionType=synchronize#pull_request
on:
pull_request:
branches:
- main
- staging
#paths:
# - 'tf/environments/production/**'
types:
- opened
- synchronize # when commits are pushed to the PR
- reopened
- edited # title or body of a pull request was edited, or the base branch of a pull request was changed

env:
tf_actions_working_dir: "./tf/environments/production"
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
Expand Down
60 changes: 60 additions & 0 deletions scripts/ghactions/update-deploy-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const parseCheckboxStatus = (markdown) => {
const lines = markdown.split("\n");
const checkboxStatuses = [];

for (let line of lines) {
// Match lines with checkboxes
const match = line.match(/^\* \[([ x])\] Trigger (.+) deploy/);
if (match) {
const isChecked = match[1] === "x"; // Check if the checkbox is marked as checked
const environment = match[2];
checkboxStatuses.push({ environment, isChecked });
}
}
return checkboxStatuses;
};

module.exports = async ({ github, context, core, newStatus = {} }) => {
const autogenPlaceholder =
"<!-- AUTOGENERATED: DO NOT EDIT THIS LINE AND BELOW -->";
const currentPrBody = github.context.payload.pull_request?.body || "";

const p = currentPrBody.split(autogenPlaceholder);
const existingText = parts[0];
const existingStatusMarkdown = parts[1];
let existingStatus;

if (existingStatusMarkdown) {
existingStatus = parseCheckboxStatus(existingStatus);
}

const actionLines = ["production", "staging", "testing"]
.map((environment) => {
let checkbox = " ";
let suffix = "";
if (newStatus[environment] == true) {
checkbox = "x";
suffix = "✅";
}
return "* [${checkbox}] Trigger ${environment} deploy ${suffix}";
})
.join("\n");

const newBody = `${existingText}
${autogenPlaceholder}
## Deployment Actions 🚀
${actionLines}
`;

const prNumber = context.payload.pull_request.number;
if (prNumber) {
await github.rest.pulls.update({
owner,
repo,
pull_number: prNumber,
body: newBody,
});
}

return existingStatus;
};

0 comments on commit f7d3205

Please sign in to comment.