github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/.github/workflows/prauto.yml (about)

     1  name: PR Automation
     2  on:
     3    pull_request_target:
     4      types: [ready_for_review, opened, reopened]
     5  
     6  permissions:
     7    contents: none
     8    issues: write
     9    pull-requests: write
    10  
    11  jobs:
    12    pr-auto:
    13      runs-on: ubuntu-latest
    14      steps:
    15        - name: lint pr
    16          env:
    17            GH_REPO: ${{ github.repository }}
    18            GH_TOKEN: ${{ secrets.AUTOMATION_TOKEN }}
    19            PRID: ${{ github.event.pull_request.node_id }}
    20            PRBODY: ${{ github.event.pull_request.body }}
    21            PRNUM: ${{ github.event.pull_request.number }}
    22            PRHEAD: ${{ github.event.pull_request.head.label }}
    23            PRAUTHOR: ${{ github.event.pull_request.user.login }}
    24            PR_AUTHOR_TYPE: ${{ github.event.pull_request.user.type }}
    25          if: "!github.event.pull_request.draft"
    26          run: |
    27            commentPR () {
    28              gh pr comment $PRNUM -b "${1}"
    29            }
    30  
    31            closePR () {
    32              gh pr close $PRNUM
    33            }
    34  
    35            colID () {
    36              gh api graphql -f query='query($owner:String!, $repo:String!) {
    37                repository(owner:$owner, name:$repo) {
    38                  project(number:1) {
    39                    columns(first:10) { nodes {id,name} }
    40                  }
    41                }
    42              }' -f owner="${GH_REPO%/*}" -f repo="${GH_REPO#*/}" \
    43                -q ".data.repository.project.columns.nodes[] | select(.name | startswith(\"$1\")) | .id"
    44            }
    45  
    46            addToBoard () {
    47              gh api graphql --silent -f query='
    48                mutation($colID:ID!, $prID:ID!) { addProjectCard(input: { projectColumnId: $colID, contentId: $prID }) { clientMutationId } }
    49              ' -f colID="$(colID "Needs review")" -f prID="$PRID"
    50            }
    51  
    52            if [ "$PR_AUTHOR_TYPE" = "Bot" ] || gh api orgs/cli/public_members/$PRAUTHOR --silent 2>/dev/null
    53            then
    54              if [ "$PR_AUTHOR_TYPE" != "Bot" ]
    55              then
    56                gh pr edit $PRNUM --add-assignee $PRAUTHOR
    57              fi
    58              if ! errtext="$(addToBoard 2>&1)"
    59              then
    60                cat <<<"$errtext" >&2
    61                if ! grep -iq 'project already has the associated issue' <<<"$errtext"
    62                then
    63                  exit 1
    64                fi
    65              fi
    66              exit 0
    67            fi
    68  
    69            gh pr edit $PRNUM --add-label "external"
    70  
    71            if [ "$PRHEAD" = "cli:trunk" ]
    72            then
    73              closePR
    74              exit 0
    75            fi
    76  
    77            if [ $(wc -c <<<"$PRBODY") -lt 10 ]
    78            then
    79              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."
    80              closePR
    81              exit 0
    82            fi
    83  
    84            if ! grep -Eq '(#|issues/)[0-9]+' <<<"$PRBODY"
    85            then
    86              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."
    87            fi
    88  
    89            addToBoard
    90            exit 0