Skip to content

Commit

Permalink
Fix race condition with slow loading of new repo labels
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveDesmond-ca committed Jun 27, 2023
1 parent 24ba75e commit d6eea78
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/GitHubLabelSync.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>2.1.1</Version>
<Version>2.1.2</Version>
<OutputType>Exe</OutputType>
<PackAsTool>true</PackAsTool>
<ToolCommandName>sync-labels</ToolCommandName>
Expand Down
10 changes: 8 additions & 2 deletions src/Synchronizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,16 @@ public async Task<IReadOnlyList<Label>> GetAccountLabels(Account account)
? await _gitHub.CreateTempRepoForOrganization(account, repoName)
: await _gitHub.CreateTempRepoForUser(account, repoName);

var labels = await _gitHub.GetLabels(repo);
var originalLabels = await _gitHub.GetLabels(repo);
var latestLabels = await _gitHub.GetLabels(repo);
while (originalLabels.Count != latestLabels.Count)
{
originalLabels = latestLabels;
latestLabels = await _gitHub.GetLabels(repo);
}
await _gitHub.DeleteTempRepo(account, repoName);

return labels;
return latestLabels;
}

private static string LabelNames(IEnumerable<Label> labels)
Expand Down
23 changes: 23 additions & 0 deletions test/SynchronizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class SynchronizerTests
};

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

[Fact]
public async Task ValidAccessReturnsSuccess()
Expand Down Expand Up @@ -300,6 +301,28 @@ public async Task GettingAccountLabelsDeletesTempRepoForUser()
Assert.StartsWith("temp-label-sync-20", tempRepoName);
}

[Fact]
public async Task GettingAccountLabelsWaitsUntilAllLabelsHaveBeenCreated()
{
//arrange
var gitHub = Substitute.For<IGitHub>();
var sync = new Synchronizer(gitHub, _noOp, _noOp);

var account = new Stubs.Organization("ecoAPM");
gitHub.GetLabels(Arg.Any<Repository>()).Returns(
new [] { EmptyLabel },
new [] { EmptyLabel, EmptyLabel },
new [] { EmptyLabel, EmptyLabel, EmptyLabel },
new [] { EmptyLabel, EmptyLabel, EmptyLabel }
);

//act
var labels = await sync.GetAccountLabels(account);

//assert
Assert.Equal(3, labels.Count);
}

[Fact]
public async Task PerformsCorrectActions()
{
Expand Down

0 comments on commit d6eea78

Please sign in to comment.