Skip to content

Commit 51014b8

Browse files
committed
Select the default branch when creating Github pull request
1 parent 7df924a commit 51014b8

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

Git.hub/Repository.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using RestSharp;
63
using Git.hub.util;
4+
using RestSharp;
75

86
namespace Git.hub
97
{
@@ -12,6 +10,7 @@ public class Repository
1210
public string Name { get; internal set; }
1311
public string Description { get; internal set; }
1412
public string Homepage { get; internal set; }
13+
public string DefaultBranch { get; internal set; }
1514
public User Owner { get; internal set; }
1615
public bool Fork { get; internal set; }
1716
public int Forks { get; internal set; }
@@ -87,10 +86,30 @@ public IList<Branch> GetBranches()
8786
return _client.GetList<Branch>(request);
8887
}
8988

89+
/// <summary>
90+
/// Retrieves the name of the default branch
91+
/// </summary>
92+
/// <returns>The name of the default branch</returns>
93+
public string GetDefaultBranch()
94+
{
95+
RestRequest request = new RestRequest("/repos/{user}/{repo}");
96+
request.AddUrlSegment("user", Owner.Login);
97+
request.AddUrlSegment("repo", Name);
98+
99+
var repo = _client.Get<Repository>(request).Data;
100+
101+
if (repo == null)
102+
{
103+
return null;
104+
}
105+
106+
return repo.DefaultBranch;
107+
}
108+
90109
/// <summary>
91110
/// Lists all open pull requests
92111
/// </summary>
93-
/// <returns>llist of all open pull requests</returns>
112+
/// <returns>list of all open pull requests</returns>
94113
public IList<PullRequest> GetPullRequests()
95114
{
96115
var request = new RestRequest("/repos/{user}/{repo}/pulls");
@@ -141,15 +160,16 @@ public PullRequest CreatePullRequest(string headBranch, string baseBranch, strin
141160

142161
request.RequestFormat = DataFormat.Json;
143162
request.JsonSerializer = new ReplacingJsonSerializer("\"x__custom__base\":\"", "\"base\":\"");
144-
request.AddBody(new {
163+
request.AddBody(new
164+
{
145165
title = title,
146166
body = body,
147167
head = headBranch,
148168
x__custom__base = baseBranch
149169
});
150170

151171
var pullrequest = _client.Post<PullRequest>(request).Data;
152-
if(pullrequest == null)
172+
if (pullrequest == null)
153173
return null;
154174

155175
pullrequest._client = _client;
@@ -170,10 +190,9 @@ public GitHubReference GetRef(string refName)
170190

171191
ghRef._client = _client;
172192
ghRef.Repository = this;
173-
return ghRef;
193+
return ghRef;
174194
}
175195

176-
177196
/// <summary>
178197
/// Creates a new issue
179198
/// </summary>

0 commit comments

Comments
 (0)