Skip to content

Add CreateForkAsync for project #262

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

Open
wants to merge 2 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
9 changes: 5 additions & 4 deletions src/GitLabApiClient/BranchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GitLabApiClient.Internal.Http;
using GitLabApiClient.Internal.Paths;
using GitLabApiClient.Internal.Queries;
using GitLabApiClient.Internal.Utilities;
using GitLabApiClient.Models.Branches.Requests;
using GitLabApiClient.Models.Branches.Responses;
using GitLabApiClient.Models.Projects.Responses;
Expand All @@ -30,7 +31,7 @@ internal BranchClient(
/// <param name="branchName">The branch name.</param>
/// <returns></returns>
public async Task<Branch> GetAsync(ProjectId projectId, string branchName) =>
await _httpFacade.Get<Branch>($"projects/{projectId}/repository/branches/{branchName}");
await _httpFacade.Get<Branch>($"projects/{projectId}/repository/branches/{branchName.UrlEncode()}");

/// <summary>
///
Expand Down Expand Up @@ -62,7 +63,7 @@ public async Task<Branch> CreateAsync(ProjectId projectId, CreateBranchRequest r
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="branchName">The branch, you want deleted.</param>
public async Task DeleteBranch(ProjectId projectId, string branchName) =>
await _httpFacade.Delete($"projects/{projectId}/repository/branches/{branchName}");
await _httpFacade.Delete($"projects/{projectId}/repository/branches/{branchName.UrlEncode()}");

/// <summary>
/// Deletes the merged branches
Expand All @@ -78,7 +79,7 @@ public async Task DeleteMergedBranches(ProjectId projectId) =>
/// <param name="branchName">The protected branch</param>
/// <returns>A protected branch</returns>
public async Task<ProtectedBranch> GetProtectedBranchesAsync(ProjectId projectId, string branchName) =>
await _httpFacade.Get<ProtectedBranch>($"projects/{projectId}/protected_branches/{branchName}");
await _httpFacade.Get<ProtectedBranch>($"projects/{projectId}/protected_branches/{branchName.UrlEncode()}");

/// <summary>
/// Retrieves a list of Protected Branches from a project.
Expand All @@ -103,6 +104,6 @@ public async Task<ProtectedBranch> ProtectBranchAsync(ProjectId projectId, Prote
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="branchName">The Branch, you want to unprotect.</param>
public async Task UnprotectBranchAsync(ProjectId projectId, string branchName) =>
await _httpFacade.Delete($"projects/{projectId}/protected_branches/{branchName}");
await _httpFacade.Delete($"projects/{projectId}/protected_branches/{branchName.UrlEncode()}");
}
}
8 changes: 8 additions & 0 deletions src/GitLabApiClient/IProjectsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ public interface IProjectsClient
/// <returns>Newly created project.</returns>
Task<Project> CreateAsync(CreateProjectRequest request);

/// <summary>
/// Create a new fork of a project
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="request">Create fork request</param>
/// <returns></returns>
Task<Project> CreateForkAsync(ProjectId projectId, CreateProjectForkRequest request);

/// <summary>
/// Creates new project label.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using GitLabApiClient.Internal.Utilities;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Projects.Requests
{
public sealed class CreateProjectForkRequest
{

/// <summary>
/// Initializes a new instance of the <see cref="CreateProjectRequest"/> class.
/// <param name="name">The name of the new project.</param>
/// </summary>
public static CreateProjectForkRequest FromName(string name)
{
Guard.NotEmpty(name, nameof(name));
return new CreateProjectForkRequest
{
Name = name,
Path = name.ToLower()
};
}

private CreateProjectForkRequest() { }

/// <summary>
/// The description assigned to the resultant project after forking
/// </summary>
[JsonProperty("description")]
public string Description { get; set; }

/// <summary>
/// For forked projects, target merge requests to this project. If <see cref="false"/>, the target is the upstream project
/// </summary>
[JsonProperty("mr_default_target_self")]
public bool DefaultTargetSelf { get; set; }

/// <summary>
/// The name assigned to the resultant project after forking
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }

/// <summary>
/// The ID of the namespace that the project is forked to
/// </summary>
[JsonProperty("namespace_id")]
public int NamespaceId { get; set; }

/// <summary>
/// The path of the namespace that the project is forked to
/// </summary>
[JsonProperty("namespace_path")]
public int NamespacePath { get; set; }

/// <summary>
/// The path assigned to the resultant project after forking
/// </summary>
[JsonProperty("path")]
public string Path { get; set; }

/// <summary>
/// The <see cref="ProjectVisibilityLevel"/> assigned to the resultant project after forking
/// </summary>
[JsonProperty("visibility")]
public ProjectVisibilityLevel? Visibility { get; set; }

}
}
13 changes: 13 additions & 0 deletions src/GitLabApiClient/ProjectsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ public async Task<Project> CreateAsync(CreateProjectRequest request)
return await _httpFacade.Post<Project>("projects", request);
}

/// <summary>
/// Create a new fork of a project
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project" /> of the project.</param>
/// <param name="request">Create fork request</param>
/// <returns>Newly created project.
/// The forking operation for a project is asynchronous and is completed in a background job. The request returns immediately. To determine whether the fork of the project has completed, query the import_status for the new project.</returns>
public async Task<Project> CreateForkAsync(ProjectId projectId, CreateProjectForkRequest request)
{
Guard.NotNull(request, nameof(request));
return await _httpFacade.Post<Project>($"projects/{projectId}/fork", request);
}

/// <summary>
/// Creates new project label.
/// </summary>
Expand Down