Skip to content

Commit

Permalink
Bitbucket (#172)
Browse files Browse the repository at this point in the history
* use --git-user in gith auth
  • Loading branch information
ATGardner authored Sep 10, 2021
1 parent afce647 commit 9557465
Show file tree
Hide file tree
Showing 15 changed files with 31 additions and 22 deletions.
6 changes: 3 additions & 3 deletions cmd/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func waitClusterReady(ctx context.Context, f kube.Factory, timeout time.Duration
})
}

func getRepoCredsSecret(token, namespace string) ([]byte, error) {
func getRepoCredsSecret(username, token, namespace string) ([]byte, error) {
return yaml.Marshal(&v1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Expand All @@ -460,7 +460,7 @@ func getRepoCredsSecret(token, namespace string) ([]byte, error) {
},
},
Data: map[string][]byte{
"git_username": []byte(store.Default.GitUsername),
"git_username": []byte(username),
"git_token": []byte(token),
},
})
Expand Down Expand Up @@ -586,7 +586,7 @@ func buildBootstrapManifests(namespace, appSpecifier string, cloneOpts *git.Clon
return nil, err
}

manifests.repoCreds, err = getRepoCredsSecret(cloneOpts.Auth.Password, namespace)
manifests.repoCreds, err = getRepoCredsSecret(cloneOpts.Auth.Username, cloneOpts.Auth.Password, namespace)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/commands/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func Test_buildBootstrapManifests(t *testing.T) {
assert.Equal(t, store.Default.RepoCredsSecretName, creds.ObjectMeta.Name)
assert.Equal(t, "foo", creds.ObjectMeta.Namespace)
assert.Equal(t, []byte("test"), creds.Data["git_token"])
assert.Equal(t, []byte(store.Default.GitUsername), creds.Data["git_username"])
assert.Equal(t, []byte(store.Default.GitHubUsername), creds.Data["git_username"])
},
},
}
Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_application.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ argocd-autopilot application [flags]

```
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
-h, --help help for application
--repo string Repository URL [GIT_REPO]
```
Expand Down
2 changes: 2 additions & 0 deletions docs/commands/argocd-autopilot_application_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ argocd-autopilot application create [APP_NAME] [flags]
```
--app string The application specifier (e.g. github.com/argoproj/argo-workflows/manifests/cluster-install/?ref=v3.0.3)
--apps-git-token string Your git provider api token [APPS_GIT_TOKEN]
--apps-git-user string Your git provider user name [APPS_GIT_USER] (not required in GitHub)
--apps-repo string Repository URL [APPS_GIT_REPO]
--dest-namespace string K8s target namespace (overrides the namespace specified in the kustomization.yaml)
--dest-server string K8s cluster URL (e.g. https://kubernetes.default.svc) (default "https://kubernetes.default.svc")
Expand All @@ -67,6 +68,7 @@ argocd-autopilot application create [APP_NAME] [flags]

```
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
--repo string Repository URL [GIT_REPO]
```

Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_application_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ argocd-autopilot application delete [APP_NAME] [flags]

```
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
--repo string Repository URL [GIT_REPO]
```

Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_application_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ argocd-autopilot application list [PROJECT_NAME] [flags]

```
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
--repo string Repository URL [GIT_REPO]
```

Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ argocd-autopilot project [flags]

```
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
-h, --help help for project
--repo string Repository URL [GIT_REPO]
```
Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_project_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ argocd-autopilot project create [PROJECT] [flags]

```
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
--repo string Repository URL [GIT_REPO]
```

Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_project_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ argocd-autopilot project delete [PROJECT_NAME] [flags]

```
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
--repo string Repository URL [GIT_REPO]
```

Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_project_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ argocd-autopilot project list [flags]

```
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
--repo string Repository URL [GIT_REPO]
```

Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_repo_bootstrap.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ argocd-autopilot repo bootstrap [flags]
--app string The application specifier (e.g. github.com/argoproj-labs/argocd-autopilot/manifests?ref=v0.2.5), overrides the default installation argo-cd manifests
--dry-run If true, print manifests instead of applying them to the cluster (nothing will be commited to git)
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
-h, --help help for bootstrap
--hide-password If true, will not print initial argo cd password
--insecure Run Argo-CD server without TLS
Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_repo_uninstall.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ argocd-autopilot repo uninstall [flags]

```
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
-h, --help help for uninstall
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
-n, --namespace string If present, the namespace scope for this CLI request
Expand Down
15 changes: 9 additions & 6 deletions pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/argoproj-labs/argocd-autopilot/pkg/fs"
"github.com/argoproj-labs/argocd-autopilot/pkg/git/gogit"
"github.com/argoproj-labs/argocd-autopilot/pkg/log"
"github.com/argoproj-labs/argocd-autopilot/pkg/store"
"github.com/argoproj-labs/argocd-autopilot/pkg/util"

