From e5c88e6aa6b17f41a83589384f5e0a2d70ff11d0 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Mon, 23 Dec 2024 00:07:11 +0100 Subject: [PATCH] PR Automation - first step --- .github/workflows/pr-automation.yml | 165 ++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 .github/workflows/pr-automation.yml diff --git a/.github/workflows/pr-automation.yml b/.github/workflows/pr-automation.yml new file mode 100644 index 0000000..6fa030f --- /dev/null +++ b/.github/workflows/pr-automation.yml @@ -0,0 +1,165 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +name: PR Automation +on: + workflow_call: + inputs: + default-label: + description: The default label to add to PR + required: false + default: 'maintenance' + type: string + +# To use we need: +# +# on: +# pull_request: +# types: +# - closed +# - unlabeled +# - demilestoned +# pull_request_review: +# types: +# - submitted +#jobs: +# pr-automation: +# name: PR Automation +# uses: apache/maven-gh-actions-shared/.github/workflows/pr-automation.yml@v4 + + + +# allow single build per branch or PR +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +# clare all permissions for GITHUB_TOKEN +permissions: {} + +jobs: + # read current PR approval status + review-decision: + if: github.event.pull_request.draft == false + runs-on: ubuntu-latest + outputs: + approved: ${{ steps.review-decision.outputs.result }} + + steps: + - name: Get Review decision + id: review-decision + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7 + with: + result-encoding: string + script: | + const query = `query($owner:String!, $name:String!, $number:Int!) { + repository(name: $name, owner: $owner) { + pullRequest(number: $number) { + reviewDecision, merged + } + } + }`; + + const variables = { + owner: context.repo.owner, + name: context.repo.repo, + number: context.issue.number + } + + const result = await github.graphql(query, variables) + console.log(result) + + return result.repository.pullRequest.reviewDecision == 'APPROVED' || result.repository.pullRequest.merged + - name: Result + run: echo "${{ steps.review-decision.outputs.result }}" + + # check PR milestone - if not set - update with current opened milestone + milestone: + permissions: + issues: write + pull-requests: write + + needs: review-decision + if: ( needs.review-decision.outputs.approved || github.event.action == 'demilestoned' ) && !github.event.pull_request.milestone + runs-on: ubuntu-latest + steps: + - name: Update milestone + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7 + with: + script: | + const milestones = await github.rest.issues.listMilestones({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open' + }); + + console.log(milestones); + + if ( milestones.data.length == 0 ) { + throw new Error('There are no open milestones ... please create one') + } + + if ( milestones.data.length > 1 ) { + throw new Error('There are more then oen open milestones ... please choose manually') + } + + console.log('Set milestone to: ' + milestones.data[0].title); + + const result = await github.rest.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + milestone: milestones.data[0].number + }); + + console.log(result); + + # check PR labels - if is empty list add one default + labels: + permissions: + issues: write + pull-requests: write + + needs: review-decision + if: ( needs.review-decision.outputs.approved || github.event.action == 'unlabeled' ) && toJSON(github.event.pull_request.labels) == '[]' + runs-on: ubuntu-latest + steps: + + - name: Set default label + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7 + env: + DEFAULT_LABEL: ${{ inputs.default-label }} + with: + script: | + + console.log(context.issue.labels); + + if ( !context.issue.labels?.length ) { + + if ( !process.env.DEFAULT_LABEL ) { + throw new ERROR('There are no labels on PR and default label is not set'); + } + + const result = await github.rest.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + labels: [ process.env.DEFAULT_LABEL ] + }); + + console.log(result); + }