Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

feat(events): new event stream for build command #1446

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/display/DeployErrorDisplayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Table = require('cli-table');
import { CodeCoverageWarnings, DeployMessage, Failures, MetadataApiDeployStatus } from '@salesforce/source-deploy-retrieve';
import SFPLogger, { Logger, LoggerLevel } from '@dxatscale/sfp-logger';
import { ZERO_BORDER_TABLE } from './TableConstants';
import { ReleaseStreamService } from '../eventStream/release';

export default class DeployErrorDisplayer {
private static printMetadataFailedToDeploy(componentFailures: DeployMessage | DeployMessage[], logger: Logger) {
Expand All @@ -19,6 +20,7 @@ export default class DeployErrorDisplayer {
componentFailure.problemType,
componentFailure.problem,
];
ReleaseStreamService.buildDeployErrorsMsg(componentFailure.componentType, componentFailure.fullName, componentFailure.problemType, componentFailure.problem);
table.push(item);
};

Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/display/PackageDependencyDisplayer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import SFPLogger, { Logger, LoggerLevel } from '@dxatscale/sfp-logger';
import { ZERO_BORDER_TABLE } from './TableConstants';
const Table = require('cli-table');
import { BuildStreamService } from '../eventStream/build';

export default class PackageDependencyDisplayer {
public static printPackageDependencies(
dependencies: { package: string; versionNumber?: string }[],
projectConfig: any,
logger: Logger
logger: Logger,
pck?: string
) {
if (Array.isArray(dependencies)) {
SFPLogger.log('Package Dependencies:', LoggerLevel.INFO, logger);
Expand All @@ -27,6 +29,9 @@ export default class PackageDependencyDisplayer {

const row = [order,dependency.package, versionNumber];
table.push(row);
if(pck){
BuildStreamService.buildPackageDependencies(pck,{order:order, pck: dependency.package, version: versionNumber});
}
order++;
}
SFPLogger.log(table.toString(), LoggerLevel.INFO, logger);
Expand Down
264 changes: 264 additions & 0 deletions packages/core/src/eventStream/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
import fs from 'fs';
import { PROCESSNAME, PATH, EVENTTYPE, BuildProps, BuildHookSchema, BuildPackageDependencies } from './types';
import SfpPackage from '../package/SfpPackage';
import { HookService } from './hooks';

export class BuildStreamService {
public static buildPackageInitialitation(pck: string, reason: string, tag: string): void {
BuildLoggerBuilder.getInstance().buildPackageInitialitation(pck, reason, tag);

Check warning on line 8 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L7-L8

Added lines #L7 - L8 were not covered by tests
}

public static sendPackageError(sfpPackage: SfpPackage, message: string, isEvent?: boolean): void {
const file = BuildLoggerBuilder.getInstance().buildPackageError(sfpPackage, message).build();

Check warning on line 12 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L11-L12

Added lines #L11 - L12 were not covered by tests
if (!isEvent) HookService.getInstance().logEvent(file.payload.events[sfpPackage.package_name]);
}

public static buildPackageErrorList(pck: string): void {
BuildLoggerBuilder.getInstance().buildPackageErrorList(pck);

Check warning on line 17 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L16-L17

Added lines #L16 - L17 were not covered by tests
}

public static buildPackageSuccessList(pck: string): void {
BuildLoggerBuilder.getInstance().buildPackageSuccessList(pck);

Check warning on line 21 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L20-L21

Added lines #L20 - L21 were not covered by tests
}

public static buildPackageAwaitingList(pck: string[]): void {
BuildLoggerBuilder.getInstance().buildPackageAwaitingList(pck);

Check warning on line 25 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L24-L25

Added lines #L24 - L25 were not covered by tests
}

public static buildPackageCurrentlyProcessedList(pck: string[]): void {
BuildLoggerBuilder.getInstance().buildPackageCurrentlyProcessedList(pck);

Check warning on line 29 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L28-L29

Added lines #L28 - L29 were not covered by tests
}

public static sendPackageCompletedInfos(sfpPackage: SfpPackage): void {
const file = BuildLoggerBuilder.getInstance().buildPackageCompletedInfos(sfpPackage).build();
HookService.getInstance().logEvent(file.payload.events[sfpPackage.package_name]);

Check warning on line 34 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L32-L34

Added lines #L32 - L34 were not covered by tests
}

public static buildPackageDependencies(pck: string, dependencies: BuildPackageDependencies): void {
BuildLoggerBuilder.getInstance().buildPackageDependencies(pck, dependencies);

Check warning on line 38 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L37-L38

Added lines #L37 - L38 were not covered by tests
}

public static buildProps(props: BuildProps): void {
BuildLoggerBuilder.getInstance().buildProps(props);

Check warning on line 42 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L41-L42

Added lines #L41 - L42 were not covered by tests
}

public static buildStatus(status: 'success' | 'failed' | 'inprogress', message: string): void {
BuildLoggerBuilder.getInstance().buildStatus(status, message);

Check warning on line 46 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L45-L46

Added lines #L45 - L46 were not covered by tests
}

public static sendStatistics(scheduled: number, success: number, failed: number, elapsedTime: number): void {
const file = BuildLoggerBuilder.getInstance().buildStatistics(scheduled, success, failed, elapsedTime).build();

Check warning on line 50 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L49-L50

Added lines #L49 - L50 were not covered by tests
}

public static buildReleaseConfig(pcks: string[]): void {
BuildLoggerBuilder.getInstance().buildReleaseConfig(pcks);

Check warning on line 54 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L53-L54

Added lines #L53 - L54 were not covered by tests
}

public static buildPackageStatus(pck: string, status: 'success' | 'inprogress', elapsedTime?: number): void {
BuildLoggerBuilder.getInstance().buildPackageStatus(pck, status, elapsedTime);

Check warning on line 58 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L57-L58

Added lines #L57 - L58 were not covered by tests
}

public static buildJobAndOrgId(jobId: string, orgId: string, devhubAlias: string, commitId: string): void {
BuildLoggerBuilder.getInstance().buildOrgAndJobId(orgId, jobId, devhubAlias, commitId);

Check warning on line 62 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L61-L62

Added lines #L61 - L62 were not covered by tests
}

public static writeArtifatcs(): void {
const file = BuildLoggerBuilder.getInstance().build();

Check warning on line 66 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L65-L66

Added lines #L65 - L66 were not covered by tests
if (!fs.existsSync(PATH.DEFAULT)) {
fs.mkdirSync(PATH.DEFAULT);

Check warning on line 68 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L68

Added line #L68 was not covered by tests
}
if (!fs.existsSync(PATH.BUILD)) {
// File doesn't exist, create it
fs.writeFileSync(PATH.BUILD, JSON.stringify(file, null, 4), 'utf-8');

Check warning on line 72 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L72

Added line #L72 was not covered by tests
}
}
}

class BuildLoggerBuilder {
private file: BuildHookSchema;
private static instance: BuildLoggerBuilder;

private constructor() {
this.file = {

Check warning on line 82 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L81-L82

Added lines #L81 - L82 were not covered by tests
payload: {
processName: PROCESSNAME.BUILD,
scheduled: 0,
success: 0,
failed: 0,
elapsedTime: 0,
status: 'inprogress',
message: '',
releaseConfig: [],
awaitingDependencies: [],
currentlyProcessed: [],
successfullyProcessed: [],
failedToProcess: [],
instanceUrl: '',
events: {},
},
eventType: 'sfpowerscripts.build',
jobId: '',
devhubAlias: '',
commitId: '',
};
}

public static getInstance(): BuildLoggerBuilder {

Check warning on line 106 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L106

Added line #L106 was not covered by tests
if (!BuildLoggerBuilder.instance) {
BuildLoggerBuilder.instance = new BuildLoggerBuilder();

Check warning on line 108 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L108

Added line #L108 was not covered by tests
}

return BuildLoggerBuilder.instance;

Check warning on line 111 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L111

Added line #L111 was not covered by tests
}

buildOrgAndJobId(orgId: string, jobId: string, devhubAlias: string, commitId: string): BuildLoggerBuilder {
this.file.jobId = jobId;
this.file.payload.instanceUrl = orgId;
this.file.devhubAlias = devhubAlias;
this.file.commitId = commitId;
return this;

Check warning on line 119 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L114-L119

Added lines #L114 - L119 were not covered by tests
}

buildPackageInitialitation(pck: string, reason: string, tag: string): BuildLoggerBuilder {
this.file.payload.events[pck] = {

Check warning on line 123 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L122-L123

Added lines #L122 - L123 were not covered by tests
event: 'sfpowerscripts.build.progress',
context: {
command: 'sfpowerscript:orchestrator:build',
eventId: `${this.file.jobId}_${Date.now().toString()}`,
jobId: this.file.jobId,
timestamp: new Date(),
instanceUrl: this.file.payload.instanceUrl,
branch: this.file.payload.buildProps.branch,
commitId: this.file.commitId,
devHubAlias: this.file.devhubAlias,
eventType: EVENTTYPE.BUILD,
},
metadata: {
package: pck,
message: [],
elapsedTime: 0,
reasonToBuild: reason,
lastKnownTag: tag,
type: '',
versionNumber: '',
versionId: '',
testCoverage: 0,
coverageCheckPassed: false,
metadataCount: 0,
apexInPackage: false,
profilesInPackage: false,
sourceVersion: '',
packageDependencies: [],
},
};
HookService.getInstance().logEvent(this.file.payload.events[pck]);
return this;

Check warning on line 155 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L154-L155

Added lines #L154 - L155 were not covered by tests
}

buildPackageCompletedInfos(sfpPackage: SfpPackage): BuildLoggerBuilder {
this.file.payload.events[sfpPackage.package_name].event = 'sfpowerscripts.build.success';
this.file.payload.events[sfpPackage.package_name].metadata.type = sfpPackage.package_type;
this.file.payload.events[sfpPackage.package_name].metadata.versionNumber = sfpPackage.package_version_number;
this.file.payload.events[sfpPackage.package_name].metadata.versionId = sfpPackage.package_version_id;
this.file.payload.events[sfpPackage.package_name].metadata.testCoverage = sfpPackage.test_coverage;
this.file.payload.events[sfpPackage.package_name].metadata.coverageCheckPassed =

Check warning on line 164 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L158-L164

Added lines #L158 - L164 were not covered by tests
sfpPackage.has_passed_coverage_check;
this.file.payload.events[sfpPackage.package_name].metadata.metadataCount = sfpPackage.metadataCount;
this.file.payload.events[sfpPackage.package_name].metadata.apexInPackage = sfpPackage.isApexFound;
this.file.payload.events[sfpPackage.package_name].metadata.profilesInPackage = sfpPackage.isProfilesFound;
this.file.payload.events[sfpPackage.package_name].metadata.sourceVersion = sfpPackage.sourceVersion;
this.file.payload.events[sfpPackage.package_name].context.timestamp = new Date();
return this;

Check warning on line 171 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L166-L171

Added lines #L166 - L171 were not covered by tests
}

buildPackageError(sfpPackage: SfpPackage, message: string): BuildLoggerBuilder {
this.file.payload.events[sfpPackage.package_name].event = 'sfpowerscripts.build.failed';
this.file.payload.events[sfpPackage.package_name].metadata.type = sfpPackage.package_type;
this.file.payload.events[sfpPackage.package_name].context.timestamp = new Date();

Check warning on line 177 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L174-L177

Added lines #L174 - L177 were not covered by tests
if (message) {
this.file.payload.events[sfpPackage.package_name].metadata.message.push(message);

Check warning on line 179 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L179

Added line #L179 was not covered by tests
}
return this;

Check warning on line 181 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L181

Added line #L181 was not covered by tests
}

buildPackageErrorList(pcks: string): BuildLoggerBuilder {
this.file.payload.failedToProcess.push(pcks);
return this;

Check warning on line 186 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L184-L186

Added lines #L184 - L186 were not covered by tests
}

buildPackageSuccessList(pcks: string): BuildLoggerBuilder {
this.file.payload.successfullyProcessed.push(pcks);
return this;

Check warning on line 191 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L189-L191

Added lines #L189 - L191 were not covered by tests
}

buildPackageAwaitingList(pcks: string[]): BuildLoggerBuilder {
this.file.payload.awaitingDependencies = pcks;
return this;

Check warning on line 196 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L194-L196

Added lines #L194 - L196 were not covered by tests
}

buildPackageCurrentlyProcessedList(pcks: string[]): BuildLoggerBuilder {
this.file.payload.currentlyProcessed = pcks;
return this;

Check warning on line 201 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L199-L201

Added lines #L199 - L201 were not covered by tests
}

buildPackageDependencies(pck: string, dependencies: BuildPackageDependencies): BuildLoggerBuilder {
this.file.payload.events[pck].metadata.packageDependencies.push(dependencies);
return this;

Check warning on line 206 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L204-L206

Added lines #L204 - L206 were not covered by tests
}

buildProps(props: BuildProps): BuildLoggerBuilder {
this.file.payload.buildProps = { ...props };
return this;

Check warning on line 211 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L209-L211

Added lines #L209 - L211 were not covered by tests
}

buildStatus(status: 'inprogress' | 'success' | 'failed', message: string): BuildLoggerBuilder {
this.file.payload.status = status;
this.file.payload.message = message;

Check warning on line 216 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L214-L216

Added lines #L214 - L216 were not covered by tests
if (status === 'failed') {
Object.values(this.file.payload.events).forEach((value) => {

Check warning on line 218 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L218

Added line #L218 was not covered by tests
if (
value.event === 'sfpowerscripts.build.awaiting' ||
value.event === 'sfpowerscripts.build.progress'

Check warning on line 221 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L221

Added line #L221 was not covered by tests
) {
value.metadata.message.push(message);
value.event = 'sfpowerscripts.build.failed';

Check warning on line 224 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L223-L224

Added lines #L223 - L224 were not covered by tests
//HookService.getInstance().logEvent(this.file.payload.events[value.metadata.package]);
}
});
}
return this;

Check warning on line 229 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L229

Added line #L229 was not covered by tests
}

buildStatistics(scheduled: number, success: number, failed: number, elapsedTime: number): BuildLoggerBuilder {
this.file.payload.scheduled = success + failed;
this.file.payload.success = success;
this.file.payload.failed = failed;
this.file.payload.elapsedTime = elapsedTime;
this.file.payload.awaitingDependencies = [];

Check warning on line 237 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L232-L237

Added lines #L232 - L237 were not covered by tests
// set status to success when scheduled = success
if (this.file.payload.scheduled > 1 && this.file.payload.scheduled === this.file.payload.success) {
this.file.payload.status = 'success';

Check warning on line 240 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L240

Added line #L240 was not covered by tests
} else {
this.file.payload.status = 'failed';

Check warning on line 242 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L242

Added line #L242 was not covered by tests
}
return this;

Check warning on line 244 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L244

Added line #L244 was not covered by tests
}

buildReleaseConfig(pcks: string[]): BuildLoggerBuilder {
this.file.payload.releaseConfig = pcks;
return this;

Check warning on line 249 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L247-L249

Added lines #L247 - L249 were not covered by tests
}

buildPackageStatus(pck: string, status: 'success' | 'inprogress', elapsedTime?: number): BuildLoggerBuilder {
this.file.payload.events[pck].event =

Check warning on line 253 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L252-L253

Added lines #L252 - L253 were not covered by tests
status === 'success' ? 'sfpowerscripts.build.success' : 'sfpowerscripts.build.progress';
if (elapsedTime) {
this.file.payload.events[pck].metadata.elapsedTime = elapsedTime;

Check warning on line 256 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L256

Added line #L256 was not covered by tests
}
return this;

Check warning on line 258 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L258

Added line #L258 was not covered by tests
}

build(): BuildHookSchema {
return this.file;

Check warning on line 262 in packages/core/src/eventStream/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/build.ts#L261-L262

Added lines #L261 - L262 were not covered by tests
}
}
92 changes: 92 additions & 0 deletions packages/core/src/eventStream/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import axios from 'axios';
import SFPLogger, { LoggerLevel, COLOR_TRACE, COLOR_WARNING } from '@dxatscale/sfp-logger';
import SFPOrg from '../org/SFPOrg';
import { SfPowerscriptsEvent__c } from './types';
import 'dotenv/config'

export class HookService<T> {
private static instance: HookService<any>;

public static getInstance(): HookService<any> {

Check warning on line 10 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L10

Added line #L10 was not covered by tests
if (!HookService.instance) {
HookService.instance = new HookService();

Check warning on line 12 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L12

Added line #L12 was not covered by tests
}
return HookService.instance;

Check warning on line 14 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L14

Added line #L14 was not covered by tests
}

public async logEvent(event: T) {

Check warning on line 17 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L17

Added line #L17 was not covered by tests
//###send webkooks### only when the env variables are set
if (process.env.EVENT_STREAM_WEBHOOK_URL) {
const axiosInstance = axios.create();
axiosInstance.defaults.headers.common['Authorization'] = process.env.EVENT_STREAM_WEBHOOK_TOKEN;
axiosInstance.defaults.baseURL = process.env.EVENT_STREAM_WEBHOOK_URL;

Check warning on line 22 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L20-L22

Added lines #L20 - L22 were not covered by tests
// datetime not enough , so we need math.random to make it unique
const payload = { eventType: event['context']['eventType'], eventId: `${event['context']['eventId']}_${Math.floor(10000 + Math.random() * 90000)}`, payload: event };

Check warning on line 24 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L24

Added line #L24 was not covered by tests


try {
const commitResponse = await axiosInstance.post(``, JSON.stringify(payload));

Check warning on line 28 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L28

Added line #L28 was not covered by tests

if (commitResponse.status === 201) {
SFPLogger.log(COLOR_TRACE(`Commit successful.`), LoggerLevel.TRACE);

Check warning on line 31 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L31

Added line #L31 was not covered by tests
} else {
SFPLogger.log(

Check warning on line 33 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L33

Added line #L33 was not covered by tests
COLOR_TRACE(`Commit failed. Status code: ${commitResponse.status}`),
LoggerLevel.TRACE
);
}
} catch (error) {
SFPLogger.log(COLOR_TRACE(`An error happens for the webkook callout: ${error}`), LoggerLevel.INFO);

Check warning on line 39 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L39

Added line #L39 was not covered by tests
}
}

if(!event['context']['devHubAlias'] && event['context']['jobId'].includes('NO_DEV_HUB_IMPL')){
return;

Check warning on line 44 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L44

Added line #L44 was not covered by tests
}

const sfpOrg = await SFPOrg.create({

Check warning on line 47 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L47

Added line #L47 was not covered by tests
aliasOrUsername: event['context']['devHubAlias'],
});

const connection = sfpOrg.getConnection();

Check warning on line 51 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L51

Added line #L51 was not covered by tests

const sfpEvent: SfPowerscriptsEvent__c[] = [

Check warning on line 53 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L53

Added line #L53 was not covered by tests
{
Name: `${event['context']['jobId']}-${event['metadata']['package']}`,
Command__c: event['context']['command'],
JobId__c: event['context']['jobId'],
Branch__c: event['context']['branch'],
Commit__c: event['context']['commitId'],
EventId__c: event['context']['eventId'],
InstanceUrl__c: event['context']['instanceUrl'],
JobTimestamp__c: event['context']['timestamp'],
EventName__c: event['event'],
Package__c: event['metadata']['package'],
ErrorMessage__c: event['metadata']['message'].length > 0 ? JSON.stringify(event['metadata']['message']) : ''
},
];

const upsertGitEvents = async () => {

Check warning on line 69 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L69

Added line #L69 was not covered by tests
try {
const result = await connection.sobject('SfPowerscriptsEvent__c').upsert(sfpEvent, 'Name');
onResolved(result);

Check warning on line 72 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L71-L72

Added lines #L71 - L72 were not covered by tests
} catch (error) {
onReject(error);

Check warning on line 74 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L74

Added line #L74 was not covered by tests
}
};

const onResolved = (res) => {
SFPLogger.log(COLOR_TRACE('Upsert successful:', res), LoggerLevel.TRACE);

Check warning on line 79 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L78-L79

Added lines #L78 - L79 were not covered by tests
// Implement your custom logic here for resolved cases
};

const onReject = (err) => {
SFPLogger.log(COLOR_TRACE('Error:', err), LoggerLevel.TRACE);
SFPLogger.log(COLOR_WARNING('We cannot send the events to your DevHub. Please check that the package id 04t2o000001B1jzAAC is installed on DevHub and the username has the permissions.'), LoggerLevel.TRACE);

Check warning on line 85 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L83-L85

Added lines #L83 - L85 were not covered by tests
};

await upsertGitEvents()
.then(() => SFPLogger.log(COLOR_TRACE('Promise resolved successfully.'), LoggerLevel.TRACE))
.catch((err) => SFPLogger.log(COLOR_TRACE('Promise rejected:', err), LoggerLevel.TRACE));

Check warning on line 90 in packages/core/src/eventStream/hooks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/eventStream/hooks.ts#L88-L90

Added lines #L88 - L90 were not covered by tests
}
}