-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Any way to checkout PR from issue_comment
event?
#331
Comments
The only solution I found was to first use the checkout event and then separately use name: Slash Command CI
on: issue_comment
jobs:
check_comments:
runs-on: ubuntu-latest
name: Check comments for /format
steps:
- name: Clone git repo
uses: actions/checkout@v2
- name: Checkout Pull Request
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_URL="${{ github.event.issue.pull_request.url }}"
PR_NUM=${PR_URL##*/}
echo "Checking out from PR #$PR_NUM based on URL: $PR_URL"
hub pr checkout $PR_NUM
- name: Configure Git Agent
run: |
git config --global user.email "[email protected]"
git config --global user.name "Me, Not Really (CI bot)"
- uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.13.0
- run: terraform fmt -recursive
- run: git commit -a -m "GitOps Bot (Autoformat)"
- run: git push Is there a better way? |
I faced the same problem with one more restriction that github account was not owned by us, I don't know the context that other users will face but in my case I can just send in the branch name through the comment. so, here is what I did: Basically pull out branch name from comment where branch name is enclosed in [], i.e [branch_name] then extract out the name to pass it to the checkout action. I would love to find better way than this but for now it works ok. steps:
- name: Get Branch name
id: branchName
run: |
echo ::set-output name=branch::$(echo $PR_COMMENT | cut -d "[" -f2 | cut -d "]" -f1)
env:
PR_COMMENT: ${{ github.event.comment.body }}
- name: Checkout code
uses: actions/checkout@v2
with:
ref: ${{ steps.branchName.outputs.branch }} |
As the required information isn't present in the event payload, you could use the GitHub API to fetch it. The github-script action allows you to write actions in your workflow file, which is pretty convenient: name: Checkout PR on comment
on:
issue_comment:
# triggers on created, edited and deleted by default, you may wanna restrict it, e.g.:
#types: [created]
jobs:
pr-commented:
name: PR commented
if: github.event.issue.pull_request
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v3
id: get-pr
with:
script: |
const request = {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
}
core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`)
try {
const result = await github.pulls.get(request)
return result.data
} catch (err) {
core.setFailed(`Request failed with error ${err}`)
}
- uses: actions/checkout@v2
with:
repository: ${{ fromJSON(steps.get-pr.outputs.result).head.repo.full_name }}
ref: ${{ fromJSON(steps.get-pr.outputs.result).head.sha }} # or .head.ref for branch name |
…eckout PR from event? · Issue #](actions/checkout#331) こちらのものを利用した
@aaronsteers do you even need to parse it? I though the following should do:
|
@zyv , yes, I think it would! The 10-months-ago-me perhaps didn't know or realize yet that a PR is an issue and the issue number is therefore the same as the PR number. 🙌 |
Can we add PR information like target branch or source branch in issue_comment webhook payload? |
This has worked for us!
|
Edit: The below information is not correct as pointed out by @zyv below
|
@tomdottom this information is not available in |
@zyv thanks for pointing this out |
I just created a tiny js action that will output It's really simple to use:
In the following steps you can reference I hope you like it 🙂 |
The action used for testing kata-deploy is entirely based on the action used to build the kata-deploy tarball, but while the latter is able to use the correct branch, the former always uses `main`. This happens as the `issue_comment`, from GitHub actions, passed the "default branch" as the GITHUB_REF. As we're not the first ones to face such a issue, I've decided to take one of the approaches suggested at one of the checkout's issues, actions/checkout#331, and take advantage of a new action provided by the community, which will get the PR where the comment was made, give us that ref, and that then can be used with the checkout action, resulting on what we originally wanted. Fixes: kata-containers#3443 Signed-off-by: Fabiano Fidêncio <[email protected]>
The action used for testing kata-deploy is entirely based on the action used to build the kata-deploy tarball, but while the latter is able to use the correct branch, the former always uses `main`. This happens as the `issue_comment`, from GitHub actions, passed the "default branch" as the GITHUB_REF. As we're not the first ones to face such a issue, I've decided to take one of the approaches suggested at one of the checkout's issues, actions/checkout#331, and take advantage of a new action provided by the community, which will get the PR where the comment was made, give us that ref, and that then can be used with the checkout action, resulting on what we originally wanted. Fixes: kata-containers#3443 Signed-off-by: Fabiano Fidêncio <[email protected]>
The action used for testing kata-deploy is entirely based on the action used to build the kata-deploy tarball, but while the latter is able to use the correct branch, the former always uses `main`. This happens as the `issue_comment`, from GitHub actions, passed the "default branch" as the GITHUB_REF. As we're not the first ones to face such a issue, I've decided to take one of the approaches suggested at one of the checkout's issues, actions/checkout#331, and take advantage of a new action provided by the community, which will get the PR where the comment was made, give us that ref, and that then can be used with the checkout action, resulting on what we originally wanted. Fixes: kata-containers#3443 Signed-off-by: Fabiano Fidêncio <[email protected]>
The action used for testing kata-deploy is entirely based on the action used to build the kata-deploy tarball, but while the latter is able to use the correct branch, the former always uses `main`. This happens as the `issue_comment`, from GitHub actions, passed the "default branch" as the GITHUB_REF. As we're not the first ones to face such a issue, I've decided to take one of the approaches suggested at one of the checkout's issues, actions/checkout#331, and take advantage of a new action provided by the community, which will get the PR where the comment was made, give us that ref, and that then can be used with the checkout action, resulting on what we originally wanted. Fixes: kata-containers#3443 Signed-off-by: Fabiano Fidêncio <[email protected]>
In case anyone else comes looking, you can use the gh cli to only get the branch name if you still want to use the
|
@jacobrask I tried your solution and I received the following output: GraphQL: Resource not accessible by integration (repository.pullRequest) What else did you do? |
@mareksuscak's method worked great for my
|
Great discovery @Zhiyuan-Amos I will add your comment in my PR if that's okay with you. |
Patch at 1631a18 doesn't actually work. Need to checkout the Pull Request branch instead of the HEAD ref. Fixes error like `fatal: You are not currently on a branch. To push the history leading to the current (detached HEAD) state now, use ...`. Xref actions/checkout#331 (comment)
Get pullrequest info with actions/checkout#331 (comment)
has anyone found a way to find the ref of the pr with this as far as I know |
I changed that for me to support both triggers (
|
* set `Checkout Pull Request step` ref: actions/checkout#331 (comment) * fix condition * fix
What worked for me was simply
|
When using the |
For what it's worth, there's a chance the ref will no longer exist if the PR is closed. If you want an action to try to check out the open pr's branch you can just check the state of the issue's state. If the pr is open, the branch will exist. You can do something like:
or
|
The example in the docs here assumes a pull request event, but it does not work when operating on Pull Request comments, since those come through the
issue_comment
event.I've spent several hours on this now and I don't see any way to checkout the branch associated with the issue_event.
Complications:
issue_comment
events have to be triggered by the workflow file on the default branch, it seems that is always the branch name provided. I haven't yet found any way to get the actual PR branch, although it seems this should be a straightforward and frequent use case for operating on PR comments.The text was updated successfully, but these errors were encountered: