-
Notifications
You must be signed in to change notification settings - Fork 40
Support projects_v2_item changes of scalar values like strings/dates/numbers #725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eba3502
Support projects_v2_item changes of scalar values like strings, dates…
DavidBoike 3f80b46
Move converter, implement serialize, and add roundtrip test
DavidBoike 388a699
Move 2 cases and throw more appropriate exception type
DavidBoike 5b13105
Apply suggestions from code review
DavidBoike 1c0e12b
Compiler fixes necessary from review
DavidBoike 8e6a2ba
Use pattern matching for more succinct case list
DavidBoike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/Octokit.Webhooks/Converter/ChangesFieldValueChangeConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| namespace Octokit.Webhooks.Converter; | ||
|
|
||
| using Octokit.Webhooks.Models.ProjectsV2ItemEvent; | ||
|
|
||
| public class ChangesFieldValueChangeConverter : JsonConverter<ChangesFieldValueChangeBase> | ||
| { | ||
| public override ChangesFieldValueChangeBase? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| switch (reader) | ||
| { | ||
| case { TokenType: JsonTokenType.StartObject }: | ||
| var changeObject = JsonSerializer.Deserialize<ChangesFieldValueChange>(ref reader, options); | ||
| return changeObject; | ||
| case { TokenType: JsonTokenType.String }: | ||
| return new ChangesFieldValueScalarChange { StringValue = reader.GetString() }; | ||
| case { TokenType: JsonTokenType.Null }: | ||
| return null; | ||
| case { TokenType: JsonTokenType.Number }: | ||
| return new ChangesFieldValueScalarChange { NumericValue = reader.GetDecimal() }; | ||
| default: | ||
| throw new JsonException($"Invalid JsonTokenType {reader.TokenType}"); | ||
| } | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, ChangesFieldValueChangeBase value, JsonSerializerOptions options) | ||
| { | ||
| switch (value) | ||
| { | ||
| case ChangesFieldValueScalarChange { StringValue: not null } scalarChange: | ||
| writer.WriteStringValue(scalarChange.StringValue); | ||
| break; | ||
| case ChangesFieldValueScalarChange { NumericValue: not null } scalarChange: | ||
| writer.WriteNumberValue(scalarChange.NumericValue.Value); | ||
| break; | ||
| case ChangesFieldValueScalarChange: | ||
| writer.WriteNullValue(); | ||
| break; | ||
| case ChangesFieldValueChange change: | ||
| JsonSerializer.Serialize(writer, change, options); | ||
| break; | ||
| default: | ||
| writer.WriteNullValue(); | ||
| break; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Octokit.Webhooks/Models/ProjectsV2ItemEvent/ChangesFieldValueChange.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Octokit.Webhooks/Models/ProjectsV2ItemEvent/ChangesFieldValueChangeBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace Octokit.Webhooks.Models.ProjectsV2ItemEvent; | ||
|
|
||
| [JsonConverter(typeof(ChangesFieldValueChangeConverter))] | ||
| public abstract record ChangesFieldValueChangeBase | ||
| { | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/Octokit.Webhooks/Models/ProjectsV2ItemEvent/ChangesFieldValueScalarChange.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace Octokit.Webhooks.Models.ProjectsV2ItemEvent; | ||
|
|
||
| [PublicAPI] | ||
| public sealed record ChangesFieldValueScalarChange : ChangesFieldValueChangeBase | ||
| { | ||
| public string? StringValue { get; init; } | ||
|
|
||
| public decimal? NumericValue { get; init; } | ||
| } | ||
72 changes: 72 additions & 0 deletions
72
test/Octokit.Webhooks.Test/Converter/ChangesFieldValueChangeConverterTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| namespace Octokit.Webhooks.Test.Converter; | ||
|
|
||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using AwesomeAssertions; | ||
| using Octokit.Webhooks.Converter; | ||
| using Octokit.Webhooks.Models.ProjectsV2ItemEvent; | ||
| using Xunit; | ||
|
|
||
| public class ChangesFieldValueChangeConverterTests(ITestOutputHelper output) | ||
| { | ||
| private readonly JsonSerializerOptions options = new() | ||
| { | ||
| WriteIndented = true, | ||
| Converters = | ||
| { | ||
| new ChangesFieldValueChangeConverter(), | ||
| }, | ||
| }; | ||
|
|
||
| [Fact] | ||
| public void Roundtrip() | ||
| { | ||
| var test = new TestObject | ||
| { | ||
| AsString = new ChangesFieldValueScalarChange { StringValue = "Hello world" }, | ||
| AsNumber = new ChangesFieldValueScalarChange { NumericValue = 3.1415926m }, | ||
| AsObject = new ChangesFieldValueChange | ||
| { | ||
| Color = "color", | ||
| Description = "description", | ||
| Id = "12345", | ||
| Name = "Name", | ||
| }, | ||
| }; | ||
|
|
||
| var serialized = JsonSerializer.Serialize(test, this.options); | ||
| output.WriteLine(serialized); | ||
|
|
||
| var deserialized = JsonSerializer.Deserialize<TestObject>(serialized, this.options); | ||
|
|
||
| deserialized.Should().NotBeNull(); | ||
| deserialized.AsString.Should().NotBeNull().And.BeOfType<ChangesFieldValueScalarChange>(); | ||
| deserialized.AsNumber.Should().NotBeNull().And.BeOfType<ChangesFieldValueScalarChange>(); | ||
|
|
||
| (deserialized.AsString as ChangesFieldValueScalarChange)?.StringValue.Should().Be("Hello world"); | ||
| (deserialized.AsString as ChangesFieldValueScalarChange)?.NumericValue.Should().BeNull(); | ||
|
|
||
| (deserialized.AsNumber as ChangesFieldValueScalarChange)?.StringValue.Should().BeNull(); | ||
| (deserialized.AsNumber as ChangesFieldValueScalarChange)?.NumericValue.Should().Be(3.1415926m); | ||
|
|
||
| deserialized.AsObject.Should().NotBeNull().And.BeOfType<ChangesFieldValueChange>(); | ||
| var changeObject = deserialized.AsObject.As<ChangesFieldValueChange>(); | ||
|
|
||
| changeObject.Color.Should().Be("color"); | ||
| changeObject.Description.Should().Be("description"); | ||
| changeObject.Id.Should().Be("12345"); | ||
| changeObject.Name.Should().Be("Name"); | ||
| } | ||
|
|
||
| internal sealed class TestObject | ||
| { | ||
| [JsonPropertyName("as_string")] | ||
| public ChangesFieldValueChangeBase AsString { get; init; } = null!; | ||
|
|
||
| [JsonPropertyName("as_number")] | ||
| public ChangesFieldValueChangeBase AsNumber { get; init; } = null!; | ||
|
|
||
| [JsonPropertyName("as_object")] | ||
| public ChangesFieldValueChangeBase AsObject { get; init; } = null!; | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
test/Octokit.Webhooks.Test/Resources/projects_v2_item/edited.with-date.payload.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| { | ||
| "action": "edited", | ||
| "projects_v2_item": { | ||
| "id": 5678510, | ||
| "node_id": "PVTI_lADOAWcTxs07G84AVqWu", | ||
| "project_node_id": "PVT_kwDOAWcTxs07Gw", | ||
| "content_node_id": "DI_lADOAWcTxs07G84AIjOy", | ||
| "content_type": "DraftIssue", | ||
| "creator": { | ||
| "login": "wolfy1339", | ||
| "id": 4595477, | ||
| "node_id": "MDQ6VXNlcjQ1OTU0Nzc=", | ||
| "avatar_url": "https://avatars.githubusercontent.com/u/4595477?v=4", | ||
| "gravatar_id": "", | ||
| "url": "https://api.github.com/users/wolfy1339", | ||
| "html_url": "https://github.com/wolfy1339", | ||
| "followers_url": "https://api.github.com/users/wolfy1339/followers", | ||
| "following_url": "https://api.github.com/users/wolfy1339/following{/other_user}", | ||
| "gists_url": "https://api.github.com/users/wolfy1339/gists{/gist_id}", | ||
| "starred_url": "https://api.github.com/users/wolfy1339/starred{/owner}{/repo}", | ||
| "subscriptions_url": "https://api.github.com/users/wolfy1339/subscriptions", | ||
| "organizations_url": "https://api.github.com/users/wolfy1339/orgs", | ||
| "repos_url": "https://api.github.com/users/wolfy1339/repos", | ||
| "events_url": "https://api.github.com/users/wolfy1339/events{/privacy}", | ||
| "received_events_url": "https://api.github.com/users/wolfy1339/received_events", | ||
| "type": "User", | ||
| "site_admin": false | ||
| }, | ||
| "created_at": "2022-06-08T20:34:26Z", | ||
| "updated_at": "2022-06-08T20:34:26Z", | ||
| "archived_at": null | ||
| }, | ||
| "changes": { | ||
| "field_value": { | ||
| "field_node_id": "PVTF_lADOAEzhac4AjFojzgbzsKM", | ||
| "field_type": "date", | ||
| "field_name": "Start Date", | ||
| "project_number": 233, | ||
| "from": null, | ||
| "to": "2025-08-01T00:00:00+00:00" | ||
| } | ||
| }, | ||
| "organization": { | ||
| "login": "Octocoders", | ||
| "id": 38302899, | ||
| "node_id": "MDEyOk9yZ2FuaXphdGlvbjM4MzAyODk5", | ||
| "url": "https://api.github.com/orgs/Octocoders", | ||
| "repos_url": "https://api.github.com/orgs/Octocoders/repos", | ||
| "events_url": "https://api.github.com/orgs/Octocoders/events", | ||
| "hooks_url": "https://api.github.com/orgs/Octocoders/hooks", | ||
| "issues_url": "https://api.github.com/orgs/Octocoders/issues", | ||
| "members_url": "https://api.github.com/orgs/Octocoders/members{/member}", | ||
| "public_members_url": "https://api.github.com/orgs/Octocoders/public_members{/member}", | ||
| "avatar_url": "https://avatars1.githubusercontent.com/u/38302899?v=4", | ||
| "description": "" | ||
| }, | ||
| "sender": { | ||
| "login": "wolfy1339", | ||
| "id": 4595477, | ||
| "node_id": "MDQ6VXNlcjQ1OTU0Nzc=", | ||
| "avatar_url": "https://avatars.githubusercontent.com/u/4595477?v=4", | ||
| "gravatar_id": "", | ||
| "url": "https://api.github.com/users/wolfy1339", | ||
| "html_url": "https://github.com/wolfy1339", | ||
| "followers_url": "https://api.github.com/users/wolfy1339/followers", | ||
| "following_url": "https://api.github.com/users/wolfy1339/following{/other_user}", | ||
| "gists_url": "https://api.github.com/users/wolfy1339/gists{/gist_id}", | ||
| "starred_url": "https://api.github.com/users/wolfy1339/starred{/owner}{/repo}", | ||
| "subscriptions_url": "https://api.github.com/users/wolfy1339/subscriptions", | ||
| "organizations_url": "https://api.github.com/users/wolfy1339/orgs", | ||
| "repos_url": "https://api.github.com/users/wolfy1339/repos", | ||
| "events_url": "https://api.github.com/users/wolfy1339/events{/privacy}", | ||
| "received_events_url": "https://api.github.com/users/wolfy1339/received_events", | ||
| "type": "User", | ||
| "site_admin": false | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
test/Octokit.Webhooks.Test/Resources/projects_v2_item/edited.with-number.payload.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| { | ||
| "action": "edited", | ||
| "projects_v2_item": { | ||
| "id": 5678510, | ||
| "node_id": "PVTI_lADOAWcTxs07G84AVqWu", | ||
| "project_node_id": "PVT_kwDOAWcTxs07Gw", | ||
| "content_node_id": "DI_lADOAWcTxs07G84AIjOy", | ||
| "content_type": "DraftIssue", | ||
| "creator": { | ||
| "login": "wolfy1339", | ||
| "id": 4595477, | ||
| "node_id": "MDQ6VXNlcjQ1OTU0Nzc=", | ||
| "avatar_url": "https://avatars.githubusercontent.com/u/4595477?v=4", | ||
| "gravatar_id": "", | ||
| "url": "https://api.github.com/users/wolfy1339", | ||
| "html_url": "https://github.com/wolfy1339", | ||
| "followers_url": "https://api.github.com/users/wolfy1339/followers", | ||
| "following_url": "https://api.github.com/users/wolfy1339/following{/other_user}", | ||
| "gists_url": "https://api.github.com/users/wolfy1339/gists{/gist_id}", | ||
| "starred_url": "https://api.github.com/users/wolfy1339/starred{/owner}{/repo}", | ||
| "subscriptions_url": "https://api.github.com/users/wolfy1339/subscriptions", | ||
| "organizations_url": "https://api.github.com/users/wolfy1339/orgs", | ||
| "repos_url": "https://api.github.com/users/wolfy1339/repos", | ||
| "events_url": "https://api.github.com/users/wolfy1339/events{/privacy}", | ||
| "received_events_url": "https://api.github.com/users/wolfy1339/received_events", | ||
| "type": "User", | ||
| "site_admin": false | ||
| }, | ||
| "created_at": "2022-06-08T20:34:26Z", | ||
| "updated_at": "2022-06-08T20:34:26Z", | ||
| "archived_at": null | ||
| }, | ||
| "changes": { | ||
| "field_value": { | ||
| "field_node_id": "PVTF_lADOAEzhac4AjFojzgyEIsU", | ||
| "field_type": "number", | ||
| "field_name": "tempNumber", | ||
| "project_number": 233, | ||
| "from": null, | ||
| "to": 123.456 | ||
| } | ||
| }, | ||
| "organization": { | ||
| "login": "Octocoders", | ||
| "id": 38302899, | ||
| "node_id": "MDEyOk9yZ2FuaXphdGlvbjM4MzAyODk5", | ||
| "url": "https://api.github.com/orgs/Octocoders", | ||
| "repos_url": "https://api.github.com/orgs/Octocoders/repos", | ||
| "events_url": "https://api.github.com/orgs/Octocoders/events", | ||
| "hooks_url": "https://api.github.com/orgs/Octocoders/hooks", | ||
| "issues_url": "https://api.github.com/orgs/Octocoders/issues", | ||
| "members_url": "https://api.github.com/orgs/Octocoders/members{/member}", | ||
| "public_members_url": "https://api.github.com/orgs/Octocoders/public_members{/member}", | ||
| "avatar_url": "https://avatars1.githubusercontent.com/u/38302899?v=4", | ||
| "description": "" | ||
| }, | ||
| "sender": { | ||
| "login": "wolfy1339", | ||
| "id": 4595477, | ||
| "node_id": "MDQ6VXNlcjQ1OTU0Nzc=", | ||
| "avatar_url": "https://avatars.githubusercontent.com/u/4595477?v=4", | ||
| "gravatar_id": "", | ||
| "url": "https://api.github.com/users/wolfy1339", | ||
| "html_url": "https://github.com/wolfy1339", | ||
| "followers_url": "https://api.github.com/users/wolfy1339/followers", | ||
| "following_url": "https://api.github.com/users/wolfy1339/following{/other_user}", | ||
| "gists_url": "https://api.github.com/users/wolfy1339/gists{/gist_id}", | ||
| "starred_url": "https://api.github.com/users/wolfy1339/starred{/owner}{/repo}", | ||
| "subscriptions_url": "https://api.github.com/users/wolfy1339/subscriptions", | ||
| "organizations_url": "https://api.github.com/users/wolfy1339/orgs", | ||
| "repos_url": "https://api.github.com/users/wolfy1339/repos", | ||
| "events_url": "https://api.github.com/users/wolfy1339/events{/privacy}", | ||
| "received_events_url": "https://api.github.com/users/wolfy1339/received_events", | ||
| "type": "User", | ||
| "site_admin": false | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.