name: Auto set milestone on PR on: pull_request: types: opened env: QGIS_MAJOR_VERSION: 3 jobs: test: runs-on: ubuntu-latest steps: # obfuscate the github token so it can be used on jobs triggered from forks - name: Clear GH Token id: token uses: opengisch/clear-token@v1.0.12 with: bot_token_encrypted: ddbdec32940df79f1adf2369b4b10f10b5a66f65 bot_token_xor_key: a1b2c3d47311f8e29e204f85a81b4df4a44e252c # list the tags and milestones - uses: octokit/graphql-action@v2.x id: list_tags_milestones with: query: | query list_tags_milestones($owner: String!, $repo: String!) { repository(owner: $owner, name: $repo) { refs(refPrefix: "refs/tags/", orderBy: {field: TAG_COMMIT_DATE, direction: DESC}, first: 30) { edges { node { name } } } milestones(orderBy: {field: CREATED_AT, direction: DESC}, first: 30) { edges { node { title number } } } } } owner: ${{ github.event.pull_request.base.repo.owner.login }} repo: ${{ github.event.pull_request.base.repo.name }} env: GITHUB_TOKEN: ${{ steps.token.outputs.token }} # calculate the milestone and determine if it exists - name: Get the latest release id: calculate_milestone env: JSON_DATA: ${{ steps.list_tags_milestones.outputs.data }} BASE_BRANCH: ${{ github.event.pull_request.base.ref }} run: | # master => NOTHING, release_3-10 => _10 MINOR_VERSION=$(echo ${BASE_BRANCH} | sed -r -e 's/^release-[0-9]_([0-9]+)/_\1/;t;d') echo "MINOR_VERSION: ${MINOR_VERSION}" # get the max release from the tags MAX_RELEASE=$(echo "${JSON_DATA}" | jq ".repository.refs.edges[].node.name | select( . | test(\"^final-${QGIS_MAJOR_VERSION}${MINOR_VERSION}\") ) | sub(\"^final-${QGIS_MAJOR_VERSION}_(?[0-9]+)_(?

.)\"; .m+\".\"+.p) | tonumber" | jq -s '. | max') echo "MAX_RELEASE: ${MAX_RELEASE}" MILESTONE_TITLE="${QGIS_MAJOR_VERSION}."$(echo "${MAX_RELEASE} + 0.1 " | bc) echo "MILESTONE_TITLE: ${MILESTONE_TITLE}" echo "::set-output name=milestone_title::${MILESTONE_TITLE}" MILESTONE_NUMBER=$(echo "${JSON_DATA}" | jq ".repository.milestones.edges[] | select( .node.title == \"${MILESTONE_TITLE}\" ) | .node.number") echo "MILESTONE_NUMBER: ${MILESTONE_NUMBER}" echo "::set-output name=milestone_number::${MILESTONE_NUMBER}" MILESTONE_EXISTS=$([[ -n $(echo "${JSON_DATA}" | jq ".repository.refs.edges[].node.name | select( . | test(\"^${MILESTONE_TITLE}\") ) ") ]] && echo "true" || echo "false" ) echo "MILESTONE_EXISTS: ${MILESTONE_EXISTS}" echo "::set-output name=milestone_exists::${MILESTONE_EXISTS}" # create the milestone if needed - name: Create milestone id: create_milestone if: steps.calculate_milestone.outputs.milestone_exists == false uses: octokit/request-action@v2.x with: route: POST /repos/:owner/:repo/milestones title: ${{ steps.calculate_milestone.outputs.milestone_title }} owner: ${{ github.event.pull_request.base.repo.owner.login }} repo: ${{ github.event.pull_request.base.repo.name }} env: GITHUB_TOKEN: ${{ steps.token.outputs.token }} - id: compute_milestone name: compute the milestone number env: MILESTONE_NUMBER_EXISTING: ${{ steps.calculate_milestone.outputs.milestone_number }} MILESTONE_NUMBER_CREATED_JSON: ${{ steps.create_milestone.outputs.data }} run: | FINAL_MILESTONE_NUMBER=$([[ -n ${MILESTONE_NUMBER_EXISTING} ]] && echo "${MILESTONE_NUMBER_EXISTING}" || $(echo "${MILESTONE_NUMBER_CREATED_JSON}" | jq '.number' )) echo "FINAL_MILESTONE_NUMBER: ${FINAL_MILESTONE_NUMBER}" echo "::set-output name=milestone_number::${FINAL_MILESTONE_NUMBER}" # update PR with milestone - name: update PR milestone uses: octokit/request-action@v2.x env: GITHUB_TOKEN: ${{ steps.token.outputs.token }} with: route: PATCH /repos/:owner/:repo/issues/:pull_number pull_number: ${{ github.event.pull_request.number }} milestone: ${{ steps.compute_milestone.outputs.milestone_number }} owner: ${{ github.event.pull_request.base.repo.owner.login }} repo: ${{ github.event.pull_request.base.repo.name }} # if the PR author could not set the milestone due to missing rights, add a comment - name: leave comment for milestone if: failure() uses: octokit/request-action@v2.x env: GITHUB_TOKEN: ${{ steps.token.outputs.token }} with: route: POST /repos/:owner/:repo/issues/:pull_number/comments pull_number: ${{ github.event.pull_request.number }} body: format('**Calculated milestone for the PR: {0}**', ${{ steps.compute_milestone.outputs.milestone_number }}) owner: ${{ github.event.pull_request.base.repo.owner.login }} repo: ${{ github.event.pull_request.base.repo.name }}