added checkout types https and ssh

This commit is contained in:
pasha1coil 2025-04-09 16:41:31 +03:00
parent 934f62af0f
commit 5bd2a3cee1
2 changed files with 40 additions and 13 deletions

@ -19,6 +19,9 @@ inputs:
serverURL:
description: 'address of the unique server on which the company`s gitea server is running'
required: true
checkoutType:
description: 'Type of checkout to use. Supported: `https`, `ssh`'
required: true
runs:
using: 'go'

@ -9,20 +9,29 @@ import (
)
type Config struct {
Repository string
Ref string
Token string
Path string
ServerURL string
Repository string
Ref string
Token string
Path string
ServerURL string
CheckoutType CheckoutType
}
type CheckoutType string
const (
HttpsType = "https"
SshType = "ssh"
)
func getConfig() Config {
return Config{
Repository: os.Getenv("INPUT_REPOSITORY"),
Ref: os.Getenv("INPUT_REF"),
Token: os.Getenv("INPUT_TOKEN"),
Path: os.Getenv("INPUT_PATH"),
ServerURL: os.Getenv("INPUT_SERVERURL"),
Repository: os.Getenv("INPUT_REPOSITORY"),
Ref: os.Getenv("INPUT_REF"),
Token: os.Getenv("INPUT_TOKEN"),
Path: os.Getenv("INPUT_PATH"),
ServerURL: os.Getenv("INPUT_SERVERURL"),
CheckoutType: CheckoutType(os.Getenv("INPUT_CHECKOUTTYPE")),
}
}
@ -42,9 +51,7 @@ func main() {
log.Fatal("field serverURL is required")
}
cloneURL := fmt.Sprintf("https://x-access-token:%s@%s/%s.git", cfg.Token,
strings.TrimPrefix(cfg.ServerURL, "https://"), cfg.Repository,
)
cloneURL := cfg.CheckoutType.GenerateCloneURL(cfg)
fmt.Println("Cloning", cloneURL, "into", cfg.Path)
run("git", "clone", "--depth=1", cloneURL, cfg.Path)
@ -64,3 +71,20 @@ func run(name string, args ...string) {
log.Fatalf("Command failed: %s %s Error: %v", name, strings.Join(args, " "), err)
}
}
func (t CheckoutType) GenerateCloneURL(cfg Config) string {
switch t {
case HttpsType:
return fmt.Sprintf("https://x-access-token:%s@%s/%s.git", cfg.Token,
strings.TrimPrefix(cfg.ServerURL, "https://"), cfg.Repository,
)
case SshType:
return fmt.Sprintf("git@%s:%s.git",
strings.TrimPrefix(cfg.ServerURL, "git@"),
cfg.Repository,
)
default:
log.Fatalf("Unknown checkout type: %s", t)
return ""
}
}