Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function invokeCacheMissListeners(optsToUse: GoalCacheOptions | GoalCacheRestoreOptions,
p: GitProject,
gi: GoalInvocation,
event: GoalProjectListenerEvent): Promise {
for (const cacheMissFallback of toArray(optsToUse.onCacheMiss)) {
const allEvents = [GoalProjectListenerEvent.before, GoalProjectListenerEvent.after];
if ((cacheMissFallback.events || allEvents).filter(e => e === event).length > 0
&& await pushTestSucceeds(cacheMissFallback.pushTest, gi, p)) {
await cacheMissFallback.listener(p, gi, event);
}
}
}
const c = new Container({ displayName: this.definition.displayName });
(c as any).register = () => {
};
(c as any).addFulfillment = () => c;
(c as any).addFulfillmentCallback = () => c;
(c as any).withProjectListener = () => c;
c.with(registration);
return executeDockerJob(c, registration)(gi);
},
name: DefaultGoalNameGenerator.generateName(`container-docker-${this.definition.displayName}`),
});
this.withProjectListener({
name: "cache-restore",
events: [GoalProjectListenerEvent.before],
listener: async (p, gi, e) => {
const registration = gi.parameters.registration as ContainerRegistration;
if (registration.input && registration.input.length > 0) {
await cacheRestore({ entries: registration.input }).listener(p, gi, e);
}
},
}).withProjectListener({
name: "cache-put",
events: [GoalProjectListenerEvent.after],
listener: async (p, gi, e) => {
const registration = gi.parameters.registration as ContainerRegistration;
if (registration.output && registration.output.length > 0) {
await cachePut({ entries: registration.output }).listener(p, gi, e);
}
},
});
return data;
}
/**
* If running as isolated goal, use [[executeK8sJob]] to execute the
* goal. Otherwise, schedule the goal execution as a Kubernetes job
* using [[scheduleK8sjob]].
*/
const containerExecutor: ExecuteGoal = gi => (process.env.ATOMIST_ISOLATED_GOAL) ? executeK8sJob()(gi) : scheduleK8sJob(gi);
/**
* Restore cache input entries before fulfilling goal.
*/
const containerFulfillerCacheRestore: GoalProjectListenerRegistration = {
name: "ContainerFulfillerCacheRestore",
events: [GoalProjectListenerEvent.before],
listener: async (project, gi, event) => {
const data = parseGoalEventData(gi.goalEvent);
if (!data[ContainerRegistrationGoalDataKey]) {
throw new Error(`Goal ${gi.goal.uniqueName} has no Kubernetes container registration: ${gi.goalEvent.data}`);
}
const registration: K8sContainerRegistration = data[ContainerRegistrationGoalDataKey];
if (registration.input && registration.input.length > 0) {
try {
const cp = cacheRestore({ entries: registration.input });
return cp.listener(project, gi, GoalProjectListenerEvent.before);
} catch (e) {
const message = `Failed to restore cache input to container for goal ${gi.goal.uniqueName}: ${e.message}`;
gi.progressLog.write(message);
return { code: 1, message };
}
} else {
listener: async (project, gi, event) => {
const data = parseGoalEventData(gi.goalEvent);
if (!data[ContainerRegistrationGoalDataKey]) {
throw new Error(`Goal ${gi.goal.uniqueName} has no Kubernetes container registration: ${gi.goalEvent.data}`);
}
const registration: K8sContainerRegistration = data[ContainerRegistrationGoalDataKey];
if (registration.input && registration.input.length > 0) {
try {
const cp = cacheRestore({ entries: registration.input });
return cp.listener(project, gi, GoalProjectListenerEvent.before);
} catch (e) {
const message = `Failed to restore cache input to container for goal ${gi.goal.uniqueName}: ${e.message}`;
gi.progressLog.write(message);
return { code: 1, message };
}
} else {
return { code: 0, message: "No container input cache entries to restore" };
}
},
};
export async function mvnVersionProjectListener(p: GitProject,
gi: GoalInvocation,
event: GoalProjectListenerEvent): Promise {
const command = await determineMavenCommand(p);
if (event === GoalProjectListenerEvent.before) {
const v = await readSdmVersion(
gi.goalEvent.repo.owner,
gi.goalEvent.repo.name,
gi.goalEvent.repo.providerId,
gi.goalEvent.sha,
gi.goalEvent.branch,
gi.context);
return spawnLog(
command, ["versions:set", `-DnewVersion=${v}`, "versions:commit", ...MavenOptions],
{ cwd: p.baseDir, log: gi.progressLog });
}
}
this.addFulfillmentCallback({
goal: this,
callback: async (goal, context) => {
const reg = JSON.parse((goal as any).parameters).registration as ContainerRegistration;
if (scheduler === "k8s") {
const c = new Container({ displayName: this.definition.displayName });
return k8sFulfillmentCallback(c, reg)(goal, context);
}
return goal;
},
});
this.withProjectListener({
name: "cache-restore",
events: [GoalProjectListenerEvent.before],
listener: async (p, gi, e) => {
const reg = gi.parameters.registration as ContainerRegistration;
if (reg.input && reg.input.length > 0) {
await cacheRestore({ entries: reg.input }).listener(p, gi, e);
}
},
}).withProjectListener({
name: "cache-put",
events: [GoalProjectListenerEvent.after],
listener: async (p, gi, e) => {
const reg = gi.parameters.registration as ContainerRegistration;
if (reg.output && reg.output.length > 0) {
await cachePut({ entries: reg.output }).listener(p, gi, e);
}
},
});
event: GoalProjectListenerEvent): Promise {
const v = await readSdmVersion(
gi.goalEvent.repo.owner,
gi.goalEvent.repo.name,
gi.goalEvent.repo.providerId,
gi.goalEvent.sha,
gi.goalEvent.branch,
gi.context);
return changeGradleVersion(v, p);
}
export const GradleVersion: GoalProjectListenerRegistration = {
name: "gradle version",
listener: gradleVersionProjectListener,
pushTest: IsGradle,
events: [GoalProjectListenerEvent.before],
};
function gradleBuildProjectListener(p: GitProject,
gi: GoalInvocation): Promise {
return gradleCommand(p, {
progressLog: gi.progressLog,
tasks: ["build"],
args: [{name: "artifact.name", value: p.id.repo}],
errorFinder: SuccessIsReturn0ErrorFinder,
});
}
export const GradleBuild: GoalProjectListenerRegistration = {
name: "gradle build",
listener: gradleBuildProjectListener,
pushTest: IsGradle,