Skip to content

Conversation

@robertbrignull
Copy link
Contributor

@robertbrignull robertbrignull commented Dec 7, 2020

This is a potential alternative to #322

Adds a workflow that can be triggered via workflow dispatch and will merge main into a branch and fix any conflicts in the lib directory if required. Will be of some use for people at GitHub to update their own PRs, but the main intended use is it'll make it much easier to update a PR from an external contributor. You don't have to wait for the contributor to update their PR, or have to manually add a remote and push changes yourself.

There are a few 3rd party actions out there for checking out PRs and committing and pushing changes (for example https://github.com/dawidd6/action-checkout-pr and https://github.com/stefanzweifel/git-auto-commit-action), but I found it easier to do manually with git and bash.

Merge / deployment checklist

  • Confirm this change is backwards compatible with existing workflows.
  • Confirm the readme has been updated if necessary.

# https://api.github.com/repos/github/codeql-action/actions/workflows/update-lib.yml/dispatches \
# -d '{"ref":"main","inputs":{"branch":"<branch name>"}}'
# Replace <token> with a personal access token from this page: https://github.com/settings/tokens
# Replace <pr-number> by the number of the pull request to update
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# Replace <pr-number> by the number of the pull request to update
# Replace <branch name> by the name of the branch of the PR.

Did you test this for forks? Should it then be <user>:<branch>?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not test this for forks and you're right this isn't going to work as it is.

Copy link
Contributor

@chrisgavin chrisgavin left a comment

Choose a reason for hiding this comment

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

Nice! I've added some suggestions that allow putting a label on the PR to trigger the job instead, which might be a bit easier. If you'd prefer to merge it as-is though, that's also fine. Either way it makes things easier.

I think unfortunately the GITHUB_TOKEN provided by Actions can't write to forks even if they've explicitly allowed edits by maintainers so I don't think it's going to be possible to use a workflow to update external contributions unless we provide a personal access token to the workflow.

Comment on lines +4 to +15
workflow_dispatch:
# Example of how to trigger this using the API:
# curl -X POST \
# -H "Authorization: Bearer <token>" \
# https://api.github.com/repos/github/codeql-action/actions/workflows/update-lib.yml/dispatches \
# -d '{"ref":"main","inputs":{"branch":"<branch name>"}}'
# Replace <token> with a personal access token from this page: https://github.com/settings/tokens
# Replace <pr-number> by the number of the pull request to update
inputs:
branch:
description: 'The branch to update'
required: true
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
workflow_dispatch:
# Example of how to trigger this using the API:
# curl -X POST \
# -H "Authorization: Bearer <token>" \
# https://api.github.com/repos/github/codeql-action/actions/workflows/update-lib.yml/dispatches \
# -d '{"ref":"main","inputs":{"branch":"<branch name>"}}'
# Replace <token> with a personal access token from this page: https://github.com/settings/tokens
# Replace <pr-number> by the number of the pull request to update
inputs:
branch:
description: 'The branch to update'
required: true
pull_request_target:
types:
- labeled

jobs:
update-lib:
runs-on: ubuntu-latest

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if: ${{ github.event.label.name == 'update-lib' }}

uses: actions/checkout@v2
with:
fetch-depth: 0
ref: ${{ github.event.inputs.branch }}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ref: ${{ github.event.inputs.branch }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}

Comment on lines +35 to +36
git fetch origin
if ! merge_output=$(git merge origin/main --no-ff); then
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
git fetch origin
if ! merge_output=$(git merge origin/main --no-ff); then
git remote add base ${{ github.event.pull_request.base.repo.clone_url }}
git fetch base
if ! merge_output=$(git merge base/${{ github.event.pull_request.base.ref }} --no-ff); then

git commit -am "merge main"
fi

git push origin HEAD:${{ github.event.inputs.branch }}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
git push origin HEAD:${{ github.event.inputs.branch }}
git push ${{ github.event.pull_request.head.repo.clone_url }} ${{ github.event.pull_request.head.ref }}
- name: Remove Label
uses: buildsville/add-remove-label@dd82a87afd13f1e78e35131fe1945380c685305e
with:
token: ${{ secrets.GITHUB_TOKEN }}
label: update-lib
type: remove

@robertbrignull
Copy link
Contributor Author

Closing this as it's unlikely to get completed. I think trying to make it work on PRs from forks will be too difficult and not worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants