Skip to content

Commit 56336db

Browse files
authored
Merge pull request #5 from MobileAct/implement/gitlab
Implement/gitlab
2 parents 7879f15 + 97bfe22 commit 56336db

15 files changed

Lines changed: 600 additions & 4 deletions

GithubLabelSetUpper.sln

100755100644
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiteaLabelSetUpper", "Gitea
1212
EndProject
1313
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiteaLabelSetUpper.Test", "GiteaLabelSetUpper.Test\GiteaLabelSetUpper.Test.csproj", "{93026D01-F1A4-46B3-A3B7-925F0EB689E5}"
1414
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitlabLabelSetUpper", "GitlabLabelSetUpper\GitlabLabelSetUpper.csproj", "{54BA476A-2D8A-4216-A4BE-B0AEFE922143}"
16+
EndProject
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitlabLabelSetUpper.Test", "GitlabLabelSetUpper.Test\GitlabLabelSetUpper.Test.csproj", "{B6A65A37-1FFE-4F68-AA22-D7B9E833343B}"
18+
EndProject
1519
Global
1620
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1721
Debug|Any CPU = Debug|Any CPU
@@ -38,6 +42,14 @@ Global
3842
{93026D01-F1A4-46B3-A3B7-925F0EB689E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
3943
{93026D01-F1A4-46B3-A3B7-925F0EB689E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
4044
{93026D01-F1A4-46B3-A3B7-925F0EB689E5}.Release|Any CPU.Build.0 = Release|Any CPU
45+
{54BA476A-2D8A-4216-A4BE-B0AEFE922143}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
46+
{54BA476A-2D8A-4216-A4BE-B0AEFE922143}.Debug|Any CPU.Build.0 = Debug|Any CPU
47+
{54BA476A-2D8A-4216-A4BE-B0AEFE922143}.Release|Any CPU.ActiveCfg = Release|Any CPU
48+
{54BA476A-2D8A-4216-A4BE-B0AEFE922143}.Release|Any CPU.Build.0 = Release|Any CPU
49+
{B6A65A37-1FFE-4F68-AA22-D7B9E833343B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
50+
{B6A65A37-1FFE-4F68-AA22-D7B9E833343B}.Debug|Any CPU.Build.0 = Debug|Any CPU
51+
{B6A65A37-1FFE-4F68-AA22-D7B9E833343B}.Release|Any CPU.ActiveCfg = Release|Any CPU
52+
{B6A65A37-1FFE-4F68-AA22-D7B9E833343B}.Release|Any CPU.Build.0 = Release|Any CPU
4153
EndGlobalSection
4254
GlobalSection(SolutionProperties) = preSolution
4355
HideSolutionNode = FALSE
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
12+
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\GitlabLabelSetUpper\GitlabLabelSetUpper.csproj" />
17+
</ItemGroup>
18+
<ItemGroup>
19+
<None Remove="JsonTest.json" />
20+
<None Remove="YamlTest.yml" />
21+
</ItemGroup>
22+
<ItemGroup>
23+
<EmbeddedResource Include="YamlTest.yml" />
24+
<EmbeddedResource Include="JsonTest.json" />
25+
</ItemGroup>
26+
27+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[
2+
{
3+
"name": "name1",
4+
"color": "ffffff",
5+
"description": "description",
6+
"aliases": [
7+
"name1_1",
8+
"name1_2"
9+
]
10+
},
11+
{
12+
"name": "name2",
13+
"color": "ffffff"
14+
}
15+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- name: name1
2+
color: ffffff
3+
description: description
4+
aliases:
5+
- name1_1
6+
- name1_2
7+
- name: name2
8+
color: ffffff

GitlabLabelSetUpper/BaseCommand.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Threading.Tasks;
2+
using McMaster.Extensions.CommandLineUtils;
3+
4+
namespace GitlabLabelSetUpper
5+
{
6+
[HelpOption("--help|-h")]
7+
public abstract class BaseCommand
8+
{
9+
protected abstract Task OnExecuteAsync(CommandLineApplication application);
10+
}
11+
}

GitlabLabelSetUpper/Command.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Threading.Tasks;
2+
using McMaster.Extensions.CommandLineUtils;
3+
4+
namespace GitlabLabelSetUpper
5+
{
6+
[Command(Name = "lablabel", ResponseFileHandling = ResponseFileHandling.ParseArgsAsLineSeparated)]
7+
[Subcommand(typeof(ListCommand))]
8+
[Subcommand(typeof(SetUpCommand))]
9+
public class Command : BaseCommand
10+
{
11+
protected override Task OnExecuteAsync(CommandLineApplication application)
12+
{
13+
application.ShowHelp();
14+
return Task.CompletedTask;
15+
}
16+
}
17+
}

GitlabLabelSetUpper/Constant.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace GitlabLabelSetUpper
2+
{
3+
public static class Constant
4+
{
5+
public const string EnvironmentHost = "GITLAB_HOST";
6+
public const string EnvironmentToken = "GITLAB_TOKEN";
7+
}
8+
}

GitlabLabelSetUpper/GitlabApi.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Core;
5+
using GitLabApiClient;
6+
using GitLabApiClient.Internal.Paths;
7+
using GitLabApiClient.Models.Projects.Requests;
8+
9+
namespace GitlabLabelSetUpper
10+
{
11+
public static class GitlabExtensions
12+
{
13+
public static Label ToLabel(this GitLabApiClient.Models.Projects.Responses.Label label)
14+
{
15+
return new Label
16+
{
17+
Id = label.Id,
18+
Name = label.Name,
19+
Description = label.Description,
20+
Color = label.Color,
21+
Priority = label.Priority
22+
};
23+
}
24+
}
25+
26+
public class GitlabApi : IApi<Label>
27+
{
28+
private const string defaultGitlabHost = "https://gitlab.com";
29+
30+
private readonly GitLabClient client;
31+
private readonly ProjectId projectId;
32+
33+
public GitlabApi(string? gitlabHost, string? gitlabToken, string projectId)
34+
{
35+
client = new GitLabClient(gitlabHost ?? defaultGitlabHost, gitlabToken ?? string.Empty);
36+
this.projectId = projectId;
37+
}
38+
39+
public async Task<IReadOnlyList<Label>> GetLabelsAsync()
40+
{
41+
var response = await client.Projects.GetLabelsAsync(projectId);
42+
return response.Select(x => x.ToLabel()).ToList();
43+
}
44+
45+
public async Task CreateLabelAsync(Label newLabel)
46+
{
47+
var request = new CreateProjectLabelRequest(newLabel.Name)
48+
{
49+
Color = newLabel.Color,
50+
Description = newLabel.Color,
51+
Priority = newLabel.Priority
52+
};
53+
await client.Projects.CreateLabelAsync(projectId, request);
54+
}
55+
56+
public async Task DeleteLabelAsync(Label oldLabel)
57+
{
58+
await client.Projects.DeleteLabelAsync(projectId, oldLabel.Name);
59+
}
60+
61+
public async Task UpdateLabelAsync(Label oldLabel, Label newLabel)
62+
{
63+
var request = UpdateProjectLabelRequest.FromNewName(oldLabel.Name, newLabel.Name);
64+
request.Color = newLabel.Color;
65+
request.Description = newLabel.Description;
66+
request.Priority = newLabel.Priority;
67+
await client.Projects.UpdateLabelAsync(projectId, request);
68+
}
69+
}
70+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
<RuntimeIdentifiers>win10-x64;osx.10.12-x64;linux-x64</RuntimeIdentifiers>
7+
<PackAsTool>true</PackAsTool>
8+
<ToolCommandName>lablabel</ToolCommandName>
9+
10+
<PackageId>MobileAct.GitlabLabelSetUpper</PackageId>
11+
<Version>0.0.1</Version>
12+
<Authors>Mobile Act</Authors>
13+
<PackageProjectUrl>https://github.com/MobileAct/GithubLabelSetUpper</PackageProjectUrl>
14+
<PackageLicenseUrl>https://github.com/MobileAct/GithubLabelSetUpper/blob/master/LICENSE.txt</PackageLicenseUrl>
15+
<PackageTags>GitLab, Label</PackageTags>
16+
<Description>dotnet global tools for GitLab label.</Description>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<PackageReference Include="Utf8Json" Version="1.3.7" />
21+
<PackageReference Include="YamlDotNet" Version="6.1.2" />
22+
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.4.1" />
23+
<PackageReference Include="GitLabApiClient" Version="1.2.0" />
24+
</ItemGroup>
25+
<ItemGroup>
26+
<ProjectReference Include="..\Core\Core.csproj" />
27+
</ItemGroup>
28+
</Project>

GitlabLabelSetUpper/Label.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
using Core;
4+
using YamlDotNet.Serialization;
5+
6+
namespace GitlabLabelSetUpper
7+
{
8+
public class Label : ILabel
9+
{
10+
private static class Key
11+
{
12+
public const string Id = "id";
13+
public const string Name = "name";
14+
public const string Color = "color";
15+
public const string Description = "description";
16+
public const string Priority = "priority";
17+
public const string Aliases = "aliases";
18+
}
19+
20+
[DataMember(Name = Key.Id, IsRequired = false)]
21+
[YamlMember(Alias = Key.Id, ApplyNamingConventions = false)]
22+
public int? Id { get; set; }
23+
24+
[DataMember(Name = Key.Name, IsRequired = true)]
25+
[YamlMember(Alias = Key.Name, ApplyNamingConventions = false)]
26+
public string? Name { get; set; }
27+
28+
[DataMember(Name = Key.Color, IsRequired = true)]
29+
[YamlMember(Alias = Key.Color, ApplyNamingConventions = false)]
30+
public string? Color { get; set; }
31+
32+
[DataMember(Name = Key.Description, IsRequired = false)]
33+
[YamlMember(Alias = Key.Description, ApplyNamingConventions = false)]
34+
public string? Description { get; set; }
35+
36+
[DataMember(Name = Key.Priority, IsRequired = false)]
37+
[YamlMember(Alias = Key.Priority, ApplyNamingConventions = false)]
38+
public int? Priority { get; set; }
39+
40+
[DataMember(Name = Key.Aliases, IsRequired = false)]
41+
[YamlMember(Alias = Key.Aliases, ApplyNamingConventions = false)]
42+
public string[]? Aliases { get; set; }
43+
44+
public void ValidateOrThrow()
45+
{
46+
if (Name is null)
47+
{
48+
throw new ArgumentNullException(nameof(Name));
49+
}
50+
if (Color is null)
51+
{
52+
throw new ArgumentNullException(nameof(Color));
53+
}
54+
}
55+
56+
public override string ToString()
57+
{
58+
if (Aliases is null)
59+
{
60+
return $"Id: {Id}, Name: {Name}, Color: {Color}, Description: {Description}, Priority: {Priority}";
61+
}
62+
else
63+
{
64+
return $"Id: {Id}, Name: {Name}, Color: {Color}, Description: {Description}, Priority: {Priority}, Aliases: {string.Join(',', Aliases)}";
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)