Skip to content

Commit

Permalink
Try to resolve problem with manual job triggering II. (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepakriz authored Mar 25, 2024
1 parent 9841839 commit 0785154
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 99 deletions.
15 changes: 0 additions & 15 deletions server/src/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ export interface JobArgs {

export type JobFunction = (args: JobArgs) => Promise<unknown> | unknown;

interface JobState {
checkManualJobs: boolean;
}

export class Job {
private _info: JobInfo;
private _status: JobStatus;
private _priority: JobPriority;
private _state: JobState = {
checkManualJobs: true,
};

private readonly _id: string;
private readonly _fn: JobFunction;
Expand Down Expand Up @@ -67,10 +60,6 @@ export class Job {
this.onChange();
}

public updateState(callback: (state: JobState) => JobState) {
this._state = callback(this._state);
}

public run(args: JobArgs): Promise<unknown> | unknown {
return this._fn(args);
}
Expand All @@ -91,10 +80,6 @@ export class Job {
return this._priority;
}

get state(): JobState {
return this._state;
}

public getData(): GQLJob {
return {
priority: this.priority,
Expand Down
122 changes: 38 additions & 84 deletions server/src/MergeRequestAcceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,6 @@ export const runAcceptingMergeRequest = async (
await tryCancelPipeline(gitlabApi, mergeRequestInfo, user);
await gitlabApi.rebaseMergeRequest(mergeRequestInfo.project_id, mergeRequestInfo.iid);
job.updateStatus(JobStatus.REBASING);
job.updateState((state) => ({
...state,
checkManualJobs: true,
}));
return;
}

Expand All @@ -513,50 +509,47 @@ export const runAcceptingMergeRequest = async (
return;
}

if (job.state.checkManualJobs) {
job.updateState((state) => ({
...state,
checkManualJobs: false,
}));
const currentPipeline = mergeRequestInfo.head_pipeline;
if (currentPipeline !== null) {
const jobs = uniqueNamedJobsByDate(
await gitlabApi.getPipelineJobs(mergeRequestInfo.project_id, currentPipeline.id),
);
const currentPipeline = mergeRequestInfo.head_pipeline;

const manualJobsToRun = jobs.filter(
(job) => PipelineJobStatus.Manual === job.status && !job.allow_failure,
);
const canceledJobsToRun = jobs.filter(
(job) => PipelineJobStatus.Canceled === job.status && !job.allow_failure,
);
if (
currentPipeline !== null &&
mergeRequestInfo.detailed_merge_status === DetailedMergeStatus.CiMustPass &&
[PipelineStatus.Manual, PipelineStatus.Canceled].includes(currentPipeline.status)
) {
const jobs = uniqueNamedJobsByDate(
await gitlabApi.getPipelineJobs(mergeRequestInfo.project_id, currentPipeline.id),
);

if (manualJobsToRun.length > 0 || canceledJobsToRun.length > 0) {
if (!config.AUTORUN_MANUAL_BLOCKING_JOBS) {
return {
kind: AcceptMergeRequestResultKind.WaitingPipeline,
mergeRequestInfo,
user,
pipeline: currentPipeline,
};
}

console.log(
`[MR][${mergeRequestInfo.iid}] there are some blocking manual or canceled. triggering again`,
);
job.updateStatus(JobStatus.WAITING_FOR_CI);
await Promise.all(
manualJobsToRun.map((job) =>
gitlabApi.runJob(mergeRequestInfo.project_id, job.id),
),
);
await Promise.all(
canceledJobsToRun.map((job) =>
gitlabApi.retryJob(mergeRequestInfo.project_id, job.id),
),
);
return;
const manualJobsToRun = jobs.filter(
(job) => PipelineJobStatus.Manual === job.status && !job.allow_failure,
);
const canceledJobsToRun = jobs.filter(
(job) => PipelineJobStatus.Canceled === job.status && !job.allow_failure,
);

if (manualJobsToRun.length > 0 || canceledJobsToRun.length > 0) {
if (!config.AUTORUN_MANUAL_BLOCKING_JOBS) {
return {
kind: AcceptMergeRequestResultKind.WaitingPipeline,
mergeRequestInfo,
user,
pipeline: currentPipeline,
};
}

console.log(
`[MR][${mergeRequestInfo.iid}] there are some blocking manual or canceled. triggering again`,
);
job.updateStatus(JobStatus.WAITING_FOR_CI);
await Promise.all(
manualJobsToRun.map((job) => gitlabApi.runJob(mergeRequestInfo.project_id, job.id)),
);
await Promise.all(
canceledJobsToRun.map((job) =>
gitlabApi.retryJob(mergeRequestInfo.project_id, job.id),
),
);
return;
}
}

Expand Down Expand Up @@ -589,7 +582,6 @@ export const runAcceptingMergeRequest = async (
};
}

const currentPipeline: MergeRequestPipeline | null = mergeRequestInfo.head_pipeline;
if (currentPipeline === null) {
console.log(
`[MR][${mergeRequestInfo.iid}] Merge request can't be merged. Pipeline does not exist`,
Expand Down Expand Up @@ -626,44 +618,6 @@ export const runAcceptingMergeRequest = async (
};
}

if ([PipelineStatus.Manual, PipelineStatus.Canceled].includes(currentPipeline.status)) {
if (!config.AUTORUN_MANUAL_BLOCKING_JOBS) {
return {
kind: AcceptMergeRequestResultKind.WaitingPipeline,
mergeRequestInfo,
user,
pipeline: currentPipeline,
};
}

const jobs = uniqueNamedJobsByDate(
await gitlabApi.getPipelineJobs(mergeRequestInfo.project_id, currentPipeline.id),
);

const manualJobsToRun = jobs.filter(
(job) => PipelineJobStatus.Manual === job.status && !job.allow_failure,
);
const canceledJobsToRun = jobs.filter(
(job) => PipelineJobStatus.Canceled === job.status && !job.allow_failure,
);

if (manualJobsToRun.length > 0 || canceledJobsToRun.length > 0) {
console.log(
`[MR][${mergeRequestInfo.iid}] there are some blocking manual or canceled. triggering again`,
);
job.updateStatus(JobStatus.WAITING_FOR_CI);
await Promise.all(
manualJobsToRun.map((job) => gitlabApi.runJob(mergeRequestInfo.project_id, job.id)),
);
await Promise.all(
canceledJobsToRun.map((job) =>
gitlabApi.retryJob(mergeRequestInfo.project_id, job.id),
),
);
return;
}
}

if (
currentPipeline.status !== PipelineStatus.Success &&
currentPipeline.status !== PipelineStatus.Skipped &&
Expand Down

0 comments on commit 0785154

Please sign in to comment.