Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR Automation - first step #111

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions .github/workflows/pr-automation.yml
Original file line number Diff line number Diff line change
@@ -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);
}
Loading