From 840867bc02129ff897883fcb750be8353b36fca5 Mon Sep 17 00:00:00 2001
From: Kat Gerasimova <ekaterinag@element.io>
Date: Tue, 18 Jan 2022 13:12:27 +0000
Subject: [PATCH] Update issue and PR automation (#20583)

* Add X-Needs-Product issues to product board
* Add product PRs to product board
* Add UISI issues to crypto team board
* Rename labelled and unlabelled triage files because they don't always
  move issues
---
 ...-move-labelled.yml => triage-labelled.yml} | 26 ++++++-
 .../workflows/triage-move-review-requests.yml | 67 +++++++++++++++++++
 .github/workflows/triage-priority-bugs.yml    |  1 +
 ...e-unlabelled.yml => triage-unlabelled.yml} |  0
 4 files changed, 93 insertions(+), 1 deletion(-)
 rename .github/workflows/{triage-move-labelled.yml => triage-labelled.yml} (88%)
 rename .github/workflows/{triage-move-unlabelled.yml => triage-unlabelled.yml} (100%)

diff --git a/.github/workflows/triage-move-labelled.yml b/.github/workflows/triage-labelled.yml
similarity index 88%
rename from .github/workflows/triage-move-labelled.yml
rename to .github/workflows/triage-labelled.yml
index 0116ddd4c3..6276cf260c 100644
--- a/.github/workflows/triage-move-labelled.yml
+++ b/.github/workflows/triage-labelled.yml
@@ -29,7 +29,7 @@ jobs:
               repo: context.repo.repo,
               labels: ['Z-Labs']
             })
- 
+
   move_needs_info_issues:
     name: X-Needs-Info issues to Need info column on triage board
     runs-on: ubuntu-latest
@@ -72,6 +72,30 @@ jobs:
           PROJECT_ID: "PN_kwDOAM0swc0sUA"
           GITHUB_TOKEN: ${{ secrets.ELEMENT_BOT_TOKEN }}
 
+  add_product_issues:
+    name: X-Needs-Product to Design project board
+    runs-on: ubuntu-latest
+    if: >
+        contains(github.event.issue.labels.*.name, 'X-Needs-Product')
+    steps:
+      - uses: octokit/graphql-action@v2.x
+        id: add_to_project
+        with:
+          headers: '{"GraphQL-Features": "projects_next_graphql"}'
+          query: |
+            mutation add_to_project($projectid:ID!,$contentid:ID!) {
+              addProjectNextItem(input:{projectId:$projectid contentId:$contentid}) {
+                projectNextItem {
+                  id
+                }
+              }
+            }
+          projectid: ${{ env.PROJECT_ID }}
+          contentid: ${{ github.event.issue.node_id }}
+        env:
+          PROJECT_ID: "PN_kwDOAM0swc4AAg6N"
+          GITHUB_TOKEN: ${{ secrets.ELEMENT_BOT_TOKEN }}
+
   Delight_issues_to_board:
     name: Delight issues to project board
     runs-on: ubuntu-latest
diff --git a/.github/workflows/triage-move-review-requests.yml b/.github/workflows/triage-move-review-requests.yml
index 91125f4345..8306cffefc 100644
--- a/.github/workflows/triage-move-review-requests.yml
+++ b/.github/workflows/triage-move-review-requests.yml
@@ -70,3 +70,70 @@ jobs:
           PROJECT_ID: "PN_kwDOAM0swc0sUA"
           TEAM: "design"
           GITHUB_TOKEN: ${{ secrets.ELEMENT_BOT_TOKEN }}
+
+  add_product_pr_to_project:
+    name: Move PRs asking for design review to the design board
+    runs-on: ubuntu-latest
+    steps:
+      - uses: octokit/graphql-action@v2.x
+        id: find_team_members
+        with:
+          headers: '{"GraphQL-Features": "projects_next_graphql"}'
+          query: |
+            query find_team_members($team: String!) {
+              organization(login: "vector-im") {
+                team(slug: $team) {
+                  members {
+                    nodes {
+                      login
+                    }
+                  }
+                }
+              }
+            }
+          team: ${{ env.TEAM }}
+        env:
+          TEAM: "product"
+          GITHUB_TOKEN: ${{ secrets.ELEMENT_BOT_TOKEN }}
+      - id: any_matching_reviewers
+        run: |
+          # Fetch requested reviewers, and people who are on the team
+          echo '${{ tojson(fromjson(steps.find_team_members.outputs.data).organization.team.members.nodes[*].login) }}' | tee /tmp/team_members.json
+          echo '${{ tojson(github.event.pull_request.requested_reviewers[*].login) }}' | tee /tmp/reviewers.json
+          jq --raw-output .[] < /tmp/team_members.json | sort | tee /tmp/team_members.txt
+          jq --raw-output .[] < /tmp/reviewers.json | sort | tee /tmp/reviewers.txt
+
+          # Fetch requested team reviewers, and the name of the team
+          echo '${{ tojson(github.event.pull_request.requested_teams[*].slug) }}' | tee /tmp/team_reviewers.json
+          jq --raw-output .[] < /tmp/team_reviewers.json | sort | tee /tmp/team_reviewers.txt
+          echo '${{ env.TEAM }}' | tee /tmp/team.txt
+
+          # If either a reviewer matches a team member, or a team matches our team, say "true"
+          if [ $(join /tmp/team_members.txt /tmp/reviewers.txt | wc -l) != 0 ]; then
+            echo "::set-output name=match::true"
+          elif [ $(join /tmp/team.txt /tmp/team_reviewers.txt | wc -l) != 0 ]; then
+            echo "::set-output name=match::true"
+          else
+            echo "::set-output name=match::false"
+          fi
+        env:
+          TEAM: "product"
+      - uses: octokit/graphql-action@v2.x
+        id: add_to_project
+        if: steps.any_matching_reviewers.outputs.match == 'true'
+        with:
+          headers: '{"GraphQL-Features": "projects_next_graphql"}'
+          query: |
+            mutation add_to_project($projectid:ID!, $contentid:ID!) {
+              addProjectNextItem(input:{projectId:$projectid contentId:$contentid}) {
+                projectNextItem {
+                  id
+                }
+              }
+            }
+          projectid: ${{ env.PROJECT_ID }}
+          contentid: ${{ github.event.pull_request.node_id }}
+        env:
+          PROJECT_ID: "PN_kwDOAM0swc4AAg6N"
+          TEAM: "product"
+          GITHUB_TOKEN: ${{ secrets.ELEMENT_BOT_TOKEN }}
diff --git a/.github/workflows/triage-priority-bugs.yml b/.github/workflows/triage-priority-bugs.yml
index 80e65c35f8..04a127818c 100644
--- a/.github/workflows/triage-priority-bugs.yml
+++ b/.github/workflows/triage-priority-bugs.yml
@@ -34,6 +34,7 @@ jobs:
   P1_issues_to_crypto_team_workboard:
     runs-on: ubuntu-latest
     if: >
+        contains(github.event.issue.labels.*.name, 'Z-UISI') ||
         (contains(github.event.issue.labels.*.name, 'A-E2EE') ||
          contains(github.event.issue.labels.*.name, 'A-E2EE-Cross-Signing') ||
          contains(github.event.issue.labels.*.name, 'A-E2EE-Dehydration') ||
diff --git a/.github/workflows/triage-move-unlabelled.yml b/.github/workflows/triage-unlabelled.yml
similarity index 100%
rename from .github/workflows/triage-move-unlabelled.yml
rename to .github/workflows/triage-unlabelled.yml