Skip to content

Commit

Permalink
Fixing the Bitbucket commit url in Jenkins task. (#9296)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalyan-microsoft authored Jan 10, 2019
1 parent d398b6c commit 245d5e9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as os from 'os';
import * as path from 'path';
import * as tl from 'vsts-task-lib/task';

import url = require('url');

import {ArtifactDetailsDownloaderBase} from "./ArtifactDetailsDownloaderBase"
import {JenkinsRestClient, JenkinsJobDetails} from "./JenkinsRestClient"

Expand Down Expand Up @@ -174,7 +176,7 @@ export class CommitsDownloader extends ArtifactDetailsDownloaderBase {

commitMessages.forEach((commit) => {
tl.debug('Normalizing url' + commit.DisplayUri);
commit.DisplayUri = this.ConvertGitProtocolUrlToHttpProtocol(commit.DisplayUri);
commit.DisplayUri = this.TransformCommitUrl(commit.DisplayUri);
});

return JSON.stringify(commitMessages);
Expand All @@ -188,6 +190,21 @@ export class CommitsDownloader extends ArtifactDetailsDownloaderBase {
return '';
};

private TransformCommitUrl(commitUrl: string): string {
commitUrl = this.ConvertGitProtocolUrlToHttpProtocol(commitUrl);

try {
if (url.parse(commitUrl).hostname.toUpperCase() == this.BitBucketHostName.toUpperCase()) {
commitUrl = commitUrl.replace('/commit/', '/commits/')
}
} catch (error) {
tl.debug(`Error while parsing the commit url ${commitUrl}`);
}

tl.debug(`Translated url ${commitUrl} after fixing the query path based on the provider`);
return commitUrl;
}

private ConvertGitProtocolUrlToHttpProtocol(commitUrl: string): string {
var result: string = '';
if (!!commitUrl) {
Expand All @@ -210,4 +227,6 @@ export class CommitsDownloader extends ArtifactDetailsDownloaderBase {
tl.debug(`Translated url ${commitUrl} to ${result}`);
return result;
}

private BitBucketHostName: string = "bitbucket.org";
}
19 changes: 19 additions & 0 deletions Tasks/JenkinsDownloadArtifactsV1/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ describe('JenkinsDownloadArtifacts L0 Suite', function () {
}
});

it('Validate bitbucket commit url', (done) => {

const tp: string = path.join(__dirname, 'L0ValidateBitBucketCommitUrl.js');
const tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);

try {
tr.run();

assert(tr.stdout.indexOf('Translated url http://bitbucket.org/commits/3cbfc14e3f482a25e5122323f3273b89677d9875 after fixing the query path based on the provider') !== -1, tr.stdout);

done();
} catch(err) {
console.log(tr.stdout);
console.log(tr.stderr);
console.log(err);
done(err);
}
});

it('Validate http commit url', (done) => {

const tp: string = path.join(__dirname, 'L0ValidateHttpCommitUrl.js');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import ma = require('vsts-task-lib/mock-answer');
import tmrm = require('vsts-task-lib/mock-run');
import path = require('path');
import mockTask = require('vsts-task-lib/mock-task');
import helper = require("./JenkinsTestHelper");

const taskPath = path.join(__dirname, '..', 'jenkinsdownloadartifacts.js');
const tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

tr.setInput("serverEndpoint", "ID1");
tr.setInput("jobName", "myfreestyleproject")
tr.setInput("saveTo", "jenkinsArtifacts");
tr.setInput("filePath", "/");
tr.setInput("jenkinsBuild", "BuildNumber");
tr.setInput("jenkinsBuildNumber", "20");
tr.setInput("itemPattern", "archive/**");
tr.setInput("downloadCommitsAndWorkItems", "true");
tr.setInput("artifactDetailsFileNameSuffix", "alias_v1.json");

process.env['ENDPOINT_URL_ID1'] = 'http://url';
process.env['ENDPOINT_AUTH_PARAMETER_connection1_username'] = 'dummyusername';
process.env['ENDPOINT_AUTH_PARAMETER_connection1_password'] = 'dummypassword';
process.env['ENDPOINT_DATA_ID1_acceptUntrustedCerts'] = 'true';

helper.RegisterArtifactEngineMock(tr);
helper.RegisterHttpClientMock(tr, (url: string) => {
if (url === "http://url/job/myfreestyleproject//api/json") {
return helper.GetSuccessExpectedResult('{}');
}

if (url === "http://url//job/myfreestyleproject//20/api/json?tree=artifacts[*]") {
return helper.GetSuccessExpectedResult('{ "_class": "hudson.model.FreeStyleBuild", "artifacts": [ "abc" ] }');
}

var commitResult = '{"actions":[{"remoteUrls":["http://bitbucket.org"]}],"changeSet":{"items": [{"commitId": "3cbfc14e3f482a25e5122323f3273b89677d9875", "author": { "fullName": "user" }, "msg": "test3", "date": "2018-07-07 12:18:48 +0530" }], "kind": "git"}}'
if (url == "http://url/job/myfreestyleproject//20/api/json?tree=number,result,actions[remoteUrls],changeSet[kind,items[commitId,date,msg,author[fullName]]]") {
return helper.GetSuccessExpectedResult(commitResult);
}
});

tr.run();
2 changes: 1 addition & 1 deletion Tasks/JenkinsDownloadArtifactsV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"demands": [],
"version": {
"Major": 1,
"Minor": 146,
"Minor": 147,
"Patch": 0
},
"groups": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/JenkinsDownloadArtifactsV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"demands": [],
"version": {
"Major": 1,
"Minor": 146,
"Minor": 147,
"Patch": 0
},
"groups": [
Expand Down

0 comments on commit 245d5e9

Please sign in to comment.