Skip to content
Merged
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
16 changes: 8 additions & 8 deletions Apps.GitLab/Actions/Base/GitLabActions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Authentication;
using Blackbird.Applications.Sdk.Common.Invocation;
using GitLabApiClient;
using Apps.GitLab.Utils;

namespace Apps.Gitlab.Actions.Base;

public class GitLabActions : BaseInvocable
{
protected IEnumerable<AuthenticationCredentialsProvider> Creds =>
InvocationContext.AuthenticationCredentialsProviders;

private BlackbirdGitlabClient GitLabClient { get; set; }

protected GitLabClient Client => GitLabClient.Client;
protected BlackbirdGitlabClient RestClient => GitLabClient;
protected BlackbirdGitlabClient RestClient { get; }

public GitLabActions(InvocationContext invocationContext) : base(invocationContext)
{
GitLabClient = new(Creds);
RestClient = new(Creds);
}
}

protected static int ParseProjectId(string repositoryId)
=> ParsingUtils.ParseIntOrThrow(repositoryId, "Repository ID");
Comment thread
RiabushenkoA marked this conversation as resolved.
}
46 changes: 20 additions & 26 deletions Apps.GitLab/Actions/BranchActions.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
using Apps.Gitlab.Actions.Base;
using Apps.Gitlab.Actions.Base;
using Apps.Gitlab.Dtos;
using Apps.Gitlab.Models.Branch.Requests;
using Apps.Gitlab.Models.Branch.Responses;
using Apps.Gitlab.Models.Respository.Requests;
using Apps.GitLab.Utils;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Actions;
using Blackbird.Applications.Sdk.Common.Invocation;
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces;
using GitLabApiClient.Internal.Paths;
using GitLabApiClient.Models.Branches.Responses;
using RestSharp;

namespace Apps.Gitlab.Actions;

[ActionList("Branch")]
public class BranchActions : GitLabActions
public class BranchActions(InvocationContext invocationContext)
: GitLabActions(invocationContext)
{
private readonly IFileManagementClient _fileManagementClient;

public BranchActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient)
: base(invocationContext)
{
_fileManagementClient = fileManagementClient;
}

[Action("List branches", Description = "List respository branches")]
public async Task<ListRepositoryBranchesResponse> ListRepositoryBranches([ActionParameter] GetRepositoryRequest input)
{
var projectId = (ProjectId)int.Parse(input.RepositoryId);
var branches = await ErrorHandler.ExecuteWithErrorHandlingAsync(() =>
Client.Branches.GetAsync(projectId, _ => { }));
var projectId = ParseProjectId(input.RepositoryId);
var request = RestClient.CreateRequest($"/projects/{projectId}/repository/branches", Method.Get);
var branches = await RestClient.ExecuteWithErrorHandling<List<Branch>>(request);

return new ListRepositoryBranchesResponse
{
Branches = branches.Select(b => new BranchDto(b))
Expand All @@ -40,10 +35,11 @@ public async Task<BranchDto> GetBranch(
[ActionParameter] GetRepositoryRequest repositoryRequest,
[ActionParameter] GetBranchRequest input)
{
var projectId = (ProjectId)int.Parse(repositoryRequest.RepositoryId);

var branch = await ErrorHandler.ExecuteWithErrorHandlingAsync(() =>
Client.Branches.GetAsync(projectId, input.Name));
var projectId = ParseProjectId(repositoryRequest.RepositoryId);
var request = RestClient.CreateRequest(
$"/projects/{projectId}/repository/branches/{Uri.EscapeDataString(input.Name)}",
Method.Get);
var branch = await RestClient.ExecuteWithErrorHandling<Branch>(request);

return new BranchDto(branch);
}
Expand All @@ -53,14 +49,12 @@ public async Task<BranchDto> CreateBranch(
[ActionParameter] GetRepositoryRequest repositoryRequest,
[ActionParameter] Models.Branch.Requests.CreateBranchRequest input)
{
var projectId = (ProjectId)int.Parse(repositoryRequest.RepositoryId);

var request = new GitLabApiClient.Models.Branches.Requests.CreateBranchRequest(
input.NewBranchName,
input.BaseBranchName);
var projectId = ParseProjectId(repositoryRequest.RepositoryId);
var request = RestClient.CreateRequest($"/projects/{projectId}/repository/branches", Method.Post);
request.AddParameter("branch", input.NewBranchName);
request.AddParameter("ref", input.BaseBranchName);

var branch = await ErrorHandler.ExecuteWithErrorHandlingAsync(() =>
Client.Branches.CreateAsync(projectId, request));
var branch = await RestClient.ExecuteWithErrorHandling<Branch>(request);
return new BranchDto(branch);
}
}
}
103 changes: 56 additions & 47 deletions Apps.GitLab/Actions/CommitActions.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using Apps.Gitlab.Actions.Base;
using Apps.Gitlab.Actions.Base;
using Apps.Gitlab.Models.Branch.Requests;
using Apps.Gitlab.Models.Commit.Requests;
using Apps.Gitlab.Models.Respository.Requests;
using Apps.Gitlab.Webhooks;
using Apps.GitLab.Constants;
using Apps.GitLab.Dtos;
using Apps.GitLab.Models.Commit.Requests;
using Apps.GitLab.Models.Commit.Responses;
using Apps.GitLab.Models.Respository.Requests;
using Apps.GitLab.Utils;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Actions;
using Blackbird.Applications.Sdk.Common.Exceptions;
using Blackbird.Applications.Sdk.Common.Invocation;
using Blackbird.Applications.Sdk.Utils.Extensions.Files;
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces;
using GitLabApiClient.Internal.Paths;
using GitLabApiClient.Models.Commits.Responses;
using RestSharp;

