[FEAT] add Triage/Maintain permission (#2467)

This commit is contained in:
Jan Verhaeghe
2022-07-11 16:12:20 +02:00
committed by GitHub
parent 73b7a7a18c
commit f92f0b8194
3 changed files with 71 additions and 7 deletions

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Octokit.Tests.Conventions
{
public class ResponseModelSetterlessAutoPropertyException : Exception
{
public ResponseModelSetterlessAutoPropertyException(Type modelType, IEnumerable<PropertyInfo> setterlessProperties)
: base(CreateMessage(modelType, setterlessProperties))
{ }
static string CreateMessage(Type modelType, IEnumerable<PropertyInfo> setterlessProperties)
{
return string.Format("Model type '{0}' contains the following setterless properties: {1}{2}",
modelType.FullName,
Environment.NewLine,
string.Join(Environment.NewLine, setterlessProperties.Select(x => x.Name)));
}
}
}

View File

@@ -90,7 +90,7 @@ namespace Octokit.Tests.Conventions
[Theory] [Theory]
[MemberData(nameof(ResponseModelTypes))] [MemberData(nameof(ResponseModelTypes))]
public void ResponseModelsHaveGetterOnlyProperties(Type modelType) public void ResponseModelsHaveNoPublicSettableProperties(Type modelType)
{ {
var mutableProperties = new List<PropertyInfo>(); var mutableProperties = new List<PropertyInfo>();
@@ -112,6 +112,33 @@ namespace Octokit.Tests.Conventions
} }
} }
[Theory]
[MemberData(nameof(ResponseModelTypes))]
public void ResponseModelsHaveNoSetterlessAutoPropertiesForReflection(Type modelType)
{
var setterlessAutoProperties = new List<PropertyInfo>();
foreach (var property in modelType.GetProperties())
{
var propertyHasNoSetter = property.GetSetMethod(true) is null;
if (IsAutoProperty(property) && propertyHasNoSetter)
{
setterlessAutoProperties.Add(property);
}
}
if (setterlessAutoProperties.Any())
{
throw new ResponseModelSetterlessAutoPropertyException(modelType, setterlessAutoProperties);
}
}
private bool IsAutoProperty(PropertyInfo prop)
{
return prop.DeclaringType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.Any(f => f.Name.Contains("<" + prop.Name + ">"));
}
[Theory] [Theory]
[MemberData(nameof(ResponseModelTypes))] [MemberData(nameof(ResponseModelTypes))]
public void ResponseModelsHaveReadOnlyCollections(Type modelType) public void ResponseModelsHaveReadOnlyCollections(Type modelType)

View File

@@ -1,5 +1,5 @@
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System;
namespace Octokit namespace Octokit
{ {
@@ -8,31 +8,46 @@ namespace Octokit
{ {
public RepositoryPermissions() { } public RepositoryPermissions() { }
public RepositoryPermissions(bool admin, bool push, bool pull) public RepositoryPermissions(bool admin, bool maintain, bool push, bool triage, bool pull)
{ {
Admin = admin; Admin = admin;
Maintain = maintain;
Push = push; Push = push;
Triage = triage;
Pull = pull; Pull = pull;
} }
/// <summary> /// <summary>
/// Whether the current user has administrative permissions /// Whether the current user has administrative permissions
/// </summary> /// </summary>
public bool Admin { get; protected set; } public bool Admin { get; private set;}
/// <summary>
/// Whether the current user has maintain permissions
/// </summary>
public bool Maintain { get; private set;}
/// <summary> /// <summary>
/// Whether the current user has push permissions /// Whether the current user has push permissions
/// </summary> /// </summary>
public bool Push { get; protected set; } public bool Push { get; private set;}
/// <summary>
/// Whether the current user has triage permissions
/// </summary>
public bool Triage { get; private set;}
/// <summary> /// <summary>
/// Whether the current user has pull permissions /// Whether the current user has pull permissions
/// </summary> /// </summary>
public bool Pull { get; protected set; } public bool Pull { get; private set;}
internal string DebuggerDisplay internal string DebuggerDisplay
{ {
get { return string.Format(CultureInfo.InvariantCulture, "Admin: {0}, Push: {1}, Pull: {2}", Admin, Push, Pull); } get
{
return FormattableString.Invariant($"Admin: {Admin}, Maintain: {Maintain}, Push: {Push}, Triage: {Triage}, Pull: {Pull}");
}
} }
} }
} }