2023-07-20 03:19:35 -04:00
|
|
|
|
name: 'Combine Dependabot PRs'
|
|
|
|
|
|
|
|
|
|
# Controls when the action will run - in this case triggered manually
|
|
|
|
|
on:
|
|
|
|
|
workflow_dispatch:
|
|
|
|
|
inputs:
|
|
|
|
|
branchPrefix:
|
|
|
|
|
description: 'Branch prefix to find combinable PRs based on'
|
|
|
|
|
required: true
|
|
|
|
|
default: 'dependabot'
|
|
|
|
|
mustBeGreen:
|
|
|
|
|
description: 'Only combine PRs that are green (status is success). Set to false if repo does not run checks'
|
|
|
|
|
type: boolean
|
|
|
|
|
required: true
|
2023-07-20 03:25:11 -04:00
|
|
|
|
default: false
|
2023-07-20 03:19:35 -04:00
|
|
|
|
combineBranchName:
|
|
|
|
|
description: 'Name of the branch to combine PRs into'
|
|
|
|
|
required: true
|
|
|
|
|
default: 'combined-prs'
|
|
|
|
|
ignoreLabel:
|
|
|
|
|
description: 'Exclude PRs with this label'
|
|
|
|
|
required: true
|
|
|
|
|
default: 'nocombine'
|
|
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
|
combine-prs:
|
2024-09-15 23:42:30 -04:00
|
|
|
|
runs-on: ubuntu-latest
|
2023-07-20 03:19:35 -04:00
|
|
|
|
|
|
|
|
|
steps:
|
|
|
|
|
- uses: actions/github-script@v6
|
|
|
|
|
id: create-combined-pr
|
|
|
|
|
name: Create Combined PR
|
|
|
|
|
with:
|
|
|
|
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
|
|
|
|
script: |
|
|
|
|
|
const pulls = await github.paginate('GET /repos/:owner/:repo/pulls', {
|
|
|
|
|
owner: context.repo.owner,
|
|
|
|
|
repo: context.repo.repo
|
|
|
|
|
});
|
|
|
|
|
let branchesAndPRStrings = [];
|
|
|
|
|
let baseBranch = null;
|
|
|
|
|
let baseBranchSHA = null;
|
|
|
|
|
for (const pull of pulls) {
|
|
|
|
|
const branch = pull['head']['ref'];
|
|
|
|
|
console.log('Pull for branch: ' + branch);
|
|
|
|
|
if (branch.startsWith('${{ github.event.inputs.branchPrefix }}')) {
|
|
|
|
|
console.log('Branch matched prefix: ' + branch);
|
|
|
|
|
let statusOK = true;
|
|
|
|
|
if(${{ github.event.inputs.mustBeGreen }}) {
|
|
|
|
|
console.log('Checking green status: ' + branch);
|
|
|
|
|
const stateQuery = `query($owner: String!, $repo: String!, $pull_number: Int!) {
|
|
|
|
|
repository(owner: $owner, name: $repo) {
|
|
|
|
|
pullRequest(number:$pull_number) {
|
|
|
|
|
commits(last: 1) {
|
|
|
|
|
nodes {
|
|
|
|
|
commit {
|
|
|
|
|
statusCheckRollup {
|
|
|
|
|
state
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}`
|
|
|
|
|
const vars = {
|
|
|
|
|
owner: context.repo.owner,
|
|
|
|
|
repo: context.repo.repo,
|
|
|
|
|
pull_number: pull['number']
|
|
|
|
|
};
|
|
|
|
|
const result = await github.graphql(stateQuery, vars);
|
|
|
|
|
const [{ commit }] = result.repository.pullRequest.commits.nodes;
|
|
|
|
|
const state = commit.statusCheckRollup.state
|
|
|
|
|
console.log('Validating status: ' + state);
|
|
|
|
|
if(state != 'SUCCESS') {
|
|
|
|
|
console.log('Discarding ' + branch + ' with status ' + state);
|
|
|
|
|
statusOK = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log('Checking labels: ' + branch);
|
|
|
|
|
const labels = pull['labels'];
|
|
|
|
|
for(const label of labels) {
|
|
|
|
|
const labelName = label['name'];
|
|
|
|
|
console.log('Checking label: ' + labelName);
|
|
|
|
|
if(labelName == '${{ github.event.inputs.ignoreLabel }}') {
|
|
|
|
|
console.log('Discarding ' + branch + ' with label ' + labelName);
|
|
|
|
|
statusOK = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (statusOK) {
|
|
|
|
|
console.log('Adding branch to array: ' + branch);
|
|
|
|
|
const prString = '#' + pull['number'] + ' ' + pull['title'];
|
|
|
|
|
branchesAndPRStrings.push({ branch, prString });
|
|
|
|
|
baseBranch = pull['base']['ref'];
|
|
|
|
|
baseBranchSHA = pull['base']['sha'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (branchesAndPRStrings.length == 0) {
|
|
|
|
|
core.setFailed('No PRs/branches matched criteria');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await github.rest.git.createRef({
|
|
|
|
|
owner: context.repo.owner,
|
|
|
|
|
repo: context.repo.repo,
|
|
|
|
|
ref: 'refs/heads/' + '${{ github.event.inputs.combineBranchName }}',
|
|
|
|
|
sha: baseBranchSHA
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
core.setFailed('Failed to create combined branch - maybe a branch by that name already exists?');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-01-06 04:49:35 -05:00
|
|
|
|
|
2023-07-20 03:19:35 -04:00
|
|
|
|
let combinedPRs = [];
|
|
|
|
|
let mergeFailedPRs = [];
|
|
|
|
|
for(const { branch, prString } of branchesAndPRStrings) {
|
|
|
|
|
try {
|
|
|
|
|
await github.rest.repos.merge({
|
|
|
|
|
owner: context.repo.owner,
|
|
|
|
|
repo: context.repo.repo,
|
|
|
|
|
base: '${{ github.event.inputs.combineBranchName }}',
|
|
|
|
|
head: branch,
|
|
|
|
|
});
|
|
|
|
|
console.log('Merged branch ' + branch);
|
|
|
|
|
combinedPRs.push(prString);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('Failed to merge branch ' + branch);
|
|
|
|
|
mergeFailedPRs.push(prString);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-06 04:49:35 -05:00
|
|
|
|
|
2023-07-20 03:19:35 -04:00
|
|
|
|
console.log('Creating combined PR');
|
|
|
|
|
const combinedPRsString = combinedPRs.join('\n');
|
2023-07-20 03:29:16 -04:00
|
|
|
|
let body = '✅ This PR was created by the Combine Dependabot PRs action by combining the following PRs:\n' + combinedPRsString;
|
2023-07-20 03:19:35 -04:00
|
|
|
|
if(mergeFailedPRs.length > 0) {
|
|
|
|
|
const mergeFailedPRsString = mergeFailedPRs.join('\n');
|
|
|
|
|
body += '\n\n⚠️ The following PRs were left out due to merge conflicts:\n' + mergeFailedPRsString
|
|
|
|
|
}
|
|
|
|
|
await github.rest.pulls.create({
|
|
|
|
|
owner: context.repo.owner,
|
|
|
|
|
repo: context.repo.repo,
|
2023-07-20 03:29:16 -04:00
|
|
|
|
title: 'Combined Dependabot PR',
|
2023-07-20 03:19:35 -04:00
|
|
|
|
head: '${{ github.event.inputs.combineBranchName }}',
|
|
|
|
|
base: baseBranch,
|
|
|
|
|
body: body
|
|
|
|
|
});
|