|
| 1 | +name: 'Combine PRs' |
| 2 | +# |
| 3 | +# Source: |
| 4 | +# https://dev.to/denvercoder1/keeping-your-dependencies-updated-automatically-with-dependabot-299g |
| 5 | +# https://github.com/hrvey/combine-prs-workflow/blob/master/combine-prs.yml |
| 6 | +# |
| 7 | +# Controls when the action will run - in this case triggered manually |
| 8 | +on: |
| 9 | + workflow_dispatch: |
| 10 | + inputs: |
| 11 | + branchPrefix: |
| 12 | + description: 'Branch prefix to find combinable PRs based on' |
| 13 | + required: true |
| 14 | + default: 'dependabot' |
| 15 | + mustBeGreen: |
| 16 | + description: 'Only combine PRs that are green (status is success)' |
| 17 | + required: true |
| 18 | + default: true |
| 19 | + combineBranchName: |
| 20 | + description: 'Name of the branch to combine PRs into' |
| 21 | + required: true |
| 22 | + default: 'combine-prs-branch' |
| 23 | + ignoreLabel: |
| 24 | + description: 'Exclude PRs with this label' |
| 25 | + required: true |
| 26 | + default: 'nocombine' |
| 27 | + |
| 28 | +# A workflow run is made up of one or more jobs that can run sequentially or in parallel |
| 29 | +jobs: |
| 30 | + # This workflow contains a single job called "combine-prs" |
| 31 | + combine-prs: |
| 32 | + # The type of runner that the job will run on |
| 33 | + runs-on: ubuntu-latest |
| 34 | + |
| 35 | + # Steps represent a sequence of tasks that will be executed as part of the job |
| 36 | + steps: |
| 37 | + - uses: actions/github-script@v3 |
| 38 | + id: fetch-branch-names |
| 39 | + name: Fetch branch names |
| 40 | + with: |
| 41 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 42 | + script: | |
| 43 | + const pulls = await github.paginate('GET /repos/:owner/:repo/pulls', { |
| 44 | + owner: context.repo.owner, |
| 45 | + repo: context.repo.repo |
| 46 | + }); |
| 47 | + branches = []; |
| 48 | + prs = []; |
| 49 | + base_branch = null; |
| 50 | + for (const pull of pulls) { |
| 51 | + const branch = pull['head']['ref']; |
| 52 | + console.log('Pull for branch: ' + branch); |
| 53 | + if (branch.startsWith('${{ github.event.inputs.branchPrefix }}')) { |
| 54 | + console.log('Branch matched: ' + branch); |
| 55 | + statusOK = true; |
| 56 | + if(${{ github.event.inputs.mustBeGreen }}) { |
| 57 | + console.log('Checking green status: ' + branch); |
| 58 | + const statuses = await github.paginate('GET /repos/{owner}/{repo}/commits/{ref}/status', { |
| 59 | + owner: context.repo.owner, |
| 60 | + repo: context.repo.repo, |
| 61 | + ref: branch |
| 62 | + }); |
| 63 | + if(statuses.length > 0) { |
| 64 | + const latest_status = statuses[0]['state']; |
| 65 | + console.log('Validating status: ' + latest_status); |
| 66 | + if(latest_status != 'success') { |
| 67 | + console.log('Discarding ' + branch + ' with status ' + latest_status); |
| 68 | + statusOK = false; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + console.log('Checking labels: ' + branch); |
| 73 | + const labels = pull['labels']; |
| 74 | + for(const label of labels) { |
| 75 | + const labelName = label['name']; |
| 76 | + console.log('Checking label: ' + labelName); |
| 77 | + if(labelName == '${{ github.event.inputs.ignoreLabel }}') { |
| 78 | + console.log('Discarding ' + branch + ' with label ' + labelName); |
| 79 | + statusOK = false; |
| 80 | + } |
| 81 | + } |
| 82 | + if (statusOK) { |
| 83 | + console.log('Adding branch to array: ' + branch); |
| 84 | + branches.push(branch); |
| 85 | + prs.push('#' + pull['number'] + ' ' + pull['title']); |
| 86 | + base_branch = pull['base']['ref']; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +
|
| 91 | + if (branches.length == 0) { |
| 92 | + core.setFailed('No PRs/branches matched criteria'); |
| 93 | + return; |
| 94 | + } |
| 95 | +
|
| 96 | + core.setOutput('base-branch', base_branch); |
| 97 | + core.setOutput('prs-string', prs.join('\n')); |
| 98 | +
|
| 99 | + combined = branches.join(' ') |
| 100 | + console.log('Combined: ' + combined); |
| 101 | + return combined |
| 102 | + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it |
| 103 | + |
| 104 | + with: |
| 105 | + fetch-depth: 0 |
| 106 | + # Creates a branch with other PR branches merged together |
| 107 | + - name: Created combined branch |
| 108 | + env: |
| 109 | + BASE_BRANCH: ${{ steps.fetch-branch-names.outputs.base-branch }} |
| 110 | + BRANCHES_TO_COMBINE: ${{ steps.fetch-branch-names.outputs.result }} |
| 111 | + COMBINE_BRANCH_NAME: ${{ github.event.inputs.combineBranchName }} |
| 112 | + run: | |
| 113 | + echo "$BRANCHES_TO_COMBINE" |
| 114 | + sourcebranches="${BRANCHES_TO_COMBINE%\"}" |
| 115 | + sourcebranches="${sourcebranches#\"}" |
| 116 | +
|
| 117 | + basebranch="${BASE_BRANCH%\"}" |
| 118 | + basebranch="${basebranch#\"}" |
| 119 | +
|
| 120 | + git config pull.rebase false |
| 121 | + git config user.name github-actions |
| 122 | + git config user.email [email protected] |
| 123 | +
|
| 124 | + git branch $COMBINE_BRANCH_NAME $basebranch |
| 125 | + git checkout $COMBINE_BRANCH_NAME |
| 126 | + git pull origin $sourcebranches --no-edit |
| 127 | + git push origin $COMBINE_BRANCH_NAME |
| 128 | + # Creates a PR with the new combined branch |
| 129 | + - uses: actions/github-script@v3 |
| 130 | + name: Create Combined Pull Request |
| 131 | + env: |
| 132 | + PRS_STRING: ${{ steps.fetch-branch-names.outputs.prs-string }} |
| 133 | + with: |
| 134 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 135 | + script: | |
| 136 | + const prString = process.env.PRS_STRING; |
| 137 | + const body = 'This PR was created by the Combine PRs action by combining the following PRs:\n' + prString; |
| 138 | + await github.pulls.create({ |
| 139 | + owner: context.repo.owner, |
| 140 | + repo: context.repo.repo, |
| 141 | + title: 'Combined PR', |
| 142 | + head: '${{ github.event.inputs.combineBranchName }}', |
| 143 | + base: '${{ steps.fetch-branch-names.outputs.base-branch }}', |
| 144 | + body: body |
| 145 | + }); |
0 commit comments