namespace Apps.Gitlab.Actions;

Expand All @@ -35,13 +35,13 @@ public async Task<ListRepositoryCommitsResponse> ListRepositoryCommits(
[ActionParameter] GetRepositoryRequest repositoryRequest,
[ActionParameter] GetOptionalBranchRequest branchRequest)
{
var projectId = (ProjectId)int.Parse(repositoryRequest.RepositoryId);
var commits = await ErrorHandler.ExecuteWithErrorHandlingAsync(() =>
Client.Commits.GetAsync(projectId, options =>
{
if (!string.IsNullOrWhiteSpace(branchRequest.Name))
options.RefName = branchRequest.Name;
}));
var projectId = ParseProjectId(repositoryRequest.RepositoryId);
var request = RestClient.CreateRequest($"/projects/{projectId}/repository/commits", Method.Get);

if (!string.IsNullOrWhiteSpace(branchRequest.Name))
request.AddQueryParameter("ref_name", branchRequest.Name);

var commits = await RestClient.ExecuteWithErrorHandling<List<Commit>>(request);
return new()
{
Commits = commits
Expand All @@ -53,10 +53,12 @@ public async Task<Commit> GetCommit(
[ActionParameter] GetRepositoryRequest repositoryRequest,
[ActionParameter] GetCommitRequest input)
{
var projectId = (ProjectId)int.Parse(repositoryRequest.RepositoryId);
var commit = await ErrorHandler.ExecuteWithErrorHandlingAsync(() =>
Client.Commits.GetAsync(projectId, input.CommitId));
return commit;
var projectId = ParseProjectId(repositoryRequest.RepositoryId);
var request = RestClient.CreateRequest(
$"/projects/{projectId}/repository/commits/{Uri.EscapeDataString(input.CommitId)}",
Method.Get);

return await RestClient.ExecuteWithErrorHandling<Commit>(request);
}

[Action("List added or modified files in X hours", Description = "List added or modified files in X hours")]
Expand All @@ -68,21 +70,23 @@ public async Task<ListAddedOrModifiedInHoursResponse> ListAddedOrModifiedInHours
{
if (hoursRequest.Hours <= 0)
throw new ArgumentException("Specify more than 0 hours!");
var projectId = (ProjectId)int.Parse(repositoryRequest.RepositoryId);

var commits = await ErrorHandler.ExecuteWithErrorHandlingAsync(() =>
Client.Commits.GetAsync(projectId, options =>
{
options.Since = DateTime.Now.AddHours(-hoursRequest.Hours);
if (!string.IsNullOrWhiteSpace(branchRequest.Name))
options.RefName = branchRequest.Name;
}));

var projectId = ParseProjectId(repositoryRequest.RepositoryId);
var request = RestClient.CreateRequest($"/projects/{projectId}/repository/commits", Method.Get);
request.AddQueryParameter("since", DateTime.Now.AddHours(-hoursRequest.Hours).ToString("O"));

if (!string.IsNullOrWhiteSpace(branchRequest.Name))
request.AddQueryParameter("ref_name", branchRequest.Name);

var commits = await RestClient.ExecuteWithErrorHandling<List<Commit>>(request);
var files = new List<AddedOrModifiedFile>();

foreach (var c in commits)
foreach (var commit in commits)
{
var diffs = await ErrorHandler.ExecuteWithErrorHandlingAsync(() =>
Client.Commits.GetDiffsAsync(projectId, c.Id));
var diffRequest = RestClient.CreateRequest(
$"/projects/{projectId}/repository/commits/{Uri.EscapeDataString(commit.Id)}/diff",
Method.Get);
var diffs = await RestClient.ExecuteWithErrorHandling<List<Diff>>(diffRequest);

files.AddRange(
diffs
Expand All @@ -92,7 +96,11 @@ public async Task<ListAddedOrModifiedInHoursResponse> ListAddedOrModifiedInHours
PushWebhooks.IsFilePathMatchingPattern(folderInput.FolderPath, f.NewPath))
.Select(x => new AddedOrModifiedFile(x)));
}
return new ListAddedOrModifiedInHoursResponse() { Files = files.DistinctBy(x => x.Filename).ToList() };

return new ListAddedOrModifiedInHoursResponse
{
Files = files.DistinctBy(x => x.Filename).ToList()
};
}

[Action("Create or update file", Description = "Create or update file")]
Expand All @@ -101,34 +109,35 @@ public async Task<CommitDto> PushFile(
[ActionParameter] GetOptionalBranchRequest branchRequest,
[ActionParameter] PushFileRequest input)
{
var projectId = (ProjectId)int.Parse(repositoryRequest.RepositoryId);
var projectId = ParseProjectId(repositoryRequest.RepositoryId);

var repContent =
await new RepositoryActions(InvocationContext, _fileManagementClient).ListRepositoryContent(
repositoryRequest, branchRequest, new FolderContentWithTypeRequest() { IncludeSubfolders = true });
if (repContent.Content.Where(x => x.Type == "blob")
.Any(p => p.Path == input.DestinationFilePath)) // update in case of existing file
repositoryRequest, branchRequest, new FolderContentWithTypeRequest { IncludeSubfolders = true });

if (repContent.Content.Where(x => x.Type == GitLabItemTypes.Blob).Any(p => p.Path == input.DestinationFilePath))
{
return await UpdateFile(
repositoryRequest,
branchRequest,
new()
new PushFileRequest
{
DestinationFilePath = input.DestinationFilePath,
File = input.File,
CommitMessage = input.CommitMessage
});
}

if (repContent.Content.Where(x => x.Type == "tree").Select(x => x.Path)
if (repContent.Content.Where(x => x.Type == GitLabItemTypes.Tree).Select(x => x.Path)
.Contains(input.DestinationFilePath.Trim('/')))
throw new PluginApplicationException("Destination file path is invalid!");
{
throw new PluginMisconfigurationException("Destination file path is invalid!");
}

var file = await _fileManagementClient.DownloadAsync(input.File);
var fileBytes = await file.GetByteData();
var pushFileResult = await ErrorHandler.ExecuteWithErrorHandlingAsync(() =>
RestClient.PushChanges(projectId, branchRequest.Name, input.CommitMessage,
input.DestinationFilePath, fileBytes, "create"));
var pushFileResult = await RestClient.PushChanges(projectId, branchRequest.Name, input.CommitMessage,
input.DestinationFilePath, fileBytes, GitLabCommitActions.Create);

return new(pushFileResult);
}
Expand All @@ -139,20 +148,21 @@ public async Task<CommitDto> UpdateFile(
[ActionParameter] GetOptionalBranchRequest branchRequest,
[ActionParameter] PushFileRequest input)
{
var projectId = (ProjectId)int.Parse(repositoryRequest.RepositoryId);
var projectId = ParseProjectId(repositoryRequest.RepositoryId);

var repContent =
await new RepositoryActions(InvocationContext, _fileManagementClient).ListRepositoryContent(
repositoryRequest, branchRequest,
new FolderContentWithTypeRequest() { IncludeSubfolders = true, ContentType = "blob" });
new FolderContentWithTypeRequest { IncludeSubfolders = true, ContentType = GitLabItemTypes.Blob });

if (!repContent.Content.Select(x => x.Path).Contains(input.DestinationFilePath.Trim('/')))
throw new PluginApplicationException("File does not exist by specified file path!");

var file = await _fileManagementClient.DownloadAsync(input.File);
var fileBytes = await file.GetByteData();
var fileUpload = await ErrorHandler.ExecuteWithErrorHandlingAsync(() =>
RestClient.PushChanges(projectId, branchRequest.Name, input.CommitMessage,
input.DestinationFilePath, fileBytes, "update"));
var fileUpload = await RestClient.PushChanges(projectId, branchRequest.Name, input.CommitMessage,
input.DestinationFilePath, fileBytes, GitLabCommitActions.Update);

return new(fileUpload);
}

Expand All @@ -162,16 +172,15 @@ public async Task<DeleteFileResponse> DeleteFile(
[ActionParameter] GetOptionalBranchRequest branchRequest,
[ActionParameter] DeleteFileRequest input)
{
var projectId = (ProjectId)int.Parse(repositoryRequest.RepositoryId);
var projectId = ParseProjectId(repositoryRequest.RepositoryId);
var fileDelete = await RestClient.PushChanges(projectId, branchRequest.Name, input.CommitMessage,
input.FilePath, null, GitLabCommitActions.Delete);

var fileDelete = await ErrorHandler.ExecuteWithErrorHandlingAsync(() =>
RestClient.PushChanges(projectId, branchRequest.Name, input.CommitMessage,
input.FilePath, null, "delete"));
return new()
{
CommitId = fileDelete.Id,
Title = fileDelete.Title,
Message = fileDelete.Message
};
}
}
}
Loading