Skip to content

Commit

Permalink
Polymorphic custom property values (fixes deserialization bug) (#521)
Browse files Browse the repository at this point in the history
* Polymorphic custom properties values

* Apply suggestions from code review

Co-authored-by: Jamie Magee <jamie.magee@gmail.com>

---------

Co-authored-by: Jamie Magee <jamie.magee@gmail.com>
  • Loading branch information
colbylwilliams and JamieMagee authored Jun 27, 2024
1 parent 8c1296f commit 936ebd8
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Octokit.Webhooks.Models.CustomPropertyEvent;

using System.Linq;

[PublicAPI]
public sealed record CustomProperty
{
Expand All @@ -14,7 +16,25 @@ public sealed record CustomProperty
public bool Required { get; init; }

[JsonPropertyName("default_value")]
public string? DefaultValue { get; init; }
public object? Default { get; init; }

public string? DefaultValue => this.Default switch
{
string str => str,
IEnumerable<string> strings => "[" + string.Join(",", strings) + "]",
JsonElement { ValueKind: JsonValueKind.String } json => json.GetString(),
JsonElement { ValueKind: JsonValueKind.Array } json => "[" + string.Join(",", json.EnumerateArray().Select(e => e.GetString()!)) + "]",
_ => null,
};

public IEnumerable<string>? DefaultValues => this.Default switch
{
string str => [str],
IEnumerable<string> strings => strings,
JsonElement { ValueKind: JsonValueKind.String } json => [json.GetString()!],
JsonElement { ValueKind: JsonValueKind.Array } json => json.EnumerateArray().Select(e => e.GetString()!),
_ => null,
};

[JsonPropertyName("description")]
public string? Description { get; init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ public enum CustomPropertyValueType

[EnumMember(Value = "single_select")]
SingleSelect,

[EnumMember(Value = "multi_select")]
MultiSelect,

[EnumMember(Value = "true_false")]
TrueFalse,
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
namespace Octokit.Webhooks.Models.CustomPropertyValuesEvent;

using System.Linq;

[PublicAPI]
public sealed record CustomPropertyValue
{
[JsonPropertyName("property_name")]
public string PropertyName { get; init; } = null!;

[JsonPropertyName("value")]
public string? Value { get; init; }
public object? Object { get; init; }

public string? Value => this.Object switch
{
string str => str,
IEnumerable<string> strings => "[" + string.Join(",", strings) + "]",
JsonElement { ValueKind: JsonValueKind.String } json => json.GetString(),
JsonElement { ValueKind: JsonValueKind.Array } json => "[" + string.Join(",", json.EnumerateArray().Select(e => e.GetString()!)) + "]",
_ => null,
};

public IEnumerable<string>? Values => this.Object switch
{
string str => [str],
IEnumerable<string> strings => strings,
JsonElement { ValueKind: JsonValueKind.String } json => [json.GetString()!],
JsonElement { ValueKind: JsonValueKind.Array } json => json.EnumerateArray().Select(e => e.GetString()!),
_ => null,
};
}
2 changes: 1 addition & 1 deletion src/Octokit.Webhooks/Models/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,5 @@ public sealed record Repository
public string? Organization { get; init; }

[JsonPropertyName("custom_properties")]
public IDictionary<string, string>? CustomProperties { get; init; }
public IDictionary<string, object>? CustomProperties { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"action": "created",
"definition": {
"property_name": "dev_platform_select",
"property_name": "test_ss",
"value_type": "single_select",
"required": true,
"default_value": "option_default",
"description": "select description",
"default_value": "option_c",
"description": "single_select property",
"allowed_values": [
"option_one",
"option_two",
"option_default"
"option_a",
"option_b",
"option_c",
"option_d",
"option_e"
],
"values_editable_by": "org_and_repo_actors"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
{
"action": "updated",
"definition": {
"property_name": "dev_platform_test",
"value_type": "single_select",
"required": false,
"property_name": "test_ms",
"value_type": "multi_select",
"required": true,
"default_value": [
"option_d",
"option_e"
],
"description": "multi_select property",
"allowed_values": [
"option_one",
"option_two"
"option_a",
"option_b",
"option_c",
"option_d",
"option_e"
],
"values_editable_by": "org_and_repo_actors"
"values_editable_by": "org_actors"
},
"organization": {
"login": "Contoso-Inc",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
{
"action": "updated",
"new_property_values": [
{
"property_name": "test_ms",
"value": [
"option_c",
"option_d"
]
},
{
"property_name": "test_ss",
"value": "option_b"
},
{
"property_name": "test_tf",
"value": "true"
}
],
"old_property_values": [
{
"property_name": "test_ms",
"value": [
"option_b",
"option_c"
]
},
{
"property_name": "test_ss",
"value": "option_c"
},
{
"property_name": "test_tf",
"value": "false"
}
],
"repository": {
"id": 798052885,
"node_id": "R_kgDOL5FSFQ",
"name": "demo01",
"full_name": "Contoso-Inc/demo01",
"private": false,
"owner": {
"login": "Contoso-Inc",
"id": 128636122,
"node_id": "O_kgDOB6rU2g",
"avatar_url": "https://avatars.githubusercontent.com/u/128636122?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Contoso-Inc",
"html_url": "https://github.com/Contoso-Inc",
"followers_url": "https://api.github.com/users/Contoso-Inc/followers",
"following_url": "https://api.github.com/users/Contoso-Inc/following{/other_user}",
"gists_url": "https://api.github.com/users/Contoso-Inc/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Contoso-Inc/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Contoso-Inc/subscriptions",
"organizations_url": "https://api.github.com/users/Contoso-Inc/orgs",
"repos_url": "https://api.github.com/users/Contoso-Inc/repos",
"events_url": "https://api.github.com/users/Contoso-Inc/events{/privacy}",
"received_events_url": "https://api.github.com/users/Contoso-Inc/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/Contoso-Inc/demo01",
"description": null,
"fork": false,
"url": "https://api.github.com/repos/Contoso-Inc/demo01",
"forks_url": "https://api.github.com/repos/Contoso-Inc/demo01/forks",
"keys_url": "https://api.github.com/repos/Contoso-Inc/demo01/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/Contoso-Inc/demo01/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/Contoso-Inc/demo01/teams",
"hooks_url": "https://api.github.com/repos/Contoso-Inc/demo01/hooks",
"issue_events_url": "https://api.github.com/repos/Contoso-Inc/demo01/issues/events{/number}",
"events_url": "https://api.github.com/repos/Contoso-Inc/demo01/events",
"assignees_url": "https://api.github.com/repos/Contoso-Inc/demo01/assignees{/user}",
"branches_url": "https://api.github.com/repos/Contoso-Inc/demo01/branches{/branch}",
"tags_url": "https://api.github.com/repos/Contoso-Inc/demo01/tags",
"blobs_url": "https://api.github.com/repos/Contoso-Inc/demo01/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/Contoso-Inc/demo01/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/Contoso-Inc/demo01/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/Contoso-Inc/demo01/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/Contoso-Inc/demo01/statuses/{sha}",
"languages_url": "https://api.github.com/repos/Contoso-Inc/demo01/languages",
"stargazers_url": "https://api.github.com/repos/Contoso-Inc/demo01/stargazers",
"contributors_url": "https://api.github.com/repos/Contoso-Inc/demo01/contributors",
"subscribers_url": "https://api.github.com/repos/Contoso-Inc/demo01/subscribers",
"subscription_url": "https://api.github.com/repos/Contoso-Inc/demo01/subscription",
"commits_url": "https://api.github.com/repos/Contoso-Inc/demo01/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/Contoso-Inc/demo01/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/Contoso-Inc/demo01/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/Contoso-Inc/demo01/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/Contoso-Inc/demo01/contents/{+path}",
"compare_url": "https://api.github.com/repos/Contoso-Inc/demo01/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/Contoso-Inc/demo01/merges",
"archive_url": "https://api.github.com/repos/Contoso-Inc/demo01/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/Contoso-Inc/demo01/downloads",
"issues_url": "https://api.github.com/repos/Contoso-Inc/demo01/issues{/number}",
"pulls_url": "https://api.github.com/repos/Contoso-Inc/demo01/pulls{/number}",
"milestones_url": "https://api.github.com/repos/Contoso-Inc/demo01/milestones{/number}",
"notifications_url": "https://api.github.com/repos/Contoso-Inc/demo01/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/Contoso-Inc/demo01/labels{/name}",
"releases_url": "https://api.github.com/repos/Contoso-Inc/demo01/releases{/id}",
"deployments_url": "https://api.github.com/repos/Contoso-Inc/demo01/deployments",
"created_at": "2024-05-09T02:09:11Z",
"updated_at": "2024-05-09T02:09:12Z",
"pushed_at": "2024-05-09T02:09:12Z",
"git_url": "git://github.com/Contoso-Inc/demo01.git",
"ssh_url": "git@github.com:Contoso-Inc/demo01.git",
"clone_url": "https://github.com/Contoso-Inc/demo01.git",
"svn_url": "https://github.com/Contoso-Inc/demo01",
"homepage": null,
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"has_discussions": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 0,
"license": null,
"allow_forking": true,
"is_template": false,
"web_commit_signoff_required": false,
"topics": [],
"visibility": "public",
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "main",
"custom_properties": {
"dev_platform": "workflows"
}
},
"organization": {
"login": "Contoso-Inc",
"id": 128636122,
"node_id": "O_kgDOB6rU2g",
"url": "https://api.github.com/orgs/Contoso-Inc",
"repos_url": "https://api.github.com/orgs/Contoso-Inc/repos",
"events_url": "https://api.github.com/orgs/Contoso-Inc/events",
"hooks_url": "https://api.github.com/orgs/Contoso-Inc/hooks",
"issues_url": "https://api.github.com/orgs/Contoso-Inc/issues",
"members_url": "https://api.github.com/orgs/Contoso-Inc/members{/member}",
"public_members_url": "https://api.github.com/orgs/Contoso-Inc/public_members{/member}",
"avatar_url": "https://avatars.githubusercontent.com/u/128636122?v=4",
"description": "Contoso Inc brings you the best of everything. "
},
"sender": {
"login": "markweitzel",
"id": 522116,
"node_id": "MDQ6VXNlcjUyMjExNg==",
"avatar_url": "https://avatars.githubusercontent.com/u/522116?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/markweitzel",
"html_url": "https://github.com/markweitzel",
"followers_url": "https://api.github.com/users/markweitzel/followers",
"following_url": "https://api.github.com/users/markweitzel/following{/other_user}",
"gists_url": "https://api.github.com/users/markweitzel/gists{/gist_id}",
"starred_url": "https://api.github.com/users/markweitzel/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/markweitzel/subscriptions",
"organizations_url": "https://api.github.com/users/markweitzel/orgs",
"repos_url": "https://api.github.com/users/markweitzel/repos",
"events_url": "https://api.github.com/users/markweitzel/events{/privacy}",
"received_events_url": "https://api.github.com/users/markweitzel/received_events",
"type": "User",
"site_admin": false
},
"installation": {
"id": 40989628,
"node_id": "MDIzOkludGVncmF0aF32SW5zdGFsbGF0aW9uNDA5ODk2Mjg="
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@
"action": "updated",
"new_property_values": [
{
"property_name": "dev_platform",
"value": "workflows"
"property_name": "test_ms",
"value": [
"option_c",
"option_b"
]
},
{
"property_name": "test_ss",
"value": "option_c"
},
{
"property_name": "test_tf",
"value": "false"
}
],
"old_property_values": [
{
"property_name": "dev_platform",
"property_name": "test_ms",
"value": null
},
{
"property_name": "test_ss",
"value": null
},
{
"property_name": "test_tf",
"value": null
}
],
Expand Down
10 changes: 9 additions & 1 deletion test/Octokit.Webhooks.Test/Resources/deployment/payload.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@
"open_issues": 2,
"watchers": 0,
"default_branch": "master",
"custom_properties": {
"test_ms": [
"option_d",
"option_e"
],
"test_ss": "option_c",
"test_tf": "foo"
},
"is_template": false,
"topics": [],
"visibility": "public",
Expand All @@ -156,4 +164,4 @@
"type": "User",
"site_admin": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@
"open_issues": 0,
"watchers": 0,
"default_branch": "master",
"custom_properties": {
"test_ms": [
"option_d",
"option_e"
],
"test_ss": "option_c",
"test_tf": "foo"
},
"is_template": false,
"topics": [],
"visibility": "public",
Expand Down Expand Up @@ -132,4 +140,4 @@
"type": "User",
"site_admin": false
}
}
}
Loading

0 comments on commit 936ebd8

Please sign in to comment.