Files
octokit.net/Octokit/Models/Response/BranchProtection.cs
Kristian Hellang 5ee4d64046 Add StringEnum to handle unknown enum values returned from API (#1595)
* Added StringEnum<TEnum>

* Added tests

* Make sure the serializer can work with StringEnum

* Use StringEnum for EventInfo.Event

* Add convention test to assert that all Response models use StringEnum<> to wrap enum properties

* Add Stringnum<> to all response types failing convention test

* Handle StringEnum to Enum conversion when Issue response model populates IssueUpdate request model

* Fix unit test

* Refactor SimpleJsonSerializer to expose the DeserializeEnum strategy so it can be used in StringEnum class

* Need to expose/use SerializeEnum functionality too, so we use the correct string representation of enum values that have custom properties (eg ReactionType Plus1 to "+1")

* fix unit tests, since the string is now the "correct" upstream api value

* Add a couple of tests for the Enum serialize/deserialize when underscores, hyphens and custom property attributes are present

* Compare parsed values for equality

* add convention test to ensure enum members all have Parameter property set

* update test to cover implicit conversions too

* this test should work but fails at the moment due to magic hyphen removal in deserializer causing a one way trip from utf-8 to EncodingType.Utf8 with no way to get back

* (unsuccesfully) expand event info test to try to catch more cases of unknown event types

* fix broken integration test while im here

* Fixed build errors after .NET Core merge

* Value -> StringValue, ParsedValue -> Value

* Don't allow StringValue to be null

* Ignore enums not used in request/response models

* Added ParameterAttribute to almost all enum values

* Ignore Language enum

* Fix failing tests

* Fix milestone sort parameter and tests

* whitespace

* fix milestone unit tests

* Fix StringEnum.Equals ... This could've been embarrassing!

* Change SimpleJsonSerializer Enum handling to only use `[Parameter()]` attributes (no more magic removal of hyphen/underscores from strings)

* Tidy up this integration test while im here

* Only test request/response enums in convention test

* Keep skipping Language

* Remove unused method

* Remove excluded enum types

* Removed unnecessary ParameterAttributes

* Remove unused enum

* Add StringEnum test for string-comparison of two invalid values

* Bring back IssueCommentSort and use it in IssueCommentRequest

This reverts commit 38a4a291d1476ef8c992fe0f76956974b6f32a49.

* Use assembly instead of namespace for Octokit check

* Add failing test to reproduce the issue where only the first enum paramter/value was added to the cache

* Fix deserializer enum cache to include all enum members rather than only the first member encountered

* Use a static SimpleJsonSerializer in StringEnum

* Remove serializer instance in StringEnum

* Add some documentation on StringEnum<TEnum>

* Fix parameter value to resolve failing integration test
2017-06-25 19:29:57 +10:00

237 lines
7.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using Octokit.Internal;
namespace Octokit
{
/// <summary>
/// Protection details for a <see cref="Branch"/>.
/// </summary>
/// <remarks>
/// Note: this is a PREVIEW api: https://developer.github.com/changes/2015-11-11-protected-branches-api/
/// </remarks>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
[Obsolete("This existing implementation will cease to work when the Branch Protection API preview period ends. Please see BranchProtectionSettings instead.")]
public class BranchProtection
{
public BranchProtection() { }
public BranchProtection(bool enabled, RequiredStatusChecks requiredStatusChecks)
{
Enabled = enabled;
RequiredStatusChecks = requiredStatusChecks;
}
/// <summary>
/// Should this branch be protected or not
/// </summary>
public bool Enabled { get; protected set; }
/// <summary>
/// The <see cref="RequiredStatusChecks"/> information for this <see cref="Branch"/>.
/// </summary>
public RequiredStatusChecks RequiredStatusChecks { get; private set; }
internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "Enabled: {0}", Enabled);
}
}
}
[DebuggerDisplay("{DebuggerDisplay,nq}")]
[Obsolete("This existing implementation will cease to work when the Branch Protection API preview period ends. Please see BranchProtectionRequiredStatusChecks instead.")]
public class RequiredStatusChecks
{
public RequiredStatusChecks() { }
public RequiredStatusChecks(EnforcementLevel enforcementLevel, IEnumerable<string> contexts)
{
EnforcementLevel = enforcementLevel;
Contexts = new ReadOnlyCollection<string>(contexts.ToList());
}
/// <summary>
/// Who required status checks apply to
/// </summary>
public StringEnum<EnforcementLevel> EnforcementLevel { get; protected set; }
/// <summary>
/// The list of status checks to require in order to merge into this <see cref="Branch"/>
/// </summary>
public IReadOnlyList<string> Contexts { get; private set; }
internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "EnforcementLevel: {0} Contexts: {1}", EnforcementLevel.ToString(), Contexts.Count);
}
}
}
/// <summary>
/// The enforcement levels that are available
/// </summary>
[Obsolete("This existing implementation will cease to work when the Branch Protection API preview period ends. Please see BranchProtection.EnforceAdmins instead.")]
public enum EnforcementLevel
{
/// <summary>
/// Turn off required status checks for this <see cref="Branch"/>.
/// </summary>
[Parameter(Value = "off")]
Off,
/// <summary>
/// Required status checks will be enforced for non-admins.
/// </summary>
[Parameter(Value = "non_admins")]
NonAdmins,
/// <summary>
/// Required status checks will be enforced for everyone (including admins).
/// </summary>
[Parameter(Value = "everyone")]
Everyone
}
/// <summary>
/// Protection details for a <see cref="Branch"/>.
/// </summary>
/// <remarks>
/// Note: this is a PREVIEW api: https://developer.github.com/changes/2016-06-27-protected-branches-api-update/
/// </remarks>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class BranchProtectionSettings
{
public BranchProtectionSettings() { }
public BranchProtectionSettings(BranchProtectionRequiredStatusChecks requiredStatusChecks, BranchProtectionPushRestrictions restrictions)
{
RequiredStatusChecks = requiredStatusChecks;
Restrictions = restrictions;
}
/// <summary>
/// Status check settings for the protected branch
/// </summary>
public BranchProtectionRequiredStatusChecks RequiredStatusChecks { get; protected set; }
/// <summary>
/// Push access restrictions for the protected branch
/// </summary>
public BranchProtectionPushRestrictions Restrictions { get; protected set; }
internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"StatusChecks: {0} Restrictions: {1}",
RequiredStatusChecks == null ? "disabled" : RequiredStatusChecks.DebuggerDisplay,
Restrictions == null ? "disabled" : Restrictions.DebuggerDisplay);
}
}
/// <summary>
/// Specifies whether the protections applied to this branch also apply to repository admins
/// </summary>
public EnforceAdmins EnforceAdmins { get; protected set; }
}
/// <summary>
/// Specifies whether the protections applied to this branch also apply to repository admins
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class EnforceAdmins
{
public bool Enabled { get; protected set; }
internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "Enabled: {0}", Enabled);
}
}
}
/// <summary>
/// Specifies settings for status checks which must pass before branches can be merged into the protected branch
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class BranchProtectionRequiredStatusChecks
{
public BranchProtectionRequiredStatusChecks() { }
public BranchProtectionRequiredStatusChecks(bool strict, IReadOnlyList<string> contexts)
{
Strict = strict;
Contexts = contexts;
}
/// <summary>
/// Require branches to be up to date before merging
/// </summary>
public bool Strict { get; protected set; }
/// <summary>
/// Require status checks to pass before merging
/// </summary>
public IReadOnlyList<string> Contexts { get; private set; }
internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"Strict: {0} Contexts: {1}",
Strict,
Contexts == null ? "" : string.Join(",", Contexts));
}
}
}
/// <summary>
/// Specifies people or teams allowed to push to the protected branch. Required status checks will still prevent these people from merging if the checks fail
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class BranchProtectionPushRestrictions
{
public BranchProtectionPushRestrictions() { }
public BranchProtectionPushRestrictions(IReadOnlyList<Team> teams, IReadOnlyList<User> users)
{
Teams = teams;
Users = users;
}
/// <summary>
/// Push access is restricted to the specified Teams
/// </summary>
public IReadOnlyList<Team> Teams { get; private set; }
/// <summary>
/// Push access is restricted to the specified Users
/// </summary>
public IReadOnlyList<User> Users { get; private set; }
internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"Teams: {0} Users: {1}",
Teams == null ? "" : String.Join(",", Teams),
Users == null ? "" : String.Join(",", Users));
}
}
}
}