-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
|
@@ -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); | ||
}); | ||
|
@@ -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); | ||
}); | ||
|
@@ -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) { | ||
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'); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/', '/'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you mentioned user name related handling for GitLab. is this required? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
|
@@ -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); | ||
|
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space after catch