Skip to content

Commit

Permalink
feat: pipeline starts without creating a trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
vfried committed Jul 9, 2019
1 parent 02b3afa commit 8a2faca
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 22 deletions.
91 changes: 69 additions & 22 deletions src/api-client/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import { fetchJson } from './utils';
import yaml from 'yaml-js';
import { pipeline } from 'stream';

const FileCategories = {
data: (path) => path.startsWith('data'),
Expand Down Expand Up @@ -163,6 +164,65 @@ function addProjectMethods(client) {
});
}

client.getProjectStatus = (projectId) =>{
const headers = client.getBasicHeaders();
headers.append('Content-Type', 'application/json');
return client.clientFetch(`${client.baseUrl}/projects/${projectId}/import`, {
method: 'GET',
headers: headers
}).then(resp => {
return resp.data.import_status;
}).catch((error) => "error");
}

function runPipeline(projectId){
const headers = client.getBasicHeaders();
headers.append('Content-Type', 'application/json');
return client.clientFetch(`${client.baseUrl}/projects/${projectId}/pipeline`, {
method: 'POST',
headers: headers,
body: JSON.stringify({
ref: "master"
})
}).catch(error => {
// In case there is an error we try to start the pipeline again.
console.log("CI Pipeline could not be started")
console.log(error.message)
console.log("Retrying CI pipeline start.")
return client.clientFetch(`${client.baseUrl}/projects/${projectId}/pipeline`, {
method: 'POST',
headers: headers,
body: JSON.stringify({
ref:"master"
})
})
})
}

client.startPipeline = (projectId) => {
const headers = client.getBasicHeaders();
headers.append('Content-Type', 'application/json');
let pipelineStarted = false;
let counter= 0;
const projectStatusTimeout = setTimeout(() => {
if(pipelineStarted === true || counter === 10)
clearTimeout(projectStatusTimeout);
else {
client.getProjectStatus(projectId).then((forkProjectStatus) => {
if(forkProjectStatus === 'finished'){
runPipeline(projectId).then(resp => {
pipelineStarted = true;
clearTimeout(projectStatusTimeout);
});
} else if(forkProjectStatus === 'failed' || forkProjectStatus === 'error'){
clearTimeout(projectStatusTimeout);
} else {
counter++;
}
})
}
}, 3000);
}

client.forkProject = (projectMeta) => {
const gitlabProject = {
Expand All @@ -177,28 +237,15 @@ function addProjectMethods(client) {
method: 'POST',
headers: headers,
body: JSON.stringify(gitlabProject)
})
.then(resp => {
if (!projectMeta.optoutKg) {
createGraphWebhookPromise = client.createGraphWebhook(resp.data.id);
}
client.clientFetch(`${client.baseUrl}/projects/${resp.data.id}/triggers` , {
method: 'POST',
headers: headers,
body: JSON.stringify({
description: `Automatic Fork trigger`
})
}).then(trigger => { client.clientFetch(`${client.baseUrl}/projects/${resp.data.id}/trigger/pipeline`, {
method: 'POST',
headers: headers,
body: JSON.stringify({
ref:'master',
token: trigger.data.token
})
});
});
return resp;
});
}).then(resp => {
if (!projectMeta.optoutKg) {
createGraphWebhookPromise = client.createGraphWebhook(resp.data.id);
}
return resp;
}).then(resp => {
client.startPipeline(resp.data.id);
return resp;
});

let promises = [newProjectPromise];
if (createGraphWebhookPromise) {
Expand Down
3 changes: 3 additions & 0 deletions src/project/fork/ProjectFork.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class Fork extends Component {

this.forkProject.set('display.loading', true);
this.props.client.forkProject(this.forkProject.get().meta)
.then((project) => {
return project;
})
.then((project) => {
this.forkProject.set('display.loading', false);
this.props.history.push(`/projects/${project.id}`);
Expand Down

0 comments on commit 8a2faca

Please sign in to comment.