Add Automerge workflow (#605)

* Add Automerge workflow

The automerge workflow will check for complete and approved pull requests and will merge them automatically.

Sources:
- For the python script to execute the merge: https://raw.githubusercontent.com/dapr/dapr/master/.github/scripts/automerge.py (only changed the label from "automerge" to "auto-merge")
- https://raw.githubusercontent.com/dapr/dapr/master/.github/workflows/dapr-bot-schedule.yml (only copied the automerge part, not the prune_stale part as that was not part of the issue.

Resolves https://github.com/dapr/java-sdk/issues/603

* Update automerge-bot.yml

* Update automerge-bot.yml

Co-authored-by: Artur Souza <artursouza.ms@outlook.com>
This commit is contained in:
Tom Cools 2021-08-21 03:27:33 +02:00 committed by GitHub
parent 34bf149a1a
commit a9397abce0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 0 deletions

60
.github/scripts/automerge.py vendored Normal file
View File

@ -0,0 +1,60 @@
# ------------------------------------------------------------
# Copyright (c) Microsoft Corporation and Dapr Contributors.
# Licensed under the MIT License.
# ------------------------------------------------------------
# This script automerges PRs
import os
from github import Github
g = Github(os.getenv("GITHUB_TOKEN"))
repo = g.get_repo(os.getenv("GITHUB_REPOSITORY"))
maintainers = [m.strip() for m in os.getenv("MAINTAINERS").split(',')]
def fetch_pulls(mergeable_state):
return [pr for pr in repo.get_pulls(state='open', sort='created') \
if pr.mergeable_state == mergeable_state and 'auto-merge' in [l.name for l in pr.labels]]
def is_approved(pr):
approvers = [r.user.login for r in pr.get_reviews() if r.state == 'APPROVED' and r.user.login in maintainers]
return len([a for a in approvers if repo.get_collaborator_permission(a) in ['admin', 'write']]) > 0
# First, find a PR that can be merged
pulls = fetch_pulls('clean')
print(f"Detected {len(pulls)} open pull requests in {repo.name} to be automerged.")
merged = False
for pr in pulls:
if is_approved(pr):
# Merge only one PR per run.
print(f"Merging PR {pr.html_url}")
try:
pr.merge(merge_method='squash')
merged = True
break
except:
print(f"Failed to merge PR {pr.html_url}")
if len(pulls) > 0 and not merged:
print("No PR was automerged.")
# Now, update all PRs that are behind.
pulls = fetch_pulls('behind')
print(f"Detected {len(pulls)} open pull requests in {repo.name} to be updated.")
for pr in pulls:
if is_approved(pr):
# Update all PRs since there is no guarantee they will all pass.
print(f"Updating PR {pr.html_url}")
try:
pr.update_branch()
except:
print(f"Failed to update PR {pr.html_url}")
pulls = fetch_pulls('dirty')
print(f"Detected {len(pulls)} open pull requests in {repo.name} to be automerged but are in dirty state.")
for pr in pulls:
print(f"PR is in dirty state: {pr.html_url}")
print("Done.")

26
.github/workflows/automerge-bot.yml vendored Normal file
View File

@ -0,0 +1,26 @@
# ------------------------------------------------------------
# Copyright (c) Microsoft Corporation and Dapr Contributors.
# Licensed under the MIT License.
# ------------------------------------------------------------
name: dapr-java-sdk-automerge-bot
on:
schedule:
- cron: '*/30 * * * *'
workflow_dispatch:
jobs:
automerge:
if: github.repository_owner == 'dapr'
name: Automerge and update PRs.
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Install dependencies
run: pip install PyGithub
- name: Automerge and update
env:
MAINTAINERS: artursouza,mukundansundar
GITHUB_TOKEN: ${{ secrets.DAPR_BOT_TOKEN }}
run: python ./.github/scripts/automerge.py