Skip to content

Commit 7d98307

Browse files
Use timestamp instead of random numbers for temp repo name
1 parent 388ac7f commit 7d98307

5 files changed

Lines changed: 33 additions & 32 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Good question!
3333

3434
The GitHub API does not provide direct access to default labels, and so they are obtained by creating (and immediately deleting) a temporary private repository.
3535

36-
You can see the code that performs these actions [here](https://github.com/ecoAPM/GitHubLabelSync/blob/main/src/Synchronizer.cs#L65-L70).
36+
You can see the code that performs these actions [here](https://github.com/ecoAPM/GitHubLabelSync/blob/1.0.0/src/Synchronizer.cs#L75-L83).
3737

3838
## Usage
3939

src/Factory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static App App(string apiKey, Action<string> setStatus, Action<string> lo
1313
var client = new GitHubClient(value) { Credentials = new Credentials(apiKey) };
1414

1515
var gitHub = new GitHub(client, setStatus, log);
16-
var sync = new Synchronizer(gitHub, new Random(), setStatus, log);
16+
var sync = new Synchronizer(gitHub, setStatus, log);
1717
return new App(sync, setStatus, log);
1818
}
1919
}

src/GitHubLabelSync.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<Version>0.2.0</Version>
3+
<Version>1.0.0</Version>
44
<OutputType>Exe</OutputType>
55
<PackAsTool>true</PackAsTool>
66
<ToolCommandName>sync-labels</ToolCommandName>

src/Synchronizer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ namespace GitHubLabelSync
1010
public class Synchronizer : ISynchronizer
1111
{
1212
private readonly IGitHub _gitHub;
13-
private readonly Random _random;
1413
private readonly Action<string> _setStatus;
1514
private readonly Action<string> _log;
1615

17-
public Synchronizer(IGitHub gitHub, Random random, Action<string> setStatus, Action<string> log)
16+
public Synchronizer(IGitHub gitHub, Action<string> setStatus, Action<string> log)
1817
{
1918
_gitHub = gitHub;
20-
_random = random;
2119
_setStatus = setStatus;
2220
_log = log;
2321
}
@@ -74,10 +72,13 @@ public async Task<IReadOnlyList<Label>> GetAccountLabels(Account account)
7472
{
7573
_setStatus($"Finding labels for {account.Login}...");
7674

77-
var repoName = $"temp-label-sync-{_random.Next()}";
75+
var now = DateTime.Now.ToString("yyyyMMdd-HHmmssff");
76+
var repoName = $"temp-label-sync-{now}";
77+
7878
var repo = account.Type == AccountType.Organization
7979
? await _gitHub.CreateTempRepoForOrganization(account, repoName)
8080
: await _gitHub.CreateTempRepoForUser(account, repoName);
81+
8182
var labels = await _gitHub.GetLabels(repo);
8283
await _gitHub.DeleteTempRepo(account, repoName);
8384

test/SynchronizerTests.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public async Task ValidAccessReturnsSuccess()
4040
var gitHub = Substitute.For<IGitHub>();
4141
gitHub.GetAccess().Returns(new[] { "repo", "delete_repo" });
4242

43-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
43+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
4444

4545
//act
4646
var validation = await sync.ValidateAccess();
@@ -56,7 +56,7 @@ public async Task ValidatingNoPrivateAccessFails()
5656
var gitHub = Substitute.For<IGitHub>();
5757
gitHub.GetAccess().Returns(new[] { "public_repo", "delete_repo" });
5858

59-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
59+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
6060

6161
//act
6262
var validation = await sync.ValidateAccess();
@@ -72,7 +72,7 @@ public async Task ValidatingNoDeleteAccessFails()
7272
var gitHub = Substitute.For<IGitHub>();
7373
gitHub.GetAccess().Returns(new[] { "repo" });
7474

75-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
75+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
7676

7777
//act
7878
var validation = await sync.ValidateAccess();
@@ -89,7 +89,7 @@ public async Task ValidOrgAdminReturnsSuccess()
8989
gitHub.GetCurrentUser().Returns(new Stubs.User("SteveDesmond-ca"));
9090
gitHub.GetRole("SteveDesmond-ca", "ecoAPM").Returns(MembershipRole.Admin);
9191

92-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
92+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
9393

9494
//act
9595
var validation = await sync.ValidateUser(new Stubs.Organization("ecoAPM"));
@@ -106,7 +106,7 @@ public async Task NonOrgAdminFails()
106106
gitHub.GetCurrentUser().Returns(new Stubs.User("SteveDesmond-ca"));
107107
gitHub.GetRole("SteveDesmond-ca", "ecoAPM").Returns(MembershipRole.Member);
108108

109-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
109+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
110110

111111
//act
112112
var validation = await sync.ValidateUser(new Stubs.Organization("ecoAPM"));
@@ -123,7 +123,7 @@ public async Task NonOrgMemberFails()
123123
gitHub.GetCurrentUser().Returns(new Stubs.User("SteveDesmond-ca"));
124124
gitHub.GetRole("SteveDesmond-ca", "ecoAPM").Returns((MembershipRole?)null);
125125

126-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
126+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
127127

128128
//act
129129
var validation = await sync.ValidateUser(new Stubs.Organization("ecoAPM"));
@@ -140,7 +140,7 @@ public async Task SameUserAsTargetReturnsSuccess()
140140
gitHub.GetCurrentUser().Returns(new Stubs.User("SteveDesmond-ca"));
141141
gitHub.GetRole("SteveDesmond-ca", "ecoAPM").Returns(MembershipRole.Admin);
142142

143-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
143+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
144144

145145
//act
146146
var validation = await sync.ValidateUser(new Stubs.Organization("ecoAPM"));
@@ -157,7 +157,7 @@ public async Task DifferentTargetUserFails()
157157
gitHub.GetCurrentUser().Returns(new Stubs.User("SteveDesmond-ca"));
158158
gitHub.GetRole("SteveDesmond-ca", "ecoAPM").Returns(MembershipRole.Member);
159159

160-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
160+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
161161

162162
//act
163163
var validation = await sync.ValidateUser(new Stubs.User("Unit-Test"));
@@ -173,7 +173,7 @@ public async Task GetsOrganizationByDefault()
173173
var gitHub = Substitute.For<IGitHub>();
174174
gitHub.GetOrganization("ecoAPM").Returns(new Organization());
175175
gitHub.GetUser("ecoAPM").Throws<Exception>();
176-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
176+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
177177

178178
//act
179179
var account = await sync.GetAccount("ecoAPM");
@@ -189,7 +189,7 @@ public async Task GetsUserIfOrganizationDoesNotExist()
189189
var gitHub = Substitute.For<IGitHub>();
190190
gitHub.GetOrganization("SteveDesmond-ca").Throws<Exception>();
191191
gitHub.GetUser("SteveDesmond-ca").Returns(new User());
192-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
192+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
193193

194194
//act
195195
var account = await sync.GetAccount("SteveDesmond-ca");
@@ -203,7 +203,7 @@ public async Task GetsRepositoriesForOrganizationBasedOnAccountType()
203203
{
204204
//arrange
205205
var gitHub = Substitute.For<IGitHub>();
206-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
206+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
207207

208208
var account = new Stubs.Organization("ecoAPM");
209209

@@ -219,7 +219,7 @@ public async Task GetsRepositoriesForUserBasedOnAccountType()
219219
{
220220
//arrange
221221
var gitHub = Substitute.For<IGitHub>();
222-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
222+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
223223

224224
var account = new Stubs.User("SteveDesmond-ca");
225225

@@ -236,7 +236,7 @@ public async Task GettingAccountLabelsCreatesTempRepoForOrganization()
236236
//arrange
237237
var tempRepoName = "";
238238
var gitHub = Substitute.For<IGitHub>();
239-
var sync = new Synchronizer(gitHub, new Random(), _noOp, _noOp);
239+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
240240

241241
var account = new Stubs.Organization("ecoAPM");
242242
await gitHub.CreateTempRepoForOrganization(account, Arg.Do<string>(name => tempRepoName = name));
@@ -245,7 +245,7 @@ public async Task GettingAccountLabelsCreatesTempRepoForOrganization()
245245
await sync.GetAccountLabels(account);
246246

247247
//assert
248-
Assert.StartsWith("temp-label-sync-", tempRepoName);
248+
Assert.StartsWith("temp-label-sync-20", tempRepoName);
249249
}
250250

251251
[Fact]
@@ -254,7 +254,7 @@ public async Task GettingAccountLabelsCreatesTempRepoForUser()
254254
//arrange
255255
var tempRepoName = "";
256256
var gitHub = Substitute.For<IGitHub>();
257-
var sync = new Synchronizer(gitHub, new Random(), _noOp, _noOp);
257+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
258258

259259
var account = new Stubs.User("SteveDesmond-ca");
260260
await gitHub.CreateTempRepoForUser(account, Arg.Do<string>(name => tempRepoName = name));
@@ -263,7 +263,7 @@ public async Task GettingAccountLabelsCreatesTempRepoForUser()
263263
await sync.GetAccountLabels(account);
264264

265265
//assert
266-
Assert.StartsWith("temp-label-sync-", tempRepoName);
266+
Assert.StartsWith("temp-label-sync-20", tempRepoName);
267267
}
268268

269269
[Fact]
@@ -272,7 +272,7 @@ public async Task GettingAccountLabelsDeletesTempRepoForOrganization()
272272
//arrange
273273
var tempRepoName = "";
274274
var gitHub = Substitute.For<IGitHub>();
275-
var sync = new Synchronizer(gitHub, new Random(), _noOp, _noOp);
275+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
276276

277277
var account = new Stubs.Organization("ecoAPM");
278278
await gitHub.DeleteTempRepo(account, Arg.Do<string>(name => tempRepoName = name));
@@ -281,7 +281,7 @@ public async Task GettingAccountLabelsDeletesTempRepoForOrganization()
281281
await sync.GetAccountLabels(account);
282282

283283
//assert
284-
Assert.StartsWith("temp-label-sync-", tempRepoName);
284+
Assert.StartsWith("temp-label-sync-20", tempRepoName);
285285
}
286286

287287
[Fact]
@@ -290,7 +290,7 @@ public async Task GettingAccountLabelsDeletesTempRepoForUser()
290290
//arrange
291291
var tempRepoName = "";
292292
var gitHub = Substitute.For<IGitHub>();
293-
var sync = new Synchronizer(gitHub, new Random(), _noOp, _noOp);
293+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
294294

295295
var account = new Stubs.User("SteveDesmond-ca");
296296
await gitHub.DeleteTempRepo(account, Arg.Do<string>(name => tempRepoName = name));
@@ -299,7 +299,7 @@ public async Task GettingAccountLabelsDeletesTempRepoForUser()
299299
await sync.GetAccountLabels(account);
300300

301301
//assert
302-
Assert.StartsWith("temp-label-sync-", tempRepoName);
302+
Assert.StartsWith("temp-label-sync-20", tempRepoName);
303303
}
304304

305305
[Fact]
@@ -308,7 +308,7 @@ public async Task PerformsCorrectActions()
308308
//arrange
309309
var gitHub = Substitute.For<IGitHub>();
310310
gitHub.GetLabels(Arg.Any<Repository>()).Returns(_repoLabels);
311-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
311+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
312312

313313
//act
314314
await sync.SyncRepo(new Repository(), new Settings(), _accountLabels);
@@ -325,7 +325,7 @@ public async Task NoAddDoesNotAdd()
325325
//arrange
326326
var gitHub = Substitute.For<IGitHub>();
327327
gitHub.GetLabels(Arg.Any<Repository>()).Returns(_repoLabels);
328-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
328+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
329329

330330
var settings = new Settings { NoAdd = true };
331331

@@ -344,7 +344,7 @@ public async Task NoEditDoesNotEdit()
344344
//arrange
345345
var gitHub = Substitute.For<IGitHub>();
346346
gitHub.GetLabels(Arg.Any<Repository>()).Returns(_repoLabels);
347-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
347+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
348348

349349
var settings = new Settings { NoEdit = true };
350350

@@ -363,7 +363,7 @@ public async Task NoDeleteDoesNotDelete()
363363
//arrange
364364
var gitHub = Substitute.For<IGitHub>();
365365
gitHub.GetLabels(Arg.Any<Repository>()).Returns(_repoLabels);
366-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
366+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
367367

368368
var settings = new Settings { NoDelete = true };
369369

@@ -382,7 +382,7 @@ public async Task DryRunPerformsNoActions()
382382
//arrange
383383
var gitHub = Substitute.For<IGitHub>();
384384
gitHub.GetLabels(Arg.Any<Repository>()).Returns(_repoLabels);
385-
var sync = new Synchronizer(gitHub, null, _noOp, _noOp);
385+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
386386

387387
var settings = new Settings { DryRun = true };
388388

0 commit comments

Comments
 (0)