- Notifications
You must be signed in to change notification settings - Fork928
feat: add support forcoder_git_auth
data source#6334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
21 commits Select commitHold shift + click to select a range
c64636b
Add git auth providers schema
kylecarbs6a61712
Pipe git auth providers to the schema
kylecarbs3d67a0d
Add git auth providers to the API
kylecarbs604cf5b
Add gitauth endpoint to query authenticated state
kylecarbs85db3c2
Add endpoint to query git state
kylecarbse418892
Use BroadcastChannel to automatically authenticate with Git
kylecarbs6b75f3e
Add error validation for submitting the create workspace form
kylecarbs1ad5db1
Fix panic on template dry-run
kylecarbs320c10d
Add tests for the template version Git auth endpoint
kylecarbsff2b822
Merge branch 'main' into gitauth
kylecarbs18488f8
Show error if no gitauth is configured
kylecarbs18523d5
Add gitauth to cliui
kylecarbs2c9fb64
Fix unused method receiver
kylecarbs7cbf190
Fix linting errors
kylecarbs1335c93
Fix dbauthz querier test
kylecarbs671be8c
Fix make gen
kylecarbs177625a
Add JavaScript test for git auth
kylecarbs2be97bb
Fix bad error message
kylecarbs69dea35
Fix provisionerd test race
kylecarbs5fdf8ce
Fix requested changes
kylecarbs993ede8
Add comment to CreateWorkspacePageView
kylecarbsFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletionscli/cliui/gitauth.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cliui | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
"github.com/briandowns/spinner" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
type GitAuthOptions struct { | ||
Fetch func(context.Context) ([]codersdk.TemplateVersionGitAuth, error) | ||
FetchInterval time.Duration | ||
} | ||
func GitAuth(ctx context.Context, writer io.Writer, opts GitAuthOptions) error { | ||
if opts.FetchInterval == 0 { | ||
opts.FetchInterval = 500 * time.Millisecond | ||
} | ||
gitAuth, err := opts.Fetch(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
spin := spinner.New(spinner.CharSets[78], 100*time.Millisecond, spinner.WithColor("fgHiGreen")) | ||
spin.Writer = writer | ||
spin.ForceOutput = true | ||
spin.Suffix = " Waiting for Git authentication..." | ||
defer spin.Stop() | ||
ticker := time.NewTicker(opts.FetchInterval) | ||
defer ticker.Stop() | ||
for _, auth := range gitAuth { | ||
if auth.Authenticated { | ||
return nil | ||
} | ||
_, _ = fmt.Fprintf(writer, "You must authenticate with %s to create a workspace with this template. Visit:\n\n\t%s\n\n", auth.Type.Pretty(), auth.AuthenticateURL) | ||
ticker.Reset(opts.FetchInterval) | ||
spin.Start() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-ticker.C: | ||
} | ||
gitAuth, err := opts.Fetch(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
var authed bool | ||
for _, a := range gitAuth { | ||
if !a.Authenticated || a.ID != auth.ID { | ||
continue | ||
} | ||
authed = true | ||
break | ||
} | ||
// The user authenticated with the provider! | ||
if authed { | ||
break | ||
} | ||
} | ||
spin.Stop() | ||
_, _ = fmt.Fprintf(writer, "Successfully authenticated with %s!\n\n", auth.Type.Pretty()) | ||
} | ||
return nil | ||
} |
55 changes: 55 additions & 0 deletionscli/cliui/gitauth_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cliui_test | ||
import ( | ||
"context" | ||
"net/url" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/codersdk" | ||
"github.com/coder/coder/pty/ptytest" | ||
"github.com/coder/coder/testutil" | ||
) | ||
func TestGitAuth(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) | ||
defer cancel() | ||
ptty := ptytest.New(t) | ||
cmd := &cobra.Command{ | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var fetched atomic.Bool | ||
return cliui.GitAuth(cmd.Context(), cmd.OutOrStdout(), cliui.GitAuthOptions{ | ||
Fetch: func(ctx context.Context) ([]codersdk.TemplateVersionGitAuth, error) { | ||
defer fetched.Store(true) | ||
return []codersdk.TemplateVersionGitAuth{{ | ||
ID: "github", | ||
Type: codersdk.GitProviderGitHub, | ||
Authenticated: fetched.Load(), | ||
AuthenticateURL: "https://example.com/gitauth/github?redirect=" + url.QueryEscape("/gitauth?notify"), | ||
}}, nil | ||
}, | ||
FetchInterval: time.Millisecond, | ||
}) | ||
}, | ||
} | ||
cmd.SetOutput(ptty.Output()) | ||
cmd.SetIn(ptty.Input()) | ||
done := make(chan struct{}) | ||
go func() { | ||
defer close(done) | ||
err := cmd.Execute() | ||
assert.NoError(t, err) | ||
}() | ||
ptty.ExpectMatchContext(ctx, "You must authenticate with") | ||
ptty.ExpectMatchContext(ctx, "https://example.com/gitauth/github") | ||
ptty.ExpectMatchContext(ctx, "Successfully authenticated with GitHub") | ||
<-done | ||
} |
10 changes: 10 additions & 0 deletionscli/create.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletionscli/create_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletionscmd/cliui/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.