Permalink
Browse files

feat(): gitlab integration

  • Loading branch information...
Izak88 committed Aug 25, 2017
1 parent 3fd4239 commit b5429c6801973b43c2317ae15ff2b7574eed936d
@@ -68,6 +68,70 @@ export function setGitHubStatusFailure(
return sendRequest(gitUrl, data, header);
}

export function setGitLabStatusSuccess(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
let data = {
'state': 'success',
'target_url': abstruseUrl,
'description': 'The Abstruse CI build succeeded',
'context': 'continuous-integration/abstruse'
};

let header = {
'PRIVATE-TOKEN': token
};

return sendRequest(gitUrl, data, header);
}

export function setGitLabStatusPending(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
let data = {
'state': 'pending',
'target_url': abstruseUrl,
'description': 'The Abstruse CI build is running',
'context': 'continuous-integration/abstruse'
};

let header = {
'PRIVATE-TOKEN': token
};

return sendRequest(gitUrl, data, header);
}

export function setGitLabStatusError(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
let data = {
'state': 'error',
'target_url': abstruseUrl,
'description': 'The Abstruse CI build errored',
'context': 'continuous-integration/abstruse'
};

let header = {
'PRIVATE-TOKEN': token
};

return sendRequest(gitUrl, data, header);
}

export function setGitLabStatusFailure(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
let data = {
'state': 'failure',
'target_url': abstruseUrl,
'description': 'The Abstruse CI build failed',
'context': 'continuous-integration/abstruse'
};

let header = {
'PRIVATE-TOKEN': token
};

return sendRequest(gitUrl, data, header);
}

