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

Add Commits Create request #214

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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: 9 additions & 0 deletions src/GitLabApiClient/CommitsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,14 @@ public async Task<IList<CommitStatuses>> GetStatusesAsync(ProjectId projectId, s
string url = _commitStatusesQueryBuilder.Build($"projects/{projectId}/repository/commits/{sha}/statuses", queryOptions);
return await _httpFacade.GetPagedList<CommitStatuses>(url);
}

/// <summary>
/// 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);
}
}
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}");
}
}
}
8 changes: 8 additions & 0 deletions src/GitLabApiClient/ICommitsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,13 @@ public interface ICommitsClient
/// <param name="sha">The commit hash</param>
/// <returns></returns>
Task<IList<CommitStatuses>> GetStatusesAsync(ProjectId projectId, string sha, Action<CommitStatusesQueryOptions> options = null);

/// <summary>
/// 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);
}
}
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;
}
}
}
88 changes: 88 additions & 0 deletions src/GitLabApiClient/Models/Commits/Requests/CreateCommitRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Collections.Generic;
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 CreateCommitRequest
{
/// <summary>
/// Name of the branch to commit into. To create a new branch, also provide either start_branch or start_sha, and optionally start_project.
/// </summary>
[JsonProperty("branch")]
public string Branch { get; set; }

/// <summary>
/// Commit message.
/// </summary>
[JsonProperty("commit_message")]
public string CommitMessage { get; set; }

/// <summary>
/// Name of the branch to start the new branch from.
/// </summary>
[JsonProperty("start_branch")]
public string StartBranch { get; set; }

/// <summary>
/// SHA of the commit to start the new branch from.
/// </summary>
[JsonProperty("start_sha")]
public string StartSha { get; set; }

/// <summary>
/// The project ID or URL-encoded path of the project to start the new branch from. Defaults to the value of project id.
/// </summary>
[JsonProperty("start_project")]
public string ReleaseDescription { get; set; }

/// <summary>
/// Specify the commit author's email address.
/// </summary>
[JsonProperty("author_email")]
public string AuthorEmail { get; set; }

/// <summary>
/// Specify the commit author's name.
/// </summary>
[JsonProperty("author_name")]
public string AuthorName { get; set; }

/// <summary>
/// Include commit stats. Default is true.
/// </summary>
[JsonProperty("stats")]
public bool Stats { get; set; }

/// <summary>
/// When true overwrites the target branch with a new commit based on the start_branch or start_sha.
/// </summary>
[JsonProperty("force")]
public bool Force { get; set; }

/// <summary>
/// A list of action hashes to commit as a batch.
/// </summary>
[JsonProperty("actions")]
public IList<CreateCommitActionRequest> Actions { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="CreateCommitRequest"/> class.
/// </summary>
/// <param name="branch">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="commitMessage">Commit message.</param>
/// <param name="actions">A list of action hashes to commit as a batch.</param>
public CreateCommitRequest(string branch, string commitMessage, IList<CreateCommitActionRequest> actions)
{
Guard.NotEmpty(branch, nameof(branch));

Branch = branch;
CommitMessage = commitMessage;
Actions = actions;
Stats = true;
}
}
}