|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +type GitAuth struct { |
| 15 | + Username string |
| 16 | + Password string // Can be either password or access token |
| 17 | + Token string // x-access-token |
| 18 | +} |
| 19 | + |
| 20 | +type GitShell struct { |
| 21 | + workDir string |
| 22 | + timeout time.Duration |
| 23 | + environment []string |
| 24 | + auth *GitAuth |
| 25 | +} |
| 26 | + |
| 27 | +func NewGitShell(workDir string, auth *GitAuth) *GitShell { |
| 28 | + env := os.Environ() |
| 29 | + |
| 30 | + // If authentication is provided, set up credential helper |
| 31 | + if auth != nil { |
| 32 | + // Add credential helper to avoid interactive password prompts |
| 33 | + env = append(env, "GIT_TERMINAL_PROMPT=0") |
| 34 | + } |
| 35 | + |
| 36 | + return &GitShell{ |
| 37 | + workDir: workDir, |
| 38 | + timeout: 30 * time.Second, |
| 39 | + environment: env, |
| 40 | + auth: auth, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func NewGitShellWithTokenAuth(workDir string, token string) *GitShell { |
| 45 | + auth := GitAuth{ |
| 46 | + Username: "x-access-token", |
| 47 | + Password: "", |
| 48 | + Token: token, |
| 49 | + } |
| 50 | + return NewGitShell(workDir, &auth) |
| 51 | +} |
| 52 | + |
| 53 | +// formatAuthURL injects credentials into the Git URL |
| 54 | +func (g *GitShell) formatAuthURL(repoURL string) (string, error) { |
| 55 | + if g.auth == nil { |
| 56 | + return repoURL, nil |
| 57 | + } |
| 58 | + |
| 59 | + parsedURL, err := url.Parse(repoURL) |
| 60 | + if err != nil { |
| 61 | + return "", fmt.Errorf("invalid URL: %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + // Handle different auth types |
| 65 | + if g.auth.Token != "" { |
| 66 | + // X-Access-Token authentication |
| 67 | + parsedURL.User = url.UserPassword("x-access-token", g.auth.Token) |
| 68 | + } else if g.auth.Username != "" { |
| 69 | + // Username/password or personal access token |
| 70 | + parsedURL.User = url.UserPassword(g.auth.Username, g.auth.Password) |
| 71 | + } |
| 72 | + |
| 73 | + return parsedURL.String(), nil |
| 74 | +} |
| 75 | + |
| 76 | +func (g *GitShell) runCommand(args ...string) (string, error) { |
| 77 | + ctx, cancel := context.WithTimeout(context.Background(), g.timeout) |
| 78 | + defer cancel() |
| 79 | + |
| 80 | + cmd := exec.CommandContext(ctx, "git", args...) |
| 81 | + cmd.Dir = g.workDir |
| 82 | + cmd.Env = g.environment |
| 83 | + |
| 84 | + // Set up credential helper for HTTPS |
| 85 | + if g.auth != nil { |
| 86 | + cmd.Env = append(cmd.Env, "GIT_ASKPASS=echo") |
| 87 | + if g.auth.Token != "" { |
| 88 | + cmd.Env = append(cmd.Env, fmt.Sprintf("GIT_ACCESS_TOKEN=%s", g.auth.Token)) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + var stdout, stderr bytes.Buffer |
| 93 | + cmd.Stdout = &stdout |
| 94 | + cmd.Stderr = &stderr |
| 95 | + |
| 96 | + err := cmd.Run() |
| 97 | + if err != nil { |
| 98 | + if stderr.Len() > 0 { |
| 99 | + return "", fmt.Errorf("git command failed: %v: %s", err, stderr.String()) |
| 100 | + } |
| 101 | + return "", err |
| 102 | + } |
| 103 | + return strings.TrimSpace(stdout.String()), nil |
| 104 | +} |
| 105 | + |
| 106 | +// Clone with authentication |
| 107 | +func (g *GitShell) Clone(repoURL, branch string) error { |
| 108 | + authURL, err := g.formatAuthURL(repoURL) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + args := []string{"clone"} |
| 114 | + if branch != "" { |
| 115 | + args = append(args, "-b", branch) |
| 116 | + } |
| 117 | + args = append(args, "--depth", "1") |
| 118 | + args = append(args, "--single-branch", authURL, g.workDir) |
| 119 | + |
| 120 | + _, err = g.runCommand(args...) |
| 121 | + return err |
| 122 | +} |
0 commit comments