Skip to content

Commit f8fd832

Browse files
Merge pull request #45 from apiiro/barnon/fix-webhook-create-array-response
Fix WebhookClient.CreateAsync array response handling
2 parents c6d30e7 + fee9c90 commit f8fd832

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/GitLabApiClient/GitLabApiClient.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<LangVersion>10</LangVersion>
66
<PackageId>Apiiro.GitLabApiClient</PackageId>
7-
<Version>0.1.44</Version>
7+
<Version>0.1.45</Version>
88
<Authors>Apiiro</Authors>
99
<Company>Apiiro</Company>
1010
<PackageDescription>GitLabApiClient</PackageDescription>

src/GitLabApiClient/WebhookClient.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Threading.Tasks;
34
using GitLabApiClient.Internal.Http;
45
using GitLabApiClient.Internal.Paths;
56
using GitLabApiClient.Models.Projects.Responses;
67
using GitLabApiClient.Models.Webhooks.Requests;
78
using GitLabApiClient.Models.Webhooks.Responses;
9+
using Newtonsoft.Json;
10+
using Newtonsoft.Json.Linq;
811

912
namespace GitLabApiClient
1013
{
@@ -37,13 +40,23 @@ public async Task<IList<Webhook>> GetAsync(ProjectId projectId)
3740
}
3841

3942
/// <summary>
40-
/// Create new webhook
43+
/// Create new webhook.
44+
/// Some GitLab versions return a JSON array from POST /projects/:id/hooks
45+
/// instead of a single object. We deserialize as JToken to handle both formats.
4146
/// </summary>
4247
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
4348
/// <param name="request">Create hook request.</param>
4449
/// <returns>newly created hook</returns>
45-
public async Task<Webhook> CreateAsync(ProjectId projectId, CreateWebhookRequest request) =>
46-
await _httpFacade.Post<Webhook>($"projects/{projectId}/hooks", request);
50+
public async Task<Webhook> CreateAsync(ProjectId projectId, CreateWebhookRequest request)
51+
{
52+
var token = await _httpFacade.Post<JToken>($"projects/{projectId}/hooks", request);
53+
return token switch
54+
{
55+
JArray array => array.FirstOrDefault()?.ToObject<Webhook>(),
56+
JObject obj => obj.ToObject<Webhook>(),
57+
_ => null
58+
};
59+
}
4760

4861
/// <summary>
4962
/// Delete a webhook

0 commit comments

Comments
 (0)