Skip to content

Commit

Permalink
Add a PR comment when a preview environment is created (#3198)
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza authored Dec 11, 2024
1 parent d423c3c commit 501f79d
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 3 deletions.
97 changes: 97 additions & 0 deletions .github/scripts/pr_env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
module.exports = (github, context) => {
const MARKER = "<!-- pr-env-bot -->";
const owner = context.repo.owner;
const repo = context.repo.repo;
const pullRequestNumber = context.issue.number;

console.log(
`Executing script with
owner: ${owner}
repo: ${repo}
pullRequestNumber: ${pullRequestNumber}`
);

function createCommentBody(domain) {
const url = `${pullRequestNumber}.${domain}`;
return `${MARKER}\n\nThe preview environment for this pull request is ready at [${url}](https://${url}).`;
}

async function addComment(commentBody) {
try {
const response = await github.rest.issues.createComment({
owner,
repo,
issue_number: pullRequestNumber,
body: commentBody,
});

console.log("Comment created successfully:", response.data.html_url);
} catch (error) {
console.error("Error creating comment:", error);
throw error;
}
}

async function removeComment(commentId) {
try {
await github.rest.issues.deleteComment({
owner,
repo,
comment_id: commentId,
});

console.log("Comment deleted successfully:", commentId);
} catch (error) {
console.error("Error deleting comment:", error);
throw error;
}
}

async function findCommentByContent(regex) {
try {
let page = 1;
const per_page = 100;
while (true) {
console.log("Fetching comments page:", page);
const comments = await github.rest.issues.listComments({
owner,
repo,
issue_number: pullRequestNumber,
per_page,
page,
});

const found = comments.data.find((comment) => regex.test(comment.body));
if (found) {
return found;
} else if (comments.data.length < Math.min(per_page, 100)) {
return undefined;
} else {
page++;
}
}
} catch (error) {
console.error("Error finding comment:", error);
throw error;
}
}

return {
async onPrEnvCreated(fqdn) {
const comment = await findCommentByContent(new RegExp(MARKER));
if (comment) {
console.log("Comment already exists:", comment.html_url);
return;
}
const body = createCommentBody(fqdn);
await addComment(body);
},

async onPrEnvClosed() {
const comment = await findCommentByContent(new RegExp(MARKER));
if (comment) {
await removeComment(comment.id);
}
},
};
};
16 changes: 16 additions & 0 deletions .github/workflows/pr_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,19 @@ jobs:
photofinish run multi-tenant -u "https://$TRENTO_PR_ENV_URL/api/v1/collect"
photofinish run hana-scale-up-angi -u "https://$TRENTO_PR_ENV_URL/api/collect"
photofinish run java-system -u "https://$TRENTO_PR_ENV_URL/api/collect"
comment_pr:
name: Comment on the PR with the environment URL
runs-on: ubuntu-20.04
needs: run-photofinish-demo-env
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Comment on the PR
uses: actions/github-script@v7
env:
FQDN: prenv.trento.suse.com
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { onPrEnvCreated } = require('.github/scripts/pr_env.js')(github, context);
onPrEnvCreated(process.env.FQDN).catch(console.error);
21 changes: 18 additions & 3 deletions .github/workflows/pr_env_close.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: Pull request environment cleanup
name: Pull request environment cleanup

on:
pull_request:
types:
- closed

env:
MANTAINERS: "[\"cdimonaco\", \"dottorblaster\", \"nelsonkopliku\", \"arbulu89\", \"jagabomb\", \"emaksy\", \"gagandeepb\", \"balanza\", \"janvhs\"]"
MANTAINERS: '["cdimonaco", "dottorblaster", "nelsonkopliku", "arbulu89", "jagabomb", "emaksy", "gagandeepb", "balanza", "janvhs"]'
PR_ENV_LABEL: env
PR_NUMBER: "${{ github.event.pull_request.number }}"

Expand All @@ -20,6 +20,21 @@ jobs:
- id: check
run: echo "create_env=${{ contains(github.event.pull_request.labels.*.name, env.PR_ENV_LABEL) }}" >> "$GITHUB_OUTPUT"

remove_pr_comment:
name: Comment on the pull request
runs-on: ubuntu-20.04
needs: check_env_creation_privilege
if: needs.check_env_creation_privilege.outputs.create_env == 'true'
steps:
- name: Comment on the pull request
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { onPrEnvClosed } = require('.github/scripts/pr_env.js')(github, context);
onPrEnvClosed().catch(console.error);
delete-pr-image:
needs: check_env_creation_privilege
name: Build and push pull request container image
Expand Down Expand Up @@ -110,4 +125,4 @@ jobs:
rabbitmq_password='trento' \
rabbitmq_username='${{ env.PR_NUMBER }}rabbitusr' \
remove_wanda_container_image='false' \
install_method='docker'"
install_method='docker'"

0 comments on commit 501f79d

Please sign in to comment.