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

Transforming git url to http url while downloading commits from Jenki… #7650

Merged
merged 3 commits into from
Jul 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ export class CommitsDownloader extends ArtifactDetailsDownloaderBase {
public static GetCommitMessagesFromCommits(commits: string): string[] {
console.log(tl.loc("GetCommitMessages"));

// remove the extra comma at the end of the commit item
let index: number = commits.lastIndexOf(",");
if (index > -1) {
commits = commits.substring(0, index) + commits.substring(index + 1);
}

let template = handlebars.compile(GetCommitMessagesTemplate);
try {
var result = template(JSON.parse(commits));
Expand Down Expand Up @@ -111,7 +105,9 @@ export class CommitsDownloader extends ArtifactDetailsDownloaderBase {

this.jenkinsClient.DownloadJsonContent(commitsUrl, CommitTemplate, null).then((commitsResult) => {
tl.debug(`Downloaded commits: ${commitsResult}`);
defer.resolve(commitsResult);

var commits: string = this.TransformCommits(commitsResult);
defer.resolve(commits);
}, (error) => {
defer.reject(error);
});
Expand All @@ -128,7 +124,9 @@ export class CommitsDownloader extends ArtifactDetailsDownloaderBase {
tl.debug(`Downloading commits from startIndex ${startIndex} and endIndex ${endIndex}`);
this.jenkinsClient.DownloadJsonContent(commitsUrl, CommitsTemplate, {'buildParameter': buildParameter}).then((commitsResult) => {
tl.debug(`Downloaded commits: ${commitsResult}`);
defer.resolve(commitsResult);

var commits: string = this.TransformCommits(commitsResult);
defer.resolve(commits);
}, (error) => {
defer.reject(error);
});
Expand Down Expand Up @@ -161,4 +159,56 @@ export class CommitsDownloader extends ArtifactDetailsDownloaderBase {

return fileName;
}

private TransformCommits(commits: string): string {
if (!!commits) {

// remove the extra comma at the end of the commit item
let index: number = commits.lastIndexOf(",");
if (index > -1) {
commits = commits.substring(0, index) + commits.substring(index + 1);
}

try {
var commitMessages = JSON.parse(commits);

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

return JSON.stringify(commitMessages);

} catch(error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after catch

console.log(tl.loc("CannotParseCommits", commits, error));
throw error;
}
}

return '';
};

private ConvertGitProtocolUrlToHttpProtocol(commitUrl: string): string {
var result: string = '';
if (!!commitUrl) {
if (commitUrl.startsWith('git@')) {
tl.debug('repo url is a git protocol url');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove blank line


if (commitUrl.startsWith('[email protected]')) {
result= commitUrl.replace('[email protected]:', 'https://gitlab.com/').replace('.git/', '/');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mentioned user name related handling for GitLab. is this required?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, yes this is sufficient. This is how the url looks like [email protected]:username/project.git we should translate this to https://gitlab.com/username/project/commit/{commitId} same with github. GitHub url looks like [email protected]:kasubram/TestRepo.git we should translate this to kasubram/TestRepo@{commitId}

}
else if (commitUrl.startsWith('[email protected]')) {
result = commitUrl.replace('[email protected]:', 'https://github.com/').replace('.git/', '/');
}
}
else if (commitUrl.startsWith('http')) {
// if its http return the url as is.
result = commitUrl;
}
}

tl.debug(`Translated url ${commitUrl} to ${result}`);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"loc.messages.SuccessfullyDownloadedCommitsAndWorkItems": "Commits and work items downloaded successfully",
"loc.messages.CommitsAndWorkItemsDownloadFailed": "Downloading commits and work items from Jenkins failed with an error: %s",
"loc.messages.CannotFindBuilds": "Cannot find builds",
"loc.messages.CannotParseCommits": "Cannot parse commit objects %s. Error: %s",
"loc.messages.JenkinsNoCommitsToFetch": "Start and end build Ids are same. Commits and work items will not be downloaded.",
"loc.messages.InvalidJenkinsBuildNumber": "Cannot parse the Jenkins Build number.",
"loc.messages.InvalidJenkinsStartBuildNumber": "The specified Jenkins start build number %s is not valid.",
Expand Down
76 changes: 76 additions & 0 deletions Tasks/JenkinsDownloadArtifactsV1/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,82 @@ describe('JenkinsDownloadArtifacts L0 Suite', function () {
}
});

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

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

try {
tr.run();

assert(tr.stdout.indexOf('Translated url [email protected]:user/TestRepo.git/commit/3cbfc14e3f482a25e5122323f3273b89677d9875 to https://github.com/user/TestRepo/commit/3cbfc14e3f482a25e5122323f3273b89677d9875') !== -1, tr.stdout);

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

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

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

try {
tr.run();

assert(tr.stdout.indexOf('Translated url [email protected]:admin/projectk.git/commit/3cbfc14e3f482a25e5122323f3273b89677d9875 to https://gitlab.com/admin/projectk/commit/3cbfc14e3f482a25e5122323f3273b89677d9875') !== -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');
const tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);

try {
tr.run();

assert(tr.stdout.indexOf('Translated url https://github.com/user/TestRepo/commit/3cbfc14e3f482a25e5122323f3273b89677d9875 to https://github.com/user/TestRepo/commit/3cbfc14e3f482a25e5122323f3273b89677d9875') !== -1, tr.stdout);

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

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

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

try {
tr.run();

assert(tr.stdout.indexOf('Translated url ssh://user@server/project.git/commit/3cbfc14e3f482a25e5122323f3273b89677d9875 to') !== -1, tr.stdout);

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

it('Should download commits from build range', (done) => {
const tp: string = path.join(__dirname, 'L0DownloadCommitsFromBuildRange.js');
const tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);
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":["[email protected]:user/TestRepo.git"]}],"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();
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":["[email protected]:admin/projectk.git"]}],"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();
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":["https://github.com/user/TestRepo"]}],"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();
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":["ssh://user@server/project.git"]}],"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();
3 changes: 2 additions & 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": 137,
"Minor": 138,
"Patch": 0
},
"groups": [
Expand Down Expand Up @@ -304,6 +304,7 @@
"SuccessfullyDownloadedCommitsAndWorkItems": "Commits and work items downloaded successfully",
"CommitsAndWorkItemsDownloadFailed": "Downloading commits and work items from Jenkins failed with an error: %s",
"CannotFindBuilds": "Cannot find builds",
"CannotParseCommits": "Cannot parse commit objects %s. Error: %s",
"JenkinsNoCommitsToFetch": "Start and end build Ids are same. Commits and work items will not be downloaded.",
"InvalidJenkinsBuildNumber": "Cannot parse the Jenkins Build number.",
"InvalidJenkinsStartBuildNumber": "The specified Jenkins start build number %s is not valid.",
Expand Down
3 changes: 2 additions & 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": 137,
"Minor": 138,
"Patch": 0
},
"groups": [
Expand Down Expand Up @@ -304,6 +304,7 @@
"SuccessfullyDownloadedCommitsAndWorkItems": "ms-resource:loc.messages.SuccessfullyDownloadedCommitsAndWorkItems",
"CommitsAndWorkItemsDownloadFailed": "ms-resource:loc.messages.CommitsAndWorkItemsDownloadFailed",
"CannotFindBuilds": "ms-resource:loc.messages.CannotFindBuilds",
"CannotParseCommits": "ms-resource:loc.messages.CannotParseCommits",
"JenkinsNoCommitsToFetch": "ms-resource:loc.messages.JenkinsNoCommitsToFetch",
"InvalidJenkinsBuildNumber": "ms-resource:loc.messages.InvalidJenkinsBuildNumber",
"InvalidJenkinsStartBuildNumber": "ms-resource:loc.messages.InvalidJenkinsStartBuildNumber",
Expand Down