diff --git a/createpullrequest/createPullRequest.ts b/createpullrequest/createPullRequest.ts index 3550f1e..70b956d 100644 --- a/createpullrequest/createPullRequest.ts +++ b/createpullrequest/createPullRequest.ts @@ -10,40 +10,78 @@ import * as tl from "azure-pipelines-task-lib/task"; async function run() { - let provider = getRepositoryProvider(); + let selectionMethod = tl.getInput("repositorySelectionMethod", true); + let repositoryId:string = null; + let project:string = null; - if (provider !== "TfsGit") { - throw `detected a repository provider that is not TfsGit. Provider "${provider}" is not supported.`; - } + if(selectionMethod === "currentBuild") { - let azdevApi: azdev.WebApi = await getWebApi(); - let gitApi: ga.IGitApi = await azdevApi.getGitApi(); + let provider = getRepositoryProvider(); - const project: string = getProject(); - const repositoryName = getRepositoryName(); + if (provider !== "TfsGit") { + throw `detected a repository provider that is not TfsGit. Provider "${provider}" is not supported.`; + } - console.log(`searching for repository "${repositoryName}" in project "${project}"`); + let azdevApi: azdev.WebApi = await getWebApi(); + let gitApi: ga.IGitApi = await azdevApi.getGitApi(); - const respositories: gi.GitRepository[] = await gitApi.getRepositories(project); + project = getProject(); + let repositoryName = getRepositoryName(); - if (respositories) { - tl.debug(`found ${respositories.length} respositories:`); - respositories.forEach((repository, index) => { - tl.debug(repository.name); - }); - } else { - throw "cannot find any repositories"; + console.log(`searching for repository "${repositoryName}" in project "${project}"`); + + const respositories: gi.GitRepository[] = await gitApi.getRepositories(project); + + if (respositories) { + tl.debug(`found ${respositories.length} respositories:`); + respositories.forEach((repository, index) => { + tl.debug(repository.name); + }); + } else { + throw "cannot find any repositories"; + } + + let repository = respositories.filter(repository => { + return repository.name === repositoryName; + })[0]; + + if (!repository) { + throw `cannot find repository with name "${repositoryName}"`; + } + + tl.debug(`found repository "${repository.name}" with id "${repository.id}"`); + repositoryId = repository.id + }else{ + repositoryId = tl.getInput("gitRepositoryId", true); + project = tl.getInput("projectId", true); } - let repository = respositories.filter(repository => { - return repository.name === repositoryName; - })[0]; + await CreatePullRequest(project, repositoryId); +} + +let taskManifestPath = path.join(__dirname, "task.json"); +tl.debug("Setting resource path to " + taskManifestPath); +tl.setResourcePath(taskManifestPath); - if (!repository) { - throw `cannot find repository with name "${repositoryName}"`; +run().then((result) => { + tl.setResult(tl.TaskResult.Succeeded, "Create pull request succeeded"); +}).catch((error) => { + tl.setResult(tl.TaskResult.Failed, !!error.message ? error.message : error); +}); + +function getEnv(name: string): string { + let val = process.env[name]; + if (!val) { + console.error(name + " environment variable is not set"); + process.exit(1); } + return val; +} + +async function CreatePullRequest(project:string, repositoryId:string) { + let azdevApi: azdev.WebApi = await getWebApi(); + let gitApi: ga.IGitApi = await azdevApi.getGitApi(); - tl.debug(`found repository "${repository.name}" with id "${repository.id}"`); let createPullRequest: gi.GitPullRequest = {}; let sourceBranch = tl.getInput("sourceBranch", true); @@ -65,7 +103,7 @@ async function run() { createPullRequest.reviewers.push(reviewer); } - let pullRequest: gi.GitPullRequest = await gitApi.createPullRequest(createPullRequest, repository.id, project, true); + let pullRequest: gi.GitPullRequest = await gitApi.createPullRequest(createPullRequest, repositoryId, project, true); console.log(`created pull request with id ${pullRequest.pullRequestId}`); if (tl.getBoolInput("approve")) { @@ -73,35 +111,16 @@ async function run() { let approve: gi.IdentityRefWithVote = {}; approve.id = pullRequest.createdBy.id; approve.vote = 10; - await gitApi.createPullRequestReviewer(approve, repository.id, pullRequest.pullRequestId, pullRequest.createdBy.id, project); + await gitApi.createPullRequestReviewer(approve, repositoryId, pullRequest.pullRequestId, pullRequest.createdBy.id, project); } if (tl.getBoolInput("autoComplete")) { let setAutoComplete = {}; setAutoComplete.autoCompleteSetBy = pullRequest.createdBy; - await gitApi.updatePullRequest(setAutoComplete, repository.id, pullRequest.pullRequestId, project); + await gitApi.updatePullRequest(setAutoComplete, repositoryId, pullRequest.pullRequestId, project); } } -let taskManifestPath = path.join(__dirname, "task.json"); -tl.debug("Setting resource path to " + taskManifestPath); -tl.setResourcePath(taskManifestPath); - -run().then((result) => { - tl.setResult(tl.TaskResult.Succeeded, "Create pull request succeeded"); -}).catch((error) => { - tl.setResult(tl.TaskResult.Failed, !!error.message ? error.message : error); -}); - -function getEnv(name: string): string { - let val = process.env[name]; - if (!val) { - console.error(name + " environment variable is not set"); - process.exit(1); - } - return val; -} - async function getWebApi(): Promise { let serverUrl = getEnv("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"); console.log(`connecting to VSTS web API"s on server: "${serverUrl}"`); diff --git a/createpullrequest/task.json b/createpullrequest/task.json index e8ef40d..c30dc1b 100644 --- a/createpullrequest/task.json +++ b/createpullrequest/task.json @@ -17,6 +17,11 @@ "satisfies": ["GitCreatePullRequest"], "demands": [], "groups": [ + { + "name": "repository", + "displayName": "Repository", + "isExpanded": true + }, { "name": "branches", "displayName": "Branches", @@ -35,6 +40,46 @@ ], "instanceNameFormat": "Create pull request", "inputs": [ + { + "name": "repositorySelectionMethod", + "type": "radio", + "label": "Repository to use", + "required": true, + "defaultValue": "currentBuild", + "options": { + "currentBuild": "Current build", + "select": "Select" + }, + "helpMarkDown": "The method for selecting the Git repository. `Current build` will use the repository for which the current build is configured. `Select` will allow you to select an Azure Repository from your account.", + "groupName": "repository" + }, + { + "name": "projectId", + "type": "picklist", + "label": "Project", + "defaultValue": "", + "properties": { + "EditableOptions": "True", + "DisableManageLink": "True" + }, + "required": true, + "helpMarkDown": "Project that contains the git repository you want to create a pull request for.", + "groupName": "repository", + "visibleRule": "repositorySelectionMethod = select" + }, + { + "name": "gitRepositoryId", + "type": "picklist", + "label": "Repository", + "defaultValue": "","properties": { + "EditableOptions": "True", + "DisableManageLink": "True" + } , + "required": true, + "helpMarkDown": "Git repository you want to create a pull request for", + "groupName": "repository", + "visibleRule": "repositorySelectionMethod = select" + }, { "name": "sourceBranch", "type": "string", @@ -105,6 +150,29 @@ "groupName": "completion" } ], + "dataSourceBindings": [ + { + "endpointId": "tfs:teamfoundation", + "target": "projectId", + "endpointUrl": "{{endpoint.url}}/_apis/projects?$skip={{skip}}&$top=1000", + "resultSelector": "jsonpath:$.value[?(@.state=='wellFormed')]", + "resultTemplate": "{ \"Value\" : \"{{{id}}}\", \"DisplayValue\" : \"{{{name}}}\" }", + "callbackContextTemplate": "{\"skip\": \"{{add skip 1000}}\"}", + "callbackRequiredTemplate": "{{isEqualNumber result.count 1000}}", + "initialContextTemplate": "{\"skip\": \"0\"}" + }, + { + "endpointId": "tfs:teamfoundation", + "target": "gitRepositoryId", + "endpointUrl": "{{endpoint.url}}/{{project}}/_apis/git/repositories", + "resultSelector": "jsonpath:$.value[*]", + "parameters": { + "project": "$(projectId)" + }, + "resultTemplate": "{ \"Value\" : \"{{{id}}}\", \"DisplayValue\" : \"{{{name}}}\" }" + } + + ], "execution": { "Node": { "target": "createPullRequest.js" diff --git a/version.json b/version.json index 682bf53..8edb855 100644 --- a/version.json +++ b/version.json @@ -1,4 +1,4 @@ { "major": 0, - "minor": 2 + "minor": 3 } diff --git a/vss-extension.json b/vss-extension.json index f62fe6f..f0cd172 100644 --- a/vss-extension.json +++ b/vss-extension.json @@ -2,7 +2,7 @@ "manifestVersion": 1, "id": "git-buildtasks", "name": "git tasks", - "version": "1.0.7", + "version": "1.0.9", "publisher": "sander-aernouts", "public" : false, "repository": {