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

Commit

Permalink
feat: add repo selection to task inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderaernouts committed Jul 26, 2019
1 parent d3e4148 commit 42e3bfa
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 47 deletions.
109 changes: 64 additions & 45 deletions createpullrequest/createPullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <gi.GitPullRequest>{};

let sourceBranch = tl.getInput("sourceBranch", true);
Expand All @@ -65,43 +103,24 @@ 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")) {

let approve: gi.IdentityRefWithVote = <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 = <gi.GitPullRequest>{};
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<azdev.WebApi> {
let serverUrl = getEnv("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI");
console.log(`connecting to VSTS web API"s on server: "${serverUrl}"`);
Expand Down
68 changes: 68 additions & 0 deletions createpullrequest/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"satisfies": ["GitCreatePullRequest"],
"demands": [],
"groups": [
{
"name": "repository",
"displayName": "Repository",
"isExpanded": true
},
{
"name": "branches",
"displayName": "Branches",
Expand All @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"major": 0,
"minor": 2
"minor": 3
}
2 changes: 1 addition & 1 deletion vss-extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 42e3bfa

Please sign in to comment.