Skip to content

Add Commits Create request #214

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 5 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
1 change: 0 additions & 1 deletion src/GitLabApiClient/CommitsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using GitLabApiClient.Internal.Paths;
using GitLabApiClient.Internal.Queries;
using GitLabApiClient.Models.Commits.Requests;
using GitLabApiClient.Models.Commits.Requests.CreateCommitRequest;
using GitLabApiClient.Models.Commits.Responses;
using GitLabApiClient.Models.Projects.Responses;

Expand Down
128 changes: 128 additions & 0 deletions src/GitLabApiClient/CommitsClient.cs.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GitLabApiClient.Internal.Http;
using GitLabApiClient.Internal.Paths;
using GitLabApiClient.Internal.Queries;
using GitLabApiClient.Models.Commits.Requests;
using GitLabApiClient.Models.Commits.Requests.CreateCommitRequest;
using GitLabApiClient.Models.Commits.Responses;
using GitLabApiClient.Models.Projects.Responses;

namespace GitLabApiClient
{
public sealed class CommitsClient : ICommitsClient
{
private readonly GitLabHttpFacade _httpFacade;
private readonly CommitQueryBuilder _commitQueryBuilder;
private readonly CommitRefsQueryBuilder _commitRefsQueryBuilder;
private readonly CommitStatusesQueryBuilder _commitStatusesQueryBuilder;

internal CommitsClient(GitLabHttpFacade httpFacade, CommitQueryBuilder commitQueryBuilder, CommitRefsQueryBuilder commitRefsQueryBuilder, CommitStatusesQueryBuilder commitStatusesQueryBuilder)
{
_httpFacade = httpFacade;
_commitQueryBuilder = commitQueryBuilder;
_commitRefsQueryBuilder = commitRefsQueryBuilder;
_commitStatusesQueryBuilder = commitStatusesQueryBuilder;
}

/// <summary>
/// Get a commit from commit sha
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="sha">The commit hash or name of a repository branch or tag</param>
/// <returns></returns>
public async Task<Commit> GetAsync(ProjectId projectId, string sha) =>
await _httpFacade.Get<Commit>($"projects/{projectId}/repository/commits/{sha}");

/// <summary>
/// Retrieve a list of commits from a project
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="options">Query Options <see cref="CommitQueryOptions"/>.</param>
/// <returns></returns>
public async Task<IList<Commit>> GetAsync(ProjectId projectId, Action<CommitQueryOptions> options = null)
{
var queryOptions = new CommitQueryOptions();
options?.Invoke(queryOptions);

string url = _commitQueryBuilder.Build($"projects/{projectId}/repository/commits", queryOptions);
return await _httpFacade.GetPagedList<Commit>(url);
}

/// <summary>
/// Retrieve a list of references (from branch or / and tag) that this commit belongs to
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="options">Query Options <see cref="CommitRefsQueryOptions"/>.</param>
/// <param name="sha">The commit hash or name of a repository branch or tag</param>
/// <returns></returns>
public async Task<IList<CommitRef>> GetRefsAsync(ProjectId projectId, string sha, Action<CommitRefsQueryOptions> options)
{
var queryOptions = new CommitRefsQueryOptions();
options?.Invoke(queryOptions);

string url = _commitRefsQueryBuilder.Build($"projects/{projectId}/repository/commits/{sha}/refs", queryOptions);
return await _httpFacade.GetPagedList<CommitRef>(url);
}

/// <summary>
/// Retrieve a list of differences in this commit
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="sha">The commit hash or name of a repository branch or tag</param>
/// <returns></returns>
public async Task<IList<Diff>> GetDiffsAsync(ProjectId projectId, string sha)
{
string url = $"projects/{projectId}/repository/commits/{sha}/diff";
return await _httpFacade.GetPagedList<Diff>(url);
}

/// <summary>
/// Retrieve a list of statuses in this commit
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="options">Query Options <see cref="CommitStatusesQueryOptions"/>.</param>
/// <param name="sha">The commit hash</param>
/// <returns></returns>
public async Task<IList<CommitStatuses>> GetStatusesAsync(ProjectId projectId, string sha, Action<CommitStatusesQueryOptions> options = null)
{
var queryOptions = new CommitStatusesQueryOptions();
options?.Invoke(queryOptions);

string url = _commitStatusesQueryBuilder.Build($"projects/{projectId}/repository/commits/{sha}/statuses", queryOptions);
return await _httpFacade.GetPagedList<CommitStatuses>(url);
}

/// <summary>
<<<<<<< HEAD
/// Create new commit
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="request">Create Commit request.</param>
/// <returns>newly created Commit</returns>
public async Task<Commit> CreateAsync(ProjectId projectId, CreateCommitRequest request) =>
await _httpFacade.Post<Commit>($"projects/{projectId}/repository/commits", request);
=======
/// Creates a commit with multiple files and actions.
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="request">Create commit request.</param>
/// <param name="autoEncodeToBase64">Automatically encode contents to base64 (default false).</param>
public async Task<Commit> CreateAsync(ProjectId projectId, CreateCommitRequest request, bool autoEncodeToBase64 = false)
{
if (autoEncodeToBase64)
{
foreach (var action in request.Actions.Where(action => !string.IsNullOrEmpty(action.Content)))
{
action.Encoding = CreateCommitRequestActionEncoding.Base64;
action.Content = Convert.ToBase64String(Encoding.UTF8.GetBytes(action.Content));
}
}
return await _httpFacade.Post<Commit>($"projects/{projectId}/repository/commits", request);
}
>>>>>>> master
}
}
6 changes: 6 additions & 0 deletions src/GitLabApiClient/FilesClient.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using GitLabApiClient.Internal.Http;
using GitLabApiClient.Internal.Paths;
Expand All @@ -16,5 +17,10 @@ public async Task<File> GetAsync(ProjectId projectId, string filePath, string re
{
return await _httpFacade.Get<File>($"projects/{projectId}/repository/files/{filePath.UrlEncode()}?ref={reference}");
}

public async Task<string> GetRawAsync(ProjectId projectId, string filePath, string reference = "master")
{
return await _httpFacade.GetString($"projects/{projectId}/repository/files/{filePath.UrlEncode()}/raw?ref={reference}");
}
}
}
1 change: 0 additions & 1 deletion src/GitLabApiClient/ICommitsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using GitLabApiClient.Internal.Paths;
using GitLabApiClient.Models.Commits.Requests;
using GitLabApiClient.Models.Commits.Requests.CreateCommitRequest;
using GitLabApiClient.Models.Commits.Responses;
using GitLabApiClient.Models.Projects.Responses;

