From e83b8633b10b679ce8eace865ee6ef7e5b364ded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Telaty=C5=84ski?= <7t3chguy@gmail.com> Date: Mon, 27 Feb 2023 12:39:09 +0000 Subject: [PATCH] Pull reviews bot automation for Web App Team room (#24679) --- .editorconfig | 2 +- .github/workflows/pending-reviews.yaml | 74 ++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/pending-reviews.yaml diff --git a/.editorconfig b/.editorconfig index d3a02ce7fc..87f4c70ae1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -23,7 +23,7 @@ indent_size = 4 trim_trailing_whitespace = true [*.{yml,yaml}] -indent_size = 2 +indent_size = 4 [package.json] indent_size = 2 diff --git a/.github/workflows/pending-reviews.yaml b/.github/workflows/pending-reviews.yaml new file mode 100644 index 0000000000..82e49f8d3c --- /dev/null +++ b/.github/workflows/pending-reviews.yaml @@ -0,0 +1,74 @@ +name: Pending reviews automation +on: + # We run it on a schedule instead of on pull_request_* events to not create confusing messaging in the PR + schedule: + - cron: "*/10 * * * *" +concurrency: ${{ github.workflow }} +jobs: + bot: + name: Pending reviews bot + runs-on: ubuntu-latest + environment: Matrix + env: + URL: "https://github.com/pulls?q=is%3Apr+is%3Aopen+repo%3Amatrix-org%2Fmatrix-js-sdk+repo%3Amatrix-org%2Fmatrix-react-sdk+repo%3Avector-im%2Felement-web+repo%3Avector-im%2Felement-desktop+review-requested%3A%40me+sort%3Aupdated-desc+" + steps: + - uses: actions/github-script@v6 + env: + HS_URL: ${{ secrets.BETABOT_HS_URL }} + ROOM_ID: ${{ secrets.ROOM_ID }} + TOKEN: ${{ secrets.BETABOT_ACCESS_TOKEN }} + with: + # PAT needed as the GITHUB_TOKEN won't be able to see cross-references from other orgs (matrix-org) + github-token: ${{ secrets.ELEMENT_BOT_TOKEN }} + script: | + const repos = [ + "vector-im/element-desktop", + "vector-im/element-web", + "matrix-org/matrix-react-sdk", + "matrix-org/matrix-js-sdk", + ]; + const teams = [ + "matrix-org/element-web-app-team", + "matrix-org/element-web", + "vector-im/element-web-app-team", + "vector-im/element-web", + ]; + + let issueCount = 0; + for (const team of teams) { + const org = team.split("/", 2)[0]; + const reposInOrg = repos.filter(repo => repo.startsWith(org + "/")); + const { data } = await github.rest.search.issuesAndPullRequests({ + q: `is:pr is:open review:required ${reposInOrg.map(r => `repo:${r}`).join(" ")} team-review-requested:${team}`, + }); + issueCount += data.total_count; + } + + const { HS_URL, ROOM_ID, TOKEN, URL } = process.env; + const apiUrl = `${HS_URL}/_matrix/client/v3/rooms/${ROOM_ID}/state/re.jki.counter/gh_reviews`; + const headers = { + "Content-Type": "application/json", + "Authorization": `Bearer ${TOKEN}`, + }; + const res = await fetch(apiUrl, { + method: "GET", + headers, + }); + + const data = await res.json(); + + if (data.value === issueCount) { + console.log("Pending review count already correct"); + return; + } + + await fetch(apiUrl, { + method: "PUT", + body: JSON.stringify({ + "link": URL, + "severity": "warning", + "title": "Pending reviews", + "value": issueCount + }), + headers, + });