Skip to content

Commit

Permalink
Slack integration (#422)
Browse files Browse the repository at this point in the history
* beta

* stealth

* Button to job

* rhaaa

* Allow multiple channels

* fix

* Fix slack actions

* doc

* Fix url link

* again

* PR info

* branch

* unfurl

* find PR

* PR info

* prinfo

* dbg

* fix

* fix

* I'm walkng on sunshine

* MegaLinter

* Walking on sunshiiiiiine wohoooo

* Author login

* View PR

* changelog

* Reformat slack message + rename folder :)

* dx

* Factorize repoOwner & repoName

* [Mega-Linter] Apply linters fixes :)

* Slack notifs for gitlab

* Send only on deployment success

* cspell

* [Mega-Linter] Apply linters fixes

---------

Co-authored-by: nvuillam <nvuillam@users.noreply.github.com>
  • Loading branch information
nvuillam and nvuillam authored Sep 18, 2023
1 parent 01aaada commit c2d02ba
Show file tree
Hide file tree
Showing 19 changed files with 725 additions and 221 deletions.
8 changes: 5 additions & 3 deletions .github/linters/.cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
"FSEDS",
"Facturation",
"Flexi",
"GHSA",
"GITLEAKS",
"GRYPE",
"Gagne",
"GHSA",
"Gmail",
"GRYPE",
"HADOLINT",
"Hardis",
"IVMA",
Expand Down Expand Up @@ -92,11 +92,11 @@
"SAST",
"SEMGREP",
"SOMENAMESPACE",
"STYLELINT",
"Scontrol",
"Scratches",
"Sfdc",
"Sfdx",
"STYLELINT",
"Sublicensing",
"Suspendre",
"Syst\u00e8me",
Expand Down Expand Up @@ -385,6 +385,7 @@
"ignoreerrors",
"ignorerights",
"ignorewarnings",
"iids",
"includemanaged",
"includepackages",
"includeprofiles",
Expand Down Expand Up @@ -449,6 +450,7 @@
"minimumapiversion",
"mkdir",
"mkdocs",
"mrkdwn",
"multiselect",
"mutingpermissionset",
"mutingpermissionsets",
Expand Down
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

Note: Can be used with `sfdx plugins:install sfdx-hardis@beta` and docker image `hardisgroupcom/sfdx-hardis@beta`

- Native BitBucket compliance for PR deployment checks and deployments to major orgs after merge
- sfdx-hardis & Slack Integration
- Easy configuration
- Deployment notifications to a common channel, and also to git branch dedicated channel

- Added new option --testlevel RunRepositoryTests which will dynamically detect all GIT repository test classes and runs the deployment with found tests. This will speed up the validation/deployment on cases where GIT repository module contains subset of all tests found in the org
- Added --runtests support in order to pass certain APEX test classes when --testlevel RunSpecifiedTests is used
- **hardis:project:deploy:dx** enhancements:
- Added new option --testlevel RunRepositoryTests which will dynamically detect all GIT repository test classes and runs the deployment with found tests. This will speed up the validation/deployment on cases where GIT repository module contains subset of all tests found in the org
- Added --runtests support in order to pass certain APEX test classes when --testlevel RunSpecifiedTests is used
- Native BitBucket compliance for PR deployment checks and deployments to major orgs after merge

## [4.5.1] 2023-09-11

Expand Down
348 changes: 174 additions & 174 deletions README.md

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/deployTips.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ description: Learn how to fix issues that can happen during sfdx deployments

This page summarizes all errors that can be detected by sfdx-hardis wrapper commands

