Skip to content

Commit

Permalink
Get Group MergeRequests (#620)
Browse files Browse the repository at this point in the history
* Get Group MergeRequests

* Fix analyzers errors

* Fix comments

* Removed constructors/methods with int parameter for groupId

* Fix CI

* Fix CI

* Removed useless method
  • Loading branch information
Yekonori authored Jan 26, 2024
1 parent f24e5b4 commit 51d2f9f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 28 deletions.
2 changes: 2 additions & 0 deletions NGitLab.Mock/Clients/GitLabClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public IGraphQLClient GraphQL

public IMergeRequestClient GetMergeRequest(ProjectId projectId) => new MergeRequestClient(Context, projectId);

public IMergeRequestClient GetGroupMergeRequest(GroupId groupId) => new MergeRequestClient(Context, groupId);

public IMilestoneClient GetMilestone(int projectId) => GetMilestone((long)projectId);

public IMilestoneClient GetMilestone(ProjectId projectId) => new MilestoneClient(Context, projectId, MilestoneScope.Projects);
Expand Down
6 changes: 6 additions & 0 deletions NGitLab.Mock/Clients/MergeRequestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public MergeRequestClient(ClientContext context, ProjectId projectId)
_projectId = Server.AllProjects.FindProject(projectId.ValueAsUriParameter()).Id;
}

public MergeRequestClient(ClientContext context, GroupId groupId)
: base(context)
{
throw new NotImplementedException();
}

private void AssertProjectId()
{
if (_projectId == null)
Expand Down
3 changes: 3 additions & 0 deletions NGitLab/GitLabClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ public IMergeRequestClient GetMergeRequest(int projectId)
public IMergeRequestClient GetMergeRequest(ProjectId projectId)
=> new MergeRequestClient(_api, projectId);

public IMergeRequestClient GetGroupMergeRequest(GroupId groupId)
=> new MergeRequestClient(_api, groupId);

public IMilestoneClient GetMilestone(int projectId)
=> GetMilestone((long)projectId);

Expand Down
2 changes: 2 additions & 0 deletions NGitLab/IGitLabClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public interface IGitLabClient

IMergeRequestClient GetMergeRequest(ProjectId projectId);

IMergeRequestClient GetGroupMergeRequest(GroupId groupId);

[EditorBrowsable(EditorBrowsableState.Never)]
IMilestoneClient GetMilestone(int projectId);

Expand Down
62 changes: 34 additions & 28 deletions NGitLab/Impl/MergeRequestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,31 @@ public class MergeRequestClient : IMergeRequestClient
private const string ResourceLabelEventUrl = "/projects/{0}/merge_requests/{1}/resource_label_events";
private const string ResourceMilestoneEventUrl = "/projects/{0}/merge_requests/{1}/resource_milestone_events";
private const string ResourceStateEventUrl = "/projects/{0}/merge_requests/{1}/resource_state_events";
private readonly string _projectPath;
private readonly string _path;
private readonly API _api;

[EditorBrowsable(EditorBrowsableState.Never)]
public MergeRequestClient(API api, int projectId)
: this(api, (long)projectId)
: this(api, projectId: (long)projectId)
{
}

public MergeRequestClient(API api, ProjectId projectId)
{
_api = api;
_projectPath = $"{Project.Url}/{projectId.ValueAsUriParameter()}";
_path = $"{Project.Url}/{projectId.ValueAsUriParameter()}";
}

public MergeRequestClient(API api, GroupId groupId)
{
_api = api;
_path = $"{Group.Url}/{groupId.ValueAsUriParameter()}";
}

public MergeRequestClient(API api)
{
_api = api;
_projectPath = string.Empty;
_path = string.Empty;
}

public IEnumerable<MergeRequest> All => Get(new MergeRequestQuery());
Expand All @@ -41,7 +47,7 @@ public MergeRequestClient(API api)

public IEnumerable<MergeRequest> Get(MergeRequestQuery query)
{
var url = _projectPath + MergeRequest.Url;
var url = _path + MergeRequest.Url;

url = Utils.AddParameter(url, "state", query.State);
url = Utils.AddParameter(url, "order_by", query.OrderBy);
Expand Down Expand Up @@ -71,7 +77,7 @@ public MergeRequest this[int iid]
{
get
{
var url = $"{_projectPath}{MergeRequest.Url}/{iid.ToStringInvariant()}";
var url = $"{_path}{MergeRequest.Url}/{iid.ToStringInvariant()}";
url = Utils.AddParameter(url, "include_rebase_in_progress", value: true);
url = Utils.AddParameter(url, "include_diverged_commits_count", value: true);

Expand All @@ -81,7 +87,7 @@ public MergeRequest this[int iid]

public Task<MergeRequest> GetByIidAsync(int iid, SingleMergeRequestQuery options, CancellationToken cancellationToken = default)
{
var url = $"{_projectPath}{MergeRequest.Url}/{iid.ToStringInvariant()}";
var url = $"{_path}{MergeRequest.Url}/{iid.ToStringInvariant()}";
if (options != null)
{
url = Utils.AddParameter(url, "include_rebase_in_progress", options.IncludeRebaseInProgress);
Expand All @@ -99,83 +105,83 @@ public MergeRequest Create(MergeRequestCreate mergeRequest)

return _api
.Post().With(mergeRequest)
.To<MergeRequest>(_projectPath + "/merge_requests");
.To<MergeRequest>(_path + "/merge_requests");
}

public MergeRequest Update(int mergeRequestIid, MergeRequestUpdate mergeRequest) => _api
.Put().With(mergeRequest)
.To<MergeRequest>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture));
.To<MergeRequest>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture));

public MergeRequest Close(int mergeRequestIid) => _api
.Put().With(new MergeRequestUpdateState { NewState = nameof(MergeRequestStateEvent.close) })
.To<MergeRequest>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture));
.To<MergeRequest>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture));

