Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit 42e3bfa

Browse files
feat: add repo selection to task inputs
1 parent d3e4148 commit 42e3bfa

4 files changed

Lines changed: 134 additions & 47 deletions

File tree

createpullrequest/createPullRequest.ts

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,78 @@ import * as tl from "azure-pipelines-task-lib/task";
1010

1111
async function run() {
1212

13-
let provider = getRepositoryProvider();
13+
let selectionMethod = tl.getInput("repositorySelectionMethod", true);
14+
let repositoryId:string = null;
15+
let project:string = null;
1416

15-
if (provider !== "TfsGit") {
16-
throw `detected a repository provider that is not TfsGit. Provider "${provider}" is not supported.`;
17-
}
17+
if(selectionMethod === "currentBuild") {
1818

19-
let azdevApi: azdev.WebApi = await getWebApi();
20-
let gitApi: ga.IGitApi = await azdevApi.getGitApi();
19+
let provider = getRepositoryProvider();
2120

22-
const project: string = getProject();
23-
const repositoryName = getRepositoryName();
21+
if (provider !== "TfsGit") {
22+
throw `detected a repository provider that is not TfsGit. Provider "${provider}" is not supported.`;
23+
}
2424

25-
console.log(`searching for repository "${repositoryName}" in project "${project}"`);
25+
let azdevApi: azdev.WebApi = await getWebApi();
26+
let gitApi: ga.IGitApi = await azdevApi.getGitApi();
2627

27-
const respositories: gi.GitRepository[] = await gitApi.getRepositories(project);
28+
project = getProject();
29+
let repositoryName = getRepositoryName();
2830

29-
if (respositories) {
30-
tl.debug(`found ${respositories.length} respositories:`);
31-
respositories.forEach((repository, index) => {
32-
tl.debug(repository.name);
33-
});
34-
} else {
35-
throw "cannot find any repositories";
31+
console.log(`searching for repository "${repositoryName}" in project "${project}"`);
32+
33+
const respositories: gi.GitRepository[] = await gitApi.getRepositories(project);
34+
35+
if (respositories) {
36+
tl.debug(`found ${respositories.length} respositories:`);
37+
respositories.forEach((repository, index) => {
38+
tl.debug(repository.name);
39+
});
40+
} else {
41+
throw "cannot find any repositories";
42+
}
43+
44+
let repository = respositories.filter(repository => {
45+
return repository.name === repositoryName;
46+
})[0];
47+
48+
if (!repository) {
49+
throw `cannot find repository with name "${repositoryName}"`;
50+
}
51+
52+
tl.debug(`found repository "${repository.name}" with id "${repository.id}"`);
53+
repositoryId = repository.id
54+
}else{
55+
repositoryId = tl.getInput("gitRepositoryId", true);
56+
project = tl.getInput("projectId", true);
3657
}
3758

38-
let repository = respositories.filter(repository => {
39-
return repository.name === repositoryName;
40-
})[0];
59+
await CreatePullRequest(project, repositoryId);
60+
}
61+
62+
let taskManifestPath = path.join(__dirname, "task.json");
63+
tl.debug("Setting resource path to " + taskManifestPath);
64+
tl.setResourcePath(taskManifestPath);
4165

42-
if (!repository) {
43-
throw `cannot find repository with name "${repositoryName}"`;
66+
run().then((result) => {
67+
tl.setResult(tl.TaskResult.Succeeded, "Create pull request succeeded");
68+
}).catch((error) => {
69+
tl.setResult(tl.TaskResult.Failed, !!error.message ? error.message : error);
70+
});
71+
72+
function getEnv(name: string): string {
73+
let val = process.env[name];
74+
if (!val) {
75+
console.error(name + " environment variable is not set");
76+
process.exit(1);
4477
}
78+
return val;
79+
}
80+
81+
async function CreatePullRequest(project:string, repositoryId:string) {
82+
let azdevApi: azdev.WebApi = await getWebApi();
83+
let gitApi: ga.IGitApi = await azdevApi.getGitApi();
4584

46-
tl.debug(`found repository "${repository.name}" with id "${repository.id}"`);
4785
let createPullRequest: gi.GitPullRequest = <gi.GitPullRequest>{};
4886

4987
let sourceBranch = tl.getInput("sourceBranch", true);
@@ -65,43 +103,24 @@ async function run() {
65103
createPullRequest.reviewers.push(reviewer);
66104
}
67105

68-
let pullRequest: gi.GitPullRequest = await gitApi.createPullRequest(createPullRequest, repository.id, project, true);
106+
let pullRequest: gi.GitPullRequest = await gitApi.createPullRequest(createPullRequest, repositoryId, project, true);
69107
console.log(`created pull request with id ${pullRequest.pullRequestId}`);
70108

71109
if (tl.getBoolInput("approve")) {
72110

73111
let approve: gi.IdentityRefWithVote = <gi.IdentityRefWithVote>{};
74112
approve.id = pullRequest.createdBy.id;
75113
approve.vote = 10;
76-
await gitApi.createPullRequestReviewer(approve, repository.id, pullRequest.pullRequestId, pullRequest.createdBy.id, project);
114+
await gitApi.createPullRequestReviewer(approve, repositoryId, pullRequest.pullRequestId, pullRequest.createdBy.id, project);
77115
}
78116

79117
if (tl.getBoolInput("autoComplete")) {
80118
let setAutoComplete = <gi.GitPullRequest>{};
81119
setAutoComplete.autoCompleteSetBy = pullRequest.createdBy;
82-
await gitApi.updatePullRequest(setAutoComplete, repository.id, pullRequest.pullRequestId, project);
120+
await gitApi.updatePullRequest(setAutoComplete, repositoryId, pullRequest.pullRequestId, project);
83121
}
84122
}
85123

86-
let taskManifestPath = path.join(__dirname, "task.json");
87-
tl.debug("Setting resource path to " + taskManifestPath);
88-
tl.setResourcePath(taskManifestPath);
89-
90-
run().then((result) => {
91-
tl.setResult(tl.TaskResult.Succeeded, "Create pull request succeeded");
92-
}).catch((error) => {
93-
tl.setResult(tl.TaskResult.Failed, !!error.message ? error.message : error);
94-
});
95-
96-
function getEnv(name: string): string {
97-
let val = process.env[name];
98-
if (!val) {
99-
console.error(name + " environment variable is not set");
100-
process.exit(1);
101-
}
102-
return val;
103-
}
104-
105124
async function getWebApi(): Promise<azdev.WebApi> {
106125
let serverUrl = getEnv("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI");
107126
console.log(`connecting to VSTS web API"s on server: "${serverUrl}"`);

createpullrequest/task.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
"satisfies": ["GitCreatePullRequest"],
1818
"demands": [],
1919
"groups": [
20+
{
21+
"name": "repository",
22+
"displayName": "Repository",
23+
"isExpanded": true
24+
},
2025
{
2126
"name": "branches",
2227
"displayName": "Branches",
@@ -35,6 +40,46 @@
3540
],
3641
"instanceNameFormat": "Create pull request",
3742
"inputs": [
43+
{
44+
"name": "repositorySelectionMethod",
45+
"type": "radio",
46+
"label": "Repository to use",
47+
"required": true,
48+
"defaultValue": "currentBuild",
49+
"options": {
50+
"currentBuild": "Current build",
51+
"select": "Select"
52+
},
53+
"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.",
54+
"groupName": "repository"
55+
},
56+
{
57+
"name": "projectId",
58+
"type": "picklist",
59+
"label": "Project",
60+
"defaultValue": "",
61+
"properties": {
62+
"EditableOptions": "True",
63+
"DisableManageLink": "True"
64+
},
65+
"required": true,
66+
"helpMarkDown": "Project that contains the git repository you want to create a pull request for.",
67+
"groupName": "repository",
68+
"visibleRule": "repositorySelectionMethod = select"
69+
},
70+
{
71+
"name": "gitRepositoryId",
72+
"type": "picklist",
73+
"label": "Repository",
74+
"defaultValue": "","properties": {
75+
"EditableOptions": "True",
76+
"DisableManageLink": "True"
77+
} ,
78+
"required": true,
79+
"helpMarkDown": "Git repository you want to create a pull request for",
80+
"groupName": "repository",
81+
"visibleRule": "repositorySelectionMethod = select"
82+
},
3883
{
3984
"name": "sourceBranch",
4085
"type": "string",
@@ -105,6 +150,29 @@
105150
"groupName": "completion"
106151
}
107152
],
153+
"dataSourceBindings": [
154+
{
155+
"endpointId": "tfs:teamfoundation",
156+
"target": "projectId",
157+
"endpointUrl": "{{endpoint.url}}/_apis/projects?$skip={{skip}}&$top=1000",
158+
"resultSelector": "jsonpath:$.value[?(@.state=='wellFormed')]",
159+
"resultTemplate": "{ \"Value\" : \"{{{id}}}\", \"DisplayValue\" : \"{{{name}}}\" }",
160+
"callbackContextTemplate": "{\"skip\": \"{{add skip 1000}}\"}",
161+
"callbackRequiredTemplate": "{{isEqualNumber result.count 1000}}",
162+
"initialContextTemplate": "{\"skip\": \"0\"}"
163+
},
164+
{
165+
"endpointId": "tfs:teamfoundation",
166+
"target": "gitRepositoryId",
167+
"endpointUrl": "{{endpoint.url}}/{{project}}/_apis/git/repositories",
168+
"resultSelector": "jsonpath:$.value[*]",
169+
"parameters": {
170+
"project": "$(projectId)"
171+
},
172+
"resultTemplate": "{ \"Value\" : \"{{{id}}}\", \"DisplayValue\" : \"{{{name}}}\" }"
173+
}
174+
175+
],
108176
"execution": {
109177
"Node": {
110178
"target": "createPullRequest.js"

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"major": 0,
3-
"minor": 2
3+
"minor": 3
44
}

vss-extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifestVersion": 1,
33
"id": "git-buildtasks",
44
"name": "git tasks",
5-
"version": "1.0.7",
5+
"version": "1.0.9",
66
"publisher": "sander-aernouts",
77
"public" : false,
88
"repository": {

0 commit comments

Comments
 (0)