Skip to content

Commit

Permalink
Basic operations to work with webhooks (#80)
Browse files Browse the repository at this point in the history
Co-authored-by: Joseph Petersen <josephp90@gmail.com>
  • Loading branch information
MindaugasLaganeckas and jetersen committed Nov 8, 2019
1 parent e49a5c7 commit b6896b3
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/GitLabApiClient/GitLabClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
Branches = new BranchClient(_httpFacade, branchQueryBuilder);
Releases = new ReleaseClient(_httpFacade, releaseQueryBuilder);
Tags = new TagClient(_httpFacade, tagQueryBuilder);
Webhooks = new WebhookClient(_httpFacade);
Commits = new CommitsClient(_httpFacade, commitQueryBuilder, commitRefsQueryBuilder);
Markdown = new MarkdownClient(_httpFacade);
Pipelines = new PipelineClient(_httpFacade, pipelineQueryBuilder);
Expand Down Expand Up @@ -109,6 +110,11 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
/// </summary>
public TagClient Tags { get; }

/// <summary>
/// Access GitLab's webhook API.
/// </summary>
public WebhookClient Webhooks { get; }

/// <summary>
/// Access GitLab's commits API.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using GitLabApiClient.Internal.Utilities;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Webhooks.Requests
{
/// <summary>
/// Used to create a webhook in a project.
/// </summary>
public sealed class CreateWebhookRequest
{
[JsonProperty("url")]
public string Url { get; set; }

[JsonProperty("push_events")]
public bool PushEvents { get; set; }

[JsonProperty("push_events_branch_filter")]
public string PushEventsBranchFilter { get; set; }

[JsonProperty("issues_events")]
public bool IssuesEvents { get; set; }

[JsonProperty("confidential_issues_events")]
public bool ConfidentialIssuesEvents { get; set; }

[JsonProperty("merge_requests_events")]
public bool MergeRequestsEvents { get; set; }

[JsonProperty("tag_push_events")]
public bool TagPushEvents { get; set; }

[JsonProperty("note_events")]
public bool NoteEvents { get; set; }

[JsonProperty("job_events")]
public bool JobEvents { get; set; }

[JsonProperty("pipeline_events")]
public bool PipelineEvents { get; set; }

[JsonProperty("wiki_page_events")]
public bool WikiPageEvents { get; set; }

[JsonProperty("enable_ssl_verification")]
public bool EnableSslVerification { get; set; }

[JsonProperty("token")]
public string Token { get; set; }

public CreateWebhookRequest(string url)
{
Guard.NotEmpty(url, nameof(url));
Url = url;
}
}
}
51 changes: 51 additions & 0 deletions src/GitLabApiClient/Models/Webhooks/Responses/Webhook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Webhooks.Responses
{
public sealed class Webhook
{
[JsonProperty("id")]
public long Id { get; set; }

[JsonProperty("url")]
public string Url { get; set; }

[JsonProperty("push_events")]
public bool PushEvents { get; set; }

[JsonProperty("push_events_branch_filter")]
public string PushEventsBranchFilter { get; set; }

[JsonProperty("issues_events")]
public bool IssuesEvents { get; set; }

[JsonProperty("confidential_issues_events")]
public bool ConfidentialIssuesEvents { get; set; }

[JsonProperty("merge_requests_events")]
public bool MergeRequestsEvents { get; set; }

[JsonProperty("tag_push_events")]
public bool TagPushEvents { get; set; }

[JsonProperty("note_events")]
public bool NoteEvents { get; set; }

[JsonProperty("job_events")]
public bool JobEvents { get; set; }

[JsonProperty("pipeline_events")]
public bool PipelineEvents { get; set; }

[JsonProperty("wiki_page_events")]
public bool WikiPageEvents { get; set; }

[JsonProperty("enable_ssl_verification")]
public bool EnableSslVerification { get; set; }

[JsonProperty("created_at")]
public DateTimeOffset CreatedAt { get; set; }

}
}
60 changes: 60 additions & 0 deletions src/GitLabApiClient/WebhookClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using GitLabApiClient.Internal.Http;
using GitLabApiClient.Internal.Paths;
using GitLabApiClient.Models.Projects.Responses;
using GitLabApiClient.Models.Webhooks.Requests;
using GitLabApiClient.Models.Webhooks.Responses;

namespace GitLabApiClient
{
public sealed class WebhookClient
{
private readonly GitLabHttpFacade _httpFacade;

internal WebhookClient(
GitLabHttpFacade httpFacade)
{
_httpFacade = httpFacade;
}

/// <summary>
/// Retrieves a hook by its id
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="hookId">The hook ID, you want to retrieve.</param>
/// <returns></returns>
public async Task<Webhook> GetAsync(ProjectId projectId, int hookId) =>
await _httpFacade.Get<Webhook>($"projects/{projectId}/hooks/{hookId}");

/// <summary>
/// Retrieves all project hooks
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <returns></returns>
public async Task<IList<Webhook>> GetAsync(ProjectId projectId)
{
return await _httpFacade.GetPagedList<Webhook>($"projects/{projectId}/hooks");
}

/// <summary>
/// Create new webhook
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="request">Create hook request.</param>
/// <returns>newly created hook</returns>
public async Task<Webhook> CreateAsync(ProjectId projectId, CreateWebhookRequest request) =>
await _httpFacade.Post<Webhook>($"projects/{projectId}/hooks", request);

/// <summary>
/// Delete a webhook
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="hookId">The hook ID, you want to delete.</param>
public async Task DeleteAsync(ProjectId projectId, int hookId) =>
await _httpFacade.Delete($"projects/{projectId}/hooks/${hookId}");
}


}

0 comments on commit b6896b3

Please sign in to comment.