Expand Down
73 changes: 73 additions & 0 deletions src/GitLabApiClient/ICommitsClient.cs.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using GitLabApiClient.Internal.Paths;
using GitLabApiClient.Models.Commits.Requests;
using GitLabApiClient.Models.Commits.Requests.CreateCommitRequest;
using GitLabApiClient.Models.Commits.Responses;
using GitLabApiClient.Models.Projects.Responses;

namespace GitLabApiClient
{
public interface ICommitsClient
{
/// <summary>
/// Get a commit from commit sha
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="sha">The commit hash or name of a repository branch or tag</param>
/// <returns></returns>
Task<Commit> GetAsync(ProjectId projectId, string sha);

/// <summary>
/// Retrieve a list of commits from a project
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="options">Query Options <see cref="CommitQueryOptions"/>.</param>
/// <returns></returns>
Task<IList<Commit>> GetAsync(ProjectId projectId, Action<CommitQueryOptions> options = null);

/// <summary>
/// Retrieve a list of references (from branch or / and tag) that this commit belongs to
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="options">Query Options <see cref="CommitRefsQueryOptions"/>.</param>
/// <param name="sha">The commit hash or name of a repository branch or tag</param>
/// <returns></returns>
Task<IList<CommitRef>> GetRefsAsync(ProjectId projectId, string sha, Action<CommitRefsQueryOptions> options);

/// <summary>
/// Retrieve a list of differences in this commit
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="sha">The commit hash or name of a repository branch or tag</param>
/// <returns></returns>
Task<IList<Diff>> GetDiffsAsync(ProjectId projectId, string sha);

/// <summary>
/// Retrieve a list of statuses in this commit
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="options">Query Options <see cref="CommitStatusesQueryOptions"/>.</param>
/// <param name="sha">The commit hash</param>
/// <returns></returns>
Task<IList<CommitStatuses>> GetStatusesAsync(ProjectId projectId, string sha, Action<CommitStatusesQueryOptions> options = null);

/// <summary>
<<<<<<< HEAD
/// Create new commit
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="request">Create Commit request.</param>
/// <returns>newly created Commit</returns>
Task<Commit> CreateAsync(ProjectId projectId, CreateCommitRequest request);
=======
/// Creates a commit with multiple files and actions.
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="request">Create commit request.</param>
/// <param name="autoEncodeToBase64">Automatically encode contents to base64 (default false).</param>
Task<Commit> CreateAsync(ProjectId projectId, CreateCommitRequest request, bool autoEncodeToBase64 = false);
>>>>>>> master
}
}
1 change: 1 addition & 0 deletions src/GitLabApiClient/IFilesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ namespace GitLabApiClient
public interface IFilesClient
{
Task<File> GetAsync(ProjectId projectId, string filePath, string reference = "master");
Task<string> GetRawAsync(ProjectId projectId, string filePath, string reference = "master");
}
}
3 changes: 3 additions & 0 deletions src/GitLabApiClient/Internal/Http/GitLabHttpFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public Task<IList<T>> GetPagedList<T>(string uri) =>
public Task<T> Get<T>(string uri) =>
_requestor.Get<T>(uri);

