From 75ac2595b30d3f1b4eabced101f7d8d42d24ea5e Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 10 Mar 2021 14:01:15 -0600 Subject: [PATCH 1/7] add workflow --- .github/workflows/prauto.yml | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 .github/workflows/prauto.yml diff --git a/.github/workflows/prauto.yml b/.github/workflows/prauto.yml new file mode 100644 index 00000000000..3160c2bc334 --- /dev/null +++ b/.github/workflows/prauto.yml @@ -0,0 +1,65 @@ +name: PR Automation +on: + pull_request: + types: [ready_for_review, opened, reopened] +jobs: + bash-redux: + runs-on: ubuntu-latest + steps: + - name: lint pr + env: + REPO: "cli/cli" + NEEDSREVIEWCOL: "MDEzOlByb2plY3RDb2x1bW43MTEwMTI4" + PRID: ${{ github.event.pull_request.node_id }} + PRBODY: ${{ github.event.pull_request.body }} + STAFF: "vilmibm,mislav,samcoe,billygriffin,ampinsk" + PRNUM: ${{ github.event.pull_request.number }} + PRBASE: ${{ github.event.pull_request.base.ref }} + GH_TOKEN: ${{ secrets.AUTOMATION_TOKEN }} + # Testing data: + #REPO: "vilmibm/testing" + #NEEDSREVIEWCOL: "MDEzOlByb2plY3RDb2x1bW4xMTAzNTA3Ng==" # vilmibm/testing "good" column + run: | + commentPR () { + gh -R$REPO pr comment $PRNUM -b "${1}" + } + + closePR () { + gh -R$REPO pr close $PRNUM + } + + addToBoard () { + # this exec is an attempt to fix some quote highlighting issues i was having in my editor. + bash -c 'gh api graphql -f query="mutation { addProjectCard(input: { projectColumnId: \"$NEEDSREVIEWCOL\", contentId: \"$PRID\" }) { clientMutationId } }"' + } + + if echo $STAFF | grep $PRAUTHOR + then + addToBoard + exit 0 + fi + + if echo $PRBODY | wc -w | grep '^[0-9]$' + then + commentPR "Thanks for the PR! We're a small team and it's helpful to have context around community submissions in order to review them appropriately. Our automation has closed this PR since the body appears to be lacking much content; please add a more descriptive PR body as well as any issues it closes and reopen. Thanks again!" + closePR + exit 0 + fi + + if echo $PRBASE | grep -Ev "^trunk$" + then + commentPR "This pull request should probably be targeting trunk; since it isn't, our automation has closed it. Please reopen with an appropriate base branch if this was in error." + closePR + exit 0 + fi + + if echo $PRBODY | grep -Ev " \#\d+ " + then + commentPR "Hi! Thanks for the PR. Please ensure that this PR is related to an issue by mentioning its issue number in the PR body. If this PR would close the issue, please put 'Fixes #' somewhere in the PR body. If this is a tiny change like fixing a typo, feel free to ignore this message." + # TODO should this be close-worthy? I feel like sometimes it's ok + # to not have an issue (like a tiny typo fix) so we should have + # this be a warning. + fi + + addToBoard + exit 0 From 8ddc82e55784bd9dcb2b2507a8bdcafe0cf73892 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 10 Mar 2021 14:13:47 -0600 Subject: [PATCH 2/7] rename --- .github/workflows/prauto.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/prauto.yml b/.github/workflows/prauto.yml index 3160c2bc334..f2f62089bd8 100644 --- a/.github/workflows/prauto.yml +++ b/.github/workflows/prauto.yml @@ -3,7 +3,7 @@ on: pull_request: types: [ready_for_review, opened, reopened] jobs: - bash-redux: + pr-auto: runs-on: ubuntu-latest steps: - name: lint pr From 31312d22d0fec6731dc21471dcd89839710d9a61 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 10 Mar 2021 14:49:42 -0600 Subject: [PATCH 3/7] meh --- .github/workflows/prauto.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/prauto.yml b/.github/workflows/prauto.yml index f2f62089bd8..71e704722e0 100644 --- a/.github/workflows/prauto.yml +++ b/.github/workflows/prauto.yml @@ -39,23 +39,23 @@ jobs: exit 0 fi - if echo $PRBODY | wc -w | grep '^[0-9]$' + if echo "${PRBODY}" | wc -w | grep -E '^[0-9]$' then commentPR "Thanks for the PR! We're a small team and it's helpful to have context around community submissions in order to review them appropriately. Our automation has closed this PR since the body appears to be lacking much content; please add a more descriptive PR body as well as any issues it closes and reopen. Thanks again!" closePR exit 0 fi - if echo $PRBASE | grep -Ev "^trunk$" + if echo "${PRBASE}" | grep -Ev "^trunk$" then commentPR "This pull request should probably be targeting trunk; since it isn't, our automation has closed it. Please reopen with an appropriate base branch if this was in error." closePR exit 0 fi - if echo $PRBODY | grep -Ev " \#\d+ " + if echo "${PRBODY}" | grep -Ev " \#\d+ " then - commentPR "Hi! Thanks for the PR. Please ensure that this PR is related to an issue by mentioning its issue number in the PR body. If this PR would close the issue, please put 'Fixes #' somewhere in the PR body. If this is a tiny change like fixing a typo, feel free to ignore this message." + commentPR "Hi! Thanks for the PR. Please ensure that this PR is related to an issue by mentioning its issue number in the PR body. If this PR would close the issue, please put the word 'Fixes' before the issue number somewhere in the PR body. If this is a tiny change like fixing a typo, feel free to ignore this message." # TODO should this be close-worthy? I feel like sometimes it's ok # to not have an issue (like a tiny typo fix) so we should have # this be a warning. From 767558828080f33ad6480e2956aabadf58c1f70a Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 10 Mar 2021 18:07:09 -0600 Subject: [PATCH 4/7] grep tweaks --- .github/workflows/prauto.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/prauto.yml b/.github/workflows/prauto.yml index 71e704722e0..23d130ed9fe 100644 --- a/.github/workflows/prauto.yml +++ b/.github/workflows/prauto.yml @@ -46,14 +46,14 @@ jobs: exit 0 fi - if echo "${PRBASE}" | grep -Ev "^trunk$" + if echo "${PRBASE}" | grep -Ev '^trunk$' then commentPR "This pull request should probably be targeting trunk; since it isn't, our automation has closed it. Please reopen with an appropriate base branch if this was in error." closePR exit 0 fi - if echo "${PRBODY}" | grep -Ev " \#\d+ " + if echo "${PRBODY}" | grep -Ev '#[0-9]+' then commentPR "Hi! Thanks for the PR. Please ensure that this PR is related to an issue by mentioning its issue number in the PR body. If this PR would close the issue, please put the word 'Fixes' before the issue number somewhere in the PR body. If this is a tiny change like fixing a typo, feel free to ignore this message." # TODO should this be close-worthy? I feel like sometimes it's ok From bfdad5bd48707e5c0332d9e4c3626b0044012713 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 10 Mar 2021 18:15:01 -0600 Subject: [PATCH 5/7] fix missing PRAUTHOR and add TODO --- .github/workflows/prauto.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/prauto.yml b/.github/workflows/prauto.yml index 23d130ed9fe..c9acd24be49 100644 --- a/.github/workflows/prauto.yml +++ b/.github/workflows/prauto.yml @@ -16,6 +16,7 @@ jobs: PRNUM: ${{ github.event.pull_request.number }} PRBASE: ${{ github.event.pull_request.base.ref }} GH_TOKEN: ${{ secrets.AUTOMATION_TOKEN }} + PRAUTHOR: ${{ github.event.pull_request.user.login }} # Testing data: #REPO: "vilmibm/testing" #NEEDSREVIEWCOL: "MDEzOlByb2plY3RDb2x1bW4xMTAzNTA3Ng==" # vilmibm/testing "good" column @@ -35,6 +36,7 @@ jobs: if echo $STAFF | grep $PRAUTHOR then + # TODO this errors if it's already on the board... addToBoard exit 0 fi From c1c936b74b250654bdec2ddb41a2f75b28c86a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 29 Mar 2021 15:05:34 +0200 Subject: [PATCH 6/7] Improve PR lint script - Do not add draft PRs to the review board - Do not enforce that the base branch must be "trunk" - Refuse PRs made with our "trunk" as the head - Improve staff check to avoid hardcoding - Improve pattern matching when suggesting to link to an issue - Use the stock GITHUB_TOKEN --- .github/workflows/prauto.yml | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/.github/workflows/prauto.yml b/.github/workflows/prauto.yml index c9acd24be49..2d642d721f7 100644 --- a/.github/workflows/prauto.yml +++ b/.github/workflows/prauto.yml @@ -8,59 +8,53 @@ jobs: steps: - name: lint pr env: - REPO: "cli/cli" + GH_REPO: ${{ github.repository }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} NEEDSREVIEWCOL: "MDEzOlByb2plY3RDb2x1bW43MTEwMTI4" PRID: ${{ github.event.pull_request.node_id }} PRBODY: ${{ github.event.pull_request.body }} - STAFF: "vilmibm,mislav,samcoe,billygriffin,ampinsk" PRNUM: ${{ github.event.pull_request.number }} - PRBASE: ${{ github.event.pull_request.base.ref }} - GH_TOKEN: ${{ secrets.AUTOMATION_TOKEN }} + PRHEAD: ${{ github.event.pull_request.head.label }} PRAUTHOR: ${{ github.event.pull_request.user.login }} - # Testing data: - #REPO: "vilmibm/testing" - #NEEDSREVIEWCOL: "MDEzOlByb2plY3RDb2x1bW4xMTAzNTA3Ng==" # vilmibm/testing "good" column + if: "!github.event.pull_request.draft" run: | commentPR () { - gh -R$REPO pr comment $PRNUM -b "${1}" + gh pr comment $PRNUM -b "${1}" } closePR () { - gh -R$REPO pr close $PRNUM + gh pr close $PRNUM } addToBoard () { - # this exec is an attempt to fix some quote highlighting issues i was having in my editor. - bash -c 'gh api graphql -f query="mutation { addProjectCard(input: { projectColumnId: \"$NEEDSREVIEWCOL\", contentId: \"$PRID\" }) { clientMutationId } }"' + gh api graphql --silent -f query=' + mutation($colID:ID!, $pr:ID!) { addProjectCard(input: { projectColumnId: $colID, contentId: $prID }) { clientMutationId } } + ' -f colID="$NEEDSREVIEWCOL" -f prID="$PRID" } - if echo $STAFF | grep $PRAUTHOR + if gh api orgs/cli/public_members/$PRAUTHOR --silent 2>/dev/null then # TODO this errors if it's already on the board... addToBoard exit 0 fi - if echo "${PRBODY}" | wc -w | grep -E '^[0-9]$' + if [ "$PRHEAD" = "cli:trunk" ] then - commentPR "Thanks for the PR! We're a small team and it's helpful to have context around community submissions in order to review them appropriately. Our automation has closed this PR since the body appears to be lacking much content; please add a more descriptive PR body as well as any issues it closes and reopen. Thanks again!" closePR exit 0 fi - if echo "${PRBASE}" | grep -Ev '^trunk$' + if [ $(wc -c <<<"$PRBODY") -lt 10 ] then - commentPR "This pull request should probably be targeting trunk; since it isn't, our automation has closed it. Please reopen with an appropriate base branch if this was in error." + commentPR "Thanks for the pull request! We're a small team and it's helpful to have context around community submissions in order to review them appropriately. Our automation has closed this pull request since it does not have an adequate description. Please edit the body of this pull request to describe what this does, then reopen it." closePR exit 0 fi - if echo "${PRBODY}" | grep -Ev '#[0-9]+' + if ! grep -Eq '(#|issues/)[0-9]+' <<<"$PRBODY" then - commentPR "Hi! Thanks for the PR. Please ensure that this PR is related to an issue by mentioning its issue number in the PR body. If this PR would close the issue, please put the word 'Fixes' before the issue number somewhere in the PR body. If this is a tiny change like fixing a typo, feel free to ignore this message." - # TODO should this be close-worthy? I feel like sometimes it's ok - # to not have an issue (like a tiny typo fix) so we should have - # this be a warning. + commentPR "Hi! Thanks for the pull request. Please ensure that this change is linked to an issue by mentioning an issue number in the description of the pull request. If this pull request would close the issue, please put the word 'Fixes' before the issue number somewhere in the pull request body. If this is a tiny change like fixing a typo, feel free to ignore this message." fi addToBoard From a238d2952d1ec296a68bd201d7b9f030f2d68fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 29 Mar 2021 15:27:16 +0200 Subject: [PATCH 7/7] Dynamically resolve the column ID for "Needs review" --- .github/workflows/prauto.yml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/.github/workflows/prauto.yml b/.github/workflows/prauto.yml index 2d642d721f7..9b6d1478dd4 100644 --- a/.github/workflows/prauto.yml +++ b/.github/workflows/prauto.yml @@ -10,7 +10,6 @@ jobs: env: GH_REPO: ${{ github.repository }} GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - NEEDSREVIEWCOL: "MDEzOlByb2plY3RDb2x1bW43MTEwMTI4" PRID: ${{ github.event.pull_request.node_id }} PRBODY: ${{ github.event.pull_request.body }} PRNUM: ${{ github.event.pull_request.number }} @@ -26,10 +25,21 @@ jobs: gh pr close $PRNUM } + colID () { + gh api graphql -f query='query($owner:String!, $repo:String!) { + repository(owner:$owner, name:$repo) { + project(number:1) { + columns(first:10) { nodes {id,name} } + } + } + }' -F owner=:owner -F repo=:repo \ + -q ".data.repository.project.columns.nodes[] | select(.name | startswith(\"$1\")) | .id" + } + addToBoard () { gh api graphql --silent -f query=' mutation($colID:ID!, $pr:ID!) { addProjectCard(input: { projectColumnId: $colID, contentId: $prID }) { clientMutationId } } - ' -f colID="$NEEDSREVIEWCOL" -f prID="$PRID" + ' -f colID="$(colID "Needs review")" -f prID="$PRID" } if gh api orgs/cli/public_members/$PRAUTHOR --silent 2>/dev/null