Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr reviewers participants #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Bitbucket.Cloud.Net.Models.v2;

namespace Bitbucket.Cloud.Net.Common.Converters
{
public class ParticipantStateConverter : JsonEnumConverter<ParticipantState>
{
private static readonly Dictionary<ParticipantState, string> s_map = new Dictionary<ParticipantState, string>
{
[ParticipantState.Approved] = "approved",
[ParticipantState.ChangesRequested] = "changes_requested",
[ParticipantState.None] = null
};

protected override string ConvertToString(ParticipantState value)
{
if (!s_map.TryGetValue(value, out string result))
{
throw new ArgumentException($"Unknown participant role: {value}");
}

return result;
}

protected override ParticipantState ConvertFromString(string s)
{
KeyValuePair<ParticipantState, string> pair = new KeyValuePair<ParticipantState, string>();
//Someone who comments on a PR (or completes task, adds task, etc) won't have a participant state.
if (string.IsNullOrEmpty(s))
{
pair = s_map.FirstOrDefault(kvp => string.IsNullOrEmpty(kvp.Value));
}
//Someone who approves or requests changes will have that participant state assocated.
else {
pair = s_map.FirstOrDefault(kvp => kvp.Value.Equals(s, StringComparison.OrdinalIgnoreCase));
}
// ReSharper disable once SuspiciousTypeConversion.Global
if (EqualityComparer<KeyValuePair<ParticipantState, string>>.Default.Equals(pair))
{
throw new ArgumentException($"Unknown participant role: {s}");
}

return pair.Key;
}
}
}
12 changes: 12 additions & 0 deletions src/Bitbucket.Cloud.Net/Models/v2/Participant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,17 @@ public class Participant : User

[JsonProperty("participated_on")]
public DateTime? ParticipatedOn { get; set; }

/// <summary>
/// Used with pull requests, a participant will have a state if they've approved/requested changes on the PR.
/// </summary>
[JsonConverter(typeof(ParticipantStateConverter))]
public ParticipantState State { get; set; }

/// <summary>
/// Despite a Participant being an extension of a user, it has a separate "user" section on it as well.
/// </summary>
[JsonProperty("user")]
public User User { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/Bitbucket.Cloud.Net/Models/v2/ParticipantState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Bitbucket.Cloud.Net.Models.v2
{
public enum ParticipantState
{
Approved,
ChangesRequested,
None
}
}
14 changes: 14 additions & 0 deletions src/Bitbucket.Cloud.Net/Models/v2/PullRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Bitbucket.Cloud.Net.Common.Converters;
using Newtonsoft.Json;

Expand Down Expand Up @@ -40,5 +41,18 @@ public class PullRequest : PullRequestInfo

[JsonProperty("closed_by")]
public object ClosedBy { get; set; }

/// <summary>
/// The participants on the pull request. Anyone who's commented, approved, requested changes, etc.
/// </summary>
[JsonProperty("participants")]
public IEnumerable<Participant> Participants { get; set; }

/// <summary>
/// Reviewers on the pull request. Technically, you can be a participant without being a reviewer, and the "Participant" record can have more information.
/// But to that point, the reviewers can also not hae participated in the PR yet.
/// </summary>
[JsonProperty("reviewers")]
public IEnumerable<Participant> Reviewers { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ public async Task<PullRequest> CreateRepositoryPullRequestAsync(string workspace

public async Task<IEnumerable<PullRequest>> GetRepositoryPullRequestsAsync(string workspaceId, string repositorySlug, int? maxPages = null, string q = null)
{
var queryParamValues = new Dictionary<string, object>
{
[nameof(q)] = q
var queryParamValues = new Dictionary<string, object>
{
[nameof(q)] = q,
//Include the particiapnts and reviewers with the pull request.
["fields"] = "+values.participants,+values.reviewers"
};

return await GetPagedResultsAsync(maxPages, queryParamValues, async qpv =>
Expand Down