export function setBitbucketStatusSuccess(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
return getBitBucketAccessToken(token)
@@ -99,7 +99,13 @@ export function getRepositoryDetails(repository, sha = null, pr = null): Promise

createTempDir()
.then(tempDir => {
let cloneUrl = repository.clone_url;
cloneDir = tempDir;
if (repository.private && repository.gitlab_id && repository.access_token) {
cloneUrl =
cloneUrl.replace('https://', `https://gitlab-ci-token:${repository.access_token}@`);
}

if (repository.bitbucket_id && repository.private && repository.access_token) {
return getBitBucketAccessToken(repository.access_token)
.then(response => {
@@ -111,7 +117,7 @@ export function getRepositoryDetails(repository, sha = null, pr = null): Promise
}).catch(err => Promise.reject(err));
}

return spawn('git', ['clone', repository.clone_url, '--depth', '1', cloneDir]);
return spawn('git', ['clone', cloneUrl, '--depth', '1', cloneDir]);
})
.then(cloned => cloned.exit === 0 ? Promise.resolve() : Promise.reject(''))
.then(() => {
@@ -26,6 +26,7 @@ export function create(): Promise<null> {
t.increments('id').unsigned().primary();
t.integer('github_id');
t.string('bitbucket_id');
t.integer('gitlab_id');
t.string('clone_url');
t.string('html_url');
t.string('default_branch');
@@ -178,6 +178,8 @@ export function updateRepository(data: any): Promise<boolean> {
repository = new Repository().where({ github_id: data.github_id });
} else if (data.bitbucket_id) {
repository = new Repository().where({ bitbucket_id: data.bitbucket_id });
} else if (data.gitlab_id) {
repository = new Repository().where({ gitlab_id: data.gitlab_id });
} else {
reject('Repository Id missing');
}
@@ -229,7 +231,40 @@ export function pingBitbucketRepository(data: any): Promise<any> {
return new Promise((resolve, reject) => {
const saveData = generateBitbucketRepositoryData(data);
new Repository().where({ bitbucket_id: saveData.bitbucket_id }).fetch()
.then(repo => {
.then(repo => {
if (!repo) {
new Repository().save(saveData, { method: 'insert' })
.then(result => {
if (!result) {
reject(result);
} else {
let repository = result.toJSON();
return addRepositoryPermissionToEveryone(result.id)
.then(() => resolve(repository))
.catch(err => reject(err));
}
})
.catch(err => reject(err));
} else {
repo.save(saveData, { method: 'update', require: false })
.then(result => {
if (!result) {
reject(result);
} else {
resolve(result.toJSON());
}
})
.catch(err => reject(err));
}
});
});
}

export function pingGitLabRepository(data: any): Promise<any> {
return new Promise((resolve, reject) => {
const saveData = generateGitLabRepositoryData(data);
new Repository().where({ gitlab_id: saveData.gitlab_id }).fetch()
.then(repo => {
if (!repo) {
new Repository().save(saveData, { method: 'insert' })
.then(result => {
@@ -357,6 +392,38 @@ export function synchronizeBitbucketPullRequest(data: any): Promise<any> {
});
}

export function synchronizeGitLabPullRequest(data: any): Promise<any> {
let repoId;
return new Promise((resolve, reject) => {
const gitlabId = data.project_id ? data.project_id : data.object_attributes.target_project_id;
new Repository().where({ gitlab_id: gitlabId }).fetch()
.then(repository => {
if (!repository) {
const repoData = generateGitLabRepositoryData(data);
return addRepository(repoData).then(repo => {
repoId = repo.id;
});
} else {
const repoJson = repository.toJSON();
repoId = repoJson.id;
const repoData = generateGitLabRepositoryData(data);
return updateRepository(repoData);
}
})
.then(() => {
const buildData = {
pr: data.object_attributes.iid,
data: data,
start_time: new Date(),
repositories_id: repoId
};

resolve(buildData);
})
.catch(err => reject(err));
});
}

function generateGitHubRepositoryData(data: any): any {
return {
github_id: data.repository.id,
@@ -395,3 +462,21 @@ function generateBitbucketRepositoryData(data: any): any {
data: data
};
}

function generateGitLabRepositoryData(data: any): any {
return {
gitlab_id: data.project_id ? data.project_id : data.object_attributes.target_project_id,
clone_url: data.repository.git_http_url ? data.repository.git_http_url : data.project.http_url,
html_url: data.repository.homepage,
default_branch: data.project.default_branch,
name: data.repository.name,
full_name: data.project.path_with_namespace,
description: data.repository.description,
private: data.repository.visibility_level > 0 ? false : true,
fork: data.repository.fork,
user_login: data.user_username ? data.user_username : data.user.username,
user_id: data.user_id ? data.user_id : data.object_attributes.author_id,
user_avatar_url: data.user_avatar ? data.user_avatar : data.user.avatar_url,
data: data
};
}
@@ -17,7 +17,11 @@ import {
setGitHubStatusSuccess,
setBitbucketStatusFailure,
setBitbucketStatusPending,
setBitbucketStatusSuccess
setBitbucketStatusSuccess,
setGitLabStatusError,
setGitLabStatusFailure,
setGitLabStatusPending,
setGitLabStatusSuccess
} from './commit-status';

export interface BuildMessage {
@@ -81,7 +85,17 @@ jobProcesses
export function startBuild(data: any): Promise<any> {
return getRepositoryOnly(data.repositories_id)
.then(repository => {
let sha = data && data.sha ? data.sha : null;
let sha = null;
if (data) {
if (data.sha) {
sha = data.sha;
} else if (data.data.checkout_sha) {
sha = data.data.checkout_sha;
} else if (data.data.object_attributes && data.data.object_attributes.last_commit) {
sha = data.data.object_attributes.last_commit.id;
}
}

const pr = data && data.pr ? data.pr : null;
let repoDetails = null;

@@ -164,6 +178,13 @@ export function startBuild(data: any): Promise<any> {

return setBitbucketStatusPending(gitUrl, abstruseUrl,
repository.access_token);
} else if (repository.gitlab_id) {
const id = data.data.project_id ?
data.data.project_id : data.data.object_attributes.target_project_id;
const gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${build.id}`;

return setGitLabStatusPending(gitUrl, abstruseUrl, repository.access_token);
} else {
return Promise.resolve();
}
@@ -398,6 +419,13 @@ function prepareJob(buildId: number, jobId: number, cmds: any, sshAndVnc = false

return setBitbucketStatusFailure(gitUrl, abstruseUrl,
build.repository.access_token);
} else if (build.repository.gitlab_id) {
const id = build.data.project_id ?
build.data.project_id : build.data.object_attributes.target_project_id;
const sha = build.data.checkout_sha || build.data.object_attributes.last_commit.id;
const gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${buildId}`;
return setGitLabStatusFailure(gitUrl, abstruseUrl, build.repository.access_token);
} else {
return Promise.resolve();
}
@@ -445,6 +473,16 @@ function prepareJob(buildId: number, jobId: number, cmds: any, sshAndVnc = false

return setBitbucketStatusSuccess(gitUrl, abstruseUrl,
build.repository.access_token);
} else if (build.repository.gitlab_id) {
const id = build.data.project_id ?
build.data.project_id : build.data.object_attributes.target_project_id;
const sha = build.data.checkout_sha
|| build.data.object_attributes.last_commit.id;
const gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${buildId}`;

return setGitLabStatusSuccess(gitUrl, abstruseUrl,
build.repository.access_token);
} else {
return Promise.resolve();
}
@@ -524,6 +562,15 @@ export function restartBuild(buildId: number): Promise<any> {

return setBitbucketStatusPending(gitUrl, abstruseUrl,
buildData.repository.access_token);
} else if (buildData.repository.gitlab_id) {
const id = buildData.data.project_id ?
buildData.data.project_id : buildData.data.object_attributes.target_project_id;
const sha = buildData.data.checkout_sha
|| buildData.data.object_attributes.last_commit.id;
const gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${buildId}`;

return setGitLabStatusPending(gitUrl, abstruseUrl, buildData.repository.access_token);
} else {
return Promise.resolve();
}
@@ -559,6 +606,15 @@ export function stopBuild(buildId: number): Promise<any> {
const abstruseUrl = `${config.url}/build/${buildId}`;

return setBitbucketStatusFailure(gitUrl, abstruseUrl, buildData.repository.access_token);
} else if (buildData.repository.gitlab_id) {
const id = buildData.data.project_id ?
buildData.data.project_id : buildData.data.object_attributes.target_project_id;
const sha = buildData.data.checkout_sha
|| buildData.data.object_attributes.last_commit.id;
const gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${buildId}`;

return setGitLabStatusFailure(gitUrl, abstruseUrl, buildData.repository.access_token);
} else {
return Promise.resolve();
}
@@ -606,6 +662,15 @@ export function restartJob(jobId: number): Promise<void> {
const abstruseUrl = `${config.url}/build/${build.id}`;

return setBitbucketStatusPending(gitUrl, abstruseUrl, build.repository.access_token);
} else if (build.repository.gitlab_id) {
const id = build.data.project_id ?
build.data.project_id : build.data.object_attributes.target_project_id;
const sha = build.data.checkout_sha
|| build.data.object_attributes.last_commit.id;
const gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${build.id}`;

return setGitLabStatusPending(gitUrl, abstruseUrl, build.repository.access_token);
} else {
return Promise.resolve();
}
@@ -655,6 +720,15 @@ export function restartJobWithSshAndVnc(jobId: number): Promise<void> {
const abstruseUrl = `${config.url}/build/${build.id}`;

return setBitbucketStatusPending(gitUrl, abstruseUrl, build.repository.access_token);
} else if (build.repository.gitlab_id) {
const id = build.data.project_id ?
build.data.project_id : build.data.object_attributes.target_project_id;
const sha = build.data.checkout_sha
|| build.data.object_attributes.last_commit.id;
const gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${build.id}`;

return setGitLabStatusPending(gitUrl, abstruseUrl, build.repository.access_token);
} else {
return Promise.resolve();
}
Oops, something went wrong.

0 comments on commit b5429c6

Please sign in to comment.