|
| 1 | +name: 'Release: Prepare PR' |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ develop ] |
| 6 | + |
| 7 | +env: |
| 8 | + GITHUB_USERNAME: codeforphilly-bot |
| 9 | + GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }} |
| 10 | + RELEASE_BRANCH: releases/v1 |
| 11 | + |
| 12 | +jobs: |
| 13 | + release-prepare: |
| 14 | + |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + |
| 18 | + - uses: actions/checkout@v2 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + |
| 22 | + # - uses: mxschmitt/action-tmate@v3 |
| 23 | + |
| 24 | + - name: Create/update pull request |
| 25 | + run: | |
| 26 | + # get latest release tag |
| 27 | + latest_release=$(git describe --tags --abbrev=0 "origin/${RELEASE_BRANCH}") |
| 28 | + latest_release_bumped=$(echo $latest_release | awk -F. -v OFS=. '{$NF++;print}') |
| 29 | +
|
| 30 | +
|
| 31 | + # create or update PR |
| 32 | + pr_body="$(cat <<EOF |
| 33 | + Release: ${latest_release_bumped} |
| 34 | +
|
| 35 | + ## Improvements |
| 36 | +
|
| 37 | + ## Technical |
| 38 | +
|
| 39 | + EOF |
| 40 | + )" |
| 41 | +
|
| 42 | + pr_number=$(hub pr list -h develop -f '%I') |
| 43 | +
|
| 44 | + if [ -n "${pr_number}" ]; then |
| 45 | + echo "Updating PR #${pr_number}" |
| 46 | + existing_comment_id=$(hub api "/repos/${GITHUB_REPOSITORY}/issues/${pr_number}/comments" | jq '.[] | select(.user.login=="${GITHUB_USERNAME}") | .id') |
| 47 | + else |
| 48 | + echo "Opening PR" |
| 49 | + hub pull-request -b "${RELEASE_BRANCH}" -h develop -F <(echo "${pr_body}") > /tmp/pr.json |
| 50 | + pr_number=$(hub pr list -h develop -f '%I') |
| 51 | + echo "Opened PR #${pr_number}" |
| 52 | + fi |
| 53 | +
|
| 54 | +
|
| 55 | + # build changelog |
| 56 | + commits=$( |
| 57 | + git log \ |
| 58 | + --first-parent \ |
| 59 | + --reverse \ |
| 60 | + --format="%H" \ |
| 61 | + "origin/${RELEASE_BRANCH}..develop" |
| 62 | + ) |
| 63 | +
|
| 64 | + changelog=() |
| 65 | +
|
| 66 | + while read -r commit; do |
| 67 | + subject="$(git show -s --format=%s "${commit}")" |
| 68 | + line="" |
| 69 | +
|
| 70 | + if [[ "${subject}" =~ Merge\ pull\ request\ \#([0-9]+) ]]; then |
| 71 | + line="$(hub pr show -f '%t [%i] @%au' "${BASH_REMATCH[1]}" || true)" |
| 72 | + fi |
| 73 | +
|
| 74 | + if [ -z "${line}" ]; then |
| 75 | + author="$(hub api "/repos/${GITHUB_REPOSITORY}/commits/${commit}" -H Accept:application/vnd.github.v3+json | jq -r '.author.login')" |
| 76 | + if [ -n "${author}" ]; then |
| 77 | + author="@${author}" |
| 78 | + else |
| 79 | + author="$(git show -s --format=%ae "${commit}")" |
| 80 | + fi |
| 81 | +
|
| 82 | + line="${subject} ${author}" |
| 83 | + fi |
| 84 | +
|
| 85 | + # move ticket number prefix into to existing square brackets at end |
| 86 | + line="$(echo "${line}" | perl -pe 's/^([A-Z]+-[0-9]+):?\s*(.*?)\s*\[([^]]+)\]\s*(\S+)$/\2 [\3, \1] \4/')" |
| 87 | +
|
| 88 | + # move ticket number prefix into to new square brackets at end |
| 89 | + line="$(echo "${line}" | perl -pe 's/^([A-Z]+-[0-9]+):?\s*(.*?)\s*(\S+)$/\2 [\1] \3/')" |
| 90 | +
|
| 91 | + # combine doubled square brackets at the end |
| 92 | + line="$(echo "${line}" | perl -pe 's/^\s*(.*?)\s*\[([A-Z]+-[0-9]+)\]\s*\[([^]]+)\]\s*(\S+)$/\1 [\3, \2] \4/')" |
| 93 | +
|
| 94 | + changelog+=("- ${line}") |
| 95 | + done <<< "${commits}" |
| 96 | +
|
| 97 | +
|
| 98 | + # create or update comment |
| 99 | + comment_body="$(cat <<EOF |
| 100 | + ## Changelog |
| 101 | +
|
| 102 | + \`\`\`markdown |
| 103 | + $(IFS=$'\n'; echo "${changelog[*]}") |
| 104 | + \`\`\` |
| 105 | + EOF |
| 106 | + )" |
| 107 | +
|
| 108 | + if [ -n "${existing_comment_id}" ]; then |
| 109 | + echo "Updating comment #${existing_comment_id}" |
| 110 | + hub api "/repos/${GITHUB_REPOSITORY}/issues/comments/${existing_comment_id}" -f body="${comment_body}" |
| 111 | + else |
| 112 | + echo "Creating comment" |
| 113 | + hub api "/repos/${GITHUB_REPOSITORY}/issues/${pr_number}/comments" -f body="${comment_body}" |
| 114 | + fi |
0 commit comments