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

Feature to update all pull requests asynchronously #9

Merged
merged 22 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 21 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
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@

Update a Pull Request from its base branch.

After updating it waits until the branch has been successfully updated,
Optionally waits until the branch has been successfully updated,
as the REST api route [doesn't](https://developer.github.com/v3/pulls/#response-3).

## Usage

To update a single pull request and wait until it's complete:
```yaml
steps:
- name: Update Pull Request
uses: juliangruber/update-pull-request-branch-action@v1
with:
number: 1
github-token: ${{ secrets.GITHUB_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
```

To update all pull requests without waiting:
Tantalon marked this conversation as resolved.
Show resolved Hide resolved
```yaml
steps:
- name: Update All Pull Requests
uses: juliangruber/update-pull-request-branch-action@v1
with:
waitForPullRequestUpdated: false
github_token: ${{ secrets.GITHUB_TOKEN }}
```

## License
Expand Down
8 changes: 6 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ branding:
icon: 'git-pull-request'
color: purple
inputs:
github-token:
github_token:
description: 'GitHub Token'
required: true
number:
description: 'Pull Request number'
required: true
required: false
waitForPullRequestUpdated:
description: 'Wait for the pull request to update before returning'
required: false
default: true
runs:
using: 'node12'
main: 'dist/index.js'
59 changes: 43 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,59 @@ const { GitHub, context } = require('@actions/github')
const sleep = dt => new Promise(resolve => setTimeout(resolve, dt))

const main = async () => {
const token = core.getInput('github-token')
const token = core.getInput('github_token')
const number = core.getInput('number')
const waitForPullRequestUpdated =
core.getInput('waitForPullRequestUpdated') === 'true'

const octokit = new GitHub(token)

const res = await octokit.pulls.get({
...context.repo,
pull_number: number
})
const oldSha = res.data.head.sha

await octokit.pulls.updateBranch({
...context.repo,
pull_number: number,
expected_head_sha: oldSha
})
let numbers
if (number) {
numbers = [number]
} else {
const listRes = await octokit.pulls.list({
...context.repo,
state: 'open'
})
numbers = listRes.data.map(pull => pull.number)
}

while (true) {
for (const number of numbers) {
const res = await octokit.pulls.get({
...context.repo,
pull_number: number
})
if (res.data.head.sha !== oldSha) return
const oldSha = res.data.head.sha
const prMergeable = res.data.mergeable
const prMergeableState = res.data.mergeable_state

// update pull request only if it's outdated and there are no conflicts
if (prMergeable && prMergeableState === 'behind') {
juliangruber marked this conversation as resolved.
Show resolved Hide resolved
core.info(`Updating pull request ${number}`)

await octokit.pulls.updateBranch({
...context.repo,
pull_number: number,
expected_head_sha: oldSha
})

if (waitForPullRequestUpdated) {
while (true) {
core.debug('sleep')
await sleep(1000)

const res = await octokit.pulls.get({
...context.repo,
pull_number: number
})

core.debug('sleep')
await sleep(1000)
if (res.data.head.sha !== oldSha) break
}
}
} else {
core.info(`Skipping ${prMergeableState} pull request ${number}`)
}
}
}

Expand Down