Skip to content

Commit

Permalink
fix(github-integration): fix GitHub integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Aug 18, 2017
1 parent 9282df8 commit 41c4dc7
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 25 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"to-string-loader": "^1.1.5",
"tree-kill": "^1.1.0",
"ts-node": "^3.3.0",
"tsc-watch": "1.0.8",
"tsc-watch": "1.0.7",
"tslint": "^5.6.0",
"typescript": "^2.4.2",
"uglify-js": "^3.0.27",
Expand Down
28 changes: 28 additions & 0 deletions src/api/db/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ export function create(): Promise<null> {
t.string('head_user_avatar_url');
t.string('head_user_url');
t.string('head_user_html_url');
t.string('base_github_id');
t.string('base_clone_url');
t.string('base_html_url');
t.string('base_default_branch');
t.string('base_name');
t.string('base_full_name');
t.string('base_description');
t.boolean('base_private');
t.boolean('base_fork');
t.string('base_user_login');
t.string('base_user_id');
t.string('base_user_avatar_url');
t.string('base_user_url');
t.string('base_user_html_url');
t.dateTime('start_time');
t.dateTime('end_time');
t.integer('repositories_id').notNullable();
Expand Down Expand Up @@ -92,6 +106,20 @@ export function create(): Promise<null> {
t.string('head_user_avatar_url');
t.string('head_user_url');
t.string('head_user_html_url');
t.string('base_github_id');
t.string('base_clone_url');
t.string('base_html_url');
t.string('base_default_branch');
t.string('base_name');
t.string('base_full_name');
t.string('base_description');
t.boolean('base_private');
t.boolean('base_fork');
t.string('base_user_login');
t.string('base_user_id');
t.string('base_user_avatar_url');
t.string('base_user_url');
t.string('base_user_html_url');
t.dateTime('start_time');
t.dateTime('end_time');
t.integer('build_id').notNullable();
Expand Down
32 changes: 30 additions & 2 deletions src/api/db/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export function createPullRequest(data: any): Promise<any> {
pr: data.number,
label: data.base.label,
ref: data.base.ref,
sha: data.base.sha,
sha: data.head.sha,
head_label: data.head.label,
head_ref: data.head.ref,
head_sha: data.head.sha,
Expand All @@ -243,6 +243,20 @@ export function createPullRequest(data: any): Promise<any> {
head_user_avatar_url: data.head.user.avatar_url,
head_user_url: data.head.user.url,
head_user_html_url: data.head.user.html_url,
base_github_id: data.base.repo.id,
base_clone_url: data.base.repo.clone_url,
base_html_url: data.base.repo.html_url,
base_default_branch: data.base.repo.default_branch,
base_name: data.base.repo.name,
base_full_name: data.base.repo.full_name,
base_description: data.base.repo.description,
base_private: data.base.repo.private,
base_fork: data.base.repo.fork,
base_user_login: data.base.user.login,
base_user_id: data.base.user.id,
base_user_avatar_url: data.base.user.avatar_url,
base_user_url: data.base.user.url,
base_user_html_url: data.base.user.html_url,
start_time: new Date(),
repositories_id: repo.id
};
Expand Down Expand Up @@ -312,7 +326,7 @@ export function synchronizePullRequest(data: any): Promise<any> {
pr: data.number,
label: data.base.label,
ref: data.base.ref,
sha: data.base.sha,
sha: data.head.sha,
head_label: data.head.label,
head_ref: data.head.ref,
head_sha: data.head.sha,
Expand All @@ -331,6 +345,20 @@ export function synchronizePullRequest(data: any): Promise<any> {
head_user_avatar_url: data.head.user.avatar_url,
head_user_url: data.head.user.url,
head_user_html_url: data.head.user.html_url,
base_github_id: data.base.repo.id,
base_clone_url: data.base.repo.clone_url,
base_html_url: data.base.repo.html_url,
base_default_branch: data.base.repo.default_branch,
base_name: data.base.repo.name,
base_full_name: data.base.repo.full_name,
base_description: data.base.repo.description,
base_private: data.base.repo.private,
base_fork: data.base.repo.fork,
base_user_login: data.base.user.login,
base_user_id: data.base.user.id,
base_user_avatar_url: data.base.user.avatar_url,
base_user_url: data.base.user.url,
base_user_html_url: data.base.user.html_url,
start_time: new Date(),
repositories_id: repo.id
};
Expand Down
6 changes: 3 additions & 3 deletions src/api/github-commit-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function setGitHubStatusPending(
let data = {
'state': 'pending',
'target_url': abstruseUrl,
'description': 'The Abstruse CI build succeeded',
'description': 'The Abstruse CI build is running',
'context': 'continuous-integration/abstruse'
};

Expand All @@ -65,7 +65,7 @@ export function setGitHubStatusError(
let data = {
'state': 'error',
'target_url': abstruseUrl,
'description': 'The Abstruse CI build succeeded',
'description': 'The Abstruse CI build errored',
'context': 'continuous-integration/abstruse'
};

Expand All @@ -82,7 +82,7 @@ export function setGitHubStatusFailure(
let data = {
'state': 'failure',
'target_url': abstruseUrl,
'description': 'The Abstruse CI build succeeded',
'description': 'The Abstruse CI build failed',
'context': 'continuous-integration/abstruse'
};

Expand Down
6 changes: 5 additions & 1 deletion src/api/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export function info(msg: string): void {

export function error(msg: string | any): void {
const time = getDateTime();
console.log(`${green('[' + time + ']')} ${red(msg.toString())}`);
if (typeof msg !== 'string') {
msg = JSON.stringify(msg);
}

console.log(`${green('[' + time + ']')} ${red(msg)}`);
}

export function warning(msg: string): void {
Expand Down
69 changes: 56 additions & 13 deletions src/api/process-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobProcesses
export function startBuild(data: any): Promise<any> {
return getRepositoryOnly(data.repositories_id)
.then(repository => {
const sha = data && data.sha ? data.sha : null;
let sha = data && data.sha ? data.sha : null;
const pr = data && data.pr ? data.pr : null;
let repoDetails = null;

Expand Down Expand Up @@ -130,6 +130,8 @@ export function startBuild(data: any): Promise<any> {
head_user_url: commit.author.url,
head_user_html_url: commit.author.html_url
});

sha = sha || repoDetails.log.commit_hash;
}

return insertBuild(data)
Expand All @@ -140,8 +142,8 @@ export function startBuild(data: any): Promise<any> {
insertBuildRun(data)
.then(() => {
if (repository.access_token) {
const gitUrl =
`https://api.github.com/repos/${data.head_full_name}/statuses/${data.sha}`;
const name = data.base_full_name;
const gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${build.id}`;
return setGitHubStatusPending(gitUrl, abstruseUrl, repository.access_token);
} else {
Expand Down Expand Up @@ -253,7 +255,7 @@ export function stopJob(jobId: number): Promise<void> {
.then(runId => dbJobRuns.getRun(runId))
.then(jobRun => {
if (jobRun.status !== 'success') {
return dbJobRuns.updateJobRun({id: jobRun.id, end_time: new Date(), status: 'failed'});
return dbJobRuns.updateJobRun({id: jobRun.id, end_time: new Date(), status: 'failed' });
}
})
.then(() => {
Expand Down Expand Up @@ -353,8 +355,9 @@ function prepareJob(buildId: number, jobId: number, cmds: any, sshAndVnc = false
.then(() => getBuild(buildId))
.then(build => {
if (build.repository.access_token) {
const gitUrl =
`https://api.github.com/repos/${build.head_full_name}/statuses/${build.sha}`;
const sha = build.sha;
const name = build.base_full_name;
const gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${buildId}`;
return setGitHubStatusFailure(gitUrl, abstruseUrl, build.repository.access_token);
} else {
Expand All @@ -378,14 +381,15 @@ function prepareJob(buildId: number, jobId: number, cmds: any, sshAndVnc = false
.then(() => getBuildStatus(buildId))
.then(status => {
if (status) {
return updateBuild({ id: buildId, end_time: new Date()})
return updateBuild({ id: buildId, end_time: new Date() })
.then(() => getLastRunId(buildId))
.then(id => updateBuildRun({ id: id, end_time: new Date()}))
.then(id => updateBuildRun({ id: id, end_time: new Date()} ))
.then(() => getBuild(buildId))
.then(build => {
if (build.repository.access_token) {
const gitUrl =
`https://api.github.com/repos/${build.head_full_name}/statuses/${build.sha}`;
const sha = build.sha;
const name = build.base_full_name;
const gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${buildId}`;
return setGitHubStatusSuccess(gitUrl, abstruseUrl,
build.repository.access_token);
Expand Down Expand Up @@ -449,9 +453,10 @@ export function restartBuild(buildId: number): Promise<any> {
})
.then(() => {
if (accessToken) {
const gitUrl =
`https://api.github.com/repos/${buildData.head_full_name}/statuses/${buildData.sha}`;
const abstruseUrl = `${config.url}/build/${buildData.id}`;
const sha = buildData.sha;
const name = buildData.base_full_name;
const gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${buildId}`;
return setGitHubStatusPending(gitUrl, abstruseUrl, accessToken);
} else {
return Promise.resolve();
Expand All @@ -467,6 +472,16 @@ export function stopBuild(buildId: number): Promise<any> {
return procs.filter(job => job.build_id === buildId).reduce((prev, current) => {
return prev.then(() => stopJob(current.job_id));
}, Promise.resolve());
})
.then(() => getBuild(buildId))
.then(buildData => {
if (buildData.repository.access_token) {
const sha = buildData.sha;
const name = buildData.base_full_name;
const gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${buildId}`;
return setGitHubStatusFailure(gitUrl, abstruseUrl, buildData.repository.access_token);
}
});
}

Expand All @@ -481,6 +496,7 @@ export function restartJob(jobId: number): Promise<void> {
log: '',
build_run_id: lastRun.build_run_id,
job_id: jobId }))
.then(() => dbJob.getJob(jobId))
.then(job => jobData = job)
.then(() => queueJob(jobData.builds_id, jobId))
.then(() => {
Expand All @@ -490,6 +506,19 @@ export function restartJob(jobId: number): Promise<void> {
job_id: jobData.id,
data: 'jobRestarted'
});
})
.then(() => getBuild(jobData.builds_id))
.then(build => {
if (build.repository.access_token) {
const sha = build.sha;
const name = build.base_full_name;
const gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${jobData.build_id}`;
return setGitHubStatusPending(gitUrl, abstruseUrl,
build.repository.access_token);
} else {
return Promise.resolve();
}
});
}

Expand All @@ -504,6 +533,7 @@ export function restartJobWithSshAndVnc(jobId: number): Promise<void> {
log: '',
build_run_id: lastRun.build_run_id,
job_id: jobId }))
.then(() => dbJob.getJob(jobId))
.then(job => jobData = job)
.then(() => queueJob(jobData.builds_id, jobId, true))
.then(() => {
Expand All @@ -513,6 +543,19 @@ export function restartJobWithSshAndVnc(jobId: number): Promise<void> {
job_id: jobData.id,
data: 'jobRestarted'
});
})
.then(() => getBuild(jobData.builds_id))
.then(build => {
if (build.repository.access_token) {
const sha = build.sha;
const name = build.base_full_name;
const gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`;
const abstruseUrl = `${config.url}/build/${jobData.build_id}`;
return setGitHubStatusPending(gitUrl, abstruseUrl,
build.repository.access_token);
} else {
return Promise.resolve();
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="columns is-multiline">
<div class="column is-10">
<h1 class="bold">
<span><a [routerLink]="['/repo', build?.repository.id]">{{ build?.head_full_name }}</a></span>
<span><a [routerLink]="['/repo', build?.repository.id]">{{ build?.base_full_name }}</a></span>
<span class="build-icon">
<img src="images/icons/clock.svg" *ngIf="status === 'queued'">
<img src="images/icons/flickr.svg" *ngIf="status === 'running'">
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/app-job/app-job.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="columns is-multiline">
<div class="column is-7">
<h1 class="bold">
<span><a [routerLink]="['/repo', job?.build?.repository?.id]">{{ job?.build?.head_full_name }}</a></span>
<span><a [routerLink]="['/repo', job?.build?.repository?.id]">{{ job?.build?.base_full_name }}</a></span>
<span class="build-icon">
<img src="images/icons/clock.svg" *ngIf="jobRun?.status === 'queued'">
<img src="images/icons/flickr.svg" *ngIf="jobRun?.status === 'running'">
Expand Down

0 comments on commit 41c4dc7

Please sign in to comment.