Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
await verifyGoal(sdmGoal, this.configuration.sdm.goalSigning, ctx);
if ((await cancelableGoal(sdmGoal, this.configuration)) && (await isGoalCanceled(sdmGoal, ctx))) {
logger.debug(`Goal ${sdmGoal.uniqueName} has been canceled. Not fulfilling`);
return Success;
}
if (sdmGoal.fulfillment.method === SdmGoalFulfillmentMethod.SideEffect &&
sdmGoal.fulfillment.registration !== this.configuration.name) {
logger.debug("Not fulfilling side-effected goal '%s' with method '%s/%s'",
sdmGoal.uniqueName, sdmGoal.fulfillment.method, sdmGoal.fulfillment.name);
return Success;
} else if (sdmGoal.fulfillment.method === SdmGoalFulfillmentMethod.Other) {
// fail goal with neither Sdm nor SideEffect fulfillment
await updateGoal(
ctx,
sdmGoal,
{
state: SdmGoalState.failure,
description: `No fulfillment for ${sdmGoal.uniqueName}`,
});
return Success;
}
const id = this.configuration.sdm.repoRefResolver.repoRefFromSdmGoal(sdmGoal);
const credentials = await resolveCredentialsPromise(this.configuration.sdm.credentialsResolver.eventHandlerCredentials(ctx, id));
const addressChannels = addressChannelsFor(sdmGoal.push.repo, ctx);
const preferences = this.configuration.sdm.preferenceStoreFactory(ctx);
const implementation = this.implementationMapper.findImplementationBySdmGoal(sdmGoal);
const { goal } = implementation;
parameters: !!event.data.SdmGoal[0].parameters ? JSON.parse(event.data.SdmGoal[0].parameters) : {},
};
const goalScheduler = await findGoalScheduler(goalInvocation, this.configuration);
if (!!goalScheduler) {
const start = Date.now();
const result = await goalScheduler.schedule(goalInvocation);
if (!!result && result.code !== undefined && result.code !== 0) {
await updateGoal(ctx, sdmGoal, {
state: SdmGoalState.failure,
description: `Failed to schedule goal`,
url: progressLog.url,
});
await reportEndAndClose(result, start, progressLog);
} else {
await updateGoal(ctx, sdmGoal, {
state: !!result && !!result.state ? result.state : SdmGoalState.in_process,
phase: !!result && !!result.phase ? result.phase : "scheduled",
description: !!result && !!result.description ? result.description : descriptionFromState(goal, SdmGoalState.in_process, sdmGoal),
url: progressLog.url,
externalUrls: !!result ? result.externalUrls : undefined,
});
}
return {
...result as any,
// successfully handled event even if goal failed
code: 0,
};
} else {
delete (sdmGoal as any).id;
const listeners = [];
const goalSet = result.SdmGoalSet[0];
const goals = await fetchGoalsForCommit(ctx, {
owner: goalSet.repo.owner,
repo: goalSet.repo.name,
sha: goalSet.sha,
branch: goalSet.branch,
} as any, goalSet.repo.providerId, goalSetId);
for (const goal of goals) {
if (![SdmGoalState.success,
SdmGoalState.canceled,
SdmGoalState.stopped,
SdmGoalState.skipped,
SdmGoalState.failure].includes(goal.state)) {
await updateGoal(ctx, goal, {
state: SdmGoalState.canceled,
description: !!goal.descriptions && !!goal.descriptions.canceled
? goal.descriptions.canceled : `Canceled: ${goal.name}`,
});
}
}
if (result && result.SdmGoalSet && result.SdmGoalSet.length === 1) {
const gs = result.SdmGoalSet[0];
const newGoalSet: any = {
...gs,
state: SdmGoalState.canceled,
};
await storeGoalSet(ctx, newGoalSet);
}
});
} else {
let g = sdmG;
const cbs = this.implementationMapper.findFulfillmentCallbackForGoal(sdmG);
for (const cb of cbs) {
g = await cb.callback(g,
{
id,
addressChannels: undefined,
preferences,
configuration: this.configuration,
credentials,
context,
});
}
return updateGoal(context, g, {
state: SdmGoalState.requested,
description: goal ? goal.requestedDescription : `Ready: ${g.name}`,
data: g.data,
});
}
}));
return Success;
const state = !!options && !!options.state ? options.state : SdmGoalState.canceled;
for (const goal of gs) {
if (goal.ts < end) {
logger.debug(
`Canceling goal '${goal.uniqueName}' of goal set '${goal.goalSetId}' because it timed out after '${formatDuration(timeout)}'`);
let description = `${state === SdmGoalState.canceled ? "Canceled" : "Failed"}: ${goal.name}`;
if (!!goal.descriptions) {
if (state === SdmGoalState.canceled && !!goal.descriptions.canceled) {
description = goal.descriptions.canceled;
} else if (state === SdmGoalState.failure && !!goal.descriptions.failed) {
description = goal.descriptions.failed;
}
}
await updateGoal(
ctx,
goal as any,
{
state,
description,
phase: `${formatDuration(timeout)} timeout`,
});
}
}
}
});
} else if (sdmGoal.state === SdmGoalState.approved) {
await updateGoal(context, sdmGoal, {
state: SdmGoalState.success,
description: !!sdmGoal.descriptions && !!sdmGoal.descriptions.completed
? sdmGoal.descriptions.completed : goal.successDescription,
});
}
break;
case GoalApprovalRequestVote.Denied:
if (sdmGoal.state === SdmGoalState.pre_approved) {
const g: SdmGoalEvent = {
...sdmGoal,
preApproval: undefined,
};
await updateGoal(context, g, {
state: SdmGoalState.waiting_for_pre_approval,
description: `${!!sdmGoal.descriptions && !!sdmGoal.descriptions.waitingForPreApproval ?
sdmGoal.descriptions.waitingForPreApproval : goal.waitingForPreApprovalDescription} \u00B7 start by @${sdmGoal.preApproval.userId} denied`,
});
} else if (sdmGoal.state === SdmGoalState.approved) {
const g: SdmGoalEvent = {
...sdmGoal,
approval: undefined,
};
await updateGoal(context, g, {
state: SdmGoalState.waiting_for_approval,
description: `${!!sdmGoal.descriptions && !!sdmGoal.descriptions.waitingForApproval ?
sdmGoal.descriptions.waitingForApproval : goal.waitingForApprovalDescription} \u00B7 approval by @${sdmGoal.approval.userId} denied`,
});
}
break;
id,
addressChannels: undefined,
preferences,
credentials,
configuration: this.configuration,
context,
});
}
await updateGoal(context, sdmGoal, {
state: SdmGoalState.requested,
description: !!sdmGoal.descriptions && !!sdmGoal.descriptions.requested
? sdmGoal.descriptions.requested : goal.requestedDescription,
data: g.data,
});
} else if (sdmGoal.state === SdmGoalState.approved) {
await updateGoal(context, sdmGoal, {
state: SdmGoalState.success,
description: !!sdmGoal.descriptions && !!sdmGoal.descriptions.completed
? sdmGoal.descriptions.completed : goal.successDescription,
});
}
break;
case GoalApprovalRequestVote.Denied:
if (sdmGoal.state === SdmGoalState.pre_approved) {
const g: SdmGoalEvent = {
...sdmGoal,
preApproval: undefined,
};
await updateGoal(context, g, {
state: SdmGoalState.waiting_for_pre_approval,
description: `${!!sdmGoal.descriptions && !!sdmGoal.descriptions.waitingForPreApproval ?
sdmGoal.descriptions.waitingForPreApproval : goal.waitingForPreApprovalDescription} \u00B7 start by @${sdmGoal.preApproval.userId} denied`,
await Promise.all(goalsToRequest.map(async sdmG => {
delete sdmG.data;
const goal = this.implementationMapper.findGoalBySdmGoal(sdmG);
if (sdmG.preApprovalRequired) {
return updateGoal(context, sdmG, {
state: SdmGoalState.waiting_for_pre_approval,
description: goal ? goal.waitingForPreApprovalDescription : `Start required: ${sdmG.name}`,
});
} else {
let g = sdmG;
const cbs = this.implementationMapper.findFulfillmentCallbackForGoal(sdmG);
for (const cb of cbs) {
g = await cb.callback(g,
{
id,
addressChannels: undefined,
preferences,
configuration: this.configuration,
credentials,
context,
});
async function rejectGoal(reason: string,
sdmGoal: SdmGoalEvent,
ctx: HandlerContext): Promise {
await updateGoal(
ctx,
sdmGoal,
{
state: SdmGoalState.failure,
description: `Rejected: ${sdmGoal.name}`,
phase: reason,
});
}
async function setBuiltContext(ctx: HandlerContext,
goal: Goal,
sdmGoal: SdmGoalEvent,
state: BuildStatus,
url: string): Promise {
const newState = buildStatusToSdmGoalState(state);
return updateGoal(ctx, sdmGoal,
{
url,
state: newState,
description: descriptionFromState(goal, newState),
});
}