MCPcopy Index your code
hub / github.com/coder/coder / gitAskpass

Function gitAskpass

cli/gitaskpass.go:47–140  ·  view source on GitHub ↗

gitAskpass is used by the Coder agent to automatically authenticate with Git providers based on a hostname.

(agentAuth *AgentAuth)

Source from the content-addressed store, hash-verified

45// gitAskpass is used by the Coder agent to automatically authenticate
46// with Git providers based on a hostname.
47func gitAskpass(agentAuth *AgentAuth) *serpent.Command {
48 cmd := &serpent.Command{
49 Use: "gitaskpass",
50 Hidden: true,
51 Handler: func(inv *serpent.Invocation) error {
52 ctx := inv.Context()
53
54 ctx, stop := inv.SignalNotifyContext(ctx, StopSignals...)
55 defer stop()
56
57 user, host, err := gitauth.ParseAskpass(inv.Args[0])
58 if err != nil {
59 return xerrors.Errorf("parse host: %w", err)
60 }
61
62 client, err := agentAuth.CreateClient()
63 if err != nil {
64 return xerrors.Errorf("create agent client: %w", err)
65 }
66
67 workingDirectory, err := os.Getwd()
68 if err != nil {
69 workingDirectory = ""
70 }
71
72 // Detect the current git branch and remote origin so
73 // the control plane can resolve diffs without needing
74 // to SSH back into the workspace.
75 gitBranch, gitRemoteOrigin := detectGitRef(workingDirectory)
76
77 token, err := client.ExternalAuth(ctx, agentsdk.ExternalAuthRequest{
78 Match: host,
79 GitBranch: gitBranch,
80 GitRemoteOrigin: gitRemoteOrigin,
81 ChatID: inv.Environ.Get("CODER_CHAT_ID"),
82 })
83 if err != nil {
84 var apiError *codersdk.Error
85 if errors.As(err, &apiError) && apiError.StatusCode() == http.StatusNotFound {
86 // This prevents the "Run 'coder --help' for usage"
87 // message from occurring.
88 lines := []string{apiError.Message}
89 if apiError.Detail != "" {
90 lines = append(lines, apiError.Detail)
91 }
92 cliui.Warn(inv.Stderr, "Coder was unable to handle this git request. The default git behavior will be used instead.",
93 lines...,
94 )
95 return cliui.ErrCanceled
96 }
97 return xerrors.Errorf("get git token: %w", err)
98 }
99 if token.URL != "" {
100 // This is to help the agent authenticate with Git.
101 if inv.Environ.Get("CODER_CHAT_AGENT") == "true" {
102 _, _ = fmt.Fprintf(inv.Stderr, `You must notify the user to authenticate with Git.\n\nThe URL is: %s\n`, token.URL)
103 return cliui.ErrCanceled
104 }

Callers 1

CommandMethod · 0.85

Calls 15

StatusCodeMethod · 0.95
ParseAskpassFunction · 0.92
WarnFunction · 0.92
InfofFunction · 0.92
detectGitRefFunction · 0.85
openURLFunction · 0.85
CreateClientMethod · 0.80
ExternalAuthMethod · 0.80
AsMethod · 0.80
ContextMethod · 0.65
GetMethod · 0.65
NewMethod · 0.65

Tested by

no test coverage detected