Permalink
Browse files

feat(): bitbucket integration

  • Loading branch information...
Izak88 committed Aug 24, 2017
1 parent dca89e5 commit 20aedc830f0cdcbd686eaf8f3ff4b96581456282
@@ -0,0 +1,138 @@
import { sendRequest, getBitBucketAccessToken } from './utils';

export function setGitHubStatusSuccess(
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 = {
'Authorization': `token ${token}`,
'User-Agent': 'Abstruse'
};

return sendRequest(gitUrl, data, header);
}

export function setGitHubStatusPending(
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 = {
'Authorization': `token ${token}`,
'User-Agent': 'Abstruse'
};

return sendRequest(gitUrl, data, header);
}

export function setGitHubStatusError(
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 = {
'Authorization': `token ${token}`,
'User-Agent': 'Abstruse'
};

return sendRequest(gitUrl, data, header);
}

export function setGitHubStatusFailure(
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 = {
'Authorization': `token ${token}`,
'User-Agent': 'Abstruse'
};

return sendRequest(gitUrl, data, header);
}

export function setBitbucketStatusSuccess(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
return getBitBucketAccessToken(token)
.then(response => {
let access_token = JSON.parse(response).access_token;
let statusData = {
'state': 'SUCCESSFUL',
'url': abstruseUrl,
'description': 'The Abstruse CI build succeeded',
'name': 'continuous-integration/abstruse',
'key': 'continuous-integration/abstruse'
};

let header = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${access_token}`
};

return sendRequest(gitUrl, statusData, header);
})
.catch(err => Promise.reject(err));
}

export function setBitbucketStatusPending(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
return getBitBucketAccessToken(token)
.then(response => {
let access_token = JSON.parse(response).access_token;
let statusData = {
'state': 'INPROGRESS',
'url': abstruseUrl,
'description': 'The Abstruse CI build is running',
'name': 'continuous-integration/abstruse',
'key': 'continuous-integration/abstruse'
};

let header = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${access_token}`
};

return sendRequest(gitUrl, statusData, header);
})
.catch(err => Promise.reject(err));
}

export function setBitbucketStatusFailure(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
return getBitBucketAccessToken(token)
.then(response => {
let access_token = JSON.parse(response).access_token;
let statusData = {
'state': 'FAILED',
'url': abstruseUrl,
'description': 'The Abstruse CI build failed',
'name': 'continuous-integration/abstruse',
'key': 'continuous-integration/abstruse'
};

let header = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${access_token}`
};

return sendRequest(gitUrl, statusData, header);
})
.catch(err => Promise.reject(err));
}
@@ -1,4 +1,4 @@
import { createTempDir } from './utils';
import { createTempDir, getBitBucketAccessToken } from './utils';
import { spawn } from './process';
import * as sh from 'shelljs';
import { existsSync, rmdir } from './fs';
@@ -90,7 +90,7 @@ export function generateCommands(repositoryUrl: string, config: Config): any[] {
return matrix;
}

export function getRepositoryDetails(url: string, sha = null, pr = null): Promise<RepositoryInfo> {
export function getRepositoryDetails(repository, sha = null, pr = null): Promise<RepositoryInfo> {
return new Promise((resolve, reject) => {
let cloneDir = null;
let configPath = null;
@@ -100,7 +100,18 @@ export function getRepositoryDetails(url: string, sha = null, pr = null): Promis
createTempDir()
.then(tempDir => {
cloneDir = tempDir;
return spawn('git', ['clone', url, '--depth', '1', cloneDir]);
if (repository.bitbucket_id && repository.private && repository.access_token) {
return getBitBucketAccessToken(repository.access_token)
.then(response => {
let access_token = JSON.parse(response).access_token;
let cloneUrl =
repository.clone_url.replace('https://', `https://x-token-auth:${access_token}@`);

return spawn('git', ['clone', cloneUrl, '--depth', '1', cloneDir]);
}).catch(err => Promise.reject(err));
}

return spawn('git', ['clone', repository.clone_url, '--depth', '1', cloneDir]);
})
.then(cloned => cloned.exit === 0 ? Promise.resolve() : Promise.reject(''))
.then(() => {
@@ -19,7 +19,7 @@ export function insertBuildRun(data: any): Promise<any> {
delete data.repositories_id;
delete data.jobs;
delete data.pr;
data.github_id = data.data.repository.id;
data.head_id = data.data.repository.id;

new BuildRun().save(data, { method: 'insert' }).then(buildRun => {
if (!buildRun) {
@@ -107,7 +107,11 @@ export function getBuild(id: number, userId?: number): Promise<any> {
new BuildRun()
.query(q => {
q.innerJoin('builds', 'builds.id', 'build_runs.build_id')
<<<<<<< HEAD
.where('builds.repositories_id', build.repositories_id)
=======
.where('builds.repositories_id', build.repository.id)
>>>>>>> feat(): bitbucket integration
.andWhere('builds.id', '<=', build.id)
.whereNotNull('build_runs.start_time')
.whereNotNull('build_runs.end_time')
@@ -36,7 +36,7 @@ export function getJob(jobId: number, userId?: number): Promise<any> {
.query(q => {
q.innerJoin('jobs', 'jobs.id', 'job_runs.job_id')
.innerJoin('builds', 'jobs.builds_id', 'builds.id')
.where('builds.github_id', build.github_id)
.where('builds.repositories_id', build.repositories_id)
.andWhere('jobs.id', '<=', job.id)
.andWhere('job_runs.status', 'success')
.andWhere('jobs.test_script', job.test_script)
@@ -25,6 +25,7 @@ export function create(): Promise<null> {
.then(() => schema.createTableIfNotExists('repositories', (t: knex.TableBuilder) => {
t.increments('id').unsigned().primary();
t.integer('github_id');
t.string('bitbucket_id');
t.string('clone_url');
t.string('html_url');
t.string('default_branch');
@@ -46,7 +47,7 @@ export function create(): Promise<null> {
.then(() => schema.createTableIfNotExists('builds', (t: knex.TableBuilder) => {
t.increments('id').unsigned().primary();
t.integer('pr');
t.integer('github_id');
t.integer('head_id');
t.json('data');
t.dateTime('start_time');
t.dateTime('end_time');
@@ -56,7 +57,7 @@ export function create(): Promise<null> {
}))
.then(() => schema.createTableIfNotExists('build_runs', (t: knex.TableBuilder) => {
t.increments('id').unsigned().primary();
t.integer('github_id');
t.integer('head_id');
t.json('data');
t.dateTime('start_time');
t.dateTime('end_time');
Oops, something went wrong.

0 comments on commit 20aedc8

Please sign in to comment.