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

Automerge PRs action #1823

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
60 changes: 60 additions & 0 deletions .github/workflows/automerge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: automerge
on:
schedule:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem: Scheduled runs only happen against the default branch, not against PRs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, that's a boomer... So the only way would be for a dedicated action to go through open PRs and see if any need to be triggered to be reran or do you see some other way?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean "merged" instead of "reran"?

Aside from that, yes, that's pretty much the only way.

- cron: '4 5 * * *' # Runs at 04:05 (random) UTC every day
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the expression is for 5:04.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

;-) thanks

Suggested change
- cron: '4 5 * * *' # Runs at 04:05 (random) UTC every day
- cron: '4 5 * * *' # Runs at 05:04 (random) UTC every day

pull_request:
types:
- labeled
- unlabeled
- synchronize
- opened
- edited
- ready_for_review
- reopened
- unlocked
pull_request_review:
types:
- submitted
- dismissed
check_suite:
types:
- completed
status: {}
jobs:
automerge:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- uses: actions/checkout@v3
- uses: actions/checkout@v4

with:
fetch-depth: 0 # Ensures all history is fetched

- name: Get date of the last commit
id: last_commit_date
run: |
LAST_COMMIT_DATE=$(git log -1 --format=%cI)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be faked by the Git user. It'd be better to fetch the head.pushed_at timestamp for the PR from the GitHub API.

echo "Last commit date: $LAST_COMMIT_DATE"
echo "::set-output name=last_commit_date::$LAST_COMMIT_DATE"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "::set-output name=last_commit_date::$LAST_COMMIT_DATE"
echo "last_commit_date=$LAST_COMMIT_DATE" >> "$GITHUB_OUTPUT"

The ::set-output method is deprecated; you need to write to $GITHUB_OUTPUT now.


- name: Calculate days since last commit
id: days_since_last_commit
run: |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add shell: python for this.

from datetime import datetime
last_commit_date = "${{ steps.last_commit_date.outputs.last_commit_date }}"
last_commit_datetime = datetime.strptime(last_commit_date, "%Y-%m-%dT%H:%M:%S%z")
now = datetime.now(last_commit_datetime.tzinfo)
delta = now - last_commit_datetime
days_since = delta.days
echo "Days since last commit: $days_since"
echo "::set-output name=days_since::$days_since"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "::set-output name=days_since::$days_since"
echo "days_since=$days_since" >> "$GITHUB_OUTPUT"


- id: automerge
name: automerge
uses: "pascalgn/[email protected]"
if: steps.days_since_last_commit.outputs.days_since > 5 && ((github.event_name == 'pull_request' && github.event.action == 'labeled') || github.event_name == 'schedule')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you add some sort of check that tests have passed, or does the action take care of that?

env:
# TODO: temporarily disabled to see if all above works
# GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
MERGE_LABELS: automerge
MERGE_METHOD_LABEL_REQUIRED: true
MERGE_COMMIT_MESSAGE: automatic
MERGE_REQUIRED_APPROVALS: 3
Loading