SEANK.H.LIAO

github action pr title fixer

spinning up a vm just to run a bash script...

github actions

Say you need your pull request titles to fit a certain format (prefix with WD-$ticketnumber) because your automation relies on it. Also you want a clickable link in the body. You could write an action to fix them for you:

todo:

 1name: PR Title Fixer
 2
 3on:
 4  pull_request:
 5    types:
 6      - opened
 7
 8jobs:
 9  fix:
10    name: Fix
11    runs-on: ubuntu-latest
12    steps:
13      # only if we can use the branch as the source of truth
14      - if: startsWith(${{ github.head_ref }}, "WD-")
15        run: |
16          # inline script
17          set -euxo pipefail
18
19          branch="${{ github.head_ref }}"
20
21          # extract WD-ticket from potentially long name: WD-ticket-some-description
22          if [[ "$branch" =~ ^(WD-[0-9]{5}).* ]] ; then
23            branch=${BASH_REMATCH[1]}
24            echo "fixed branch is $branch"
25          else
26            # not in expected format
27            exit 0
28          fi
29
30          cat << EOF | \
31            jq \
32              --arg b $branch \
33              --arg u "jira: [$branch](https://example.com/browse/$branch)" \
34             '{title: [$b, (.title | sub("^[Ww][Dd][- ][0-9]{5}"; ""))] | join(" "), body: [$u, .body] | join("\n\n")}' | \
35            curl \
36              --silent \
37              --request PATCH \
38              --header "Content-Type: application/json"  \
39              --header "Authorization: Bearer ${{ github.token }}" \
40              --data-binary @- \
41              "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.number }}" > /dev/null
42          ${{ toJson(github.event.pull_request) }}
43          EOF