Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit acd655f

Browse files
authored
Merge pull request #108 from github-for-unity/features/publish
UI and Functionality to Publish
2 parents 29af0b3 + 969af14 commit acd655f

File tree

6 files changed

+361
-33
lines changed

6 files changed

+361
-33
lines changed

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,17 @@ private async Task CreateRepositoryInternal(NewRepository newRepository, Action<
221221
{
222222
try
223223
{
224-
logger.Trace("Creating Repository");
225-
226224
Octokit.Repository repository;
227-
if (organization != null)
225+
if (!string.IsNullOrEmpty(organization))
228226
{
227+
logger.Trace("Creating repository for organization");
228+
229229
repository = await githubClient.Repository.Create(organization, newRepository);
230230
}
231231
else
232232
{
233+
logger.Trace("Creating repository for user");
234+
233235
repository = await githubClient.Repository.Create(newRepository);
234236
}
235237

src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="Logging\UnityLogAdapter.cs" />
8585
<Compile Include="EntryPoint.cs" />
8686
<Compile Include="Misc\Installer.cs" />
87+
<Compile Include="UI\PublishWindow.cs" />
8788
<Compile Include="UI\ProjectWindowInterface.cs" />
8889
<Compile Include="Misc\Styles.cs" />
8990
<Compile Include="Misc\Utility.cs" />
@@ -99,6 +100,7 @@
99100
<Compile Include="UI\ChangesView.cs" />
100101
<Compile Include="UI\HistoryView.cs" />
101102
<Compile Include="UI\IView.cs" />
103+
<Compile Include="UI\PublishView.cs" />
102104
<Compile Include="UI\SettingsView.cs" />
103105
<Compile Include="UI\Subview.cs" />
104106
<Compile Include="UI\Window.cs" />

src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Styles.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Styles
2222
CommitAreaDefaultRatio = .4f,
2323
CommitAreaMaxHeight = 12 * 15f,
2424
CommitAreaPadding = 5f,
25+
PublishViewSpacingHeight = 5f,
2526
MinCommitTreePadding = 20f,
2627
FoldoutWidth = 11f,
2728
FoldoutIndentation = -2f,

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class HistoryView : Subview
3030
private const string ClearSelectionButton = "×";
3131
private const string NoRepoTitle = "No Git repository found for this project";
3232
private const string NoRepoDescription = "Initialize a Git repository to track changes and collaborate with others.";
33+
private const string PublishButton = "Publish";
3334
private const string FetchActionTitle = "Fetch Changes";
3435
private const string FetchButtonText = "Fetch";
3536
private const string FetchFailureDescription = "Could not fetch changes";
@@ -346,40 +347,57 @@ public void OnEmbeddedGUI()
346347

347348
GUILayout.FlexibleSpace();
348349

349-
GUI.enabled = currentRemote != null;
350-
var fetchClicked = GUILayout.Button(FetchButtonText, Styles.HistoryToolbarButtonStyle);
351-
GUI.enabled = true;
352-
if (fetchClicked)
353-
{
354-
Fetch();
355-
}
356350

357-
var pullButtonText = statusBehind > 0 ? String.Format(PullButtonCount, statusBehind) : PullButton;
358-
GUI.enabled = currentRemote != null;
359-
var pullClicked = GUILayout.Button(pullButtonText, Styles.HistoryToolbarButtonStyle);
360-
GUI.enabled = true;
361-
if (pullClicked &&
362-
EditorUtility.DisplayDialog(PullConfirmTitle,
363-
String.Format(PullConfirmDescription, currentRemote),
364-
PullConfirmYes,
365-
PullConfirmCancel)
366-
)
351+
var isPublished = Repository.CurrentRemote.HasValue;
352+
if (isPublished)
367353
{
368-
Pull();
369-
}
354+
GUI.enabled = currentRemote != null;
355+
var fetchClicked = GUILayout.Button(FetchButtonText, Styles.HistoryToolbarButtonStyle);
356+
GUI.enabled = true;
357+
if (fetchClicked)
358+
{
359+
Fetch();
360+
}
361+
362+
// Pull / Push buttons
363+
var pullButtonText = statusBehind > 0 ? String.Format(PullButtonCount, statusBehind) : PullButton;
364+
GUI.enabled = currentRemote != null;
365+
var pullClicked = GUILayout.Button(pullButtonText, Styles.HistoryToolbarButtonStyle);
366+
GUI.enabled = true;
367+
if (pullClicked &&
368+
EditorUtility.DisplayDialog(PullConfirmTitle,
369+
String.Format(PullConfirmDescription, currentRemote),
370+
PullConfirmYes,
371+
PullConfirmCancel)
372+
)
373+
{
374+
Pull();
375+
}
370376

371-
var pushButtonText = statusAhead > 0 ? String.Format(PushButtonCount, statusAhead) : PushButton;
372-
GUI.enabled = currentRemote != null && statusBehind == 0;
373-
var pushClicked = GUILayout.Button(pushButtonText, Styles.HistoryToolbarButtonStyle);
374-
GUI.enabled = true;
375-
if (pushClicked &&
376-
EditorUtility.DisplayDialog(PushConfirmTitle,
377-
String.Format(PushConfirmDescription, currentRemote),
378-
PushConfirmYes,
379-
PushConfirmCancel)
380-
)
377+
var pushButtonText = statusAhead > 0 ? String.Format(PushButtonCount, statusAhead) : PushButton;
378+
GUI.enabled = currentRemote != null && statusBehind == 0;
379+
var pushClicked = GUILayout.Button(pushButtonText, Styles.HistoryToolbarButtonStyle);
380+
GUI.enabled = true;
381+
if (pushClicked &&
382+
EditorUtility.DisplayDialog(PushConfirmTitle,
383+
String.Format(PushConfirmDescription, currentRemote),
384+
PushConfirmYes,
385+
PushConfirmCancel)
386+
)
387+
{
388+
Push();
389+
}
390+
}
391+
else
381392
{
382-
Push();
393+
// Publishing a repo
394+
GUI.enabled = Platform.Keychain.Connections.Any();
395+
var publishedClicked = GUILayout.Button(PublishButton, Styles.HistoryToolbarButtonStyle);
396+
if (publishedClicked)
397+
{
398+
PublishWindow.Open();
399+
}
400+
GUI.enabled = true;
383401
}
384402
}
385403
GUILayout.EndHorizontal();
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
using System;
2+
using System.Linq;
3+
using Octokit;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace GitHub.Unity
8+
{
9+
class PublishView : Subview
10+
{
11+
private const string Title = "Publish this repository to GitHub";
12+
private const string PrivateRepoMessage = "You choose who can see and commit to this repository";
13+
private const string PublicRepoMessage = "Anyone can see this repository. You choose who can commit";
14+
private const string PublishViewCreateButton = "Create";
15+
16+
[SerializeField] private string username;
17+
[SerializeField] private string[] owners = { };
18+
[SerializeField] private int selectedOwner;
19+
[SerializeField] private string repoName = String.Empty;
20+
[SerializeField] private string repoDescription = "";
21+
[SerializeField] private bool togglePrivate;
22+
23+
[NonSerialized] private IApiClient client;
24+
[NonSerialized] private bool isBusy;
25+
[NonSerialized] private string error;
26+
27+
public IApiClient Client
28+
{
29+
get
30+
{
31+
if (client == null)
32+
{
33+
var repository = Environment.Repository;
34+
UriString host;
35+
if (repository != null && !string.IsNullOrEmpty(repository.CloneUrl))
36+
{
37+
host = repository.CloneUrl.ToRepositoryUrl();
38+
}
39+
else
40+
{
41+
host = UriString.ToUriString(HostAddress.GitHubDotComHostAddress.WebUri);
42+
}
43+
44+
client = ApiClient.Create(host, Platform.Keychain);
45+
}
46+
47+
return client;
48+
}
49+
}
50+
51+
public override void InitializeView(IView parent)
52+
{
53+
base.InitializeView(parent);
54+
PopulateView();
55+
}
56+
57+
private void PopulateView()
58+
{
59+
try
60+
{
61+
var keychainConnections = Platform.Keychain.Connections;
62+
if (keychainConnections.Any())
63+
{
64+
Logger.Trace("GetCurrentUser");
65+
66+
Client.GetCurrentUser(user => {
67+
if (user == null)
68+
{
69+
Logger.Warning("Unable to get current user");
70+
return;
71+
}
72+
73+
owners = new[] { user.Login };
74+
username = user.Login;
75+
76+
Logger.Trace("GetOrganizations");
77+
78+
Client.GetOrganizations(organizations =>
79+
{
80+
if (organizations == null)
81+
{
82+
Logger.Warning("Unable to get list of organizations");
83+
return;
84+
}
85+
86+
Logger.Trace("Loaded {0} organizations", organizations.Count);
87+
88+
var organizationLogins = organizations
89+
.OrderBy(organization => organization.Login)
90+
.Select(organization => organization.Login);
91+
92+
owners = owners.Union(organizationLogins).ToArray();
93+
});
94+
});
95+
}
96+
else
97+
{
98+
Logger.Warning("No Keychain connections to use");
99+
}
100+
}
101+
catch (Exception e)
102+
{
103+
Logger.Error(e, "Error PopulateView & GetOrganizations");
104+
throw;
105+
}
106+
}
107+
108+
public override void OnGUI()
109+
{
110+
GUILayout.BeginHorizontal(Styles.AuthHeaderBoxStyle);
111+
{
112+
GUILayout.BeginVertical(GUILayout.Width(16));
113+
{
114+
GUILayout.Space(9);
115+
GUILayout.Label(Styles.BigLogo, GUILayout.Height(20), GUILayout.Width(20));
116+
}
117+
GUILayout.EndVertical();
118+
119+
GUILayout.BeginVertical();
120+
{
121+
GUILayout.Space(11);
122+
GUILayout.Label(Title, EditorStyles.boldLabel);
123+
}
124+
GUILayout.EndVertical();
125+
}
126+
GUILayout.EndHorizontal();
127+
128+
GUILayout.Space(Styles.PublishViewSpacingHeight);
129+
130+
GUILayout.BeginHorizontal();
131+
{
132+
GUILayout.BeginVertical();
133+
{
134+
GUILayout.Label("Owner");
135+
136+
GUI.enabled = !isBusy;
137+
selectedOwner = EditorGUILayout.Popup(0, owners);
138+
GUI.enabled = true;
139+
}
140+
GUILayout.EndVertical();
141+
142+
GUILayout.BeginVertical(GUILayout.Width(8));
143+
{
144+
GUILayout.Space(20);
145+
GUILayout.Label("/");
146+
}
147+
GUILayout.EndVertical();
148+
149+
GUILayout.BeginVertical();
150+
{
151+
GUILayout.Label("Repository Name");
152+
GUI.enabled = !isBusy;
153+
repoName = EditorGUILayout.TextField(repoName);
154+
GUI.enabled = true;
155+
}
156+
GUILayout.EndVertical();
157+
}
158+
GUILayout.EndHorizontal();
159+
160+
GUILayout.Label("Description");
161+
GUI.enabled = !isBusy;
162+
repoDescription = EditorGUILayout.TextField(repoDescription);
163+
GUI.enabled = true;
164+
GUILayout.Space(Styles.PublishViewSpacingHeight);
165+
166+
GUILayout.BeginVertical();
167+
{
168+
GUILayout.BeginHorizontal();
169+
{
170+
GUI.enabled = !isBusy;
171+
togglePrivate = GUILayout.Toggle(togglePrivate, "Create as a private repository");
172+
GUI.enabled = true;
173+
}
174+
GUILayout.EndHorizontal();
175+
176+
GUILayout.BeginHorizontal();
177+
{
178+
GUILayout.Space(Styles.PublishViewSpacingHeight);
179+
var repoPrivacyExplanation = togglePrivate ? PrivateRepoMessage : PublicRepoMessage;
180+
GUILayout.Label(repoPrivacyExplanation, Styles.LongMessageStyle);
181+
}
182+
GUILayout.EndHorizontal();
183+
}
184+
GUILayout.EndVertical();
185+
186+
187+
GUILayout.Space(Styles.PublishViewSpacingHeight);
188+
189+
if (error != null)
190+
GUILayout.Label(error, Styles.ErrorLabel);
191+
192+
GUILayout.FlexibleSpace();
193+
194+
GUILayout.BeginHorizontal();
195+
{
196+
GUILayout.FlexibleSpace();
197+
GUI.enabled = !string.IsNullOrEmpty(repoName) && !isBusy;
198+
if (GUILayout.Button(PublishViewCreateButton))
199+
{
200+
isBusy = true;
201+
202+
var organization = owners[selectedOwner] == username ? null : owners[selectedOwner];
203+
204+
Client.CreateRepository(new NewRepository(repoName)
205+
{
206+
Private = togglePrivate,
207+
}, (repository, ex) =>
208+
{
209+
Logger.Trace("Create Repository Callback");
210+
211+
if (ex != null)
212+
{
213+
error = ex.Message;
214+
isBusy = false;
215+
return;
216+
}
217+
218+
if (repository == null)
219+
{
220+
Logger.Warning("Returned Repository is null");
221+
isBusy = false;
222+
return;
223+
}
224+
225+
GitClient.RemoteAdd("origin", repository.CloneUrl)
226+
.Then(GitClient.Push("origin", Repository.CurrentBranch.Value.Name))
227+
.ThenInUI(Parent.Finish)
228+
.Start();
229+
}, organization);
230+
}
231+
GUI.enabled = true;
232+
}
233+
GUILayout.EndHorizontal();
234+
GUILayout.Space(10);
235+
}
236+
}
237+
}

0 commit comments

Comments
 (0)