From 315e6489dcec8ed88c2cdda718a76425ebc8f47b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Mu=C3=B1oz=20Redondo?= <105782439+JaimeSeqLabs@users.noreply.github.com> Date: Mon, 28 Aug 2023 11:04:43 +0200 Subject: [PATCH] Add support for Gitea credentials (#325) * Gitea credentials add cmd * gitea credentials missing reflection file * gitea credentials unit test --- conf/reflect-config.json | 12 +++++ .../cli/commands/credentials/AddCmd.java | 2 + .../commands/credentials/add/AddGiteaCmd.java | 32 ++++++++++++ .../credentials/providers/GiteaProvider.java | 39 ++++++++++++++ .../providers/GiteaProviderTest.java | 51 +++++++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 src/main/java/io/seqera/tower/cli/commands/credentials/add/AddGiteaCmd.java create mode 100644 src/main/java/io/seqera/tower/cli/commands/credentials/providers/GiteaProvider.java create mode 100644 src/test/java/io/seqera/tower/cli/credentials/providers/GiteaProviderTest.java diff --git a/conf/reflect-config.json b/conf/reflect-config.json index ac0db551..8a073724 100644 --- a/conf/reflect-config.json +++ b/conf/reflect-config.json @@ -835,6 +835,12 @@ "allDeclaredMethods":true, "methods":[{"name":"","parameterTypes":[] }] }, +{ + "name":"io.seqera.tower.cli.commands.credentials.providers.GiteaProvider", + "allDeclaredFields":true, + "allDeclaredMethods":true, + "methods":[{"name":"","parameterTypes":[] }] +}, { "name":"io.seqera.tower.cli.commands.credentials.providers.GitlabProvider", "allDeclaredFields":true, @@ -2648,6 +2654,12 @@ "allDeclaredMethods":true, "allDeclaredConstructors":true }, +{ + "name":"io.seqera.tower.model.GiteaSecurityKeys", + "allDeclaredFields":true, + "allDeclaredMethods":true, + "allDeclaredConstructors":true +}, { "name":"io.seqera.tower.model.GithubActionConfig", "allDeclaredFields":true, diff --git a/src/main/java/io/seqera/tower/cli/commands/credentials/AddCmd.java b/src/main/java/io/seqera/tower/cli/commands/credentials/AddCmd.java index f3f734c2..9876c5ae 100644 --- a/src/main/java/io/seqera/tower/cli/commands/credentials/AddCmd.java +++ b/src/main/java/io/seqera/tower/cli/commands/credentials/AddCmd.java @@ -17,6 +17,7 @@ import io.seqera.tower.cli.commands.credentials.add.AddAzureCmd; import io.seqera.tower.cli.commands.credentials.add.AddBitbucketCmd; import io.seqera.tower.cli.commands.credentials.add.AddContainerRegistryCmd; +import io.seqera.tower.cli.commands.credentials.add.AddGiteaCmd; import io.seqera.tower.cli.commands.credentials.add.AddGithubCmd; import io.seqera.tower.cli.commands.credentials.add.AddGitlabCmd; import io.seqera.tower.cli.commands.credentials.add.AddGoogleCmd; @@ -36,6 +37,7 @@ AddGoogleCmd.class, AddGithubCmd.class, AddGitlabCmd.class, + AddGiteaCmd.class, AddBitbucketCmd.class, AddSshCmd.class, AddK8sCmd.class, diff --git a/src/main/java/io/seqera/tower/cli/commands/credentials/add/AddGiteaCmd.java b/src/main/java/io/seqera/tower/cli/commands/credentials/add/AddGiteaCmd.java new file mode 100644 index 00000000..3383828f --- /dev/null +++ b/src/main/java/io/seqera/tower/cli/commands/credentials/add/AddGiteaCmd.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021, Seqera Labs. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This Source Code Form is "Incompatible With Secondary Licenses", as + * defined by the Mozilla Public License, v. 2.0. + */ + +package io.seqera.tower.cli.commands.credentials.add; + +import io.seqera.tower.cli.commands.credentials.providers.CredentialsProvider; +import io.seqera.tower.cli.commands.credentials.providers.GiteaProvider; +import picocli.CommandLine.Command; +import picocli.CommandLine.Mixin; + +@Command( + name = "gitea", + description = "Add new Gitea workspace credentials." +) +public class AddGiteaCmd extends AbstractAddCmd { + + @Mixin + GiteaProvider provider; + + @Override + protected CredentialsProvider getProvider() { + return provider; + } +} diff --git a/src/main/java/io/seqera/tower/cli/commands/credentials/providers/GiteaProvider.java b/src/main/java/io/seqera/tower/cli/commands/credentials/providers/GiteaProvider.java new file mode 100644 index 00000000..1f651b27 --- /dev/null +++ b/src/main/java/io/seqera/tower/cli/commands/credentials/providers/GiteaProvider.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021, Seqera Labs. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This Source Code Form is "Incompatible With Secondary Licenses", as + * defined by the Mozilla Public License, v. 2.0. + */ + +package io.seqera.tower.cli.commands.credentials.providers; + +import io.seqera.tower.model.Credentials.ProviderEnum; +import io.seqera.tower.model.GiteaSecurityKeys; +import picocli.CommandLine.Option; + +import java.io.IOException; + +public class GiteaProvider extends AbstractGitProvider { + + @Option(names = {"-u", "--username"}, description = "Gitea username.", required = true) + public String userName; + + // NOTE: setting 'arity' + 'interactive' allows both passing value as param and prompting user for input + @Option(names = {"-p", "--password"}, description = "Gitea account password.", arity = "0..1", interactive = true, required = true) + public String password; + + public GiteaProvider() { + super(ProviderEnum.GITEA); + } + + @Override + public GiteaSecurityKeys securityKeys() throws IOException { + return new GiteaSecurityKeys() + .username(userName) + .password(password); + } +} diff --git a/src/test/java/io/seqera/tower/cli/credentials/providers/GiteaProviderTest.java b/src/test/java/io/seqera/tower/cli/credentials/providers/GiteaProviderTest.java new file mode 100644 index 00000000..cb4714aa --- /dev/null +++ b/src/test/java/io/seqera/tower/cli/credentials/providers/GiteaProviderTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021, Seqera Labs. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This Source Code Form is "Incompatible With Secondary Licenses", as + * defined by the Mozilla Public License, v. 2.0. + */ + +/* + * This Java source file was generated by the Gradle 'init' task. + */ +package io.seqera.tower.cli.credentials.providers; + +import io.seqera.tower.cli.BaseCmdTest; +import io.seqera.tower.cli.commands.enums.OutputType; +import io.seqera.tower.cli.responses.CredentialsAdded; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockserver.client.MockServerClient; +import org.mockserver.model.MediaType; + +import static io.seqera.tower.cli.commands.AbstractApiCmd.USER_WORKSPACE_NAME; +import static org.mockserver.matchers.Times.exactly; +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; +import static org.mockserver.model.JsonBody.json; + +class GiteaProviderTest extends BaseCmdTest { + + @ParameterizedTest + @EnumSource(OutputType.class) + void testAdd(OutputType format, MockServerClient mock) { + + mock.when( + request() + .withMethod("POST") + .withPath("/credentials") + .withBody(json("{\"credentials\":{\"keys\":{\"username\":\"jordi@seqera.io\",\"password\":\"mysecret\"},\"name\":\"TestGitea\",\"provider\":\"gitea\"}}")), + exactly(1) + ).respond( + response().withStatusCode(200).withBody("{\"credentialsId\":\"1cz5A8cuBkB5iJliCwJCFU\"}").withContentType(MediaType.APPLICATION_JSON) + ); + + ExecOut out = exec(format, mock, "credentials", "add", "gitea", "-n", "TestGitea", "-u", "jordi@seqera.io", "-p", "mysecret"); + assertOutput(format, out, new CredentialsAdded("GITEA", "1cz5A8cuBkB5iJliCwJCFU", "TestGitea", USER_WORKSPACE_NAME)); + } + +}