Reply to ChatOps commands in a Slack thread
What does this MR do and why?
ChatOps job results from /gitlab <project> run <job> go to the channel root. In busy channels it is hard to tell which output belongs to which command.
With the GitLab for Slack app, and the chatops_slack_threaded_replies flag on for the project:
-
When the command is scheduled, the bot posts a status message with the full command. The user who ran the command gets a private confirmation instead of the command echo:
⏳ Runningfeature set --group=my-group my_flag trueingroup/projectby@username· job -
When the job finishes, the bot edits the status message to
:white_check_mark: ... passedor:x: ... failed. -
The job output, or the failure message, is posted as a reply in the thread of the status message. The reply mentions the user, as before.
The channel gets one root message per command. The username is a GitLab username in a code span, so the status message notifies nobody.
If a Slack call fails, the command works as before. Slack shows the echo and the reply goes through response_url to the channel root. This happens when:
- the project has no active GitLab for Slack app integration, or it has no bot token
- the installation belongs to a different Slack workspace
- the bot is not in the channel (
not_in_channel), for example in private channels and DMs - the Slack API returns an error or the request fails
Mattermost and the legacy Slack slash commands integration don't change.
Implementation notes
ci_pipeline_chat_datagets three nullable text columns:channel_xidandthread_ts, limited to 255 characters, andchat_input, limited to 500. The table is small, so this is a regular migration. The thread reference is saved with an update by primary key.Gitlab::Chat::Commandstores the channel the command came from. After the status message is posted,channel_xidandthread_tsare set from thechat.postMessageresponse.- The status message is built from the job name and the command arguments.
Gitlab::Chat::Commandsaves the arguments tochat_inputwhen the command runs, so the status message can be rebuilt when the job finishes without reading the pipeline variables. - Slack link and mention markup (
<>[]|) and backticks are removed from the command text. The text sits in a code span and is cut off at 500 characters. - When the status message is posted,
Responder::Slack#scheduled_outputreturns an ephemeral response with a short text.Presenters::Runmerges it over itsin_channeldefault. Slack rejects an ephemeral response without text (invalid_command_response), so the text is required. - The flag is checked only when the status message is posted. Completion depends only on whether
thread_tsis set, so switching the flag does not break runs in progress. - The status message stays at "Running" if the job never finishes, or if
ChatNotificationWorkergives up on the trace withTimeoutExceeded. The old behavior sent no reply in these cases either. - Posting the status message adds one synchronous Slack API call inside the 3-second slash command window.
References
- https://gitlab.com/gitlab-org/gitlab/-/work_items/638845
- Rollout: https://gitlab.com/gitlab-org/gitlab/-/work_items/638889
- Slack: Implementing slash commands
Screenshots or screen recordings
No GitLab UI changes. The change is to messages in Slack.
How to set up and validate locally
All steps below were verified on a local GDK with a Slack developer sandbox workspace.
Prerequisites: a GDK with the GitLab for Slack app set up for a project, a runner, and ChatOps jobs in that project's .gitlab-ci.yml:
hello:
rules:
- if: $CI_PIPELINE_SOURCE == "chat"
script:
- echo "Hello world"
fail:
rules:
- if: $CI_PIPELINE_SOURCE == "chat"
script:
- exit 1-
In the Rails console, enable the flag for the project:
Feature.enable(:chatops_slack_threaded_replies, Project.find_by_full_path('group/project')) -
In a public Slack channel, run
/gitlab <project-alias> run hello some args. -
Check that you get a private "Running
hello, the output goes to the thread." confirmation, that the command is not echoed, and that the status message showshello some args. -
When the job finishes, check that the status message shows
passedand thatHello worldis in its thread. -
Run
/gitlab <project-alias> run fail. The status message showsfailedand the thread has "Sorry, the build failed!". -
Repeat in a private channel without the bot. Slack echoes the command and the output goes to the channel root, as before.
-
Disable the flag and repeat step 2. The command works as before.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.