diff --git a/Apps.GitLab/Actions/RepositoryActions.cs b/Apps.GitLab/Actions/RepositoryActions.cs index c34720a..c4c777c 100644 --- a/Apps.GitLab/Actions/RepositoryActions.cs +++ b/Apps.GitLab/Actions/RepositoryActions.cs @@ -36,17 +36,18 @@ public RepositoryActions(InvocationContext invocationContext, IFileManagementCli } [Action("Create new repository", Description = "Create new repository")] - public Task CreateRepository([ActionParameter] CreateRepositoryInput input) + public async Task CreateRepository([ActionParameter] CreateRepositoryInput input) { var endpoint = "/projects"; if (input.UserId != null) endpoint += $"/user/{input.UserId}"; - var request = new GitLabRequest(endpoint, Method.Post, Creds) + var request = RestClient.CreateRequest(endpoint, Method.Post) .WithJsonBody(input.GetNewRepositoryRequest(), JsonConfig.JsonSettings); - return RestClient.ExecuteWithErrorHandling(request); + var project = await RestClient.ExecuteWithErrorHandling(request); + return RepositoryResponse.FromProject(project); } [Action("Get repository file", Description = "Get repository file by path")] @@ -143,8 +144,11 @@ public async Task GetAllFilesInFolder( } [Action("Get repository", Description = "Get repository info")] - public Task GetRepositoryById([ActionParameter] GetRepositoryRequest input) - => GetProject(ParseProjectId(input.RepositoryId)); + public async Task GetRepositoryById([ActionParameter] GetRepositoryRequest input) + { + var project = await GetProject(ParseProjectId(input.RepositoryId)); + return RepositoryResponse.FromProject(project); + } [Action("Get repository issues", Description = "Get opened issues against repository")] public async Task GetIssuesInRepository([ActionParameter] RepositoryRequest input) diff --git a/Apps.GitLab/Apps.GitLab.csproj b/Apps.GitLab/Apps.GitLab.csproj index 90edc59..da56bb7 100644 --- a/Apps.GitLab/Apps.GitLab.csproj +++ b/Apps.GitLab/Apps.GitLab.csproj @@ -6,7 +6,7 @@ enable GitLab Software development & version control - 1.0.11 + 1.0.12 Apps.GitLab diff --git a/Apps.GitLab/DataSourceHandlers/EnumHandlers/RepoVisibilityDataHandler.cs b/Apps.GitLab/DataSourceHandlers/EnumHandlers/RepoVisibilityDataHandler.cs index 55bc50b..13d14c4 100644 --- a/Apps.GitLab/DataSourceHandlers/EnumHandlers/RepoVisibilityDataHandler.cs +++ b/Apps.GitLab/DataSourceHandlers/EnumHandlers/RepoVisibilityDataHandler.cs @@ -8,9 +8,9 @@ public Dictionary GetData() { return new() { - {"0", "Private"}, - {"1", "Internal"}, - {"2", "Public"}, + {"private", "Private"}, + {"internal", "Internal"}, + {"public", "Public"}, }; } -} \ No newline at end of file +} diff --git a/Apps.GitLab/Models/Respository/Requests/CreateRepositoryInput.cs b/Apps.GitLab/Models/Respository/Requests/CreateRepositoryInput.cs index 9727817..cbc2b96 100644 --- a/Apps.GitLab/Models/Respository/Requests/CreateRepositoryInput.cs +++ b/Apps.GitLab/Models/Respository/Requests/CreateRepositoryInput.cs @@ -5,7 +5,6 @@ using Blackbird.Applications.Sdk.Common; using Blackbird.Applications.Sdk.Common.Dictionaries; using Blackbird.Applications.Sdk.Common.Dynamic; -using GitLabApiClient.Models.Projects.Requests; namespace Apps.Gitlab.Models.Respository.Requests; @@ -97,7 +96,7 @@ public CreateRepositoryRequest GetNewRepositoryRequest() EnableSnippets = EnableSnippets, EnableContainerRegistry = EnableContainerRegistry, EnableSharedRunners = EnableSharedRunners, - Visibility = Visibility != null ? (ProjectVisibilityLevel)int.Parse(Visibility) : null, + Visibility = Visibility, ImportUrl = ImportUrl, PublicJobs = PublicJobs, OnlyAllowMergeIfPipelineSucceeds = OnlyAllowMergeIfPipelineSucceeds, @@ -110,4 +109,4 @@ public CreateRepositoryRequest GetNewRepositoryRequest() InitializeWithReadme = InitializeWithReadme }; } -} \ No newline at end of file +} diff --git a/Apps.GitLab/Models/Respository/Requests/CreateRepositoryRequest.cs b/Apps.GitLab/Models/Respository/Requests/CreateRepositoryRequest.cs index 7a54e4e..6db7733 100644 --- a/Apps.GitLab/Models/Respository/Requests/CreateRepositoryRequest.cs +++ b/Apps.GitLab/Models/Respository/Requests/CreateRepositoryRequest.cs @@ -1,4 +1,3 @@ -using GitLabApiClient.Models.Projects.Requests; using Newtonsoft.Json; namespace Apps.GitLab.Models.Respository.Requests; @@ -45,7 +44,7 @@ public class CreateRepositoryRequest public bool? EnableSharedRunners { get; set; } [JsonProperty("visibility")] - public ProjectVisibilityLevel? Visibility { get; set; } + public string? Visibility { get; set; } [JsonProperty("import_url")] public string ImportUrl { get; set; } @@ -76,4 +75,4 @@ public class CreateRepositoryRequest [JsonProperty("initialize_with_readme")] public bool? InitializeWithReadme { get; set; } -} \ No newline at end of file +} diff --git a/Apps.GitLab/Models/Respository/Responses/RepositoryResponse.cs b/Apps.GitLab/Models/Respository/Responses/RepositoryResponse.cs new file mode 100644 index 0000000..5218b23 --- /dev/null +++ b/Apps.GitLab/Models/Respository/Responses/RepositoryResponse.cs @@ -0,0 +1,59 @@ +using Blackbird.Applications.Sdk.Common; +using GitLabApiClient.Models.Projects.Responses; + +namespace Apps.GitLab.Models.Respository.Responses; + +public class RepositoryResponse +{ + [Display("Repository ID")] + public string Id { get; set; } = string.Empty; + + public string Name { get; set; } = string.Empty; + + [Display("Path")] + public string? Path { get; set; } + + [Display("Path with namespace")] + public string? PathWithNamespace { get; set; } + + [Display("Default branch")] + public string? DefaultBranch { get; set; } + + public string? Description { get; set; } + + public string? Visibility { get; set; } + + [Display("Web URL")] + public string? WebUrl { get; set; } + + [Display("HTTP URL to repo")] + public string? HttpUrlToRepo { get; set; } + + [Display("SSH URL to repo")] + public string? SshUrlToRepo { get; set; } + + [Display("Created at")] + public string? CreatedAt { get; set; } + + [Display("Last activity at")] + public string? LastActivityAt { get; set; } + + public static RepositoryResponse FromProject(Project project) + { + return new() + { + Id = project.Id.ToString(), + Name = project.Name, + Path = project.Path, + PathWithNamespace = project.PathWithNamespace, + DefaultBranch = project.DefaultBranch, + Description = project.Description, + Visibility = project.Visibility.ToString(), + WebUrl = project.WebUrl, + HttpUrlToRepo = project.HttpUrlToRepo, + SshUrlToRepo = project.SshUrlToRepo, + CreatedAt = project.CreatedAt, + LastActivityAt = project.LastActivityAt + }; + } +} diff --git a/Tests.GitLab/RepositoryActionTests.cs b/Tests.GitLab/RepositoryActionTests.cs new file mode 100644 index 0000000..2e5ce75 --- /dev/null +++ b/Tests.GitLab/RepositoryActionTests.cs @@ -0,0 +1,41 @@ +using Apps.Gitlab.Actions; +using Apps.Gitlab.Models.Respository.Requests; +using Apps.GitLab.Constants; +using Apps.GitLab.Models.Respository.Requests; +using Blackbird.Applications.Sdk.Common.Invocation; +using Tests.GitLab.Base; + +namespace Tests.GitLab; + +[TestClass] +public class RepositoryActionTests : TestBaseWithContext +{ + [TestMethod, ContextDataSource(ConnectionTypes.PersonalAccessToken)] + public async Task GetRepository_WithExistingRepository_ReturnsRepository(InvocationContext context) + { + var action = new RepositoryActions(context, FileManagementClient); + + var result = await action.GetRepositoryById(new GetRepositoryRequest + { + RepositoryId = "71835863" + }); + + Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.Indented)); + Assert.IsNotNull(result); + } + + [TestMethod, ContextDataSource(ConnectionTypes.PersonalAccessToken)] + public async Task CreateRepository_WithExistingRepository_ReturnsRepository(InvocationContext context) + { + var action = new RepositoryActions(context, FileManagementClient); + + var result = await action.CreateRepository(new CreateRepositoryInput + { + Name = "Test Repository", + }); + + Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.Indented)); + Assert.IsNotNull(result); + } +} +