Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pipeline starts without creating a trigger #512

Merged
merged 1 commit into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 86 additions & 25 deletions src/api-client/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,52 +192,113 @@ 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"
})
})
}

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) => {
function redirectWhenForkFinished(projectId, history){
const headers = client.getBasicHeaders();
headers.append('Content-Type', 'application/json');
let redirected = false;
let counter= 0;
const projectStatusTimeout = setTimeout(() => {
if(redirected === true || counter === 200)
clearTimeout(projectStatusTimeout);
else {
client.getProjectStatus(projectId).then((forkProjectStatus) => {
if(forkProjectStatus === 'finished'){
redirected = true;
clearTimeout(projectStatusTimeout);
history.push(`/projects/${projectId}`);
} else if(forkProjectStatus === 'failed' || forkProjectStatus === 'error'){
clearTimeout(projectStatusTimeout);
} else {
counter++;
}
})
}
}, 3000);
}

client.forkProject = (projectMeta, history) => {
const gitlabProject = {
id: projectMeta.id
};
if (projectMeta.projectNamespace != null) gitlabProject.namespace = projectMeta.projectNamespace.id;
const headers = client.getBasicHeaders();
headers.append('Content-Type', 'application/json');

let createGraphWebhookPromise;
let createGraphWebhookPromise, startPipelinePromise;
const newProjectPromise = client.clientFetch(`${client.baseUrl}/projects/${projectMeta.id}/fork`, {
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) {
promises = promises.concat(createGraphWebhookPromise);
}

promises = promises.concat(startPipelinePromise);

return Promise.all(promises)
.then((results) => {
if (results.errorData)
return Promise.reject(results);
return Promise.resolve(results).then(() => results[0].data);
return Promise.resolve(results).then(() => redirectWhenForkFinished(results[0].data.id, history));
});
}

Expand Down
6 changes: 1 addition & 5 deletions src/project/fork/ProjectFork.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ class Fork extends Component {
}

this.forkProject.set('display.loading', true);
this.props.client.forkProject(this.forkProject.get().meta)
.then((project) => {
this.forkProject.set('display.loading', false);
this.props.history.push(`/projects/${project.id}`);
})
this.props.client.forkProject(this.forkProject.get().meta , this.props.history)
.catch(error => {
let display_messages = [];
if (error.errorData && error.errorData.message) {
Expand Down
20 changes: 17 additions & 3 deletions src/project/fork/ProjectFork.present.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,18 @@ class ForkProjectModal extends Component {
<SubmitLoader loading={this.props.model.display.loading} />
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={this.props.handlers.onSubmit}>Fork</Button>{' '}
<Button color="secondary" onClick={this.props.handlers.toogleForkModal}>Cancel</Button>
<Button
color="primary"
disabled={ this.props.model.display.loading }
onClick={this.props.handlers.onSubmit}>
Fork
</Button>{' '}
<Button
color="secondary"
disabled={ this.props.model.display.loading }
onClick={this.props.handlers.toogleForkModal}>
Cancel
</Button>
</ModalFooter>
</Modal>
</div>
Expand All @@ -221,7 +231,11 @@ class ForkProjectModal extends Component {
class SubmitLoader extends Component {
render() {
if (!this.props.loading) return null;
return(<Loader size="16" inline="true" margin="2" />)
return(
<FormText color="primary">
<Loader size="16" inline="true" margin="2"/>
The project is being forked...
</FormText> )
}
}

Expand Down