Skip to content
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

Wait for Required Checks on Protected Branches #269

Open
wants to merge 4 commits into
base: releases/v5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This action will bump version, tag commit and generate a changelog with conventi
- **Optional** `pre-release`: Marks the release as pre-release. Default `false`.
- **Optional** `pre-release-identifier`: The identifier to use for the pre-release. Default `rc`.
- **Optional** `skip-bump`: Prevents the action from bumping the version.
- **Optional** `is-protected-branch`: If set to `true`, the action will wait for required checks to pass before proceeding. Default `false`.

### Presets
This action comes pre-compiled with the `angular` (default) and `conventionalcommits`, if you wish to use an other preset
Expand Down
31 changes: 31 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const core = require('@actions/core')
const conventionalRecommendedBump = require('conventional-recommended-bump')
const path = require('path')
const github = require('@actions/github')

const getVersioning = require('./version')
const git = require('./helpers/git')
Expand Down Expand Up @@ -54,6 +55,7 @@ async function run() {
const createSummary = core.getBooleanInput('create-summary')
const prerelease = core.getBooleanInput('pre-release')
const skipBump = core.getBooleanInput('skip-bump')
const isProtectedBranch = core.getBooleanInput('is-protected-branch')

if (skipCi) {
gitCommitMessage += ' [skip ci]'
Expand Down Expand Up @@ -218,6 +220,12 @@ async function run() {
if (gitPush) {
try {
core.info('Push all changes')

if (isProtectedBranch) {
core.info('Branch is protected. Waiting for required checks to pass...')
await waitForRequiredChecks(gitBranch)
}

await git.push(gitBranch)

} catch (error) {
Expand Down Expand Up @@ -265,6 +273,29 @@ async function run() {
}
}

async function waitForRequiredChecks(branch) {
const octokit = github.getOctokit(core.getInput('github-token'))
const { owner, repo } = github.context.repo

while (true) {
const { data: checks } = await octokit.rest.checks.listForRef({
owner,
repo,
ref: branch,
})

const pendingChecks = checks.check_runs.filter(check => check.status !== 'completed')

if (pendingChecks.length === 0) {
core.info('All required checks have passed')
break
}

core.info(`Waiting for ${pendingChecks.length} checks to complete...`)
await new Promise(resolve => setTimeout(resolve, 30000)) // Wait for 30 seconds before checking again
}
}

Comment on lines +276 to +298
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this into the helpers folder?

process.on('unhandledRejection', (reason, promise) => {
let error = `Unhandled Rejection occurred. ${reason.stack}`
console.error(error)
Expand Down