|
1 | 1 | using System.Collections.Generic; |
| 2 | +using System.Linq; |
2 | 3 | using System.Threading.Tasks; |
3 | 4 | using GitLabApiClient.Internal.Http; |
4 | 5 | using GitLabApiClient.Internal.Paths; |
5 | 6 | using GitLabApiClient.Models.Projects.Responses; |
6 | 7 | using GitLabApiClient.Models.Webhooks.Requests; |
7 | 8 | using GitLabApiClient.Models.Webhooks.Responses; |
| 9 | +using Newtonsoft.Json; |
| 10 | +using Newtonsoft.Json.Linq; |
8 | 11 |
|
9 | 12 | namespace GitLabApiClient |
10 | 13 | { |
@@ -37,13 +40,23 @@ public async Task<IList<Webhook>> GetAsync(ProjectId projectId) |
37 | 40 | } |
38 | 41 |
|
39 | 42 | /// <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. |
41 | 46 | /// </summary> |
42 | 47 | /// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param> |
43 | 48 | /// <param name="request">Create hook request.</param> |
44 | 49 | /// <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 | + } |
47 | 60 |
|
48 | 61 | /// <summary> |
49 | 62 | /// Delete a webhook |
|
0 commit comments