diff --git a/.github/scripts/pr_env.js b/.github/scripts/pr_env.js new file mode 100644 index 0000000000..260216b37f --- /dev/null +++ b/.github/scripts/pr_env.js @@ -0,0 +1,98 @@ +module.exports = (github, context) => { + const MARKER = ""; + const owner = context.repo.owner; + const repo = context.repo.repo; + const pullRequestNumber = context.issue.number; + + console.log( + "Excuting 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) { + 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(domain) { + const comment = await findCommentByContent(new RegExp(MARKER)); + if (comment) { + console.log("Comment already exists:", comment.html_url); + return; + } + const body = createCommentBody(domain); + await addComment(body); + }, + + async onPrEnvClosed() { + const comment = await findCommentByContent(new RegExp(MARKER)); + if (comment) { + await removeComment(comment.id); + } + }, + }; +}; diff --git a/.github/workflows/pr_env.yaml b/.github/workflows/pr_env.yaml index 086ef21295..b4b8efa1ca 100644 --- a/.github/workflows/pr_env.yaml +++ b/.github/workflows/pr_env.yaml @@ -164,3 +164,16 @@ 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: Comment on the PR + uses: actions/github-script@v4 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const { onPrEnvCreated } = require('.github/scripts/pr_env.js')(github, context); + + onPrEnvCreated("prenv.trento.suse.com").catch(console.error); diff --git a/.github/workflows/pr_env_close.yaml b/.github/workflows/pr_env_close.yaml index 2f0dccd71f..5517bd1d49 100644 --- a/.github/workflows/pr_env_close.yaml +++ b/.github/workflows/pr_env_close.yaml @@ -1,4 +1,4 @@ -name: Pull request environment cleanup +name: Pull request environment cleanup on: pull_request: @@ -6,7 +6,7 @@ on: - 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 }}" @@ -20,6 +20,21 @@ jobs: - id: check run: echo "create_env=${{ contains(github.event.pull_request.labels.*.name, env.PR_ENV_LABEL) }}" >> "$GITHUB_OUTPUT" + comment-pr: + 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@v4 + 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 @@ -110,4 +125,4 @@ jobs: rabbitmq_password='trento' \ rabbitmq_username='${{ env.PR_NUMBER }}rabbitusr' \ remove_wanda_container_image='false' \ - install_method='docker'" \ No newline at end of file + install_method='docker'"