Add workflow to check for wrong branches #1
This file contains 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
name: Wrong Branch | |
on: | |
pull_request: | |
types: [labeled, unlabeled] | |
jobs: | |
check: | |
name: Check | |
runs-on: ubuntu-latest | |
steps: | |
- name: Get labels | |
id: get_labels | |
uses: actions/github-script@v7.0.1 | |
with: | |
script: | | |
const { data: labels } = await github.rest.issues.listLabelsOnIssue({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number | |
}); | |
return labels.map(label => label.name); | |
- name: Add wrong-base-branch label | |
uses: actions/github-script@v7.0.1 | |
env: | |
LABELS: ${{ steps.get_labels.outputs.result }} | |
with: | |
script: | | |
const labels = JSON.parse(process.env.LABELS); | |
const current = labels.includes('current'); | |
const hasParent = labels.includes('has-parent'); | |
if (current && hasParent) { | |
await github.rest.issues.addLabels({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
labels: ['wrong-base-branch'] | |
}); | |
} | |
- name: Check for wrong-base-branch label | |
uses: actions/github-script@v7.0.1 | |
with: | |
script: | | |
const labels = JSON.parse(process.env.LABELS); | |
if (labels.includes('wrong-base-branch')) { | |
core.setFailed('This PR has the wrong-base-branch label'); | |
} | |
- if: failure() | |
name: Review PR | |
uses: actions/github-script@v7.0.1 | |
with: | |
script: | | |
const labels = JSON.parse(process.env.LABELS); | |
if (labels.includes("current")) { | |
await github.rest.pulls.createReview({ | |
pull_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
event: 'REQUEST_CHANGES', | |
body: "As this is a feature matched with a PR in https://github.com/esphome/esphome, please target your PR to the 'next' branch and rebase." | |
}); | |
} else { | |
await github.rest.pulls.createReview({ | |
pull_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
event: 'REQUEST_CHANGES', | |
body: "Please target your PR to the 'current' branch and rebase." | |
} | |
- if: success() | |
name: Dismiss review | |
uses: actions/github-script@v7.0.1 | |
with: | |
script: | | |
let reviews = await github.rest.pulls.listReviews({ | |
pull_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo | |
}); | |
for (let review of reviews.data) { | |
if (review.user.login === 'github-actions[bot]' && review.state === 'CHANGES_REQUESTED') { | |
await github.rest.pulls.dismissReview({ | |
pull_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
review_id: review.id, | |
message: 'Target branch is correct.' | |
}); | |
} | |
} |