add support for organization web hooks (#1884)

This commit is contained in:
Martin Alex Philip Dawson
2020-06-07 20:53:20 +01:00
committed by GitHub
parent 62c0b1fe08
commit 8d3e7b3c2c
23 changed files with 1726 additions and 16 deletions
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using Octokit.Internal;
namespace Octokit
{
/// <summary>
/// Represents the requested changes to an edit repository hook.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class EditOrganizationHook
{
/// <summary>
/// Initializes a new instance of the <see cref="EditOrganizationHook"/> class.
/// </summary>
public EditOrganizationHook() : this(null)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="EditOrganizationHook"/> class.
/// </summary>
/// <param name="config">The configuration.</param>
public EditOrganizationHook(IDictionary<string, string> config)
{
Config = config;
}
public IDictionary<string, string> Config { get; private set; }
/// <summary>
/// Gets or sets the events.
/// </summary>
/// <value>
/// The events.
/// </value>
public IEnumerable<string> Events { get; set; }
/// <summary>
/// Gets or sets the active.
/// </summary>
/// <value>
/// The active.
/// </value>
public bool? Active { get; set; }
internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"Organizaton Hook: Events: {0}", Events == null ? "no" : string.Join(", ", Events));
}
}
}
}
@@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
namespace Octokit
{
/// <summary>
/// Creates a Webhook for the organization.
/// </summary>
/// <remarks>
/// To create a webhook, the following fields are required by the config:
/// <list type="bullet">
/// <item>
/// <term>url</term>
/// <description>A required string defining the URL to which the payloads will be delivered.</description>
/// </item>
/// <item>
/// <term>content_type</term>
/// <description>
/// An optional string defining the media type used to serialize the payloads. Supported values include json and
/// form. The default is form.
/// </description>
/// </item>
/// <item>
/// <term>secret</term>
/// <description>
/// An optional string thats passed with the HTTP requests as an X-Hub-Signature header. The value of this
/// header is computed as the HMAC hex digest of the body, using the secret as the key.
/// </description>
/// </item>
/// <item>
/// <term>insecure_ssl:</term>
/// <description>
/// An optional string that determines whether the SSL certificate of the host for url will be verified when
/// delivering payloads. Supported values include "0" (verification is performed) and "1" (verification is not
/// performed). The default is "0".
/// </description>
/// </item>
/// </list>
/// <para>
/// API: https://developer.github.com/v3/repos/hooks/#create-a-hook
/// </para>
/// </remarks>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class NewOrganizationHook
{
/// <summary>
/// Initializes a new instance of the <see cref="NewOrganizationHook"/> class.
/// </summary>
/// <param name="name">
/// Use "web" for a webhook or use the name of a valid service. (See
/// <see href="https://api.github.com/hooks">https://api.github.com/hooks</see> for the list of valid service
/// names.)
/// </param>
/// <param name="config">
/// Key/value pairs to provide settings for this hook. These settings vary between the services and are
/// defined in the github-services repository. Booleans are stored internally as “1” for true, and “0” for
/// false. Any JSON true/false values will be converted automatically.
/// </param>
public NewOrganizationHook(string name, IReadOnlyDictionary<string, string> config)
{
Name = name;
Config = config;
}
/// <summary>
/// Gets the name of the hook to create. Use "web" for a webhook or use the name of a valid service. (See
/// <see href="https://api.github.com/hooks">https://api.github.com/hooks</see> for the list of valid service
/// names.)
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; private set; }
/// <summary>
/// Key/value pairs to provide settings for this hook. These settings vary between the services and are
/// defined in the github-services repository. Booleans are stored internally as “1” for true, and “0” for
/// false. Any JSON true/false values will be converted automatically.
/// </summary>
/// <value>
/// The configuration.
/// </value>
public IReadOnlyDictionary<string, string> Config { get; protected set; }
/// <summary>
/// Determines what events the hook is triggered for. Default: ["push"]
/// </summary>
/// <value>
/// The events.
/// </value>
public IEnumerable<string> Events { get; set; }
/// <summary>
/// Determines whether the hook is actually triggered on pushes.
/// </summary>
/// <value>
/// <c>true</c> if active; otherwise, <c>false</c>.
/// </value>
public bool Active { get; set; }
public virtual NewOrganizationHook ToRequest()
{
return this;
}
internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"Organization Hook: Name: {0}, Events: {1}", Name, string.Join(", ", Events));
}
}
}
}
@@ -0,0 +1,139 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Octokit
{
/// <summary>
/// Creates a Webhook for the repository.
/// </summary>
/// <remarks>
/// To create a webhook, the following fields are required by the config:
/// <list type="bullet">
/// <item>
/// <term>url</term>
/// <description>A required string defining the URL to which the payloads will be delivered.</description>
/// </item>
/// <item>
/// <term>content_type</term>
/// <description>
/// An optional string defining the media type used to serialize the payloads. Supported values include json and
/// form. The default is form.
/// </description>
/// </item>
/// <item>
/// <term>secret</term>
/// <description>
/// An optional string thats passed with the HTTP requests as an X-Hub-Signature header. The value of this
/// header is computed as the HMAC hex digest of the body, using the secret as the key.
/// </description>
/// </item>
/// <item>
/// <term>insecure_ssl:</term>
/// <description>
/// An optional string that determines whether the SSL certificate of the host for url will be verified when
/// delivering payloads. Supported values include "0" (verification is performed) and "1" (verification is not
/// performed). The default is "0".
/// </description>
/// </item>
/// </list>
/// <para>
/// API: https://developer.github.com/v3/repos/hooks/#create-a-hook
/// </para>
/// </remarks>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class NewOrganizationWebHook : NewOrganizationHook
{
/// <summary>
/// Initializes a new instance of the <see cref="NewOrganizationWebHook"/> class.
/// Using default values for ContentType, Secret and InsecureSsl.
/// </summary>
/// <param name="name">
/// Use "web" for a webhook or use the name of a valid service. (See
/// <see href="https://api.github.com/hooks">https://api.github.com/hooks</see> for the list of valid service
/// names.)
/// </param>
/// <param name="config">
/// Key/value pairs to provide settings for this hook. These settings vary between the services and are
/// defined in the github-services repository. Booleans are stored internally as “1” for true, and “0” for
/// false. Any true/false values will be converted automatically.
/// </param>
/// <param name="url">
/// A required string defining the URL to which the payloads will be delivered.
/// </param>
public NewOrganizationWebHook(string name, IReadOnlyDictionary<string, string> config, string url)
: base(name, config)
{
Ensure.ArgumentNotNullOrEmptyString(url, "url");
Url = url;
ContentType = OrgWebHookContentType.Form;
Secret = "";
InsecureSsl = false;
}
/// <summary>
/// Gets the URL of the hook to create.
/// </summary>
/// <value>
/// The URL.
/// </value>
public string Url { get; protected set; }
/// <summary>
/// Gets the content type used to serialize the payload. The default is `form`.
/// </summary>
/// <value>
/// The content type.
/// </value>
public OrgWebHookContentType ContentType { get; set; }
/// <summary>
/// Gets the secret used as the key for the HMAC hex digest
/// of the body passed with the HTTP requests as an X-Hub-Signature header.
/// </summary>
/// <value>
/// The secret.
/// </value>
public string Secret { get; set; }
/// <summary>
/// Gets whether the SSL certificate of the host will be verified when
/// delivering payloads. The default is `false`.
/// </summary>
/// <value>
/// <c>true</c> if SSL certificate verification is not performed;
/// otherwise, <c>false</c>.
/// </value>
public bool InsecureSsl { get; set; }
public override NewOrganizationHook ToRequest()
{
Config = GetWebHookConfig()
.Union(Config, new WebHookConfigComparer())
.ToDictionary(k => k.Key, v => v.Value);
return this;
}
Dictionary<string, string> GetWebHookConfig()
{
return new Dictionary<string, string>
{
{ "url", Url },
{ "content_type", ContentType.ToParameter() },
{ "secret", Secret },
{ "insecure_ssl", InsecureSsl.ToString() }
};
}
}
/// <summary>
/// The supported content types for payload serialization.
/// </summary>
public enum OrgWebHookContentType
{
Form,
Json
}
}