Snippets
Created by
Dragos Oancea-Zevri
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | function handleHook(input, pushId, callback) {
const validateError = validateInput(input);
if (validateError) {
return callback(validateError);
}
// Lets shorten variables we need multiple times
const projectCloneUrl = input.projectCloneUrl;
const branch = input.branch;
async.waterfall([
// Get the packageJson from gitlab
(done) => {
const gitlabApiInput = {
group: Project.getGroupByCloneUrl(projectCloneUrl),
project: Project.getProjectByCloneUrl(projectCloneUrl),
branch,
fileName: 'package.json',
};
gitlabApiClient.getFile(
gitlabApiInput,
errorCallback(`Package.json not found in ${branch} branch`, done)
);
},
// // Get the project, based on the name in the packageJson
(packageJson, done) => {
projectService.findProjectByID(packageJson.name,
errorCallback(
`An error occured when retrieving ${packageJson.name}`,
packageJson,
done
)
);
},
// Check if build is required
(packageJson, project, done) => {
let onboard = false;
if (project.onboard_branch_prefix) {
const regex = new RegExp(`^${project.onboard_branch_prefix}\\w+`);
onboard = regex.test(branch);
}
let release_configuration;
if (input.mergeRequest === true) {
release_configuration = Project.getReleaseConfigurationForBranch(input.targetBranch, project.release_configuration);
release_configuration.release = false;
} else {
release_configuration = Project.getReleaseConfigurationForBranch(branch, project.release_configuration) || Project.getReleaseConfigurationForBranch(project.onboard_branch_prefix, project.release_configuration);
}
if (!release_configuration) {
logger.info(`Not triggering build for ${branch} branch of ${packageJson.name} no runner job specified`);
return done('not triggering');
}
return done(null, packageJson, project, release_configuration, onboard);
},
// check versions
(packageJson, project, release_configuration, onboard, done) => {
getVersionToBuild(
packageJson,
project.id,
branch,
input.sha,
errorCallback(
`Could not find a version for ${packageJson.name}`,
packageJson,
project,
release_configuration,
onboard,
done
)
);
},
// Create and save the new build
(packageJson, project, release_configuration, onboard, version, done) => {
const build = new Build({
initiator_project_id: project.id,
project_id: project.id,
type: project.type,
branch,
version,
push_id: pushId,
visible_at_dashboard: release_configuration.release,
onboard,
});
build.setStatus('queued');
buildService.insertBuild(build, errorCallback(
`Could not save the build for ${packageJson.name}`,
packageJson,
project,
release_configuration,
onboard,
version,
done
));
},
// Start the jenkins job
(packageJson, project, release_configuration, onboard, version, savedBuild, done) => {
const buildId = savedBuild.generated_keys[0];
gitlabApiClient.setCommitStatus(buildId, (errorSetCommit) => {
if (errorSetCommit) {
logger.info('could not update the status of the git commit.');
}
buildServerClient.triggerJenkinsJob({
project_type: project.type,
project,
project_clone_url: projectCloneUrl,
project_branch: branch,
project_version: version,
build_id: buildId,
onboard,
release_configuration,
}, (errorTriggerJenkinsJob, resultTriggerJenkinsJob) => {
if (!errorTriggerJenkinsJob && resultTriggerJenkinsJob === true) {
return done();
}
const subject = `Could not trigger jenkins job for v${packageJson.version}`;
notificationService.insertNotificationForBuild(
subject,
errorTriggerJenkinsJob.toString(),
'error',
project.id,
branch,
buildId
);
buildServiceService.updateBuildStatus({
build_id: buildId,
status: 'error',
extra_information: errorTriggerJenkinsJob.toString(),
enddatetime: new Date(),
}, (errorUpdateBuild, resultUpdateBuild) => {
if (!errorUpdateBuild) {
return errorCallback(`Could not trigger jenkins job for ${packageJson.name}`, done)(errorTriggerJenkinsJob, resultTriggerJenkinsJob);
}
notificationService.insertNotificationForBuild(
subject,
errorUpdateBuild.toString(),
'error',
project.id,
branch,
buildId
);
return errorCallback(`Could not update build record with id: ${buildId}`, done)(errorUpdateBuild, resultUpdateBuild);
});
});
});
}
], callback);
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.