@fabasoad/sarif-to-slack 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +1 -0
- package/.github/CODEOWNERS +1 -0
- package/.github/FUNDING.yml +9 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +26 -0
- package/.github/dependabot.yml +11 -0
- package/.github/pull_request_template.md +59 -0
- package/.github/workflows/linting.yml +18 -0
- package/.github/workflows/release.yml +75 -0
- package/.github/workflows/security.yml +19 -0
- package/.github/workflows/sync-labels.yml +13 -0
- package/.github/workflows/unit-tests.yml +22 -0
- package/.github/workflows/update-license.yml +12 -0
- package/.markdownlint.yml +9 -0
- package/.markdownlintignore +1 -0
- package/.pre-commit-config.yaml +105 -0
- package/.tool-versions +1 -0
- package/.yamllint.yml +7 -0
- package/CONTRIBUTING.md +61 -0
- package/LICENSE +21 -0
- package/Makefile +42 -0
- package/README.md +34 -0
- package/api-extractor.json +454 -0
- package/biome.json +81 -0
- package/dist/sarif-to-slack.d.ts +175 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/etc/sarif-to-slack.api.md +61 -0
- package/jest.config.json +33 -0
- package/package.json +54 -0
- package/sample.png +0 -0
- package/src/Logger.ts +34 -0
- package/src/Processors.ts +100 -0
- package/src/SarifToSlackService.ts +106 -0
- package/src/SlackMessageBuilder.ts +176 -0
- package/src/index.ts +52 -0
- package/src/types.ts +94 -0
- package/tests/Processors.spec.ts +115 -0
- package/tsconfig.json +21 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sarif to Slack message converter library.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* This library provides a service to send a Slack messages based on the provided
|
|
6
|
+
* SARIF (Static Analysis Results Interchange Format) files.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { SarifToSlackService } from 'sarif-to-slack';
|
|
11
|
+
*
|
|
12
|
+
* const service = new SarifToSlackService({
|
|
13
|
+
* webhookUrl: 'https://hooks.slack.com/services/your/webhook/url',
|
|
14
|
+
* sarifPath: 'path/to/your/sarif/file.sarif',
|
|
15
|
+
* logLevel: 'info',
|
|
16
|
+
* username: 'SARIF Bot',
|
|
17
|
+
* iconUrl: 'https://example.com/icon.png',
|
|
18
|
+
* color: '#36a64f',
|
|
19
|
+
* header: {
|
|
20
|
+
* include: true,
|
|
21
|
+
* value: 'SARIF Analysis Results'
|
|
22
|
+
* },
|
|
23
|
+
* footer: {
|
|
24
|
+
* include: true,
|
|
25
|
+
* value: 'Generated by @fabasoad/sarif-to-slack'
|
|
26
|
+
* },
|
|
27
|
+
* actor: {
|
|
28
|
+
* include: true,
|
|
29
|
+
* value: 'fabasoad'
|
|
30
|
+
* },
|
|
31
|
+
* run: {
|
|
32
|
+
* include: true
|
|
33
|
+
* },
|
|
34
|
+
* });
|
|
35
|
+
* await service.sendAll();
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @see {@link SarifToSlackService}
|
|
39
|
+
*
|
|
40
|
+
* @packageDocumentation
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
import type { Log } from 'sarif';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Type representing properties that indicate whether to include certain information
|
|
47
|
+
* in the Slack message.
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
export declare type IncludeAwareProps = {
|
|
51
|
+
include: boolean;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Type representing properties that indicate whether to include certain information
|
|
56
|
+
* in the Slack message, along with an optional value.
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
export declare type IncludeAwareWithValueProps = IncludeAwareProps & {
|
|
60
|
+
value?: string;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Enum representing log levels for the service.
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
67
|
+
export declare enum LogLevel {
|
|
68
|
+
/**
|
|
69
|
+
* Represents the most verbose logging level, typically used for detailed debugging information.
|
|
70
|
+
*/
|
|
71
|
+
Silly = 0,
|
|
72
|
+
/**
|
|
73
|
+
* Represents a logging level for tracing the flow of the application.
|
|
74
|
+
*/
|
|
75
|
+
Trace = 1,
|
|
76
|
+
/**
|
|
77
|
+
* Represents a logging level for debugging information that is less verbose than silly.
|
|
78
|
+
*/
|
|
79
|
+
Debug = 2,
|
|
80
|
+
/**
|
|
81
|
+
* Represents a logging level for general informational messages that highlight the progress of the application.
|
|
82
|
+
*/
|
|
83
|
+
Info = 3,
|
|
84
|
+
/**
|
|
85
|
+
* Represents a logging level for potentially harmful situations that require attention.
|
|
86
|
+
*/
|
|
87
|
+
Warning = 4,
|
|
88
|
+
/**
|
|
89
|
+
* Represents a logging level for error conditions that do not require immediate action but should be noted.
|
|
90
|
+
*/
|
|
91
|
+
Error = 5,
|
|
92
|
+
/**
|
|
93
|
+
* Represents a logging level for critical errors that require immediate attention and may cause the application to terminate.
|
|
94
|
+
*/
|
|
95
|
+
Fatal = 6
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Type representing a SARIF log.
|
|
100
|
+
* @public
|
|
101
|
+
*/
|
|
102
|
+
export declare type Sarif = Log;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Service to convert SARIF files to Slack messages and send them.
|
|
106
|
+
* @public
|
|
107
|
+
*/
|
|
108
|
+
export declare class SarifToSlackService {
|
|
109
|
+
private _slackMessages;
|
|
110
|
+
private constructor();
|
|
111
|
+
/**
|
|
112
|
+
* Gets the Slack messages prepared for each SARIF file.
|
|
113
|
+
* @returns A read-only map where keys are SARIF file paths and values are SlackMessage instances.
|
|
114
|
+
* @public
|
|
115
|
+
*/
|
|
116
|
+
get slackMessages(): ReadonlyMap<string, SlackMessage>;
|
|
117
|
+
/**
|
|
118
|
+
* Creates an instance of SarifToSlackService.
|
|
119
|
+
* @param opts - Options for the service, including webhook URL, SARIF path, and other configurations.
|
|
120
|
+
* @returns A promise that resolves to an instance of SarifToSlackService.
|
|
121
|
+
* @throws Error if no SARIF files are found at the provided path.
|
|
122
|
+
* @public
|
|
123
|
+
*/
|
|
124
|
+
static create(opts: SarifToSlackServiceOptions): Promise<SarifToSlackService>;
|
|
125
|
+
/**
|
|
126
|
+
* Sends all prepared Slack messages.
|
|
127
|
+
* @returns A promise that resolves when all messages have been sent.
|
|
128
|
+
* @throws Error if a Slack message was not prepared for a SARIF path.
|
|
129
|
+
* @public
|
|
130
|
+
*/
|
|
131
|
+
sendAll(): Promise<void>;
|
|
132
|
+
/**
|
|
133
|
+
* Sends a Slack message for a specific SARIF path.
|
|
134
|
+
* @param sarifPath - The path of the SARIF file for which the message should be sent.
|
|
135
|
+
* @returns A promise that resolves when the message has been sent.
|
|
136
|
+
* @throws Error if a Slack message was not prepared for the given SARIF path.
|
|
137
|
+
* @public
|
|
138
|
+
*/
|
|
139
|
+
send(sarifPath: string): Promise<void>;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Options for the SarifToSlackService.
|
|
144
|
+
* @public
|
|
145
|
+
*/
|
|
146
|
+
export declare type SarifToSlackServiceOptions = {
|
|
147
|
+
webhookUrl: string;
|
|
148
|
+
sarifPath: string;
|
|
149
|
+
username?: string;
|
|
150
|
+
iconUrl?: string;
|
|
151
|
+
color?: string;
|
|
152
|
+
logLevel?: LogLevel | string;
|
|
153
|
+
header?: IncludeAwareWithValueProps;
|
|
154
|
+
footer?: IncludeAwareWithValueProps;
|
|
155
|
+
actor?: IncludeAwareWithValueProps;
|
|
156
|
+
run?: IncludeAwareProps;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Interface for a Slack message that can be sent.
|
|
161
|
+
* @public
|
|
162
|
+
*/
|
|
163
|
+
export declare interface SlackMessage {
|
|
164
|
+
/**
|
|
165
|
+
* Sends the Slack message.
|
|
166
|
+
* @returns A promise that resolves to the response from the Slack webhook.
|
|
167
|
+
*/
|
|
168
|
+
send: () => Promise<string>;
|
|
169
|
+
/**
|
|
170
|
+
* The SARIF log associated with this Slack message.
|
|
171
|
+
*/
|
|
172
|
+
sarif: Sarif;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export { }
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
+
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
+
{
|
|
4
|
+
"tsdocVersion": "0.12",
|
|
5
|
+
"toolPackages": [
|
|
6
|
+
{
|
|
7
|
+
"packageName": "@microsoft/api-extractor",
|
|
8
|
+
"packageVersion": "7.52.8"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
## API Report File for "@fabasoad/sarif-to-slack"
|
|
2
|
+
|
|
3
|
+
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
|
|
7
|
+
import type { Log } from 'sarif';
|
|
8
|
+
|
|
9
|
+
// @public
|
|
10
|
+
export type IncludeAwareProps = {
|
|
11
|
+
include: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// @public
|
|
15
|
+
export type IncludeAwareWithValueProps = IncludeAwareProps & {
|
|
16
|
+
value?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// @public
|
|
20
|
+
export enum LogLevel {
|
|
21
|
+
Debug = 2,
|
|
22
|
+
Error = 5,
|
|
23
|
+
Fatal = 6,
|
|
24
|
+
Info = 3,
|
|
25
|
+
Silly = 0,
|
|
26
|
+
Trace = 1,
|
|
27
|
+
Warning = 4
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// @public
|
|
31
|
+
export type Sarif = Log;
|
|
32
|
+
|
|
33
|
+
// @public
|
|
34
|
+
export class SarifToSlackService {
|
|
35
|
+
static create(opts: SarifToSlackServiceOptions): Promise<SarifToSlackService>;
|
|
36
|
+
send(sarifPath: string): Promise<void>;
|
|
37
|
+
sendAll(): Promise<void>;
|
|
38
|
+
get slackMessages(): ReadonlyMap<string, SlackMessage>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// @public
|
|
42
|
+
export type SarifToSlackServiceOptions = {
|
|
43
|
+
webhookUrl: string;
|
|
44
|
+
sarifPath: string;
|
|
45
|
+
username?: string;
|
|
46
|
+
iconUrl?: string;
|
|
47
|
+
color?: string;
|
|
48
|
+
logLevel?: LogLevel | string;
|
|
49
|
+
header?: IncludeAwareWithValueProps;
|
|
50
|
+
footer?: IncludeAwareWithValueProps;
|
|
51
|
+
actor?: IncludeAwareWithValueProps;
|
|
52
|
+
run?: IncludeAwareProps;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// @public
|
|
56
|
+
export interface SlackMessage {
|
|
57
|
+
sarif: Sarif;
|
|
58
|
+
send: () => Promise<string>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
```
|
package/jest.config.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"clearMocks": true,
|
|
3
|
+
"collectCoverageFrom": [
|
|
4
|
+
"**/*.ts",
|
|
5
|
+
"!**/*.d.ts",
|
|
6
|
+
"!**/node_modules/**"
|
|
7
|
+
],
|
|
8
|
+
"coverageReporters": [
|
|
9
|
+
"lcov", "text", "text-summary"
|
|
10
|
+
],
|
|
11
|
+
"coverageThreshold": {
|
|
12
|
+
"global": {
|
|
13
|
+
"branches": 20,
|
|
14
|
+
"functions": 15,
|
|
15
|
+
"lines": 25,
|
|
16
|
+
"statements": 25
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"moduleFileExtensions": [
|
|
20
|
+
"js",
|
|
21
|
+
"ts"
|
|
22
|
+
],
|
|
23
|
+
"testEnvironment": "node",
|
|
24
|
+
"testMatch": [
|
|
25
|
+
"**/*.spec.ts"
|
|
26
|
+
],
|
|
27
|
+
"testRunner": "jest-circus/runner",
|
|
28
|
+
"testTimeout": 20000,
|
|
29
|
+
"transform": {
|
|
30
|
+
"^.+\\.ts$": "ts-jest"
|
|
31
|
+
},
|
|
32
|
+
"verbose": true
|
|
33
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fabasoad/sarif-to-slack",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "TypeScript library to send results of SARIF file to Slack webhook URL.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"lint": "biome lint --write src",
|
|
9
|
+
"test": "jest --config=jest.config.json --json --outputFile=jest-report.json --coverage",
|
|
10
|
+
"clean": "rm -rf coverage && rm -rf temp",
|
|
11
|
+
"clean:unsafe": "rm -f package-lock.json && rm -rf node_modules && rm -rf dist && rm -rf lib",
|
|
12
|
+
"build": "tsc && api-extractor run --local --verbose",
|
|
13
|
+
"prepublishOnly": "npm run build",
|
|
14
|
+
"version:patch": "npm version patch --commit-hooks --git-tag-version --message 'chore: bump to version %s'",
|
|
15
|
+
"version:minor": "npm version minor --commit-hooks --git-tag-version --message 'chore: bump to version %s'",
|
|
16
|
+
"version:major": "npm version major --commit-hooks --git-tag-version --message 'chore: bump to version %s'",
|
|
17
|
+
"preversion": "npm test",
|
|
18
|
+
"version": "npm run build && git add .",
|
|
19
|
+
"postversion": "git push && git push --tags && npm run clean"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/fabasoad/sarif-to-slack.git"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"npm",
|
|
27
|
+
"sarif",
|
|
28
|
+
"slack"
|
|
29
|
+
],
|
|
30
|
+
"author": "Yevhen Fabizhevskyi",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/fabasoad/sarif-to-slack-action/issues"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/fabasoad/sarif-to-slack-action#readme",
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@slack/webhook": "7.0.5",
|
|
41
|
+
"@types/sarif": "2.1.7",
|
|
42
|
+
"tslog": "4.9.3"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@biomejs/biome": "1.9.4",
|
|
46
|
+
"@microsoft/api-documenter": "7.26.29",
|
|
47
|
+
"@microsoft/api-extractor": "7.52.8",
|
|
48
|
+
"@types/jest": "30.0.0",
|
|
49
|
+
"jest": "30.0.3",
|
|
50
|
+
"jest-circus": "30.0.3",
|
|
51
|
+
"ts-jest": "29.4.0",
|
|
52
|
+
"typescript": "5.8.3"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/sample.png
ADDED
|
Binary file
|
package/src/Logger.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Logger as TSLogger, ILogObj } from 'tslog'
|
|
2
|
+
import { LogLevel } from './types'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Logger options for configuring the logging behavior.
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export type LoggerOptions = {
|
|
9
|
+
logLevel?: LogLevel
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Logger class for managing logging operations.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export default class Logger {
|
|
17
|
+
private static instance: TSLogger<ILogObj> = new TSLogger();
|
|
18
|
+
|
|
19
|
+
public static initialize({ logLevel = LogLevel.Info }: LoggerOptions): void {
|
|
20
|
+
if (!Logger.instance) {
|
|
21
|
+
Logger.instance = new TSLogger({
|
|
22
|
+
minLevel: process.env.ACTIONS_STEP_DEBUG === 'true' ? 0 : logLevel,
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public static info(...args: string[]): void {
|
|
28
|
+
Logger.instance.info(args)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public static debug(...args: string[]): void {
|
|
32
|
+
Logger.instance.debug(args)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import * as fs from 'fs'
|
|
2
|
+
import * as path from 'path'
|
|
3
|
+
import Logger from './Logger'
|
|
4
|
+
import { LogLevel } from './types'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Processes a color string and converts it to a specific hex code if it matches
|
|
8
|
+
* a CI status identifier.
|
|
9
|
+
* @param color - The color string to process, which can be a CI status identifier
|
|
10
|
+
* or a custom color.
|
|
11
|
+
* @returns The processed color as a hex string or undefined if the input is not
|
|
12
|
+
* a recognized CI status identifier.
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export function processColor(color?: string): string | undefined {
|
|
16
|
+
switch (color) {
|
|
17
|
+
case 'success':
|
|
18
|
+
Logger.info(`Converting "${color}" to #008000`)
|
|
19
|
+
return '#008000'
|
|
20
|
+
case 'failure':
|
|
21
|
+
Logger.info(`Converting "${color}" to #ff0000`)
|
|
22
|
+
return '#ff0000'
|
|
23
|
+
case 'cancelled':
|
|
24
|
+
Logger.info(`Converting "${color}" to #0047ab`)
|
|
25
|
+
return '#0047ab'
|
|
26
|
+
case 'skipped':
|
|
27
|
+
Logger.info(`Converting "${color}" to #808080`)
|
|
28
|
+
return '#808080'
|
|
29
|
+
default:
|
|
30
|
+
Logger.debug(`"${color}" color is not a CI status identifier. Returning as is...`)
|
|
31
|
+
return color
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Processes a log level string or number and converts it to a numeric log level.
|
|
37
|
+
* @param logLevel
|
|
38
|
+
* @returns The numeric log level corresponding to the input string or number.
|
|
39
|
+
* @throws Error If the input string does not match any known log level.
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
export function processLogLevel(logLevel?: LogLevel | string): LogLevel | undefined {
|
|
43
|
+
if (typeof logLevel === 'string') {
|
|
44
|
+
switch (logLevel.toLowerCase()) {
|
|
45
|
+
case 'silly':
|
|
46
|
+
return 0
|
|
47
|
+
case 'trace':
|
|
48
|
+
return 1
|
|
49
|
+
case 'debug':
|
|
50
|
+
return 2
|
|
51
|
+
case 'info':
|
|
52
|
+
return 3
|
|
53
|
+
case 'warning':
|
|
54
|
+
return 4
|
|
55
|
+
case 'error':
|
|
56
|
+
return 5
|
|
57
|
+
case 'fatal':
|
|
58
|
+
return 6
|
|
59
|
+
default:
|
|
60
|
+
throw new Error(`Unknown log level: ${logLevel}`)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return logLevel
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Processes the SARIF path, which can be a file or a directory. If it's a
|
|
68
|
+
* directory, it returns an array of paths to all `.sarif` files, otherwise it
|
|
69
|
+
* returns an array with a single path to the file.
|
|
70
|
+
* @param sarifPath - The path to the SARIF file or directory.
|
|
71
|
+
* @returns An array of strings representing the paths to the SARIF files.
|
|
72
|
+
* @throws Error If the path does not exist, or if it is neither a file nor a
|
|
73
|
+
* directory.
|
|
74
|
+
* @internal
|
|
75
|
+
*/
|
|
76
|
+
export function processSarifPath(sarifPath: string): string[] {
|
|
77
|
+
if (!fs.existsSync(sarifPath)) {
|
|
78
|
+
throw new Error(`"sarif-path" does not exist: ${sarifPath}`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const sarifStats: fs.Stats = fs.statSync(sarifPath)
|
|
82
|
+
|
|
83
|
+
if (sarifStats.isDirectory()) {
|
|
84
|
+
Logger.info(`"sarif-path" is a directory: ${sarifPath}`)
|
|
85
|
+
const files: string[] = fs.readdirSync(sarifPath)
|
|
86
|
+
const filteredFiles: string[] = files.filter((file: string) =>
|
|
87
|
+
path.extname(file).toLowerCase() === '.sarif'
|
|
88
|
+
)
|
|
89
|
+
Logger.info(`Found ${filteredFiles.length} SARIF files in ${sarifPath} directory`)
|
|
90
|
+
Logger.debug(`Filtered SARIF files: ${filteredFiles.join(', ')}`)
|
|
91
|
+
return filteredFiles.map((file: string) => path.join(sarifPath, file))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (sarifStats.isFile()) {
|
|
95
|
+
Logger.info(`"sarif-path" is a file: ${sarifPath}`)
|
|
96
|
+
return [sarifPath]
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
throw new Error(`"sarif-path" is neither a file nor a directory: ${sarifPath}`)
|
|
100
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
import Logger from './Logger'
|
|
3
|
+
import { processColor, processLogLevel, processSarifPath } from './Processors'
|
|
4
|
+
import { SlackMessageBuilder } from './SlackMessageBuilder'
|
|
5
|
+
import {
|
|
6
|
+
Sarif,
|
|
7
|
+
SarifToSlackServiceOptions,
|
|
8
|
+
SlackMessage
|
|
9
|
+
} from './types'
|
|
10
|
+
|
|
11
|
+
async function initialize(opts: SarifToSlackServiceOptions): Promise<Map<string, SlackMessage>> {
|
|
12
|
+
const slackMessages = new Map<string, SlackMessage>();
|
|
13
|
+
const sarifFiles: string[] = processSarifPath(opts.sarifPath)
|
|
14
|
+
if (sarifFiles.length === 0) {
|
|
15
|
+
throw new Error(`No SARIF files found at the provided path: ${opts.sarifPath}`)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
for (const sarifFile of sarifFiles) {
|
|
19
|
+
const jsonString: string = await fs.readFile(sarifFile, 'utf8')
|
|
20
|
+
|
|
21
|
+
const messageBuilder = new SlackMessageBuilder(opts.webhookUrl, {
|
|
22
|
+
username: opts.username,
|
|
23
|
+
iconUrl: opts.iconUrl,
|
|
24
|
+
color: processColor(opts.color),
|
|
25
|
+
sarif: JSON.parse(jsonString) as Sarif
|
|
26
|
+
})
|
|
27
|
+
if (opts.header?.include) {
|
|
28
|
+
messageBuilder.withHeader(opts.header?.value)
|
|
29
|
+
}
|
|
30
|
+
if (opts.footer?.include) {
|
|
31
|
+
messageBuilder.withFooter(opts.footer?.value)
|
|
32
|
+
}
|
|
33
|
+
if (opts.actor?.include) {
|
|
34
|
+
messageBuilder.withActor(opts.actor?.value)
|
|
35
|
+
}
|
|
36
|
+
if (opts.run?.include) {
|
|
37
|
+
messageBuilder.withRun()
|
|
38
|
+
}
|
|
39
|
+
slackMessages.set(sarifFile, messageBuilder)
|
|
40
|
+
}
|
|
41
|
+
return slackMessages;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Service to convert SARIF files to Slack messages and send them.
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export class SarifToSlackService {
|
|
49
|
+
private _slackMessages: ReadonlyMap<string, SlackMessage>;
|
|
50
|
+
|
|
51
|
+
private constructor() {
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Gets the Slack messages prepared for each SARIF file.
|
|
56
|
+
* @returns A read-only map where keys are SARIF file paths and values are SlackMessage instances.
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
public get slackMessages(): ReadonlyMap<string, SlackMessage> {
|
|
60
|
+
return this._slackMessages;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Creates an instance of SarifToSlackService.
|
|
65
|
+
* @param opts - Options for the service, including webhook URL, SARIF path, and other configurations.
|
|
66
|
+
* @returns A promise that resolves to an instance of SarifToSlackService.
|
|
67
|
+
* @throws Error if no SARIF files are found at the provided path.
|
|
68
|
+
* @public
|
|
69
|
+
*/
|
|
70
|
+
public static async create(opts: SarifToSlackServiceOptions): Promise<SarifToSlackService> {
|
|
71
|
+
Logger.initialize({
|
|
72
|
+
logLevel: processLogLevel(opts.logLevel)
|
|
73
|
+
})
|
|
74
|
+
const instance: SarifToSlackService = new SarifToSlackService()
|
|
75
|
+
instance._slackMessages = await initialize(opts)
|
|
76
|
+
return instance
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Sends all prepared Slack messages.
|
|
81
|
+
* @returns A promise that resolves when all messages have been sent.
|
|
82
|
+
* @throws Error if a Slack message was not prepared for a SARIF path.
|
|
83
|
+
* @public
|
|
84
|
+
*/
|
|
85
|
+
public async sendAll(): Promise<void> {
|
|
86
|
+
for (const sarifPath of this._slackMessages.keys()) {
|
|
87
|
+
await this.send(sarifPath);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Sends a Slack message for a specific SARIF path.
|
|
93
|
+
* @param sarifPath - The path of the SARIF file for which the message should be sent.
|
|
94
|
+
* @returns A promise that resolves when the message has been sent.
|
|
95
|
+
* @throws Error if a Slack message was not prepared for the given SARIF path.
|
|
96
|
+
* @public
|
|
97
|
+
*/
|
|
98
|
+
public async send(sarifPath: string): Promise<void> {
|
|
99
|
+
const message: SlackMessage | undefined = this._slackMessages.get(sarifPath)
|
|
100
|
+
if (!message) {
|
|
101
|
+
throw new Error(`Slack message was not prepared for SARIF path: ${sarifPath}.`)
|
|
102
|
+
}
|
|
103
|
+
const text: string = await message.send()
|
|
104
|
+
Logger.info(`Message sent for ${sarifPath} file. Status:`, text)
|
|
105
|
+
}
|
|
106
|
+
}
|