This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
PullRequestsSample.cs
207 lines (177 loc) · 8.22 KB
/
PullRequestsSample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.Azure.DevOps.ClientSamples.Git
{
[ClientSample(GitWebApiConstants.AreaName, "pullRequests")]
public class PullRequestsSample : ClientSample
{
[ClientSampleMethod]
public IEnumerable<GitPullRequest> ListPullRequestsIntoDefault()
{
VssConnection connection = this.Context.Connection;
GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context);
GitRepository repo = GitSampleHelpers.FindAnyRepository(this.Context, project.Id);
string branchName = repo.DefaultBranch;
List<GitPullRequest> prs = gitClient.GetPullRequestsAsync(
repo.Id,
new GitPullRequestSearchCriteria()
{
TargetRefName = branchName,
}).Result;
Console.WriteLine("project {0}, repo {1}", project.Name, repo.Name);
foreach (GitPullRequest pr in prs)
{
Console.WriteLine("{0} #{1} {2} -> {3}",
pr.Title.Substring(0, Math.Min(40, pr.Title.Length)),
pr.PullRequestId,
pr.SourceRefName,
pr.TargetRefName);
}
return prs;
}
[ClientSampleMethod]
public IEnumerable<GitPullRequest> ListPullRequestsForProject()
{
VssConnection connection = this.Context.Connection;
GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context);
List<GitPullRequest> prs = gitClient.GetPullRequestsByProjectAsync(project.Id, null).Result;
Console.WriteLine("project {0}", project.Name);
foreach (GitPullRequest pr in prs)
{
Console.WriteLine("{0} #{1} {2} -> {3}",
pr.Title.Substring(0, Math.Min(40, pr.Title.Length)),
pr.PullRequestId,
pr.SourceRefName,
pr.TargetRefName);
}
return prs;
}
[ClientSampleMethod]
public GitPullRequest CreatePullRequest()
{
return CreatePullRequestInner(cleanUp: true);
}
public GitPullRequest CreatePullRequestInner(bool cleanUp)
{
VssConnection connection = this.Context.Connection;
GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context);
GitRepository repo = GitSampleHelpers.FindAnyRepository(this.Context, project.Id);
// we need a new branch with changes in order to create a PR
// first, find the default branch
string defaultBranchName = GitSampleHelpers.WithoutRefsPrefix(repo.DefaultBranch);
GitRef defaultBranch = gitClient.GetRefsAsync(repo.Id, filter: defaultBranchName).Result.First();
// next, craft the branch and commit that we'll push
GitRefUpdate newBranch = new GitRefUpdate()
{
Name = $"refs/heads/vsts-api-sample/{GitSampleHelpers.ChooseRefsafeName()}",
OldObjectId = defaultBranch.ObjectId,
};
string newFileName = $"{GitSampleHelpers.ChooseItemsafeName()}.md";
GitCommitRef newCommit = new GitCommitRef()
{
Comment = "Add a sample file",
Changes = new GitChange[]
{
new GitChange()
{
ChangeType = VersionControlChangeType.Add,
Item = new GitItem() { Path = $"/vsts-api-sample/{newFileName}" },
NewContent = new ItemContent()
{
Content = "# Thank you for using VSTS!",
ContentType = ItemContentType.RawText,
},
}
},
};
// create the push with the new branch and commit
GitPush push = gitClient.CreatePushAsync(new GitPush()
{
RefUpdates = new GitRefUpdate[] { newBranch },
Commits = new GitCommitRef[] { newCommit },
}, repo.Id).Result;
// finally, create a PR
var pr = gitClient.CreatePullRequestAsync(new GitPullRequest()
{
SourceRefName = newBranch.Name,
TargetRefName = repo.DefaultBranch,
Title = $"Add {newFileName} (from VSTS REST samples)",
Description = "Adding this file from the pull request samples",
},
repo.Id).Result;
Console.WriteLine("project {0}, repo {1}", project.Name, repo.Name);
Console.WriteLine("{0} (#{1}) {2} -> {3}",
pr.Title.Substring(0, Math.Min(40, pr.Title.Length)),
pr.PullRequestId,
pr.SourceRefName,
pr.TargetRefName);
if (cleanUp)
{
// clean up after ourselves (and in case logging is on, don't log these calls)
ClientSampleHttpLogger.SetSuppressOutput(this.Context, true);
// abandon the PR
GitPullRequest updatedPr = new GitPullRequest()
{
Status = PullRequestStatus.Abandoned,
};
pr = gitClient.UpdatePullRequestAsync(updatedPr, repo.Id, pr.PullRequestId).Result;
// delete the branch
GitRefUpdateResult refDeleteResult = gitClient.UpdateRefsAsync(
new GitRefUpdate[]
{
new GitRefUpdate()
{
OldObjectId = push.RefUpdates.First().NewObjectId,
NewObjectId = new string('0', 40),
Name = push.RefUpdates.First().Name,
}
},
repositoryId: repo.Id).Result.First();
}
return pr;
}
[ClientSampleMethod]
public GitPullRequest AbandonPullRequest()
{
VssConnection connection = this.Context.Connection;
GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context);
GitRepository repo = GitSampleHelpers.FindAnyRepository(this.Context, project.Id);
// first we need to create a pull request
GitPullRequest pr = CreatePullRequestInner(cleanUp: false);
// now abandon the PR
GitPullRequest updatedPr = new GitPullRequest()
{
Status = PullRequestStatus.Abandoned,
};
GitPullRequest abandonedPr = gitClient.UpdatePullRequestAsync(updatedPr, repo.Id, pr.PullRequestId).Result;
Console.WriteLine("{0} (#{1}) {2}",
abandonedPr.Title.Substring(0, Math.Min(40, pr.Title.Length)),
abandonedPr.PullRequestId,
abandonedPr.Status);
// delete the branch that was associated with the PR (and do not log)
ClientSampleHttpLogger.SetSuppressOutput(this.Context, true);
GitRefUpdateResult refDeleteResult = gitClient.UpdateRefsAsync(
new GitRefUpdate[]
{
new GitRefUpdate()
{
OldObjectId = gitClient.GetRefsAsync(repo.Id, filter: GitSampleHelpers.WithoutRefsPrefix(pr.SourceRefName)).Result.First().ObjectId,
NewObjectId = new string('0', 40),
Name = pr.SourceRefName,
}
},
repositoryId: repo.Id).Result.First();
return abandonedPr;
}
}
}