Skip to content

Commit beba787

Browse files
authored
Fix NRE in LinkIndexProvider while adding a new branch to existing repository (#1294)
* Fix NRE in LinkIndexProvider while adding a new branch to existing repository * Throw exception when save goes wrong so its put back on the queue * fix typo
1 parent b06a8fb commit beba787

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using System.Net;
56
using Amazon.Lambda.Core;
67
using Amazon.S3;
78
using Amazon.S3.Model;
@@ -36,27 +37,40 @@ private async Task<LinkReferenceRegistry> GetLinkIndex()
3637
return _linkIndex;
3738
}
3839

39-
public async Task UpdateLinkIndexEntry(LinkRegistryEntry linkRegistryEntry)
40+
public async Task UpdateLinkIndexEntry(LinkRegistryEntry newEntry)
4041
{
4142
_linkIndex ??= await GetLinkIndex();
42-
if (_linkIndex.Repositories.TryGetValue(linkRegistryEntry.Repository, out var existingEntry))
43+
var repository = newEntry.Repository;
44+
var branch = newEntry.Branch;
45+
// repository already exists in links.json
46+
if (_linkIndex.Repositories.TryGetValue(repository, out var existingRepositoryEntry))
4347
{
44-
var newEntryIsNewer = DateTime.Compare(linkRegistryEntry.UpdatedAt, existingEntry[linkRegistryEntry.Branch].UpdatedAt) > 0;
45-
if (newEntryIsNewer)
48+
// The branch already exists in the repository entry
49+
if (existingRepositoryEntry.TryGetValue(branch, out var existingBranchEntry))
4650
{
47-
existingEntry[linkRegistryEntry.Branch] = linkRegistryEntry;
48-
logger.LogInformation("Updated existing entry for {repository}@{branch}", linkRegistryEntry.Repository, linkRegistryEntry.Branch);
51+
if (newEntry.UpdatedAt > existingBranchEntry.UpdatedAt)
52+
{
53+
existingRepositoryEntry[branch] = newEntry;
54+
logger.LogInformation("Updated existing entry for {repository}@{branch}", repository, branch);
55+
}
56+
else
57+
logger.LogInformation("Skipping update for {repository}@{branch} because the existing entry is newer or equal", repository, branch);
4958
}
59+
// branch does not exist in the repository entry
5060
else
51-
logger.LogInformation("Skipping update for {repository}@{branch} because the existing entry is newer", linkRegistryEntry.Repository, linkRegistryEntry.Branch);
61+
{
62+
existingRepositoryEntry[branch] = newEntry;
63+
logger.LogInformation("Added new entry '{repository}@{branch}' to existing entry for '{repository}'", repository, branch, repository);
64+
}
5265
}
66+
// onboarding new repository
5367
else
5468
{
55-
_linkIndex.Repositories.Add(linkRegistryEntry.Repository, new Dictionary<string, LinkRegistryEntry>
69+
_linkIndex.Repositories.Add(repository, new Dictionary<string, LinkRegistryEntry>
5670
{
57-
{ linkRegistryEntry.Branch, linkRegistryEntry }
71+
{ branch, newEntry }
5872
});
59-
logger.LogInformation("Added new entry for {repository}@{branch}", linkRegistryEntry.Repository, linkRegistryEntry.Branch);
73+
logger.LogInformation("Added new entry for {repository}@{branch}", repository, branch);
6074
}
6175
}
6276

@@ -74,7 +88,13 @@ public async Task Save()
7488
ContentType = "application/json",
7589
IfMatch = _etag // Only update if the ETag matches. Meaning the object has not been changed in the meantime.
7690
};
77-
_ = await s3Client.PutObjectAsync(putObjectRequest);
78-
logger.LogInformation("Successfully saved link index to s3://{bucketName}/{key}", bucketName, key);
91+
var putResponse = await s3Client.PutObjectAsync(putObjectRequest);
92+
if (putResponse.HttpStatusCode == HttpStatusCode.OK)
93+
logger.LogInformation("Successfully saved link index to s3://{bucketName}/{key}", bucketName, key);
94+
else
95+
{
96+
logger.LogError("Unable to save index to s3://{bucketName}/{key}", bucketName, key);
97+
throw new Exception($"Unable to save index to s3://{bucketName}/{key}");
98+
}
7999
}
80100
}

0 commit comments

Comments
 (0)