-
Notifications
You must be signed in to change notification settings - Fork 1
feat(auto-merge): create auto merge action MAPCO-9003 #104
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
0482082
ci(auto-merge): add action to automatically merge PRs with 'auto-merg…
92b0d19
ci: update action to include repository input and enhance authenticat…
361a38b
ci(auto-merge): update workflow to use dynamic PR number and improve …
51bb00d
ci(auto-merge): refine installation and authentication steps for gh
6b301bb
ci(auto-merge): wait for required checks to pass before merging PRs
4b1c8f5
feat(auto-merge): add auto-merge action for labeled PRs with passing …
e0cd093
Merge branch 'master' into auto-merge
michalby24 7ae4a68
ci(auto-merge): update test workflow to use dynamic repository input
967c805
Merge branch 'auto-merge' of https://github.com/MapColonies/shared-wo…
d6da8a4
docs(auto-merge): add README for auto-merge
d35edd1
ci: refine commit message format and update PR labels handling
c37435c
ci: add merge_method input to configure merge strategy
e897d92
fix: correct typo in installation message for gh
3928196
trigger CI
356f898
ci: add test action with labels for auto-merge workflow
ae3bcd9
ci: update checkout actions and repository references in auto-merge w…
cb6ef65
fix: update branch references from main to master in auto-merge workflow
30b60bb
ci: update GitHub token secret for auto-merge action
363b9f4
trigger CI
5c8fcd9
ci: update GitHub token secret from GITHUB_TOKEN to GH_PAT in auto-me…
d629dea
ci: add dependency on create_pr step for auto-merge action
d5e48f9
ci: add dependency on create_pr step for auto-merge action
927a018
ci: update GitHub token secret to GH_PAT_TEST in auto-merge workflow
4d9cfed
trigger CI
0a7aa15
trigger CI
b3d7dcf
ci: update GitHub token secret from GH_PAT_TEST to GH_PAT in auto-mer…
30fbb4b
trigger CI
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| name: Test auto-merge action | ||
|
|
||
| on: | ||
| pull_request: | ||
| paths: | ||
| - "actions/auto-merge/**" | ||
|
|
||
| jobs: | ||
| test-action: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout this repo | ||
| uses: actions/checkout@v6 | ||
|
|
||
| - name: Run auto-merge action | ||
| uses: ./actions/auto-merge | ||
| with: | ||
| pr_number: ${{ github.event.pull_request.number }} | ||
| github_token: ${{ secrets.GH_PAT }} | ||
| repository: ${{ github.repository }} | ||
|
|
||
| test-action-with-labels: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout this repo | ||
| uses: actions/checkout@v6 | ||
|
|
||
| - name: Checkout test repo | ||
| uses: actions/checkout@v6 | ||
| with: | ||
| repository: MapColonies/test-site | ||
| token: ${{ secrets.GH_PAT }} | ||
| ref: master | ||
| persist-credentials: false | ||
|
|
||
| - name: Create empty commit in test repo | ||
| run: | | ||
| git config user.name "GitHub Actions Bot" | ||
| git config user.email "actions@github.com" | ||
| git commit --allow-empty -m "chore: empty PR for auto-merge test (${GITHUB_SHA})" | ||
|
|
||
| - name: Create Pull Request | ||
| id: create_pr | ||
| uses: peter-evans/create-pull-request@v6 | ||
| with: | ||
| token: ${{ secrets.GH_PAT }} | ||
| repository: MapColonies/test-site | ||
| branch: "auto-merge-e2e-${{ github.sha }}" | ||
| base: master | ||
| title: 'chore: auto-merge e2e test' | ||
| body: | | ||
| Trigger commit: https://github.com/${{ github.repository }}/commit/${{ github.sha }} | ||
|
|
||
| Empty commit PR for auto-merge test. | ||
| labels: auto-merge | ||
|
|
||
| - name: Run auto-merge action | ||
| uses: ./actions/auto-merge | ||
| with: | ||
| pr_number: ${{ steps.create_pr.outputs.pull-request-number }} | ||
| github_token: ${{ secrets.GH_PAT }} | ||
| repository: MapColonies/test-site |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Auto-merge PR by label | ||
|
|
||
| Checks if a pull request has the `auto-merge` label, waits for all checks to pass, and then merges the PR. | ||
|
|
||
| --- | ||
|
|
||
| ## ✨ What It Does | ||
|
|
||
| - Verifies the PR has the `auto-merge` label | ||
| - Waits for all PR checks to complete and pass | ||
| - Merges the PR with a squash strategy | ||
| - Skips merge if the label is missing or checks fail | ||
|
|
||
| --- | ||
|
|
||
| ## 🛠 Inputs | ||
|
|
||
| | Name | Description | Required | Default | | ||
| |------|-------------|----------|---------| | ||
| | `pr_number` | Pull Request number to check and merge | ✅ Yes | | | ||
| | `github_token` | GitHub token with permissions to merge PRs | ✅ Yes | | | ||
| | `repository` | Target repository in `owner/repo` format (typically `${{ github.repository }}`) | ✅ Yes | | | ||
| | `merge_label` | Label that triggers auto-merge (if present on the PR) | ❌ No | `auto-merge` | | ||
| | `merge_method` | Merge method to use (merge, squash, rebase). Defaults to `squash`. | ❌ No | `squash` | | ||
|
|
||
|
|
||
| ## 🚀 Usage | ||
|
|
||
| <!-- x-release-please-start-version --> | ||
|
|
||
| ```yaml | ||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
|
|
||
| steps: | ||
| - name: Auto-merge PR by label | ||
| uses: MapColonies/shared-workflows/actions/auto-merge@auto-merge-v0.1.0 | ||
| with: | ||
| pr_number: ${{ github.event.pull_request.number }} | ||
| github_token: ${{ secrets.GH_PAT }} | ||
| repository: ${{ github.repository }} | ||
| ``` | ||
|
|
||
| <!-- x-release-please-end-version --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| name: "Auto-merge PR by label" | ||
| description: "Checks if a PR has the 'auto-merge' label and merges it using gh if present" | ||
|
|
||
| inputs: | ||
alex131290 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pr_number: | ||
| description: "Pull Request number to check and merge" | ||
| required: true | ||
| github_token: | ||
| description: "GitHub token with permissions to merge PRs (repo scope)" | ||
| required: true | ||
| repository: | ||
| description: "Target repository (owner/repo). Defaults to the current repository." | ||
| required: true | ||
| merge_label: | ||
| description: "Label that triggers auto-merge" | ||
| required: false | ||
| default: "auto-merge" | ||
| merge_method: | ||
| description: "Merge method to use (merge, squash, rebase). Defaults to 'squash'." | ||
| required: false | ||
| default: "squash" | ||
|
|
||
| runs: | ||
| using: "composite" | ||
| steps: | ||
| - name: Install gh (for act) | ||
alex131290 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| shell: bash | ||
| run: | | ||
| if command -v gh >/dev/null; then | ||
| echo "gh already instlled"; exit 0; | ||
| fi | ||
| sudo apt-get update -y | ||
| sudo apt-get install -y git curl | ||
| curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg \ | ||
| | sudo tee /usr/share/keyrings/githubcli-archive-keyring.gpg >/dev/null | ||
| echo "deb [signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \ | ||
| | sudo tee /etc/apt/sources.list.d/github-cli.list >/dev/null | ||
| sudo apt-get update -y | ||
| sudo apt-get install -y gh | ||
|
|
||
| - name: Authenticate gh with github_token | ||
alex131290 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| shell: bash | ||
| env: | ||
| GITHUB_TOKEN: ${{ inputs.github_token }} | ||
| GH_TOKEN: ${{ inputs.github_token }} | ||
| run: | | ||
| set -euo pipefail | ||
| # If GH_TOKEN/GITHUB_TOKEN is set, gh uses it automatically; avoid forcing login. | ||
| if gh auth status >/dev/null 2>&1; then | ||
| echo "gh is already authenticated" | ||
| else | ||
| # Probe API to confirm token works; this avoids storing credentials when env vars are set. | ||
| if gh api user >/dev/null 2>&1; then | ||
| echo "gh authenticated via environment token" | ||
| else | ||
| echo "Environment token appears invalid; attempting login via --with-token" | ||
| echo -n "$GITHUB_TOKEN" | gh auth login --with-token | ||
| gh auth status | ||
| fi | ||
| fi | ||
|
|
||
| - name: Check for '${{ inputs.merge_label }}' label | ||
| id: check_label | ||
| shell: bash | ||
| env: | ||
| GH_TOKEN: ${{ inputs.github_token }} | ||
| run: | | ||
| set -euo pipefail | ||
| labels=$(gh pr view "${{ inputs.pr_number }}" --repo "${{ inputs.repository }}" --json labels -q '.labels[].name') | ||
|
|
||
| if grep -qx "${{ inputs.merge_label }}" <<< "$labels"; then | ||
| echo "has_label=true" >> "$GITHUB_OUTPUT" | ||
| else | ||
| echo "has_label=false" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| - name: Wait for PR checks | ||
| id: wait_checks | ||
| if: steps.check_label.outputs.has_label == 'true' | ||
| shell: bash | ||
| env: | ||
| GH_TOKEN: ${{ inputs.github_token }} | ||
| run: | | ||
| set -euo pipefail | ||
| echo "Waiting for all checks on PR #${{ inputs.pr_number }} to complete" | ||
| if gh pr checks "${{ inputs.pr_number }}" \ | ||
| --repo "${{ inputs.repository }}" \ | ||
| --watch; then | ||
| echo "All checks completed." | ||
| echo "passed=true" >> "$GITHUB_OUTPUT" | ||
| else | ||
| echo "Checks did not complete successfully." | ||
| echo "passed=false" >> "$GITHUB_OUTPUT" | ||
| exit 1 | ||
| fi | ||
|
|
||
| - name: Merge PR if labeled | ||
| if: steps.check_label.outputs.has_label == 'true' && steps.wait_checks.outputs.passed == 'true' | ||
| shell: bash | ||
| env: | ||
| GH_TOKEN: ${{ inputs.github_token }} | ||
| run: | | ||
| set -euo pipefail | ||
| echo "Merging PR #${{ inputs.pr_number }} in '${{ inputs.repository }}' (label '${{ inputs.merge_label }}' found)" | ||
| gh pr merge "${{ inputs.pr_number }}" \ | ||
alex131290 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| --repo "${{ inputs.repository }}" \ | ||
| --${{ inputs.merge_method }} \ | ||
|
|
||
| - name: Skip merge (label missing) | ||
| if: steps.check_label.outputs.has_label != 'true' | ||
| shell: bash | ||
| run: | | ||
| echo "Skipping merge: PR #${{ inputs.pr_number }} in repo '${{ inputs.repository }}' does not have label '${{ inputs.merge_label }}'" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.