Skip to content

Commit

Permalink
JENKINS-41387 - Work with and without crumbs enabled (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
kzantow committed Jan 24, 2017
1 parent 5b620f5 commit a29dc9d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 44 deletions.
Expand Up @@ -29,6 +29,7 @@
import hudson.Extension;
import hudson.ExtensionList;
import hudson.model.Descriptor;
import hudson.security.csrf.CrumbIssuer;
import io.jenkins.blueocean.commons.stapler.TreeResponse;
import io.jenkins.blueocean.rest.ApiRoutable;
import jenkins.model.Jenkins;
Expand All @@ -53,7 +54,11 @@ public String getUrlName() {

@GET
public String doCrumbInfo() {
return Jenkins.getInstance().getCrumbIssuer().getCrumbRequestField() + "=" + Jenkins.getInstance().getCrumbIssuer().getCrumb();
CrumbIssuer crumbIssuer = Jenkins.getInstance().getCrumbIssuer();
if (crumbIssuer != null) {
return crumbIssuer.getCrumbRequestField() + "=" + crumbIssuer.getCrumb();
}
return "";
}

/**
Expand Down
48 changes: 5 additions & 43 deletions src/main/js/services/PipelineSyntaxConverter.js
@@ -1,6 +1,6 @@
// @flow

import { Fetch, UrlConfig } from '@jenkins-cd/blueocean-core-js';
import fetch from './fetchClassic';
import { UnknownSection } from './PipelineStore';
import type { PipelineInfo, StageInfo, StepInfo } from './PipelineStore';
import pipelineMetadataService from './PipelineMetadataService';
Expand Down Expand Up @@ -326,47 +326,9 @@ export function convertInternalModelToJson(pipeline: PipelineInfo): PipelineJson
}
return out;
}

function fetch(url, body, handler) {
Fetch.fetch(`${UrlConfig.getJenkinsRootURL()}/blue/rest/pipeline-metadata/crumbInfo`, {
fetchOptions: { method: 'GET' }
}).then(response => {
if (!response.ok) {
console.log('An error happened fetching ', url);
return;
}
const useCrumb = function (crumb) {
crumb = crumb.split('=');
const headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
headers[crumb[0]] = crumb[1];
Fetch.fetchJSON(url, {
fetchOptions: {
method: 'POST',
body: body,
headers: headers,
}
}).then(data => {
if (data.status === 'ok') {
handler(data.data);
} else {
console.log(data);
}
});
};
let crumb = response.text();
if (crumb instanceof Promise) {
crumb.then(useCrumb);
} else {
useCrumb(crumb);
}
});
}

export function convertPipelineToJson(pipeline: string, handler: Function) {
pipelineMetadataService.getStepListing(steps => {
fetch(`${UrlConfig.getJenkinsRootURL()}/pipeline-model-converter/toJson`,
fetch('/pipeline-model-converter/toJson',
'jenkinsfile=' + encodeURIComponent(pipeline), data => {
if (data.errors) {
console.log(data);
Expand All @@ -378,7 +340,7 @@ export function convertPipelineToJson(pipeline: string, handler: Function) {

export function convertJsonToPipeline(json: string, handler: Function) {
pipelineMetadataService.getStepListing(steps => {
fetch(`${UrlConfig.getJenkinsRootURL()}/pipeline-model-converter/toJenkinsfile`,
fetch('/pipeline-model-converter/toJenkinsfile',
'json=' + encodeURIComponent(json), data => {
if (data.errors) {
console.log(data);
Expand All @@ -390,7 +352,7 @@ export function convertJsonToPipeline(json: string, handler: Function) {

export function convertPipelineStepsToJson(pipeline: string, handler: Function) {
pipelineMetadataService.getStepListing(steps => {
fetch(`${UrlConfig.getJenkinsRootURL()}/pipeline-model-converter/stepsToJson`,
fetch('/pipeline-model-converter/stepsToJson',
'jenkinsfile=' + encodeURIComponent(pipeline), data => {
if (data.errors) {
console.log(data);
Expand All @@ -402,7 +364,7 @@ export function convertPipelineStepsToJson(pipeline: string, handler: Function)

export function convertJsonStepsToPipeline(step: PipelineStep, handler: Function) {
pipelineMetadataService.getStepListing(steps => {
fetch(`${UrlConfig.getJenkinsRootURL()}/pipeline-model-converter/stepsToJenkinsfile`,
fetch('/pipeline-model-converter/stepsToJenkinsfile',
'json=' + encodeURIComponent(JSON.stringify(step)), data => {
if (data.errors) {
console.log(data);
Expand Down
41 changes: 41 additions & 0 deletions src/main/js/services/fetchClassic.js
@@ -0,0 +1,41 @@
import { Fetch, UrlConfig } from '@jenkins-cd/blueocean-core-js';

export default function fetch(path, body, handler) {
Fetch.fetch(`${UrlConfig.getJenkinsRootURL()}/blue/rest/pipeline-metadata/crumbInfo`, {
fetchOptions: { method: 'GET' }
}).then(response => {
if (!response.ok) {
console.log('An error happened fetching ', path);
return;
}
const useCrumb = function (crumb) {
const headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
if (crumb.indexOf('=') > 0) {
crumb = crumb.split('=');
headers[crumb[0]] = crumb[1];
}
Fetch.fetchJSON(UrlConfig.getJenkinsRootURL() + path, {
fetchOptions: {
method: 'POST',
body: body,
headers: headers,
}
}).then(data => {
if (data.status === 'ok') {
handler(data.data);
} else {
console.log(data);
}
});
};
let crumb = response.text();
if (crumb instanceof Promise) {
crumb.then(useCrumb);
} else {
useCrumb(crumb);
}
});
}

0 comments on commit a29dc9d

Please sign in to comment.