public MergeRequest Reopen(int mergeRequestIid) => _api
.Put().With(new MergeRequestUpdateState { NewState = nameof(MergeRequestStateEvent.reopen) })
.To<MergeRequest>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture));
.To<MergeRequest>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture));

public void Delete(int mergeRequestIid) => _api
.Delete()
.Execute(_projectPath + "/merge_requests/" + mergeRequestIid.ToStringInvariant());
.Execute(_path + "/merge_requests/" + mergeRequestIid.ToStringInvariant());

public MergeRequest CancelMergeWhenPipelineSucceeds(int mergeRequestIid) => _api
.Post()
.To<MergeRequest>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/cancel_merge_when_pipeline_succeeds");
.To<MergeRequest>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/cancel_merge_when_pipeline_succeeds");

public MergeRequest Accept(int mergeRequestIid, MergeRequestAccept message) => _api
.Put().With(message)
.To<MergeRequest>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/merge");
.To<MergeRequest>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/merge");

public MergeRequest Accept(int mergeRequestIid, MergeRequestMerge message) => _api
.Put().With(message)
.To<MergeRequest>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/merge");
.To<MergeRequest>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/merge");

public MergeRequest Approve(int mergeRequestIid, MergeRequestApprove message) => _api
.Post().With(message)
.To<MergeRequest>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/approve");
.To<MergeRequest>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/approve");

public RebaseResult Rebase(int mergeRequestIid) => _api
.Put()
.To<RebaseResult>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/rebase");
.To<RebaseResult>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/rebase");

public Task<RebaseResult> RebaseAsync(int mergeRequestIid, MergeRequestRebase options, CancellationToken cancellationToken = default) => _api
.Put().With(options)
.ToAsync<RebaseResult>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/rebase", cancellationToken);
.ToAsync<RebaseResult>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/rebase", cancellationToken);

public IEnumerable<PipelineBasic> GetPipelines(int mergeRequestIid)
{
return _api.Get().GetAll<PipelineBasic>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/pipelines");
return _api.Get().GetAll<PipelineBasic>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/pipelines");
}

public IEnumerable<Author> GetParticipants(int mergeRequestIid)
{
return _api.Get().GetAll<Author>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/participants");
return _api.Get().GetAll<Author>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/participants");
}

public IEnumerable<Issue> ClosesIssues(int mergeRequestIid)
{
return _api.Get().GetAll<Issue>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/closes_issues");
return _api.Get().GetAll<Issue>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/closes_issues");
}

public GitLabCollectionResponse<MergeRequestVersion> GetVersionsAsync(int mergeRequestIid)
{
return _api.Get().GetAllAsync<MergeRequestVersion>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/versions");
return _api.Get().GetAllAsync<MergeRequestVersion>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/versions");
}

