From 573e24ca34a2d2bd1a7f7af2710b671f4e0953c1 Mon Sep 17 00:00:00 2001 From: rbelkhir Date: Fri, 4 Aug 2023 19:09:04 +0200 Subject: [PATCH 1/8] feat: ssh cloning with env var activation --- main.go | 19 +++++++++++++++++++ plugin.go | 17 +++++++++++++++++ types.go | 3 +++ 3 files changed, 39 insertions(+) diff --git a/main.go b/main.go index 8907975..3c7f03e 100644 --- a/main.go +++ b/main.go @@ -140,6 +140,22 @@ func main() { Usage: "Define safe directories", EnvVars: []string{"PLUGIN_SAFE_DIRECTORY", "CI_WORKSPACE"}, }, + &cli.BoolFlag{ + Name: "use-ssh", + Usage: "Using ssh for git clone", + EnvVars: []string{"PLUGIN_USE_SSH"}, + Value: false, + }, + &cli.StringFlag{ + Name: "ssh-key", + Usage: "SSH key for ssh clone", + EnvVars: []string{"PLUGIN_SSH_KEY"}, + }, + &cli.StringFlag{ + Name: "forge-url", + Usage: "Forge URL for ssh clone", + EnvVars: []string{"CI_FORGE_URL"}, + }, } if err := app.Run(os.Args); err != nil { @@ -155,6 +171,7 @@ func run(c *cli.Context) error { plugin := Plugin{ Repo: Repo{ Clone: c.String("remote"), + Forge: c.String("forge-url"), }, Build: Build{ Commit: c.String("sha"), @@ -180,6 +197,8 @@ func run(c *cli.Context) error { Partial: c.Bool("partial"), Home: c.String("home"), SafeDirectory: c.String("safe-directory"), + UseSSH: c.Bool("use-ssh"), + SSHKey: c.String("ssh-key"), }, Backoff: Backoff{ Attempts: c.Int("backoff-attempts"), diff --git a/plugin.go b/plugin.go index 3b146bd..b5d51d3 100644 --- a/plugin.go +++ b/plugin.go @@ -63,6 +63,10 @@ func (p Plugin) Exec() error { if isDirEmpty(filepath.Join(p.Build.Path, ".git")) { cmds = append(cmds, initGit(p.Config.Branch)) cmds = append(cmds, safeDirectory(p.Config.SafeDirectory)) + if p.Config.UseSSH { + // If env var PLUGIN_USE_SSH is set to true, use SSH instead of HTTPS + cmds = append(cmds, useSSH(p.Repo.Forge)) + } cmds = append(cmds, remote(p.Repo.Clone)) } @@ -210,6 +214,19 @@ func safeDirectory(safeDirectory string) *exec.Cmd { return appendEnv(exec.Command("git", "config", "--global", "safe.directory", safeDirectory), defaultEnvVars...) } +// Replace the https protocol with the ssh protocol. +func useSSH(forgeURL string) *exec.Cmd { + // Parsing FQDN from remote url to use it in config + var remoteURL string + if strings.Contains(forgeURL, "https://") { + remoteURL = strings.Replace(forgeURL, "https://", "", 1) + } else if strings.Contains(forgeURL, "http://") { + remoteURL = strings.Replace(forgeURL, "http://", "", 1) + } + return appendEnv(exec.Command("git", "config", fmt.Sprintf("url.git@%s:.insteadOf", remoteURL), forgeURL+"/"), defaultEnvVars...) +} + + // Sets the remote origin for the repository. func remote(remote string) *exec.Cmd { return appendEnv(exec.Command( diff --git a/types.go b/types.go index 3449f91..433af40 100644 --- a/types.go +++ b/types.go @@ -8,6 +8,7 @@ import ( type ( Repo struct { Clone string + Forge string } Build struct { @@ -38,6 +39,8 @@ type ( Partial bool filter string SafeDirectory string + UseSSH bool + SSHKey string } Backoff struct { From 573244de024f7ff4a4d64d85c89d6e10cf5c16ed Mon Sep 17 00:00:00 2001 From: rbelkhir Date: Fri, 4 Aug 2023 21:00:44 +0200 Subject: [PATCH 2/8] chores: Formating the code --- main.go | 4 ++-- plugin.go | 1 - types.go | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 3c7f03e..53386ab 100644 --- a/main.go +++ b/main.go @@ -197,8 +197,8 @@ func run(c *cli.Context) error { Partial: c.Bool("partial"), Home: c.String("home"), SafeDirectory: c.String("safe-directory"), - UseSSH: c.Bool("use-ssh"), - SSHKey: c.String("ssh-key"), + UseSSH: c.Bool("use-ssh"), + SSHKey: c.String("ssh-key"), }, Backoff: Backoff{ Attempts: c.Int("backoff-attempts"), diff --git a/plugin.go b/plugin.go index b5d51d3..52f6a90 100644 --- a/plugin.go +++ b/plugin.go @@ -226,7 +226,6 @@ func useSSH(forgeURL string) *exec.Cmd { return appendEnv(exec.Command("git", "config", fmt.Sprintf("url.git@%s:.insteadOf", remoteURL), forgeURL+"/"), defaultEnvVars...) } - // Sets the remote origin for the repository. func remote(remote string) *exec.Cmd { return appendEnv(exec.Command( diff --git a/types.go b/types.go index 433af40..f75ca55 100644 --- a/types.go +++ b/types.go @@ -39,8 +39,8 @@ type ( Partial bool filter string SafeDirectory string - UseSSH bool - SSHKey string + UseSSH bool + SSHKey string } Backoff struct { From 1ed90ef290799d3369256ee42f3c84a34a1efb0d Mon Sep 17 00:00:00 2001 From: RayaneB75 Date: Mon, 7 Aug 2023 14:13:30 +0200 Subject: [PATCH 3/8] feat(ssh): Handling PLUGIN_SSH_KEY env var --- plugin.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/plugin.go b/plugin.go index 52f6a90..ff50a4d 100644 --- a/plugin.go +++ b/plugin.go @@ -66,6 +66,10 @@ func (p Plugin) Exec() error { if p.Config.UseSSH { // If env var PLUGIN_USE_SSH is set to true, use SSH instead of HTTPS cmds = append(cmds, useSSH(p.Repo.Forge)) + if p.Config.SSHKey != "" { + // If env var PLUGIN_SSH_KEY is set, use it as the SSH key + cmds = append(cmds, sshKeyHandler(p.Config.SSHKey)) + } } cmds = append(cmds, remote(p.Repo.Clone)) } @@ -214,7 +218,7 @@ func safeDirectory(safeDirectory string) *exec.Cmd { return appendEnv(exec.Command("git", "config", "--global", "safe.directory", safeDirectory), defaultEnvVars...) } -// Replace the https protocol with the ssh protocol. +// Replace the http(s) protocol with the ssh protocol. func useSSH(forgeURL string) *exec.Cmd { // Parsing FQDN from remote url to use it in config var remoteURL string @@ -226,6 +230,11 @@ func useSSH(forgeURL string) *exec.Cmd { return appendEnv(exec.Command("git", "config", fmt.Sprintf("url.git@%s:.insteadOf", remoteURL), forgeURL+"/"), defaultEnvVars...) } +// Use custom SSH Key thanks to core.sshCommand +func sshKeyHandler(sshKey string) *exec.Cmd { + return appendEnv(exec.Command("git", "config", "core.sshCommand", "ssh -i "+sshKey), defaultEnvVars...) +} + // Sets the remote origin for the repository. func remote(remote string) *exec.Cmd { return appendEnv(exec.Command( @@ -272,7 +281,7 @@ func checkoutLFS() *exec.Cmd { ), defaultEnvVars...) } -// fetch retuns git command that fetches from origin. If tags is true +// fetch returns git command that fetches from origin. If tags is true // then tags will be fetched. func fetch(ref string, tags bool, depth int, filter string) *exec.Cmd { tags_option := "--no-tags" From 470ec6ddee5ce807b026b5e36ffa844e9e1fce96 Mon Sep 17 00:00:00 2001 From: Rayane Belkhir Date: Fri, 11 Aug 2023 21:10:31 +0200 Subject: [PATCH 4/8] feat(env): Added git user env var --- main.go | 6 ++++++ plugin.go | 6 +++--- types.go | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 53386ab..d74614c 100644 --- a/main.go +++ b/main.go @@ -151,6 +151,11 @@ func main() { Usage: "SSH key for ssh clone", EnvVars: []string{"PLUGIN_SSH_KEY"}, }, + &cli.StringFlag{ + Name: "ssh-user", + Usage: "Username for ssh clone", + EnvVars: []string{"PLUGIN_SSH_USER"}, + }, &cli.StringFlag{ Name: "forge-url", Usage: "Forge URL for ssh clone", @@ -199,6 +204,7 @@ func run(c *cli.Context) error { SafeDirectory: c.String("safe-directory"), UseSSH: c.Bool("use-ssh"), SSHKey: c.String("ssh-key"), + SSHUser: c.String("ssh-user"), }, Backoff: Backoff{ Attempts: c.Int("backoff-attempts"), diff --git a/plugin.go b/plugin.go index ff50a4d..85c2576 100644 --- a/plugin.go +++ b/plugin.go @@ -65,7 +65,7 @@ func (p Plugin) Exec() error { cmds = append(cmds, safeDirectory(p.Config.SafeDirectory)) if p.Config.UseSSH { // If env var PLUGIN_USE_SSH is set to true, use SSH instead of HTTPS - cmds = append(cmds, useSSH(p.Repo.Forge)) + cmds = append(cmds, useSSH(p.Config.SSHUser, p.Repo.Forge)) if p.Config.SSHKey != "" { // If env var PLUGIN_SSH_KEY is set, use it as the SSH key cmds = append(cmds, sshKeyHandler(p.Config.SSHKey)) @@ -219,7 +219,7 @@ func safeDirectory(safeDirectory string) *exec.Cmd { } // Replace the http(s) protocol with the ssh protocol. -func useSSH(forgeURL string) *exec.Cmd { +func useSSH(gitUser string, forgeURL string) *exec.Cmd { // Parsing FQDN from remote url to use it in config var remoteURL string if strings.Contains(forgeURL, "https://") { @@ -227,7 +227,7 @@ func useSSH(forgeURL string) *exec.Cmd { } else if strings.Contains(forgeURL, "http://") { remoteURL = strings.Replace(forgeURL, "http://", "", 1) } - return appendEnv(exec.Command("git", "config", fmt.Sprintf("url.git@%s:.insteadOf", remoteURL), forgeURL+"/"), defaultEnvVars...) + return appendEnv(exec.Command("git", "config", fmt.Sprintf("url.%s@%s:.insteadOf", gitUser, remoteURL), forgeURL+"/"), defaultEnvVars...) } // Use custom SSH Key thanks to core.sshCommand diff --git a/types.go b/types.go index f75ca55..228cb90 100644 --- a/types.go +++ b/types.go @@ -41,6 +41,7 @@ type ( SafeDirectory string UseSSH bool SSHKey string + SSHUser string } Backoff struct { From 73a1ea7e8f7307f56dc64357062e66304f77500a Mon Sep 17 00:00:00 2001 From: Rayane Belkhir Date: Mon, 14 Aug 2023 23:45:15 +0200 Subject: [PATCH 5/8] feat: Now supporting woodpecker's ssh clone var --- main.go | 15 +++++++-------- plugin.go | 18 ++++-------------- types.go | 4 ++-- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index d74614c..a1eacdc 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,11 @@ func main() { Usage: "git remote url", EnvVars: []string{"PLUGIN_REMOTE", "CI_REPO_CLONE_URL", "CI_REPO_REMOTE", "CI_REMOTE_URL"}, }, + &cli.StringFlag{ + Name: "remote-ssh", + Usage: "git clone ssh url", + EnvVars: []string{"CI_REPO_CLONE_SSH_URL"}, + }, &cli.StringFlag{ Name: "path", Usage: "git clone path", @@ -156,11 +161,6 @@ func main() { Usage: "Username for ssh clone", EnvVars: []string{"PLUGIN_SSH_USER"}, }, - &cli.StringFlag{ - Name: "forge-url", - Usage: "Forge URL for ssh clone", - EnvVars: []string{"CI_FORGE_URL"}, - }, } if err := app.Run(os.Args); err != nil { @@ -175,8 +175,8 @@ func run(c *cli.Context) error { plugin := Plugin{ Repo: Repo{ - Clone: c.String("remote"), - Forge: c.String("forge-url"), + Clone: c.String("remote"), + CloneSSH: c.String("remote-ssh"), }, Build: Build{ Commit: c.String("sha"), @@ -204,7 +204,6 @@ func run(c *cli.Context) error { SafeDirectory: c.String("safe-directory"), UseSSH: c.Bool("use-ssh"), SSHKey: c.String("ssh-key"), - SSHUser: c.String("ssh-user"), }, Backoff: Backoff{ Attempts: c.Int("backoff-attempts"), diff --git a/plugin.go b/plugin.go index 85c2576..d6cda10 100644 --- a/plugin.go +++ b/plugin.go @@ -65,13 +65,15 @@ func (p Plugin) Exec() error { cmds = append(cmds, safeDirectory(p.Config.SafeDirectory)) if p.Config.UseSSH { // If env var PLUGIN_USE_SSH is set to true, use SSH instead of HTTPS - cmds = append(cmds, useSSH(p.Config.SSHUser, p.Repo.Forge)) + cmds = append(cmds, remote(p.Repo.CloneSSH)) if p.Config.SSHKey != "" { // If env var PLUGIN_SSH_KEY is set, use it as the SSH key cmds = append(cmds, sshKeyHandler(p.Config.SSHKey)) } + } else { + cmds = append(cmds, remote(p.Repo.Clone)) } - cmds = append(cmds, remote(p.Repo.Clone)) + } // fetch ref in any case @@ -218,18 +220,6 @@ func safeDirectory(safeDirectory string) *exec.Cmd { return appendEnv(exec.Command("git", "config", "--global", "safe.directory", safeDirectory), defaultEnvVars...) } -// Replace the http(s) protocol with the ssh protocol. -func useSSH(gitUser string, forgeURL string) *exec.Cmd { - // Parsing FQDN from remote url to use it in config - var remoteURL string - if strings.Contains(forgeURL, "https://") { - remoteURL = strings.Replace(forgeURL, "https://", "", 1) - } else if strings.Contains(forgeURL, "http://") { - remoteURL = strings.Replace(forgeURL, "http://", "", 1) - } - return appendEnv(exec.Command("git", "config", fmt.Sprintf("url.%s@%s:.insteadOf", gitUser, remoteURL), forgeURL+"/"), defaultEnvVars...) -} - // Use custom SSH Key thanks to core.sshCommand func sshKeyHandler(sshKey string) *exec.Cmd { return appendEnv(exec.Command("git", "config", "core.sshCommand", "ssh -i "+sshKey), defaultEnvVars...) diff --git a/types.go b/types.go index 228cb90..bc161bc 100644 --- a/types.go +++ b/types.go @@ -7,8 +7,8 @@ import ( type ( Repo struct { - Clone string - Forge string + Clone string + CloneSSH string } Build struct { From 3bc5709a87360eb3a21f40526e041162f26b6ae2 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 21 Aug 2023 18:15:57 +0200 Subject: [PATCH 6/8] Update main.go Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com> --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 7deaa5e..9492bf7 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ func main() { &cli.StringFlag{ Name: "remote-ssh", Usage: "git clone ssh url", - EnvVars: []string{"CI_REPO_CLONE_SSH_URL"}, + EnvVars: []string{"PLUGIN_REMOTE_SSH", "CI_REPO_CLONE_SSH_URL"}, }, &cli.StringFlag{ Name: "path", From b7dd0652494e6d9071d4b22e1c52a2828064fa91 Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Tue, 29 Aug 2023 16:29:02 +0200 Subject: [PATCH 7/8] Update types.go --- types.go | 1 - 1 file changed, 1 deletion(-) diff --git a/types.go b/types.go index 5a909d5..8833994 100644 --- a/types.go +++ b/types.go @@ -41,7 +41,6 @@ type ( SafeDirectory string UseSSH bool SSHKey string - SSHUser string } Backoff struct { From ea59f7832629a6777d47155914065ee4e1817291 Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Tue, 29 Aug 2023 16:29:29 +0200 Subject: [PATCH 8/8] Update main.go --- main.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/main.go b/main.go index 9492bf7..b633466 100644 --- a/main.go +++ b/main.go @@ -156,11 +156,6 @@ func main() { Usage: "SSH key for ssh clone", EnvVars: []string{"PLUGIN_SSH_KEY"}, }, - &cli.StringFlag{ - Name: "ssh-user", - Usage: "Username for ssh clone", - EnvVars: []string{"PLUGIN_SSH_USER"}, - }, } if err := app.Run(os.Args); err != nil {