billy "github.com/go-git/go-billy/v5"
Expand Down Expand Up @@ -105,13 +106,16 @@ func AddFlags(cmd *cobra.Command, opts *AddFlagsOptions) *CloneOptions {

envPrefix := strings.ReplaceAll(strings.ToUpper(opts.Prefix), "-", "_")
cmd.PersistentFlags().StringVar(&co.Auth.Password, opts.Prefix+"git-token", "", fmt.Sprintf("Your git provider api token [%sGIT_TOKEN]", envPrefix))
cmd.PersistentFlags().StringVar(&co.Auth.Username, opts.Prefix+"git-user", "", fmt.Sprintf("Your git provider user name [%sGIT_USER] (not required in GitHub)", envPrefix))
cmd.PersistentFlags().StringVar(&co.Repo, opts.Prefix+"repo", "", fmt.Sprintf("Repository URL [%sGIT_REPO]", envPrefix))

util.Die(viper.BindEnv(opts.Prefix+"git-token", envPrefix+"GIT_TOKEN"))
util.Die(viper.BindEnv(opts.Prefix+"git-user", envPrefix+"GIT_USER"))
util.Die(viper.BindEnv(opts.Prefix+"repo", envPrefix+"GIT_REPO"))

if opts.Prefix == "" {
cmd.Flag("git-token").Shorthand = "t"
cmd.Flag("git-user").Shorthand = "u"
}

if opts.CreateIfNotExist {
Expand All @@ -135,6 +139,10 @@ func (o *CloneOptions) Parse() {

host, orgRepo, o.path, o.revision, _, suffix, _ = util.ParseGitUrl(o.Repo)
o.url = host + orgRepo + suffix

if o.Auth.Username == "" {
o.Auth.Username = store.Default.GitHubUsername
}
}

func (o *CloneOptions) GetRepo(ctx context.Context) (Repository, fs.FS, error) {
Expand Down Expand Up @@ -377,13 +385,8 @@ func getAuth(auth Auth) transport.AuthMethod {
return nil
}

username := auth.Username
if username == "" {
username = "git"
}

return &http.BasicAuth{
Username: username,
Username: auth.Username,
Password: auth.Password,
}
}
15 changes: 5 additions & 10 deletions pkg/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,7 @@ func Test_getAuth(t *testing.T) {
auth Auth
want transport.AuthMethod
}{
"Basic": {
auth: Auth{
Password: "123",
},
want: &http.BasicAuth{
Username: "git",
Password: "123",
},
},
"Username": {
"Should use the supplied username": {
auth: Auth{
Username: "test",
Password: "123",
Expand All @@ -104,6 +95,10 @@ func Test_getAuth(t *testing.T) {
Password: "123",
},
},
"Should return nil if no password is supplied": {
auth: Auth{},
want: nil,
},
}
for tname, tt := range tests {
t.Run(tname, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var Default = struct {
DestServer string
DummyName string
DestServerAnnotation string
GitUsername string
GitHubUsername string
LabelKeyAppName string
LabelKeyAppManagedBy string
LabelValueManagedBy string
Expand All @@ -66,7 +66,7 @@ var Default = struct {
DestServer: "https://kubernetes.default.svc",
DestServerAnnotation: "argocd-autopilot.argoproj-labs.io/default-dest-server",
DummyName: "DUMMY",
GitUsername: "username",
GitHubUsername: "username",
LabelKeyAppName: "app.kubernetes.io/name",
LabelKeyAppManagedBy: "app.kubernetes.io/managed-by",
LabelValueManagedBy: "argocd-autopilot",
Expand Down

0 comments on commit 9557465

Please sign in to comment.