Skip to content

Commit d6eea78

Browse files
Fix race condition with slow loading of new repo labels
1 parent 24ba75e commit d6eea78

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

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>2.1.1</Version>
3+
<Version>2.1.2</Version>
44
<OutputType>Exe</OutputType>
55
<PackAsTool>true</PackAsTool>
66
<ToolCommandName>sync-labels</ToolCommandName>

src/Synchronizer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,16 @@ public async Task<IReadOnlyList<Label>> GetAccountLabels(Account account)
7575
? await _gitHub.CreateTempRepoForOrganization(account, repoName)
7676
: await _gitHub.CreateTempRepoForUser(account, repoName);
7777

78-
var labels = await _gitHub.GetLabels(repo);
78+
var originalLabels = await _gitHub.GetLabels(repo);
79+
var latestLabels = await _gitHub.GetLabels(repo);
80+
while (originalLabels.Count != latestLabels.Count)
81+
{
82+
originalLabels = latestLabels;
83+
latestLabels = await _gitHub.GetLabels(repo);
84+
}
7985
await _gitHub.DeleteTempRepo(account, repoName);
8086

81-
return labels;
87+
return latestLabels;
8288
}
8389

8490
private static string LabelNames(IEnumerable<Label> labels)

test/SynchronizerTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class SynchronizerTests
3030
};
3131

3232
private readonly Action<string> _noOp = _ => { };
33+
private static readonly Stubs.Label EmptyLabel = new Stubs.Label(string.Empty, string.Empty, string.Empty);
3334

3435
[Fact]
3536
public async Task ValidAccessReturnsSuccess()
@@ -300,6 +301,28 @@ public async Task GettingAccountLabelsDeletesTempRepoForUser()
300301
Assert.StartsWith("temp-label-sync-20", tempRepoName);
301302
}
302303

304+
[Fact]
305+
public async Task GettingAccountLabelsWaitsUntilAllLabelsHaveBeenCreated()
306+
{
307+
//arrange
308+
var gitHub = Substitute.For<IGitHub>();
309+
var sync = new Synchronizer(gitHub, _noOp, _noOp);
310+
311+
var account = new Stubs.Organization("ecoAPM");
312+
gitHub.GetLabels(Arg.Any<Repository>()).Returns(
313+
new [] { EmptyLabel },
314+
new [] { EmptyLabel, EmptyLabel },
315+
new [] { EmptyLabel, EmptyLabel, EmptyLabel },
316+
new [] { EmptyLabel, EmptyLabel, EmptyLabel }
317+
);
318+
319+
//act
320+
var labels = await sync.GetAccountLabels(account);
321+
322+
//assert
323+
Assert.Equal(3, labels.Count);
324+
}
325+
303326
[Fact]
304327
public async Task PerformsCorrectActions()
305328
{

0 commit comments

Comments
 (0)