public Task<TimeStats> TimeStatsAsync(int mergeRequestIid, CancellationToken cancellationToken = default)
{
return _api.Get().ToAsync<TimeStats>(_projectPath + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/time_stats", cancellationToken);
return _api.Get().ToAsync<TimeStats>(_path + "/merge_requests/" + mergeRequestIid.ToString(CultureInfo.InvariantCulture) + "/time_stats", cancellationToken);
}

public IMergeRequestCommentClient Comments(int mergeRequestIid) => new MergeRequestCommentClient(_api, _projectPath, mergeRequestIid);
public IMergeRequestCommentClient Comments(int mergeRequestIid) => new MergeRequestCommentClient(_api, _path, mergeRequestIid);

public IMergeRequestDiscussionClient Discussions(int mergeRequestIid) => new MergeRequestDiscussionClient(_api, _projectPath, mergeRequestIid);
public IMergeRequestDiscussionClient Discussions(int mergeRequestIid) => new MergeRequestDiscussionClient(_api, _path, mergeRequestIid);

public IMergeRequestCommitClient Commits(int mergeRequestIid) => new MergeRequestCommitClient(_api, _projectPath, mergeRequestIid);
public IMergeRequestCommitClient Commits(int mergeRequestIid) => new MergeRequestCommitClient(_api, _path, mergeRequestIid);

public IMergeRequestApprovalClient ApprovalClient(int mergeRequestIid) => new MergeRequestApprovalClient(_api, _projectPath, mergeRequestIid);
public IMergeRequestApprovalClient ApprovalClient(int mergeRequestIid) => new MergeRequestApprovalClient(_api, _path, mergeRequestIid);

public IMergeRequestChangeClient Changes(int mergeRequestIid) => new MergeRequestChangeClient(_api, _projectPath, mergeRequestIid);
public IMergeRequestChangeClient Changes(int mergeRequestIid) => new MergeRequestChangeClient(_api, _path, mergeRequestIid);

public GitLabCollectionResponse<ResourceLabelEvent> ResourceLabelEventsAsync(int projectId, int mergeRequestIid)
{
Expand Down
3 changes: 3 additions & 0 deletions NGitLab/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ NGitLab.GitLabClient.GetEnvironmentClient(NGitLab.Models.ProjectId projectId) ->
NGitLab.GitLabClient.GetEvents() -> NGitLab.IEventClient
NGitLab.GitLabClient.GetGroupBadgeClient(int groupId) -> NGitLab.IGroupBadgeClient
NGitLab.GitLabClient.GetGroupBadgeClient(NGitLab.Models.GroupId groupId) -> NGitLab.IGroupBadgeClient
NGitLab.GitLabClient.GetGroupMergeRequest(NGitLab.Models.GroupId groupId) -> NGitLab.IMergeRequestClient
NGitLab.GitLabClient.GetGroupMilestone(int groupId) -> NGitLab.IMilestoneClient
NGitLab.GitLabClient.GetGroupMilestone(NGitLab.Models.GroupId groupId) -> NGitLab.IMilestoneClient
NGitLab.GitLabClient.GetGroupSearchClient(int groupId) -> NGitLab.ISearchClient
Expand Down Expand Up @@ -195,6 +196,7 @@ NGitLab.IGitLabClient.GetEnvironmentClient(NGitLab.Models.ProjectId projectId) -
NGitLab.IGitLabClient.GetEvents() -> NGitLab.IEventClient
NGitLab.IGitLabClient.GetGroupBadgeClient(int groupId) -> NGitLab.IGroupBadgeClient
NGitLab.IGitLabClient.GetGroupBadgeClient(NGitLab.Models.GroupId groupId) -> NGitLab.IGroupBadgeClient
NGitLab.IGitLabClient.GetGroupMergeRequest(NGitLab.Models.GroupId groupId) -> NGitLab.IMergeRequestClient
NGitLab.IGitLabClient.GetGroupMilestone(int groupId) -> NGitLab.IMilestoneClient
NGitLab.IGitLabClient.GetGroupMilestone(NGitLab.Models.GroupId groupId) -> NGitLab.IMilestoneClient
NGitLab.IGitLabClient.GetGroupSearchClient(int groupId) -> NGitLab.ISearchClient
Expand Down Expand Up @@ -672,6 +674,7 @@ NGitLab.Impl.MergeRequestClient.GetPipelines(int mergeRequestIid) -> System.Coll
NGitLab.Impl.MergeRequestClient.GetVersionsAsync(int mergeRequestIid) -> NGitLab.GitLabCollectionResponse<NGitLab.Models.MergeRequestVersion>
NGitLab.Impl.MergeRequestClient.MergeRequestClient(NGitLab.Impl.API api) -> void
NGitLab.Impl.MergeRequestClient.MergeRequestClient(NGitLab.Impl.API api, int projectId) -> void
NGitLab.Impl.MergeRequestClient.MergeRequestClient(NGitLab.Impl.API api, NGitLab.Models.GroupId groupId) -> void
NGitLab.Impl.MergeRequestClient.MergeRequestClient(NGitLab.Impl.API api, NGitLab.Models.ProjectId projectId) -> void
NGitLab.Impl.MergeRequestClient.Rebase(int mergeRequestIid) -> NGitLab.Models.RebaseResult
NGitLab.Impl.MergeRequestClient.RebaseAsync(int mergeRequestIid, NGitLab.Models.MergeRequestRebase options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<NGitLab.Models.RebaseResult>
Expand Down

0 comments on commit 51d2f9f

Please sign in to comment.