Skip to content

Commit 31fd6d7

Browse files
authored
Merge pull request #13363 from dependabot/dev/brettfo/nuget-job-file-shape
allow new format for existing pull requests
2 parents 602874f + e8b5704 commit 31fd6d7

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Run/SerializationTests.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ public void DeserializeDependencyGroups()
407407
}
408408

409409
[Fact]
410-
public void DeserializeExistingPullRequests()
410+
public void DeserializeExistingPullRequestsOldFormat()
411411
{
412412
var jsonWrapperJson = """
413413
{
@@ -429,6 +429,41 @@ public void DeserializeExistingPullRequests()
429429
""";
430430
var jobWrapper = RunWorker.Deserialize(jsonWrapperJson)!;
431431
Assert.Single(jobWrapper.Job.ExistingPullRequests);
432+
Assert.Null(jobWrapper.Job.ExistingPullRequests[0].PrNumber);
433+
Assert.Single(jobWrapper.Job.ExistingPullRequests[0].Dependencies);
434+
Assert.Equal("Some.Package", jobWrapper.Job.ExistingPullRequests[0].Dependencies[0].DependencyName);
435+
Assert.Equal(NuGetVersion.Parse("1.2.3"), jobWrapper.Job.ExistingPullRequests[0].Dependencies[0].DependencyVersion);
436+
Assert.False(jobWrapper.Job.ExistingPullRequests[0].Dependencies[0].DependencyRemoved);
437+
Assert.Null(jobWrapper.Job.ExistingPullRequests[0].Dependencies[0].Directory);
438+
}
439+
440+
[Fact]
441+
public void DeserializeExistingPullRequestsNewFormat()
442+
{
443+
var jsonWrapperJson = """
444+
{
445+
"job": {
446+
"source": {
447+
"provider": "github",
448+
"repo": "some/repo"
449+
},
450+
"existing-pull-requests": [
451+
{
452+
"pr-number": 123,
453+
"dependencies": [
454+
{
455+
"dependency-name": "Some.Package",
456+
"dependency-version": "1.2.3"
457+
}
458+
]
459+
}
460+
]
461+
}
462+
}
463+
""";
464+
var jobWrapper = RunWorker.Deserialize(jsonWrapperJson)!;
465+
Assert.Single(jobWrapper.Job.ExistingPullRequests);
466+
Assert.Equal(123, jobWrapper.Job.ExistingPullRequests[0].PrNumber);
432467
Assert.Single(jobWrapper.Job.ExistingPullRequests[0].Dependencies);
433468
Assert.Equal("Some.Package", jobWrapper.Job.ExistingPullRequests[0].Dependencies[0].DependencyName);
434469
Assert.Equal(NuGetVersion.Parse("1.2.3"), jobWrapper.Job.ExistingPullRequests[0].Dependencies[0].DependencyVersion);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using System.Collections.Immutable;
2+
using System.Text.Json.Serialization;
23

34
namespace NuGetUpdater.Core.Run.ApiModel;
45

56
public record PullRequest
67
{
8+
[JsonPropertyName("pr-number")]
9+
public int? PrNumber { get; init; } = null;
10+
[JsonPropertyName("dependencies")]
711
public ImmutableArray<PullRequestDependency> Dependencies { get; init; } = [];
812
}

nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Run/PullRequestConverter.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Immutable;
1+
using System.Collections.Immutable;
22
using System.Text.Json;
33
using System.Text.Json.Serialization;
44

@@ -10,16 +10,36 @@ public class PullRequestConverter : JsonConverter<PullRequest>
1010
{
1111
public override PullRequest? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1212
{
13-
if (reader.TokenType != JsonTokenType.StartArray)
13+
PullRequest? result;
14+
switch (reader.TokenType)
1415
{
15-
throw new JsonException("expected array of pull request dependencies");
16+
case JsonTokenType.StartArray:
17+
// old format, array of arrays of dependencies
18+
var dependencies = JsonSerializer.Deserialize<ImmutableArray<PullRequestDependency>>(ref reader, options);
19+
result = new PullRequest()
20+
{
21+
Dependencies = dependencies
22+
};
23+
break;
24+
case JsonTokenType.StartObject:
25+
// new format, direct object
26+
// use the same deserializer options but exclude this special converter
27+
var optionsWithoutThisCustomConverter = new JsonSerializerOptions(options);
28+
for (int i = optionsWithoutThisCustomConverter.Converters.Count - 1; i >= 0; i--)
29+
{
30+
if (optionsWithoutThisCustomConverter.Converters[i].GetType() == typeof(PullRequestConverter))
31+
{
32+
optionsWithoutThisCustomConverter.Converters.RemoveAt(i);
33+
}
34+
}
35+
36+
result = JsonSerializer.Deserialize<PullRequest>(ref reader, optionsWithoutThisCustomConverter);
37+
break;
38+
default:
39+
throw new JsonException("expected pull request object or array of pull request dependencies");
1640
}
1741

18-
var dependencies = JsonSerializer.Deserialize<ImmutableArray<PullRequestDependency>>(ref reader, options);
19-
return new PullRequest()
20-
{
21-
Dependencies = dependencies
22-
};
42+
return result;
2343
}
2444

2545
public override void Write(Utf8JsonWriter writer, PullRequest value, JsonSerializerOptions options)

0 commit comments

Comments
 (0)