| sfdx command | sfdx-hardis wrapper command |
| :----------- | :-------------------------- |
| [sfdx force:source:deploy](https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_source.htm#cli_reference_force_source_deploy) | [sfdx hardis:source:deploy](https://sfdx-hardis.cloudity.com/hardis/source/deploy/) |
| [sfdx force:source:push](https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_source.htm#cli_reference_force_source_push) | [sfdx hardis:source:push](https://sfdx-hardis.cloudity.com/hardis/source/push/) |
| [sfdx force:mdapi:deploy](https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_mdapi.htm#cli_reference_force_mdapi_beta_deploy) | [sfdx hardis:mdapi:deploy](https://sfdx-hardis.cloudity.com/hardis/mdapi/deploy/) |
| sfdx command | sfdx-hardis wrapper command |
|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------------------------------------------------------|
| [sfdx force:source:deploy](https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_source.htm#cli_reference_force_source_deploy) | [sfdx hardis:source:deploy](https://sfdx-hardis.cloudity.com/hardis/source/deploy/) |
| [sfdx force:source:push](https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_source.htm#cli_reference_force_source_push) | [sfdx hardis:source:push](https://sfdx-hardis.cloudity.com/hardis/source/push/) |
| [sfdx force:mdapi:deploy](https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_mdapi.htm#cli_reference_force_mdapi_beta_deploy) | [sfdx hardis:mdapi:deploy](https://sfdx-hardis.cloudity.com/hardis/mdapi/deploy/) |

You can also use this function on a [sfdx-hardis Salesforce CI/CD project](https://sfdx-hardis.cloudity.com/salesforce-ci-cd-home/)

Expand Down Expand Up @@ -302,7 +302,7 @@ You probably also need to add CRM Analytics Admin Permission Set assignment to t
## Error parsing file
- `Error (.*) Error parsing file: (.*) `
- `Error (.*) Error parsing file: (.*)`
**Resolution tip**
Expand Down Expand Up @@ -902,7 +902,7 @@ Please check https://developer.salesforce.com/forums/?id=9060G0000005kVLQAY
## Test classes with 0% coverage
- ` 0%`
- `0%`
**Resolution tip**
Expand Down
4 changes: 2 additions & 2 deletions docs/hardis/project/deploy/sources/dx.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ ENV PUPPETEER_EXECUTABLE_PATH="$\{CHROMIUM_PATH}" // remove \ before {

## Parameters

| Name | Type | Description | Default | Required | Options |
| Name | Type | Description | Default | Required | Options |
|:----------------------|:-------:|:---------------------------------------------------------------------|:-------------:|:--------:|:---------------------------------------------------------------------------------------------:|
| apiversion | option | override the api version used for api requests made by this command | | | |
| check<br/>-c | boolean | Only checks the deployment, there is no impact on target org | | | |
| debug<br/>-d | boolean | Activate debug mode (more logs) | | | |
| json | boolean | format output as json | | | |
| loglevel | option | logging level for this command invocation | warn | | trace<br/>debug<br/>info<br/>warn<br/>error<br/>fatal |
| loglevel | option | logging level for this command invocation | warn | | trace<br/>debug<br/>info<br/>warn<br/>error<br/>fatal |
| packagexml<br/>-p | option | Path to package.xml containing what you want to deploy in target org | | | |
| skipauth | boolean | Skip authentication check when a default username is required | | | |
| targetusername<br/>-u | option | username or alias for the target org; overrides default target org | | | |
Expand Down
37 changes: 37 additions & 0 deletions docs/salesforce-ci-cd-setup-integration-slack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: Configure Integrations between sfdx-hardis and Slack
description: Send notifications on slack channels during CI/CD operations
---
<!-- markdownlint-disable MD013 -->

## Slack Integration

You can receive notifications on slack channels when CI/CD events are happening:
- Deployment from a major branch to a major Salesforce org (ex: integration git branch to Integration Org)
- More soon (ask for them !)

## Configure Slack Application

### Create slack app

> Process only if a sfdx-hardis bot has not yet been configured on your slack. Otherwise, just request the slack token value to your slack administrator
Create a slack app here -> <https://api.slack.com/apps>

- Name it `sfdx-hardis bot`` or _any nickname you like_, like your guinea pig name !
- Go to permissions and add the following scopes
- chat-write
- chat-write.customize
- chat-write.public
- Create auth token and copy its values

### Configure sfdx-hardis for slack

- Create a secret value named **SLACK_TOKEN** with auth token value in your Git provider configuration
- Create a slack channel that will receive all notifications (ex: _#notifs-sfdx-hardis_)
- Open the channel info, copy its ID and create a secret value named **SLACK_CHANNEL_ID** in your git provider configuration
- Additionally, you can create branch-scoped channels by creating new channels and create appropriate variables
- Example: Channel _#notifs-sfdx-hardis-integration_ and variable **SLACK_CHANNEL_ID_iNTEGRATION**
- Make sure all those variables are visible to your CI/CD pipelines

That's all, you're all set !
6 changes: 4 additions & 2 deletions docs/salesforce-ci-cd-setup-integrations-home.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ Depending of your git provider, configure one of the following integrations.

## Message notifications

- [Slack](salesforce-ci-cd-setup-integration-slack.md)
- Notifications

- [Microsoft Teams](salesforce-ci-cd-setup-integration-ms-teams.md)
- Notifications
- Alerts

- Slack
- Coming soon !

3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,10 @@ nav:
- Init from Existing Org: salesforce-ci-cd-setup-existing-org.md
- Integrations:
- Integrations Home: salesforce-ci-cd-setup-integrations-home.md
- GitHub: salesforce-ci-cd-setup-integration-github.md
- GitHub: salesforce-ci-cd-setup-integration-github.md
- Gitlab: salesforce-ci-cd-setup-integration-gitlab.md
- Azure DevOps: salesforce-ci-cd-setup-integration-azure.md
- Slack: salesforce-ci-cd-setup-integration-slack.md
- Microsoft Teams: salesforce-ci-cd-setup-integration-ms-teams.md
- First merge request: salesforce-ci-cd-setup-merge-request.md
- Configuration reference: sfdx-hardis-config-file.md
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@salesforce/core": "^2.33.1",
"@salesforce/ts-sinon": "^1.2.4",
"@salesforce/ts-types": "^1.5.20",
"@slack/web-api": "^6.9.0",
"@supercharge/promise-pool": "^1.7.0",
"@types/mocha": "^8.2.1",
"@types/ws": "^7.4.0",
Expand Down
48 changes: 38 additions & 10 deletions src/commands/hardis/project/deploy/sources/dx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import { AnyJson } from "@salesforce/ts-types";
import * as c from "chalk";
import * as fs from "fs-extra";
import { MetadataUtils } from "../../../../../common/metadata-utils";
import { isCI, uxLog } from "../../../../../common/utils";
import { getCurrentGitBranch, isCI, uxLog } from "../../../../../common/utils";
import { getConfig } from "../../../../../config";
import { forceSourceDeploy } from "../../../../../common/utils/deployUtils";
import { promptOrg } from "../../../../../common/utils/orgUtils";
import { getApexTestClasses } from "../../../../../common/utils/classUtils";
import { restoreListViewMine } from "../../../../../common/utils/orgConfigUtils";
import { NotifProvider } from "../../../../../common/notifProvider";
import { GitProvider } from "../../../../../common/gitProvider";

// Initialize Messages with the current plugin directory
Messages.importMessagesDirectory(__dirname);
Expand Down Expand Up @@ -198,23 +200,23 @@ If you need to increase the deployment waiting time (force:source:deploy --wait
let testClasses = this.flags.runtests || this.configInfo.runtests || "";

// Auto-detect all APEX test classes within project in order to run "dynamic" RunSpecifiedTests deployment
if(givenTestlevel === 'RunRepositoryTests') {
if (givenTestlevel === "RunRepositoryTests") {
const testClassList = await getApexTestClasses();
if(Array.isArray(testClassList) && testClassList.length) {
this.flags.testlevel = 'RunSpecifiedTests';
if (Array.isArray(testClassList) && testClassList.length) {
this.flags.testlevel = "RunSpecifiedTests";
testClasses = testClassList.join();
} else {
// Default back to RunLocalTests in case if repository has zero tests
this.flags.testlevel = 'RunLocalTests';
testClasses = '';
this.flags.testlevel = "RunLocalTests";
testClasses = "";
}
}

const testlevel = this.flags.testlevel || this.configInfo.testLevel || "RunLocalTests";

// Test classes are only valid for RunSpecifiedTests
if(testlevel != 'RunSpecifiedTests') {
testClasses = '';
if (testlevel != "RunSpecifiedTests") {
testClasses = "";
}

const packageXml = this.flags.packagexml || null;
Expand Down Expand Up @@ -257,7 +259,7 @@ If you need to increase the deployment waiting time (force:source:deploy --wait
const forceSourceDeployOptions: any = {
targetUsername: targetUsername,
conn: this.org?.getConnection(),
testClasses: testClasses
testClasses: testClasses,
};
// Get destructiveChanges.xml and add it in options if existing
const packageDeletedXmlFile =
Expand Down Expand Up @@ -301,6 +303,32 @@ If you need to increase the deployment waiting time (force:source:deploy --wait
await restoreListViewMine(this.configInfo.listViewsToSetToMine, this.org.getConnection(), { debug: this.debugMode });
}

// Send notification of deployment success
if (!check) {
const targetLabel = this.org?.getConnection()?.getUsername() === targetUsername ? this.org?.getConnection()?.instanceUrl : targetUsername;
const linkMarkdown = `<${targetLabel}|*${targetLabel.replace("https://", "").replace(".my.salesforce.com", "")}*>`;
const currentGitBranch = await getCurrentGitBranch();
let branchMd = `*${currentGitBranch}*`;
const branchUrl = await GitProvider.getCurrentBranchUrl();
if (branchUrl) {
branchMd = `<${branchUrl}|*${currentGitBranch}*>`;
}
let notifMessage = `Deployment has been successfully processed from branch ${branchMd} to org ${linkMarkdown}`;
const notifButtons = [];
const jobUrl = await GitProvider.getJobUrl();
if (jobUrl) {
notifButtons.push({ text: "View Deployment Job", url: jobUrl });
}
const pullRequestInfo = await GitProvider.getPullRequestInfo();
if (pullRequestInfo) {
const prUrl = pullRequestInfo.web_url || pullRequestInfo.html_url || pullRequestInfo.url;
const prAuthor = pullRequestInfo?.author?.login || pullRequestInfo?.author?.name || null;
notifMessage += `\nRelated: <${prUrl}|${pullRequestInfo.title}>` + (prAuthor ? ` by ${prAuthor}` : "");
const prButtonText = "View Pull Request";
notifButtons.push({ text: prButtonText, url: prUrl });
}
NotifProvider.postNotifications(notifMessage, notifButtons);
}
return { orgId: this.org.getOrgId(), outputString: messages.join("\n") };
}
}
15 changes: 15 additions & 0 deletions src/common/gitProvider/gitProviderRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ export abstract class GitProviderRoot {
return null;
}

public async getCurrentJobUrl(): Promise<string> {
uxLog(this, `Method getCurrentJobUrl is not implemented yet on ${this.getLabel()}`);
return null;
}

public async getCurrentBranchUrl(): Promise<string> {
uxLog(this, `Method getCurrentBranchUrl is not implemented yet on ${this.getLabel()}`);
return null;
}

public async getPullRequestInfo(): Promise<any> {
uxLog(this, `Method getPullRequestInfo is not implemented yet on ${this.getLabel()}`);
return null;
}

public async postPullRequestMessage(prMessage: PullRequestMessageRequest): Promise<PullRequestMessageResult> {
uxLog(this, c.yellow("Method postPullRequestMessage is not yet implemented on " + this.getLabel() + " to post " + JSON.stringify(prMessage)));
return { posted: false, providerResult: { error: "Not implemented in sfdx-hardis" } };
Expand Down
Loading

0 comments on commit c2d02ba

Please sign in to comment.