From 5dfec1784b6d0f0515aac778501f13dd922a7800 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 27 Mar 2024 22:09:55 +1300 Subject: [PATCH 1/5] Add workflow to check for wrong branches --- .github/workflows/wrong-branch.yml | 95 ++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 .github/workflows/wrong-branch.yml diff --git a/.github/workflows/wrong-branch.yml b/.github/workflows/wrong-branch.yml new file mode 100644 index 0000000000..899ace7ab9 --- /dev/null +++ b/.github/workflows/wrong-branch.yml @@ -0,0 +1,95 @@ +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.' + }); + } + } From f5f46682803bd350775266c23f7bc1b88b90a87e Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 27 Mar 2024 22:21:12 +1300 Subject: [PATCH 2/5] Rework to use available data --- .github/workflows/wrong-branch.yml | 69 +++++++++++------------------- 1 file changed, 25 insertions(+), 44 deletions(-) diff --git a/.github/workflows/wrong-branch.yml b/.github/workflows/wrong-branch.yml index 899ace7ab9..03cebcb27f 100644 --- a/.github/workflows/wrong-branch.yml +++ b/.github/workflows/wrong-branch.yml @@ -9,67 +9,48 @@ jobs: name: Check runs-on: ubuntu-latest steps: - - name: Get labels - id: get_labels + - name: Add wrong-base-branch label uses: actions/github-script@v7.0.1 + if: contains(github.event.pull_request.labels.*.name, 'current') && contains(github.event.pull_request.labels.*.name, 'has-parent') with: script: | - const { data: labels } = await github.rest.issues.listLabelsOnIssue({ + await github.rest.issues.addLabels({ owner: context.repo.owner, repo: context.repo.repo, - issue_number: context.issue.number + issue_number: context.issue.number, + labels: ['wrong-base-branch'] }); - return labels.map(label => label.name); - - name: Add wrong-base-branch label + - name: Has wrong-base-branch label + if: contains(github.event.pull_request.labels.*.name, 'wrong-base-branch') uses: actions/github-script@v7.0.1 - env: - LABELS: ${{ steps.get_labels.outputs.result }} with: - script: | - const labels = JSON.parse(process.env.LABELS); + script: core.setFailed('This PR has the wrong-base-branch label'); - 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 + - if: failure() && contains(github.event.pull_request.labels.*.name, 'current') + name: Review PR to change to next 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'); - } + 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." + }); - - if: failure() - name: Review PR + - if: failure() && contains(github.event.pull_request.labels.*.name, 'next') + name: Review PR to change to current 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." + 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() From 2cd528128553b73d072d6d6dedffbe8b464bef1a Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 27 Mar 2024 22:30:48 +1300 Subject: [PATCH 3/5] Check if label was added in previous step --- .github/workflows/wrong-branch.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/wrong-branch.yml b/.github/workflows/wrong-branch.yml index 03cebcb27f..7f247ddb5a 100644 --- a/.github/workflows/wrong-branch.yml +++ b/.github/workflows/wrong-branch.yml @@ -10,6 +10,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Add wrong-base-branch label + id: add-wrong-base-branch-label uses: actions/github-script@v7.0.1 if: contains(github.event.pull_request.labels.*.name, 'current') && contains(github.event.pull_request.labels.*.name, 'has-parent') with: @@ -22,7 +23,7 @@ jobs: }); - name: Has wrong-base-branch label - if: contains(github.event.pull_request.labels.*.name, 'wrong-base-branch') + if: contains(github.event.pull_request.labels.*.name, 'wrong-base-branch') || steps.add-wrong-base-branch-label.outcome == 'success' uses: actions/github-script@v7.0.1 with: script: core.setFailed('This PR has the wrong-base-branch label'); From b9510003f0d616e0829e85dfae0d95f675178b65 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 27 Mar 2024 22:41:02 +1300 Subject: [PATCH 4/5] Fix --- .github/workflows/wrong-branch.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/wrong-branch.yml b/.github/workflows/wrong-branch.yml index 7f247ddb5a..1f06f3d142 100644 --- a/.github/workflows/wrong-branch.yml +++ b/.github/workflows/wrong-branch.yml @@ -52,7 +52,7 @@ jobs: repo: context.repo.repo, event: 'REQUEST_CHANGES', body: "Please target your PR to the 'current' branch and rebase." - } + }); - if: success() name: Dismiss review From be843f9ee3787890f9391a64c41da8965a30ca66 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 27 Mar 2024 22:41:10 +1300 Subject: [PATCH 5/5] Try remove label --- .github/workflows/wrong-branch.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/.github/workflows/wrong-branch.yml b/.github/workflows/wrong-branch.yml index 1f06f3d142..f56856255a 100644 --- a/.github/workflows/wrong-branch.yml +++ b/.github/workflows/wrong-branch.yml @@ -22,6 +22,25 @@ jobs: labels: ['wrong-base-branch'] }); + - name: Remove wrong-base-branch label + id: remove-wrong-base-branch-label + uses: actions/github-script@v7.0.1 + if: | + contains(github.event.pull_request.labels.*.name, 'wrong-base-branch') && + ( + (contains(github.event.pull_request.labels.*.name, 'current') && !contains(github.event.pull_request.labels.*.name, 'has-parent')) + || + (contains(github.event.pull_request.labels.*.name, 'next') && contains(github.event.pull_request.labels.*.name, 'has-parent')) + ) + with: + script: | + await github.rest.issues.removeLabel({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + name: 'wrong-base-branch' + }); + - name: Has wrong-base-branch label if: contains(github.event.pull_request.labels.*.name, 'wrong-base-branch') || steps.add-wrong-base-branch-label.outcome == 'success' uses: actions/github-script@v7.0.1