Skip to content

Commit 37f6584

Browse files
committed
(split from main repo) Support older Gerrit API
Close #7200
1 parent 05cc776 commit 37f6584

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

FormGerritBase.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using System;
22
using GitUI;
33
using GitUIPluginInterfaces;
4+
using RestSharp;
45

56
namespace Gerrit
67
{
78
public class FormGerritBase : GitExtensionsForm
89
{
910
protected GerritSettings Settings { get; private set; }
11+
protected Version Version { get; private set; }
12+
protected readonly RestClient client = new RestClient();
1013
protected readonly IGitUICommands UICommands;
1114
protected IGitModule Module => UICommands.GitModule;
1215

@@ -36,7 +39,34 @@ protected override void OnLoad(EventArgs e)
3639
return;
3740
}
3841

42+
SetRestClientUrl();
43+
Version = GetGerritVersion();
44+
3945
base.OnLoad(e);
4046
}
47+
48+
private void SetRestClientUrl()
49+
{
50+
string host = Settings.Host;
51+
52+
if (!host.StartsWith("http://") || !host.StartsWith("https://"))
53+
{
54+
host = "https://" + host;
55+
}
56+
57+
if (!host.EndsWith("/"))
58+
{
59+
host += "/";
60+
}
61+
62+
client.BaseUrl = new Uri(host);
63+
}
64+
65+
private Version GetGerritVersion()
66+
{
67+
RestRequest request = new RestRequest("/config/server/version");
68+
IRestResponse response = client.Execute(request);
69+
return Version.Parse(response.Content.Replace(")]}'", "").Replace("\n", "").Replace("\"", "").Split('-')[0]);
70+
}
4171
}
4272
}

FormGerritPublish.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@ public FormGerritPublish(IGitUICommands uiCommand)
3737
PublishType.Items.AddRange(new object[]
3838
{
3939
new KeyValuePair<string, string>(_publishTypeReview.Text, ""),
40-
new KeyValuePair<string, string>(_publishTypeWip.Text, "wip"),
41-
new KeyValuePair<string, string>(_publishTypePrivate.Text, "private"),
40+
new KeyValuePair<string, string>(_publishTypeWip.Text, "wip")
4241
});
4342
PublishType.SelectedIndex = 0;
4443
}
4544

45+
protected override void OnLoad(EventArgs e)
46+
{
47+
base.OnLoad(e);
48+
if (Version >= Version.Parse("2.15"))
49+
{
50+
PublishType.Items.Add(new KeyValuePair<string, string>(_publishTypePrivate.Text, "private"));
51+
}
52+
}
53+
4654
private void PublishClick(object sender, EventArgs e)
4755
{
4856
if (PublishChange(this))
@@ -80,7 +88,6 @@ private bool PublishChange(IWin32Window owner)
8088
GerritUtil.StartAgent(owner, Module, _NO_TRANSLATE_Remotes.Text);
8189

8290
List<string> additionalOptions = new List<string>();
83-
additionalOptions.Add(((KeyValuePair<string, string>)PublishType.SelectedItem).Value);
8491

8592
string reviewers = _NO_TRANSLATE_Reviewers.Text.Trim();
8693
if (!string.IsNullOrEmpty(reviewers))
@@ -112,7 +119,18 @@ private bool PublishChange(IWin32Window owner)
112119

113120
additionalOptions = additionalOptions.Where(r => !string.IsNullOrEmpty(r)).ToList();
114121

115-
string targetBranch = "refs/for/" + branch;
122+
string publishType = ((KeyValuePair<string, string>)PublishType.SelectedItem).Value;
123+
string targetRef = "for";
124+
if (Version >= Version.Parse("2.15"))
125+
{
126+
additionalOptions.Add(publishType);
127+
}
128+
else if (publishType == "wip")
129+
{
130+
targetRef = "drafts";
131+
}
132+
133+
string targetBranch = $"refs/{targetRef}/{branch}";
116134
if (additionalOptions.Count > 0)
117135
{
118136
targetBranch += "%" + string.Join(",", additionalOptions);

0 commit comments

Comments
 (0)