Slack integration

This section details how to install, configure, and use the P4 Code Review Slack integration. The P4 Code Review Slack integration lets teams collaborate with P4 Code Review notifications in Slack. The Slack integration posts a message in the configured P4 Code Review project Slack channels each time a change is committed, a review is created, or a review is updated in the project.

Prerequisites

  • You must be an admin or super user to integrate P4 Code Review with Slack.

  • Find the names of the channels in Slack to which you want project notifications sent.

  • Ensure you have write access to the P4 Code Reviewconfig.php file as you will need to add the Slack channel names as project ID to complete the integration.

Install the Slack integration

To install the Slack integration:

  1. Navigate to Helix Swarm Slack Integration.

  2. Click Add to Slack button.

  3. Select the workspace from the top of the page that you wish to integrate Slack with.

  4. Request install permissions from your Application Manager.

  5. Once the Application Manager approves your request, P4 Code Review Slack integration is installed into your selected workspace.

  6. To enable P4 Code Review to work with Slack, edit the P4 Code Review config.php file to include the displayed Slack configuration.

    The P4 Code Review config.php file is saved in the SWARM_ROOT/data/ directory.

After the P4 Code Review Slack integration is installed, you must configure the project in the P4 Code Reviewconfig.php file. For more information on how to configure a project for Slack integration, see Setup project configurations.

Slack configuration

Add the following Slack configuration to the P4 Code Review config.php file. The P4 Code Review config.php file is saved in the SWARM_ROOT/data/ directory.

'slack' => array(
	     'token' => 'TOKEN',
	     'project_channels' => array(), //See Setup project configurations to configure a project for Slack integration
	     'summary_file_names' => false,
	     'bypass_restricted_changelist' => false,
	     'summary_file_limit' => 10,
	     'user' => array(
		        'enabled' => true,
		        'name' =>'Helix Swarm',
		        'icon' => 'URL',
		        'force_user_header' => false,
		),
),

token

The Slack app provides a token that is used by P4 Code Review.

project_channels

Specifies the P4 Code Review projects ID and Slack channels they post to in the project_channels array. You can configure multiple channels to receive a notification for a project. For more information on how to use project_channels configurable, see Setup project configurations.

summary_file_names

Attaches the file to the original notification message sent to a Slack channel.

bypass_restricted_changelist

Allows P4 Code Review to post notification messages to a Slack channel when a change is committed or a review is created for a restricted changelist, default value is false. For more information about a restricted changelist, see Restricted Changes.

summary_file_limit

Limits the number of files shown in the original notification message sent to a Slack channel, default value is 10.

user

Specifies details about the P4 Code Review user in a Slack notification.

  • enabled: Forces the Swarm app to use the custom user name. This overrides the P4 Code Review app details.

  • name: This is the user name shown in the Slack channel when a notification is posted.

  • icon: This is the avatar icon shown in the Slack channel when a notification is posted, overrides the avatar set in the P4 Code Review app.

  • force_user_header: The Slack notification shows the user name and avatar only for the first notification by a user. Slack does not apply the user name and avatar to any more notifications from the same user. By default this is set to false.

    When set to true, Slack adds the user name and avatar for every notification.

Use Slack integration through a proxy server

This section details how you can configure the Slack integration to use a proxy server. For example, add the following Squid proxy server configuration to the SWARM_ROOT/data/config.php file.

<?php
// this block should be a peer of 'p4'
'http_client_options' => array(
// optional, per-host overrides;
// host as key, array of options as value
'hosts' => array(
'slack.com' => array( 'adapter' => 'Laminas\Http\Client\Adapter\Proxy',
'proxy_host' => 'squidproxy',
'proxy_port' => '3128',
), ),
),

adapter

Specifies the connection adapter used to establish a connection to the server, sending requests, and receiving responses.

proxy_host

Specifies the proxy server address.

proxy_port

Specifies the proxy server Transmission Control Protocol (TCP) port.

See the Laminas Framework Socket Adapter documentation for more information.

Setup project configurations

This section details how you can configure a project in P4 Code Review to send Slack notifications to a Slack channel.

After project configuration is complete, you must restart the workers and the configuration cache for the updated configuration to take effect.

  • For more information on how to restart workers, see Workers section.

  • For more information on how to restart configuration cache, see Reload Configuration button section.

To keep the private projects secure, ensure that the Slack channel you wish to send notifications to is a private channel. For more information on how to add the P4 Code Review application to a private channel, see Add P4 Code Review application to a private channel.

To send notifications to a channel for a project, you must configure the project_channels block as follows:

<?php
    //This block should be a peer of 'p4' 
    'slack' => array(
         'token' => 'xoxb-3875744143189-3925283455653-pxxZoTKz3JqUeDXGd66XFW4D',  
         'project_channels' => array(
              //Project myproject will go into slack channel myproject-channel
              'myproject' => array( 
                  'myproject-channel',
               ),
              //Project with no setting will go to no-project-channel
              '*no-project-channel*' => array(
                  'no-project-channel',
               ),
              //No project on this review/change 
              '*without_project*' => array(
                   'no-link-project',  
               ),
               //All notification get sent here even if we matched above
               '*all*' => array(
                    'all-notifications', 
               ),
        ),
  ),

project_channels

Specify the P4 Code Review projects ID and the Slack channels they post to in the project_channels array. You can configure multiple channels to receive a notification for a project.

Example

  • P4 Code Review project: myproject

  • Slack channel: myproject-channel

All notifications for the project myproject will go to the myproject-channel Slack channel.

*no-project-channel* (optional)

For a project that has no defined project mapping and with *no-project-channel* configurable defined, the Slack notifications are sent to *no-project-channel*. For example, all notifications for the project myproject goes to myproject-channel and similarly all notifications for any other project goes to *no-project-channel*.

*without_project* (optional)

When *without_project* configurable is defined, it means that no notification is sent if the notification is not associated with a project.

*all* (optional)

When *all* configurable is defined, all notifications are sent here even if *no-project-channel* and *without_project* options are configured.

P4 Code Review is now configured to send notifications to a Slack channel for a project.

Add P4 Code Review application to a private channel

To send notifications to a private channel you must add the P4 Code Review application to the private channel as follows:

  1. In Slack, open the private channel you would like to send notifications to.

  2. Select your picture or avatar in the top right to open the channel menu.

  3. Select the Integrations tab.

  4. For the P4 Code Review Bot select Add apps to add the application to your private channel.

P4 Code Review can now send notifications to your private channel.

Slack notification trigger

P4 Code Review will post a message on a Slack channel for every commit. This includes each time a change is committed, a review is created, or a review is updated. Any update to a review in P4 Code Review is displayed as a recently occurred activity stream in the associated Slack channel.

Here is a list of actions that will trigger P4 Code Review to send a notification to the associated project Slack channel.

Event Description
joined When a user joins the review
left When a user leaves the review
made their vote required on When a user's vote has been made required
made their vote optional on When a user's vote has been made optional
disabled notifications on When a user disables the notifications for a review
re-enabled notifications on When a user re-enables the notifications for a review
edited reviewers on When the reviewers are edited for a review
voted When a user votes up or down
cleared their vote on When a user clears their vote
requested further review of When a user changes the review state to needsReview
requested revisions to When a user changes the review state to needsRevision
rejected When a user changes the review state to rejected
archived When a user changes the review state to archived
approved When a user changes the review state to approved
updated description of When a user updates the description of the review
updated files in When a user updates the files in a review