From 2070dbbad7915f5f337d9ceafb450bca2b5b1602 Mon Sep 17 00:00:00 2001 From: Roger Versluis Date: Wed, 17 Mar 2021 10:45:20 -0600 Subject: [PATCH 1/4] Add GetRawAsync for files. --- src/GitLabApiClient/FilesClient.cs | 6 ++++++ src/GitLabApiClient/IFilesClient.cs | 1 + src/GitLabApiClient/Internal/Http/GitLabHttpFacade.cs | 3 +++ src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs | 8 ++++++++ 4 files changed, 18 insertions(+) diff --git a/src/GitLabApiClient/FilesClient.cs b/src/GitLabApiClient/FilesClient.cs index 57e14f8c..048a4bd3 100644 --- a/src/GitLabApiClient/FilesClient.cs +++ b/src/GitLabApiClient/FilesClient.cs @@ -1,3 +1,4 @@ +using System; using System.Threading.Tasks; using GitLabApiClient.Internal.Http; using GitLabApiClient.Internal.Paths; @@ -16,5 +17,10 @@ public async Task GetAsync(ProjectId projectId, string filePath, string re { return await _httpFacade.Get($"projects/{projectId}/repository/files/{filePath.UrlEncode()}?ref={reference}"); } + + public async Task GetRawAsync(ProjectId projectId, string filePath, string reference = "master") + { + return await _httpFacade.GetString($"projects/{projectId}/repository/files/{filePath.UrlEncode()}/raw?ref={reference}"); + } } } diff --git a/src/GitLabApiClient/IFilesClient.cs b/src/GitLabApiClient/IFilesClient.cs index f848d759..b100a8f1 100644 --- a/src/GitLabApiClient/IFilesClient.cs +++ b/src/GitLabApiClient/IFilesClient.cs @@ -7,5 +7,6 @@ namespace GitLabApiClient public interface IFilesClient { Task GetAsync(ProjectId projectId, string filePath, string reference = "master"); + Task GetRawAsync(ProjectId projectId, string filePath, string reference = "master"); } } diff --git a/src/GitLabApiClient/Internal/Http/GitLabHttpFacade.cs b/src/GitLabApiClient/Internal/Http/GitLabHttpFacade.cs index b9b1fc8b..51448964 100644 --- a/src/GitLabApiClient/Internal/Http/GitLabHttpFacade.cs +++ b/src/GitLabApiClient/Internal/Http/GitLabHttpFacade.cs @@ -68,6 +68,9 @@ public Task> GetPagedList(string uri) => public Task Get(string uri) => _requestor.Get(uri); + public Task GetString(string uri) => + _requestor.GetString(uri); + public Task GetFile(string uri, string outputPath) => _requestor.GetFile(uri, outputPath); diff --git a/src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs b/src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs index c4d6947f..f0091f45 100644 --- a/src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs +++ b/src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs @@ -28,6 +28,14 @@ public async Task Get(string url) return await ReadResponse(responseMessage); } + public async Task GetString(string url) + { + var responseMessage = await _client.GetAsync(url); + await EnsureSuccessStatusCode(responseMessage); + string response = await responseMessage.Content.ReadAsStringAsync(); + return response; + } + public async Task GetFile(string url, string outputPath) { var response = await _client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead); From 319fb1bb0bb8856fb874517c9db3cf89ed92e26d Mon Sep 17 00:00:00 2001 From: Roger Versluis Date: Wed, 17 Mar 2021 10:48:47 -0600 Subject: [PATCH 2/4] Formatting issue --- src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs b/src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs index f0091f45..6ebbf7a5 100644 --- a/src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs +++ b/src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs @@ -32,8 +32,7 @@ public async Task GetString(string url) { var responseMessage = await _client.GetAsync(url); await EnsureSuccessStatusCode(responseMessage); - string response = await responseMessage.Content.ReadAsStringAsync(); - return response; + return await responseMessage.Content.ReadAsStringAsync(); } public async Task GetFile(string url, string outputPath) From 4fe64aa0f5cd6a6c7a620276f6730e0640900667 Mon Sep 17 00:00:00 2001 From: Roger Versluis Date: Wed, 17 Mar 2021 15:16:00 -0600 Subject: [PATCH 3/4] Add createCommit --- src/GitLabApiClient/CommitsClient.cs | 9 ++ src/GitLabApiClient/ICommitsClient.cs | 8 ++ .../Requests/CreateCommitActionRequest.cs | 68 ++++++++++++++ .../Commits/Requests/CreateCommitRequest.cs | 88 +++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 src/GitLabApiClient/Models/Commits/Requests/CreateCommitActionRequest.cs create mode 100644 src/GitLabApiClient/Models/Commits/Requests/CreateCommitRequest.cs diff --git a/src/GitLabApiClient/CommitsClient.cs b/src/GitLabApiClient/CommitsClient.cs index ae21f70f..6b504060 100644 --- a/src/GitLabApiClient/CommitsClient.cs +++ b/src/GitLabApiClient/CommitsClient.cs @@ -92,5 +92,14 @@ public async Task> GetStatusesAsync(ProjectId projectId, s string url = _commitStatusesQueryBuilder.Build($"projects/{projectId}/repository/commits/{sha}/statuses", queryOptions); return await _httpFacade.GetPagedList(url); } + + /// + /// Create new commit + /// + /// The ID, path or of the project. + /// Create Commit request. + /// newly created Commit + public async Task CreateAsync(ProjectId projectId, CreateCommitRequest request) => + await _httpFacade.Post($"projects/{projectId}/repository/commits", request); } } diff --git a/src/GitLabApiClient/ICommitsClient.cs b/src/GitLabApiClient/ICommitsClient.cs index ff8f1bad..14c2d5d5 100644 --- a/src/GitLabApiClient/ICommitsClient.cs +++ b/src/GitLabApiClient/ICommitsClient.cs @@ -51,5 +51,13 @@ public interface ICommitsClient /// The commit hash /// Task> GetStatusesAsync(ProjectId projectId, string sha, Action options = null); + + /// + /// Create new commit + /// + /// The ID, path or of the project. + /// Create Commit request. + /// newly created Commit + Task CreateAsync(ProjectId projectId, CreateCommitRequest request); } } diff --git a/src/GitLabApiClient/Models/Commits/Requests/CreateCommitActionRequest.cs b/src/GitLabApiClient/Models/Commits/Requests/CreateCommitActionRequest.cs new file mode 100644 index 00000000..02fcfbf6 --- /dev/null +++ b/src/GitLabApiClient/Models/Commits/Requests/CreateCommitActionRequest.cs @@ -0,0 +1,68 @@ +using GitLabApiClient.Internal.Utilities; +using Newtonsoft.Json; + +namespace GitLabApiClient.Models.Commits.Requests +{ + /// + /// Used to create a commit in a project. + /// + public sealed class CreateCommitActionRequest + { + /// + /// The action to perform, create, delete, move, update, chmod. + /// + [JsonProperty("action")] + public string Action { get; set; } + + /// + /// Full path to the file. Ex. lib/class.rb. + /// + [JsonProperty("file_path")] + public string FilePath { get; set; } + + /// + /// Original full path to the file being moved. Ex. lib/class1.rb. Only considered for move action. + /// + [JsonProperty("previous_path")] + public string PreviousPath { get; set; } + + /// + /// 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. + /// + [JsonProperty("content")] + public string Content { get; set; } + + /// + /// text or base64. text is default. + /// + [JsonProperty("encoding")] + public string Encoding { get; set; } + + /// + /// Last known file commit ID. Only considered in update, move, and delete actions. + /// + [JsonProperty("last_commit_id")] + public string LastCommitId { get; set; } + + /// + /// When true/false enables/disables the execute flag on the file. Only considered for chmod action. + /// + [JsonProperty("execute_filemode")] + public bool ExecuteFilemode { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// Name of the branch to commit into. To create a new branch, also provide either start_branch or start_sha, and optionally start_project. + /// Commit message. + public CreateCommitActionRequest(string action, string filePath) + { + Guard.NotEmpty(action, nameof(action)); + Guard.NotEmpty(filePath, nameof(filePath)); + + Action = action; + FilePath = filePath; + } + } +} diff --git a/src/GitLabApiClient/Models/Commits/Requests/CreateCommitRequest.cs b/src/GitLabApiClient/Models/Commits/Requests/CreateCommitRequest.cs new file mode 100644 index 00000000..8071738c --- /dev/null +++ b/src/GitLabApiClient/Models/Commits/Requests/CreateCommitRequest.cs @@ -0,0 +1,88 @@ +using System.Collections.Generic; +using GitLabApiClient.Internal.Utilities; +using Newtonsoft.Json; + +namespace GitLabApiClient.Models.Commits.Requests +{ + /// + /// Used to create a commit in a project. + /// + public sealed class CreateCommitRequest + { + /// + /// Name of the branch to commit into. To create a new branch, also provide either start_branch or start_sha, and optionally start_project. + /// + [JsonProperty("branch")] + public string Branch { get; set; } + + /// + /// Commit message. + /// + [JsonProperty("commit_message")] + public string CommitMessage { get; set; } + + /// + /// Name of the branch to start the new branch from. + /// + [JsonProperty("start_branch")] + public string StartBranch { get; set; } + + /// + /// SHA of the commit to start the new branch from. + /// + [JsonProperty("start_sha")] + public string StartSha { get; set; } + + /// + /// The project ID or URL-encoded path of the project to start the new branch from. Defaults to the value of project id. + /// + [JsonProperty("start_project")] + public string ReleaseDescription { get; set; } + + /// + /// Specify the commit author's email address. + /// + [JsonProperty("author_email")] + public string AuthorEmail { get; set; } + + /// + /// Specify the commit author's name. + /// + [JsonProperty("author_name")] + public string AuthorName { get; set; } + + /// + /// Include commit stats. Default is true. + /// + [JsonProperty("stats")] + public bool Stats { get; set; } + + /// + /// When true overwrites the target branch with a new commit based on the start_branch or start_sha. + /// + [JsonProperty("force")] + public bool Force { get; set; } + + /// + /// A list of action hashes to commit as a batch. + /// + [JsonProperty("actions")] + public IList Actions { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// Name of the branch to commit into. To create a new branch, also provide either start_branch or start_sha, and optionally start_project. + /// Commit message. + /// A list of action hashes to commit as a batch. + public CreateCommitRequest(string branch, string commitMessage, IList actions) + { + Guard.NotEmpty(branch, nameof(branch)); + + Branch = branch; + CommitMessage = commitMessage; + Actions = actions; + Stats = true; + } + } +} From 8f865b0fb8b25bdc5feb7bb456799c5db9103058 Mon Sep 17 00:00:00 2001 From: Roger Versluis Date: Wed, 17 Mar 2021 15:17:01 -0600 Subject: [PATCH 4/4] Revert "Add createCommit" This reverts commit 78e72dd1f1e56c01e699939c750e2b268021cbc8. --- src/GitLabApiClient/CommitsClient.cs | 9 -- src/GitLabApiClient/ICommitsClient.cs | 8 -- .../Requests/CreateCommitActionRequest.cs | 68 -------------- .../Commits/Requests/CreateCommitRequest.cs | 88 ------------------- 4 files changed, 173 deletions(-) delete mode 100644 src/GitLabApiClient/Models/Commits/Requests/CreateCommitActionRequest.cs delete mode 100644 src/GitLabApiClient/Models/Commits/Requests/CreateCommitRequest.cs diff --git a/src/GitLabApiClient/CommitsClient.cs b/src/GitLabApiClient/CommitsClient.cs index 6b504060..ae21f70f 100644 --- a/src/GitLabApiClient/CommitsClient.cs +++ b/src/GitLabApiClient/CommitsClient.cs @@ -92,14 +92,5 @@ public async Task> GetStatusesAsync(ProjectId projectId, s string url = _commitStatusesQueryBuilder.Build($"projects/{projectId}/repository/commits/{sha}/statuses", queryOptions); return await _httpFacade.GetPagedList(url); } - - /// - /// Create new commit - /// - /// The ID, path or of the project. - /// Create Commit request. - /// newly created Commit - public async Task CreateAsync(ProjectId projectId, CreateCommitRequest request) => - await _httpFacade.Post($"projects/{projectId}/repository/commits", request); } } diff --git a/src/GitLabApiClient/ICommitsClient.cs b/src/GitLabApiClient/ICommitsClient.cs index 14c2d5d5..ff8f1bad 100644 --- a/src/GitLabApiClient/ICommitsClient.cs +++ b/src/GitLabApiClient/ICommitsClient.cs @@ -51,13 +51,5 @@ public interface ICommitsClient /// The commit hash /// Task> GetStatusesAsync(ProjectId projectId, string sha, Action options = null); - - /// - /// Create new commit - /// - /// The ID, path or of the project. - /// Create Commit request. - /// newly created Commit - Task CreateAsync(ProjectId projectId, CreateCommitRequest request); } } diff --git a/src/GitLabApiClient/Models/Commits/Requests/CreateCommitActionRequest.cs b/src/GitLabApiClient/Models/Commits/Requests/CreateCommitActionRequest.cs deleted file mode 100644 index 02fcfbf6..00000000 --- a/src/GitLabApiClient/Models/Commits/Requests/CreateCommitActionRequest.cs +++ /dev/null @@ -1,68 +0,0 @@ -using GitLabApiClient.Internal.Utilities; -using Newtonsoft.Json; - -namespace GitLabApiClient.Models.Commits.Requests -{ - /// - /// Used to create a commit in a project. - /// - public sealed class CreateCommitActionRequest - { - /// - /// The action to perform, create, delete, move, update, chmod. - /// - [JsonProperty("action")] - public string Action { get; set; } - - /// - /// Full path to the file. Ex. lib/class.rb. - /// - [JsonProperty("file_path")] - public string FilePath { get; set; } - - /// - /// Original full path to the file being moved. Ex. lib/class1.rb. Only considered for move action. - /// - [JsonProperty("previous_path")] - public string PreviousPath { get; set; } - - /// - /// 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. - /// - [JsonProperty("content")] - public string Content { get; set; } - - /// - /// text or base64. text is default. - /// - [JsonProperty("encoding")] - public string Encoding { get; set; } - - /// - /// Last known file commit ID. Only considered in update, move, and delete actions. - /// - [JsonProperty("last_commit_id")] - public string LastCommitId { get; set; } - - /// - /// When true/false enables/disables the execute flag on the file. Only considered for chmod action. - /// - [JsonProperty("execute_filemode")] - public bool ExecuteFilemode { get; set; } - - /// - /// Initializes a new instance of the class. - /// - /// Name of the branch to commit into. To create a new branch, also provide either start_branch or start_sha, and optionally start_project. - /// Commit message. - public CreateCommitActionRequest(string action, string filePath) - { - Guard.NotEmpty(action, nameof(action)); - Guard.NotEmpty(filePath, nameof(filePath)); - - Action = action; - FilePath = filePath; - } - } -} diff --git a/src/GitLabApiClient/Models/Commits/Requests/CreateCommitRequest.cs b/src/GitLabApiClient/Models/Commits/Requests/CreateCommitRequest.cs deleted file mode 100644 index 8071738c..00000000 --- a/src/GitLabApiClient/Models/Commits/Requests/CreateCommitRequest.cs +++ /dev/null @@ -1,88 +0,0 @@ -using System.Collections.Generic; -using GitLabApiClient.Internal.Utilities; -using Newtonsoft.Json; - -namespace GitLabApiClient.Models.Commits.Requests -{ - /// - /// Used to create a commit in a project. - /// - public sealed class CreateCommitRequest - { - /// - /// Name of the branch to commit into. To create a new branch, also provide either start_branch or start_sha, and optionally start_project. - /// - [JsonProperty("branch")] - public string Branch { get; set; } - - /// - /// Commit message. - /// - [JsonProperty("commit_message")] - public string CommitMessage { get; set; } - - /// - /// Name of the branch to start the new branch from. - /// - [JsonProperty("start_branch")] - public string StartBranch { get; set; } - - /// - /// SHA of the commit to start the new branch from. - /// - [JsonProperty("start_sha")] - public string StartSha { get; set; } - - /// - /// The project ID or URL-encoded path of the project to start the new branch from. Defaults to the value of project id. - /// - [JsonProperty("start_project")] - public string ReleaseDescription { get; set; } - - /// - /// Specify the commit author's email address. - /// - [JsonProperty("author_email")] - public string AuthorEmail { get; set; } - - /// - /// Specify the commit author's name. - /// - [JsonProperty("author_name")] - public string AuthorName { get; set; } - - /// - /// Include commit stats. Default is true. - /// - [JsonProperty("stats")] - public bool Stats { get; set; } - - /// - /// When true overwrites the target branch with a new commit based on the start_branch or start_sha. - /// - [JsonProperty("force")] - public bool Force { get; set; } - - /// - /// A list of action hashes to commit as a batch. - /// - [JsonProperty("actions")] - public IList Actions { get; set; } - - /// - /// Initializes a new instance of the class. - /// - /// Name of the branch to commit into. To create a new branch, also provide either start_branch or start_sha, and optionally start_project. - /// Commit message. - /// A list of action hashes to commit as a batch. - public CreateCommitRequest(string branch, string commitMessage, IList actions) - { - Guard.NotEmpty(branch, nameof(branch)); - - Branch = branch; - CommitMessage = commitMessage; - Actions = actions; - Stats = true; - } - } -}