Skip to content

Commit 5a40e8d

Browse files
authored
Change BinderPR action to generate Binder link on /binder comment (#631)
Copying example from https://mybinder.readthedocs.io/en/latest/howto/gh-actions-badges.html#example-2-comment-with-a-binder-badge-in-response-to-a-comment, only create the Binder button when requested in a comment with `/binder`.
1 parent 1cd3274 commit 5a40e8d

1 file changed

Lines changed: 33 additions & 17 deletions

File tree

.github/workflows/BinderPR.yml

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
# Add Binder Link to PR for testing pangeo-notebook image
2-
name: BinderPR
3-
4-
on:
5-
pull_request_target:
6-
types: [opened]
2+
# https://mybinder.readthedocs.io/en/latest/howto/gh-actions-badges.html#example-2-comment-with-a-binder-badge-in-response-to-a-comment
3+
name: Chatops Binder
4+
on: [issue_comment] # issues and PRs are equivalent in terms of comments for the GitHub API
75

86
permissions: {}
97

108
jobs:
11-
add-binder-links:
9+
trigger-chatops:
10+
# Make sure the comment is on a PR, and contains the command "/binder"
11+
if: (github.event.issue.pull_request != null) && contains(github.event.comment.body, '/binder')
1212
runs-on: ubuntu-latest
13+
permissions:
14+
pull-requests: write
15+
1316
steps:
14-
- name: Add BinderHub Links
15-
uses: peter-evans/create-or-update-comment@v4
16-
env:
17-
PR_HEAD_REF: ${{ github.event.pull_request.head.ref }}
18-
PR_HEAD_USERREPO: ${{ github.event.pull_request.head.repo.full_name }}
19-
with:
20-
token: ${{ secrets.GITHUB_TOKEN }}
21-
issue-number: ${{github.event.number}}
22-
reactions: rocket
23-
body: |
24-
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/${{env.PR_HEAD_USERREPO}}/${{env.PR_HEAD_REF}}) :point_left: Try on Mybinder.org!
17+
# Use the GitHub API to:
18+
# (1) Get the branch name of the PR that has been commented on with "/binder"
19+
# (2) make a comment on the PR with the binder badge
20+
- name: comment on PR with Binder link
21+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
22+
with:
23+
github-token: ${{secrets.GITHUB_TOKEN}}
24+
script: |
25+
// Get the branch name
26+
github.rest.pulls.get({
27+
owner: context.repo.owner,
28+
repo: context.repo.repo,
29+
pull_number: context.payload.issue.number
30+
}).then( (pr) => {
31+
32+
// use the branch name to make a comment on the PR with a Binder badge
33+
var BRANCH_NAME = pr.data.head.ref
34+
github.rest.issues.createComment({
35+
issue_number: context.payload.issue.number,
36+
owner: context.repo.owner,
37+
repo: context.repo.repo,
38+
body: `[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/${context.repo.owner}/${context.repo.repo}/${BRANCH_NAME}) :point_left: Launch a binder notebook on this branch`
39+
})
40+
})

0 commit comments

Comments
 (0)