Skip to content

Commit

Permalink
[FEAT] add Triage/Maintain permission (#2467)
Browse files Browse the repository at this point in the history
  • Loading branch information
janv8000 authored Jul 11, 2022
1 parent 73b7a7a commit f92f0b8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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)));
}
}
}
29 changes: 28 additions & 1 deletion Octokit.Tests.Conventions/ModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void AllResponseModelsHavePublicCtorWithAllProperties(Type modelType)

[Theory]
[MemberData(nameof(ResponseModelTypes))]
public void ResponseModelsHaveGetterOnlyProperties(Type modelType)
public void ResponseModelsHaveNoPublicSettableProperties(Type modelType)
{
var mutableProperties = new List<PropertyInfo>();

Expand All @@ -112,6 +112,33 @@ public void ResponseModelsHaveGetterOnlyProperties(Type modelType)
}
}

[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]
[MemberData(nameof(ResponseModelTypes))]
public void ResponseModelsHaveReadOnlyCollections(Type modelType)
Expand Down
27 changes: 21 additions & 6 deletions Octokit/Models/Response/RepositoryPermissions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Diagnostics;
using System.Globalization;
using System;

namespace Octokit
{
Expand All @@ -8,31 +8,46 @@ public class 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;
Maintain = maintain;
Push = push;
Triage = triage;
Pull = pull;
}

/// <summary>
/// Whether the current user has administrative permissions
/// </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>
/// Whether the current user has push permissions
/// </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>
/// Whether the current user has pull permissions
/// </summary>
public bool Pull { get; protected set; }
public bool Pull { get; private set;}

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}");
}
}
}
}

0 comments on commit f92f0b8

Please sign in to comment.