From 221c979a235d0c56639d9088d7eb25deabc63740 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 22 Mar 2022 14:49:05 +0100 Subject: [PATCH] workflows: add new changeset feedback automation Signed-off-by: Patrik Oldsberg --- .../workflows/automate_changeset_feedback.yml | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 .github/workflows/automate_changeset_feedback.yml diff --git a/.github/workflows/automate_changeset_feedback.yml b/.github/workflows/automate_changeset_feedback.yml new file mode 100644 index 0000000000..492ef976dd --- /dev/null +++ b/.github/workflows/automate_changeset_feedback.yml @@ -0,0 +1,82 @@ +name: Automate Changeset Feedback +on: + pull_request: + types: + - opened + - synchronize + +permissions: + issues: write + +jobs: + feedback: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + + - name: fetch base + run: git fetch origin ${{ github.base_ref }} + + - name: Generate Feedback + id: generate-feedback + run: | + wget -O generate.js https://raw.githubusercontent.com/backstage/backstage/master/scripts/generate-changeset-feedback.js 1>&2 + node generate.js origin/${{ github.base_ref }} > feedback.txt + + - name: Post Feedback + uses: actions/github-script@v5 + env: + ISSUE_NUMBER: ${{ github.event.pull_request.number }} + with: + script: | + const owner = "backstage"; + const repo = "backstage"; + const marker = ""; + const feedback = require('fs').readFileSync('feedback.txt', 'utf8'); + const issue_number = Number(process.env.ISSUE_NUMBER); + const body = feedback.trim() ? feedback + marker : undefined + + const existingComments = await github.paginate(github.rest.issues.listComments, { + owner, + repo, + issue_number, + }); + + const existingComment = existingComments.find((c) => + c.user.login === "github-actions[bot]" && + c.body.includes(marker) + ); + + if (existingComment) { + if (body) { + if (existingComment.body !== body) { + console.log(`updating existing comment in #${issue_number}`); + await github.rest.issues.updateComment({ + repo, + owner, + comment_id: existingComment.id, + body, + }); + } else { + console.log(`skipped update of identical comment in #${issue_number}`); + } + } else { + console.log(`removing comment from #${issue_number}`); + await github.rest.issues.deleteComment({ + repo, + owner, + comment_id: existingComment.id, + body, + }); + } + } else if (body) { + console.log(`creating comment for #${issue_number}`); + await github.rest.issues.createComment({ + repo, + owner, + issue_number, + body, + }); + }