Skip to content

Commit 3d9024f

Browse files
committed
added Repository.CreatePullRequest
1 parent 14ba367 commit 3d9024f

File tree

3 files changed

+93
-16
lines changed

3 files changed

+93
-16
lines changed

Git.hub/Git.hub.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Compile Include="Organization.cs" />
4747
<Compile Include="Properties\AssemblyInfo.cs" />
4848
<Compile Include="PullRequest.cs" />
49+
<Compile Include="util\ReplacingJsonSerializer.cs" />
4950
<Compile Include="Repository.cs" />
5051
<Compile Include="User.cs" />
5152
</ItemGroup>

Git.hub/Repository.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using RestSharp;
6+
using Git.hub.util;
67

78
namespace Git.hub
89
{
@@ -124,36 +125,37 @@ public PullRequest GetPullRequest(int id)
124125
pullrequest.Repository = this;
125126
return pullrequest;
126127
}
127-
128-
#if _
129128
/// <summary>
130129
/// Creates a new pull request
131130
/// </summary>
132131
/// <param name="headBranch">branch in the own repository, like mabako:new-awesome-thing</param>
133132
/// <param name="baseBranch">branch it should be merged into in the original repository, like master</param>
134133
/// <param name="title">title of the request</param>
135-
/// <param name="body">body</param>
134+
/// <param name="body">body/message</param>
136135
/// <returns></returns>
137136
public PullRequest CreatePullRequest(string headBranch, string baseBranch, string title, string body)
138137
{
139-
/*
140-
var request = new RestRequest("/repos/{name}/{repo}/pulls", Method.POST);
138+
var request = new RestRequest("/repos/{name}/{repo}/pulls");
141139
request.AddUrlSegment("name", Owner.Login);
142140
request.AddUrlSegment("repo", Name);
143141

144142
request.RequestFormat = DataFormat.Json;
145-
request.AddParameter("title", title);
146-
request.AddParameter("body", body);
147-
request.AddParameter("head", headBranch);
148-
request.AddParameter("base", baseBranch);
149-
150-
var pullrequest = _client.Execute<PullRequest>(request);
151-
Console.WriteLine(pullrequest.Content);
152-
return pullrequest.Data;
153-
*/
154-
return null;
143+
request.JsonSerializer = new ReplacingJsonSerializer("\"x__custom__base\":\"", "\"base\":\"");
144+
request.AddBody(new {
145+
title = title,
146+
body = body,
147+
head = headBranch,
148+
x__custom__base = baseBranch
149+
});
150+
151+
var pullrequest = _client.Post<PullRequest>(request).Data;
152+
if(pullrequest == null)
153+
return null;
154+
155+
pullrequest._client = _client;
156+
pullrequest.Repository = this;
157+
return pullrequest;
155158
}
156-
#endif
157159

158160
public override bool Equals(object obj)
159161
{
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using RestSharp.Serializers;
6+
7+
namespace Git.hub.util
8+
{
9+
class ReplacingJsonSerializer : ISerializer
10+
{
11+
private JsonSerializer serializer = new JsonSerializer();
12+
private string what;
13+
private string with;
14+
15+
public ReplacingJsonSerializer(string what, string with)
16+
{
17+
this.what = what;
18+
this.with = with;
19+
}
20+
21+
public string Serialize(object obj)
22+
{
23+
return serializer.Serialize(obj).Replace(what, with);
24+
}
25+
26+
public string ContentType
27+
{
28+
get
29+
{
30+
return serializer.ContentType;
31+
}
32+
set
33+
{
34+
serializer.ContentType = value;
35+
}
36+
}
37+
38+
public string DateFormat
39+
{
40+
get
41+
{
42+
return serializer.DateFormat;
43+
}
44+
set
45+
{
46+
serializer.DateFormat = value;
47+
}
48+
}
49+
50+
public string Namespace
51+
{
52+
get
53+
{
54+
return serializer.Namespace;
55+
}
56+
set
57+
{
58+
serializer.Namespace = value;
59+
}
60+
}
61+
62+
public string RootElement
63+
{
64+
get
65+
{
66+
return serializer.RootElement;
67+
}
68+
set
69+
{
70+
serializer.RootElement = value;
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)