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:

  1. 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:

    ⏳ Running feature set --group=my-group my_flag true in group/project by @username · job

  2. When the job finishes, the bot edits the status message to :white_check_mark: ... passed or :x: ... failed.

  3. 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_data gets three nullable text columns: channel_xid and thread_ts, limited to 255 characters, and chat_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::Command stores the channel the command came from. After the status message is posted, channel_xid and thread_ts are set from the chat.postMessage response.
  • The status message is built from the job name and the command arguments. Gitlab::Chat::Command saves the arguments to chat_input when 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_output returns an ephemeral response with a short text. Presenters::Run merges it over its in_channel default. 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_ts is set, so switching the flag does not break runs in progress.
  • The status message stays at "Running" if the job never finishes, or if ChatNotificationWorker gives up on the trace with TimeoutExceeded. 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

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
  1. In the Rails console, enable the flag for the project:

    Feature.enable(:chatops_slack_threaded_replies, Project.find_by_full_path('group/project'))
  2. In a public Slack channel, run /gitlab <project-alias> run hello some args.

  3. 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 shows hello some args.

  4. When the job finishes, check that the status message shows passed and that Hello world is in its thread.

  5. Run /gitlab <project-alias> run fail. The status message shows failed and the thread has "Sorry, the build failed!".

  6. Repeat in a private channel without the bot. Slack echoes the command and the output goes to the channel root, as before.

  7. 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.

Edited by Oleg Yakovenko

Merge request reports

Loading
Loading