Skip to content

Commit 88063af

Browse files
author
Danny McCormick
committed
Added gitApi sample
1 parent 8699c48 commit 88063af

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

samples/git.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import * as common from './common';
2+
import * as nodeApi from 'azure-devops-node-api';
3+
4+
import * as GitApi from 'azure-devops-node-api/GitApi';
5+
import * as GitInterfaces from 'azure-devops-node-api/interfaces/GitInterfaces';
6+
7+
export async function run() {
8+
let webApi: nodeApi.WebApi = await common.getWebApi();
9+
let gitApiObject: GitApi.IGitApi = await webApi.getGitApi();
10+
11+
common.banner('Git Samples');
12+
let project: string = common.getProject();
13+
console.log('Project:', project);
14+
15+
common.heading("Get Repositories");
16+
const repos: GitInterfaces.GitRepository[] = await gitApiObject.getRepositories(project);
17+
console.log("There are", repos.length, "repositories in this project");
18+
19+
common.heading("Create a repository");
20+
const createOptions: GitInterfaces.GitRepositoryCreateOptions = <GitInterfaces.GitRepositoryCreateOptions>{name: 'new repo'};
21+
let newRepo: GitInterfaces.GitRepository = await gitApiObject.createRepository(createOptions, project);
22+
console.log("New repo:", newRepo.name);
23+
24+
common.heading("Delete repository");
25+
await gitApiObject.deleteRepository(newRepo.id);
26+
console.log("Repo deleted");
27+
28+
common.heading("Get commits for a repository");
29+
if (repos.length > 0) {
30+
const firstRepo = repos[0]
31+
console.log("The first repository in this project is named", firstRepo.name);
32+
const commitCriteria: GitInterfaces.GitQueryCommitsCriteria = <GitInterfaces.GitQueryCommitsCriteria> {$skip: 0, $top: 10};
33+
const commits: GitInterfaces.GitCommitRef[] = await gitApiObject.getCommits(firstRepo.id, commitCriteria, project);
34+
const commitNames: string[] = commits.map((commit) => {return commit.comment});
35+
console.log("Top 10 commits in this repo:", commitNames);
36+
}
37+
else {
38+
console.log("Must have an active repository for this part of the sample");
39+
}
40+
41+
common.heading("Get pull requests for a repository");
42+
if (repos.length > 0) {
43+
const firstRepo = repos[0]
44+
const pullRequestCriteria: GitInterfaces.GitPullRequestSearchCriteria = <GitInterfaces.GitPullRequestSearchCriteria>{};
45+
const pullRequests = await gitApiObject.getPullRequests(firstRepo.id, pullRequestCriteria);
46+
const pullRequestTitles = pullRequests.map((request) => {return request.title});
47+
console.log("Pull requests on this repo:", pullRequestTitles);
48+
49+
common.heading("Create comment on a pull request");
50+
let threads: GitInterfaces.GitPullRequestCommentThread[] = [];
51+
if (pullRequests.length > 0) {
52+
threads = await gitApiObject.getThreads(firstRepo.id, pullRequests[0].pullRequestId);
53+
}
54+
if (pullRequests.length > 0 && threads.length > 0) {
55+
let comment: GitInterfaces.Comment = <GitInterfaces.Comment>{content: 'Hello comment'};
56+
comment = await gitApiObject.createComment(comment, firstRepo.id, pullRequests[0].pullRequestId, threads[0].id);
57+
console.log("Comment created:", comment.content);
58+
common.heading("Delete a comment on a pull request");
59+
await gitApiObject.deleteComment(firstRepo.id, pullRequests[0].pullRequestId, threads[0].id, comment.id);
60+
console.log("Comment deleted");
61+
}
62+
else {
63+
console.log("Must have an active pull request in repo with an active comment thread", firstRepo.name, "for this part of the sample");
64+
}
65+
}
66+
else {
67+
console.log("Must have an active repository for this part of the sample");
68+
}
69+
}

samples/samples.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"buildArtifact",
44
"creation",
55
"task",
6-
"filecontainer"
6+
"filecontainer",
7+
"git"
78
]

samples/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"buildArtifact.ts",
1111
"creation.ts",
1212
"task.ts",
13-
"filecontainer.ts"
13+
"filecontainer.ts",
14+
"git.ts"
1415
]
1516
}

0 commit comments

Comments
 (0)