Skip to content

Commit

Permalink
JENKINS-43014 save to/from branches properly, use optimized indexing (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kzantow committed Mar 23, 2017
1 parent bdefbef commit d2e6795
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
31 changes: 25 additions & 6 deletions src/main/js/EditorPage.jsx
Expand Up @@ -169,6 +169,7 @@ class PipelineLoader extends React.Component {
agent: { type: 'any' },
children: [],
});
this.forceUpdate();
};

if (!pipeline) {
Expand All @@ -193,6 +194,23 @@ class PipelineLoader extends React.Component {
});
};

if (!branch) {
const split = pipeline.split('/');
const team = split[0];
const repo = split[1];
const provider = 'github';
Fetch.fetchJSON(`${getRestUrl({organization})}scm/${provider}/`)
.then( ({ credentialId }) =>
Fetch.fetchJSON(`${getRestUrl({organization})}scm/${provider}/organizations/${team}/repositories/${repo}/?credentialId=${credentialId}`)
)
.then( ({ defaultBranch }) => {
this.defaultBranch = defaultBranch || 'master';
})
.catch(err => this.defaultBranch = 'master');
} else {
this.defaultBranch = branch;
}

Fetch.fetchJSON(`${getRestUrl(this.props.params)}scm/content/?branch=${encodeURIComponent(branch)}&path=Jenkinsfile`)
.then( ({ content }) => {
const pipelineScript = Base64.decode(content.base64Data);
Expand Down Expand Up @@ -348,7 +366,8 @@ class PipelineLoader extends React.Component {
"content": {
"message": saveMessage,
"path": "Jenkinsfile",
branch: saveToBranch || 'master',
branch: saveToBranch || this.defaultBranch,
sourceBranch: branch,
repo: repo,
"sha": this.state.sha,
"base64Data": Base64.encode(pipelineScript),
Expand All @@ -369,18 +388,18 @@ class PipelineLoader extends React.Component {
if (this.state.sha && branch === body.content.branch) {
RunApi.startRun({ _links: { self: { href: this.href + 'branches/' + encodeURIComponent(branch) + '/' }}})
.then(() => this.goToActivity())
.catch(err => errorHandler(err, body));//this.showErrorDialog(err));
.catch(err => errorHandler(err, body));
} else {
// otherwise, call indexing so this branch gets picked up
saveApi.index(organization, team, () => this.goToActivity(), err => errorHandler(err));//this.showErrorDialog(err));
saveApi.index(organization, team, repo, () => this.goToActivity(), err => errorHandler(err));
}
this.setState({ sha: data.sha });
})
.catch(err => {
errorHandler(err, body);//this.showErrorDialog(err, { saveRequest: body });
errorHandler(err, body);
});
} else {
errorHandler(err);//this.showErrorDialog(err);
errorHandler(err);
}
});
}
Expand Down Expand Up @@ -409,7 +428,7 @@ class PipelineLoader extends React.Component {
</div>
}
{this.state.dialog}
{this.state.showSaveDialog && <SaveDialog branch={branch || 'master'}
{this.state.showSaveDialog && <SaveDialog branch={branch || this.defaultBranch}
cancel={() => this.setState({showSaveDialog: false})}
functions={this} />
}
Expand Down
53 changes: 32 additions & 21 deletions src/main/js/SaveApi.js
@@ -1,10 +1,38 @@
// @flow

import { action, computed, observable } from 'mobx';
import { Fetch, Paths, sseService, loadingIndicator } from '@jenkins-cd/blueocean-core-js';
import { Fetch, getRestUrl, sseService, loadingIndicator, capabilityAugmenter } from '@jenkins-cd/blueocean-core-js';

export class SaveApi {
index(organization, folder, complete, onError, progress) {

indexRepo(organization, teamName, repoName) {
const createUrl = `${getRestUrl({organization})}/pipelines/`;

const requestBody = {
name: teamName,
$class: 'io.jenkins.blueocean.blueocean_github_pipeline.GithubPipelineCreateRequest',
scmConfig: {
uri: 'https://api.github.com',
config: {
orgName: teamName,
repos: [repoName],
},
},
};

const fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
};

return Fetch.fetchJSON(createUrl, { fetchOptions })
.then(pipeline => capabilityAugmenter.augmentCapabilities(pipeline));
}

index(organization, folder, repo, complete, onError, progress) {
const cleanup = err => {
sseService.removeHandler(sseId);
clearTimeout(timeoutId);
Expand All @@ -24,31 +52,14 @@ export class SaveApi {

const sseId = sseService.registerHandler(event => {
if (event.job_multibranch_indexing_result === 'SUCCESS') {
if (progress) progress(event);
}
if (event.job_orgfolder_indexing_result === 'SUCCESS') {
cleanup();
}
if (event.job_orgfolder_indexing_result === 'FAILURE') {
if (event.job_multibranch_indexing_result === 'FAILURE') {
cleanup({ message: 'Indexing failed' });
}
});

Fetch.fetchJSON(Paths.rest.apiRoot() + '/organizations/' + organization + '/pipelines/' + folder + '/runs/1/replay/', {
fetchOptions: {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: '{}',
}
})
.then(data => {
// Nothing to do here
})
.catch(err => {
cleanup(err);
});
this.indexRepo(organization, folder, repo);
}
}

Expand Down

0 comments on commit d2e6795

Please sign in to comment.