public Task<string> GetString(string uri) =>
_requestor.GetString(uri);

public Task GetFile(string uri, string outputPath) =>
_requestor.GetFile(uri, outputPath);

Expand Down
7 changes: 7 additions & 0 deletions src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public async Task<T> Get<T>(string url)
return await ReadResponse<T>(responseMessage);
}

public async Task<string> GetString(string url)
{
var responseMessage = await _client.GetAsync(url);
await EnsureSuccessStatusCode(responseMessage);
return await responseMessage.Content.ReadAsStringAsync();
}

public async Task GetFile(string url, string outputPath)
{
var response = await _client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
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.Commits.Requests
{
/// <summary>
/// Used to create a commit in a project.
/// </summary>
public sealed class CreateCommitActionRequest
{
/// <summary>
/// The action to perform, create, delete, move, update, chmod.
/// </summary>
[JsonProperty("action")]
public string Action { get; set; }

/// <summary>
/// Full path to the file. Ex. lib/class.rb.
/// </summary>
[JsonProperty("file_path")]
public string FilePath { get; set; }

/// <summary>
/// Original full path to the file being moved. Ex. lib/class1.rb. Only considered for move action.
/// </summary>
[JsonProperty("previous_path")]
public string PreviousPath { get; set; }

/// <summary>
/// File content, required for all except delete, chmod, and move. Move actions that do not specify content preserve the existing file content,
/// and any other value of content overwrites the file content.
/// </summary>
[JsonProperty("content")]
public string Content { get; set; }

/// <summary>
/// text or base64. text is default.
/// </summary>
[JsonProperty("encoding")]
public string Encoding { get; set; }

/// <summary>
/// Last known file commit ID. Only considered in update, move, and delete actions.
/// </summary>
[JsonProperty("last_commit_id")]
public string LastCommitId { get; set; }

/// <summary>
/// When true/false enables/disables the execute flag on the file. Only considered for chmod action.
/// </summary>
[JsonProperty("execute_filemode")]
public bool ExecuteFilemode { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="CreateCommitActionRequest"/> class.
/// </summary>
/// <param name="action">Name of the branch to commit into. To create a new branch, also provide either start_branch or start_sha, and optionally start_project.</param>
/// <param name="filePath">Commit message.</param>
public CreateCommitActionRequest(string action, string filePath)
{
Guard.NotEmpty(action, nameof(action));
Guard.NotEmpty(filePath, nameof(filePath));

Action = action;
FilePath = filePath;
}
}
}
Loading