Skip to content
Permalink
Browse files

[github] use a cron job to set milestone on PRs

hence, no rights issue
  • Loading branch information
3nids committed Jan 10, 2020
1 parent 5ef1607 commit 480095a3762e4f3f70a661c0d64a41c4c5a26e3c
Showing with 74 additions and 44 deletions.
  1. +74 −44 .github/workflows/pr-auto-milestone.yml
@@ -1,84 +1,130 @@
name: Auto set milestone on PR

# this job is scheduled to be run with proper rights
on:
pull_request:
types: opened
schedule:
- cron: '*/5 * * * *'

env:
QGIS_MAJOR_VERSION: 3

jobs:
test:
pr-without-milestones:
runs-on: ubuntu-latest
steps:
# list the tags and milestones
- uses: octokit/graphql-action@v2.x
id: list_tags_milestones
id: graphql_request
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) {
pullRequests(states: OPEN, last: 100) {
edges {
node {
name
id
title
labels(first: 100) {
edges {
node {
name
}
}
}
milestone {
number
}
baseRef {
name
}
}
}
}
milestones(orderBy: {field: CREATED_AT, direction: DESC}, first: 30) {
milestones(orderBy: {field: CREATED_AT, direction: DESC}, first: 50) {
edges {
node {
title
number
}
}
}
refs(refPrefix: "refs/tags/", orderBy: {field: TAG_COMMIT_DATE, direction: DESC}, first: 30) {
edges {
node {
name
}
}
}
}
}
owner: ${{ github.event.pull_request.base.repo.owner.login }}
repo: ${{ github.event.pull_request.base.repo.name }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# calculate the milestone and determine if it exists
- name: Get the latest release
id: calculate_milestone
# take the first unprocessed PR and determine if some remain
- name: Filter PR to check
id: extract_data
env:
JSON_DATA: ${{ steps.list_tags_milestones.outputs.data }}
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
JSON_DATA: ${{ steps.graphql_request.outputs.data }}
run: |
# get PRs without milestones
PRS_TO_PROCESS=$(echo "${JSON_DATA}" | jq '.repository.pullRequests.edges[] | select( .node.milestone.number | not ) | .node.number')
NUMBER_OF_PRS=$(echo "${PRS_TO_PROCESS}" | jq -s '. | length')
echo "NUMBER_OF_PRS: ${NUMBER_OF_PRS}"
# early exit
[[ ${NUMBER_OF_PRS} == 0 ]] && echo "::set-output name=has_milestone_to_set::false" && exit 0
# Take the first
PR_NUMBER=$(echo "${PRS_TO_PROCESS}" | jq -s 'first')
echo "PR_NUMBER: ${PR_NUMBER}"
# Not used for now
RE_RUN_JOB=$(echo "${JSON_DATA}" | jq -s '. | length > 1')
echo "RE_RUN_JOB: ${RE_RUN_JOB}"
# Get the base branch
BASE_BRANCH=$(echo "${JSON_DATA}" | jq ".repository.pullRequests.edges[] | select( .node.number == ${PR_NUMBER} ) | .baseRef.name"
echo "BASE_BRANCH: ${BASE_BRANCH}"
# 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}_(?<m>[0-9]+)_(?<p>.)\"; .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" )
MILESTONE_EXISTS=$(echo "${JSON_DATA}" | jq ".repository.milestones.edges[] | select( .node.title == \"${MILESTONE_TITLE}\" ) ")
echo "MILESTONE_EXISTS: ${MILESTONE_EXISTS}"
echo "::set-output name=has_milestone_to_set::true"
echo "::set-output name=pr_number::${PR_NUMBER}"
echo "::set-output name=milestone_title::${MILESTONE_TITLE}"
echo "::set-output name=milestone_number::${MILESTONE_NUMBER}"
echo "::set-output name=milestone_exists::${MILESTONE_EXISTS}"
# create the milestone if needed
- name: Create milestone
- name: Create milestone if needed
id: create_milestone
if: steps.calculate_milestone.outputs.milestone_exists == false
if: steps.extract_data.outputs.has_milestone_to_set && steps.extract_data.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 }}
route: POST /repos/qgis/QGIS/milestones
title: ${{ steps.extract_data.outputs.milestone_title }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- id: compute_milestone
# discard error on previous job (most likely user has no right to create milestone)
if: always()
# Get the milestone number
- name: Compute milestone number from existing or created
id: compute_milestone
if: steps.extract_data.outputs.has_milestone_to_set
name: compute the milestone number
env:
MILESTONE_NUMBER_EXISTING: ${{ steps.calculate_milestone.outputs.milestone_number }}
@@ -90,27 +136,11 @@ jobs:
# update PR with milestone
- name: update PR milestone
if: steps.extract_data.outputs.has_milestone_to_set
uses: octokit/request-action@v2.x
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
route: PATCH /repos/:owner/:repo/issues/:pull_number
pull_number: ${{ github.event.pull_request.number }}
route: PATCH /repos/qgis/QGIS/issues/:pull_number
pull_number: ${{ steps.extract_data.outputs.pr_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: ${{ secrets.GITHUB_TOKEN }}
with:
route: POST /repos/:owner/:repo/issues/:pull_number/comments
pull_number: ${{ github.event.pull_request.number }}
body: |
**Milestone: ${{ steps.compute_milestone.outputs.milestone_message }}**
(this could not be set by the bot due to missing rights)
owner: ${{ github.event.pull_request.base.repo.owner.login }}
repo: ${{ github.event.pull_request.base.repo.name }}

0 comments on commit 480095a

Please sign in to comment.
You can’t perform that action at this time.