Using PR tabs in code channels
A PR tab shows a GitHub pull request in a code channel, right next to the conversation. People in the channel can follow the pull request's state, checks, and reviews without leaving Slack.
Your app adds a PR tab by creating a pull_request view with the agents.conversations.setView API method. The view stores only the pull request's URL. Slack reads the pull request live from GitHub for each person who opens the tab, with their GitHub account, so your app never sends the pull request's title, state, or CI results.
Requirements
For a PR tab to load, all of the following must be true:
- The Slack Code GitHub App is installed. An owner of the GitHub organization installs the Slack Code GitHub App on the organization, for all or selected repositories. Install it on an organization, not a personal account, since installations on personal accounts can't be approved in Slack.
- The installation is approved in Slack. An admin or owner of the Slack organization, or of the workspace if it isn't part of an organization, approves it in the admin dashboard under Feature Access > Slack Code > GitHub organizations. A GitHub organization is listed there as Pending approval only when the app is installed after your app has added a PR view for one of its repositories, so add a PR view before installing the app. If the app was installed first, an admin enters the organization's name under Don't see your organization? Enter its name and selects Add, which approves it.
- The viewer has connected their GitHub account in Slack. Slack reads and takes action on the pull request with the viewer's GitHub account.
- The viewer can use GitHub in Slack Code. Admins choose who can under Feature Access > Slack Code > GitHub in Slack Code. It's on by default for owners, admins, and full members, and in an organization, admins can also turn it on for specific people or user groups. For anyone else, Slack doesn't use their connected GitHub account, so they don't see PR tabs.
Until an approved installation covers the pull request's repository, the PR tab can't load, and setView returns the github_not_connected warning. The view is still saved. When you get that warning, also send the changes as a diff view so people can review them.
Choosing between a PR tab and the diff
| View | Use it for | Where the content comes from |
|---|---|---|
PR tab (type: "pull_request"), recommended | A pull request on github.com. | GitHub. The pull request's changes, status, and activity come from GitHub, and each viewer connects their GitHub account. |
Diff (type: "diff") | Changes that won't be tied to a pull request, or when a github.com pull request is unavailable. | Your app sends the diff content. With no view_key, it's shown in the Code tab. With a view_key, it gets its own tab. Refer to diff views. |
For work that will become a pull request, open a draft pull request as soon as your agent has changes to show, and add a PR tab for it. If the work has several pull requests, add one PR tab for each.
If the channel has a Code tab diff for work that now has a PR tab, remove the diff with removeView and view_id: "code":
POST /api/agents.conversations.removeView
{
"channel_id": "C9876543210",
"view_id": "code"
}
Then stop sending diffs for that work, since a later type: "diff" call with no view_key brings the Code tab back. To remove a keyed diff, pass its view_key or view_id.
Adding a PR tab
Call the agents.conversations.setView API method with type set to pull_request, a view_key you choose, the pull request's pr_url, and an optional name:
POST /api/agents.conversations.setView
{
"channel_id": "C9876543210",
"type": "pull_request",
"view_key": "borant/billing#42",
"name": "Migrate billing cron",
"pr_url": "https://github.com/borant/billing/pull/42"
}
{
"ok": true,
"channel_id": "C9876543210",
"view_id": "Ct0123456789",
"file_id": "",
"content_version": 1,
"type": "pull_request",
"pr_url": "https://github.com/borant/billing/pull/42"
}
A few details to keep in mind:
- Pick a
view_keythat names the pull request, such asowner/repo#number, and keep it for the life of the pull request. Calls with the sameview_keyupdate the same PR tab. - The
nameargument sets the tab label. - Treat a PR view's
view_idas an opaque ID. Use it with theagents.conversations.removeViewAPI method, or read it back with theagents.conversations.listViewsAPI method. - Slack places a new PR tab ahead of the channel's existing PR tabs. People in the channel can reorder tabs.
Set the channel's code_channel.pr_status to draft at the same time, and update it as the channel's pull requests change. Refer to setting the channel's PR status.
Supported hosts
PR tabs support pull requests on github.com. The pr_url must be an https:// URL of the form https://github.com/<owner>/<repo>/pull/<number>. Pull requests on other hosts, including GitHub Enterprise Server instances, GitLab, and Bitbucket, return the unsupported_pr_host error.
Removing a PR tab
When a pull request no longer belongs to the session, remove its view with the agents.conversations.removeView API method:
POST /api/agents.conversations.removeView
{
"channel_id": "C9876543210",
"view_id": "Ct0123456789"
}
Removing the view removes its tab and stops the pull request's GitHub activity from reaching the channel, unless another pull_request view in the channel links the same pull request. The pull request itself isn't affected. You can also pass the view_key instead of the view_id, unless another view type shares that key, which returns ambiguous_view_key.
What people see
Each person sees the pull request as their own GitHub account can, including its state and checks, so your app doesn't update the view when the pull request changes. A person who hasn't connected GitHub sees a Connect to GitHub button in the PR tab, and your app has nothing to do.
Slack doesn't persist the pull request's contents. Its description, diff, comments, and checks come from GitHub, so they aren't searchable in Slack or part of the channel's exports. A diff view is different, since Slack stores its content as a file.
People can view PR tabs in Slack on desktop, on the web, and in the iOS and Android apps.
Setting the channel's PR status
A PR tab describes one pull request. The channel as a whole also has a PR status, which Slack shows in places such as the channel's sidebar icon, the status tag in the channel header, and the channel browser. That status is the pr_status property, a rollup of all of the channel's pull requests.
Your app owns pr_status, and Slack never sets or changes it. Update it whenever the rollup of the channel's pull requests changes. Set it with the agents.conversations.setProperties API method:
POST /api/agents.conversations.setProperties
{
"channel_id": "C9876543210",
"code_channel": {
"pr_status": "open"
}
}
The pr_status property must be one of the following values:
| Value | Meaning |
|---|---|
open | A pull request is open and ready for review. |
draft | The open pull requests are drafts. |
merged | The work has merged, and no pull request is still open. |
closed | Every pull request was closed without merging. |
blocked | A pull request is open but needs the user, for example because a reviewer requested changes, checks are failing, or it has merge conflicts. |
"" | An empty string clears the status, so none is shown. |
Any other value, including a different case such as Open, returns invalid_pr_status, and nothing in that call is saved. Leaving pr_status out of a call keeps the stored value.
Rolling up several pull requests
When a channel has more than one pull request, pick the one value that best describes the work as a whole. We suggest checking these rules in order and using the first that matches:
- Use
blockedwhen an open pull request needs the user. - Use
openwhen any open pull request is ready for review. - Use
draftwhen every open pull request is a draft. - Use
mergedwhen no pull request is still open and at least one has merged. - Use
closedwhen every pull request was closed without merging.
Receiving pull request events
Slack routes a pull request's GitHub activity to the channel once the pull request's tab has loaded for someone who can view the channel. Routing also needs an approved installation of the Slack Code GitHub App that covers the repository. Like removing the view, a person removing the PR tab in Slack stops it. Renaming the GitHub organization or repository also stops it for existing views, so add new PR views with the new URLs.
Limits
A channel can hold up to 10 pull_request views, and adding another returns too_many_pull_request_views. That limit is separate from the channel's other views, and PR tabs don't count against the channel's 15-tab limit, so a PR tab still fits in a channel whose tabs are full. Refer to the setView method's limits for the other views.
Errors
These are the errors your app is most likely to see when it works with PR tabs:
| Error | Returned by | What to do |
|---|---|---|
missing_required_arg | setView | Pass pr_url and view_key with type: "pull_request". |
invalid_pr_url | setView | Pass an https:// pull request URL. |
unsupported_pr_host | setView | Use a pull request on github.com. Other hosts aren't supported. |
too_many_pull_request_views | setView | Remove a PR view the session no longer needs, then try again. |
not_view_owner | setView, removeView | Another agent bot created this view, and only it can update or remove it. For setView, use a different view_key. For removeView, leave the view to its owner. |
ambiguous_view_key | removeView | More than one view has this view_key. Pass the PR view's view_id instead. |
pr_link_failed | setView, setProperties | The view or properties were saved, but Slack couldn't update the channel's link to its pull requests, which routing uses. Repeat the same call. |
invalid_pr_status | setProperties | Pass one of the PR status values. Nothing in the call was saved. |
invalid_arguments | setProperties | Remove pr_url, pr_number, pr_title, ci_state, and ci_url from code_channel, and add a PR view instead. |
Next steps
- Refer to the
agents.conversations.setViewAPI method for every view type and argument. - Refer to code channel properties for the
pr_statusproperty and the channel's view list. - Read about the code channel lifecycle to see where PR tabs fit in a working session.