Skip to content

Commit 91c8421

Browse files
Chris Pineeseligermrnugget
authored
if namespace is omitted, default to username (#372)
* if namespace is omitted, default to username * just fetch the ID and do not worry about the username * remove -namespace from help text * Update internal/campaigns/service.go Co-authored-by: Erik Seliger <[email protected]> * explain the default * cannot return a nil for a string * Update cmd/src/campaigns_common.go Co-authored-by: Thorsten Ball <[email protected]> * Update internal/campaigns/service.go Co-authored-by: Thorsten Ball <[email protected]> * Update cmd/src/campaigns_apply.go Co-authored-by: Thorsten Ball <[email protected]> * Update internal/campaigns/service.go Co-authored-by: Thorsten Ball <[email protected]> * added CHANGELOG entry Co-authored-by: Erik Seliger <[email protected]> Co-authored-by: Thorsten Ball <[email protected]>
1 parent 0381800 commit 91c8421

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ All notable changes to `src-cli` are documented in this file.
1313

1414
### Added
1515

16+
- Commands for campaigns no longer require the `-namespace` parameter. If omitted, campaigns will use the currently authenticated user as the namespace. [#372](https://github.com/sourcegraph/src-cli/pull/372)
17+
1618
### Changed
1719

1820
### Fixed

cmd/src/campaigns_apply.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ instance, creating or updating the described campaign if necessary.
1616
1717
Usage:
1818
19-
src campaigns apply -f FILE -namespace NAMESPACE [command options]
19+
src campaigns apply -f FILE [command options]
2020
2121
Examples:
2222
23+
$ src campaigns apply -f campaign.spec.yaml
24+
2325
$ src campaigns apply -f campaign.spec.yaml -namespace myorg
2426
2527
`

cmd/src/campaigns_common.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func newCampaignsApplyFlags(flagSet *flag.FlagSet, cacheDir, tempDir string) *ca
7979
)
8080
flagSet.StringVar(
8181
&caf.namespace, "namespace", "",
82-
"The user or organization namespace to place the campaign within.",
82+
"The user or organization namespace to place the campaign within. Default is the currently authenticated user.",
8383
)
8484
flagSet.StringVar(&caf.namespace, "n", "", "Alias for -namespace.")
8585

@@ -169,10 +169,6 @@ func campaignsExecute(ctx context.Context, out *output.Output, svc *campaigns.Se
169169
defer specFile.Close()
170170
}
171171

172-
if flags.namespace == "" {
173-
errs = multierror.Append(errs, &usageError{errors.New("a namespace must be provided with -namespace")})
174-
}
175-
176172
opts := campaigns.ExecutorOpts{
177173
Cache: svc.NewExecutionCache(flags.cacheDir),
178174
Creator: svc.NewWorkspaceCreator(flags.cacheDir, flags.cleanArchives),

cmd/src/campaigns_preview.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ a Sourcegraph instance, ready to be previewed and applied.
1616
1717
Usage:
1818
19-
src campaigns preview -f FILE -namespace NAMESPACE [command options]
19+
src campaigns preview -f FILE [command options]
2020
2121
Examples:
2222
23-
$ src campaigns preview -f campaign.spec.yaml -namespace myuser
23+
$ src campaigns preview -f campaign.spec.yaml
2424
2525
`
2626

internal/campaigns/service.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package campaigns
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"io"
78
"io/ioutil"
89
"reflect"
@@ -312,7 +313,34 @@ query NamespaceQuery($name: String!) {
312313
}
313314
`
314315

316+
const usernameQuery = `
317+
query GetCurrentUserID {
318+
currentUser {
319+
id
320+
}
321+
}
322+
`
323+
315324
func (svc *Service) ResolveNamespace(ctx context.Context, namespace string) (string, error) {
325+
if namespace == "" {
326+
// if no namespace is provided, default to logged in user as namespace
327+
var resp struct {
328+
Data struct {
329+
CurrentUser struct {
330+
ID string `json:"id"`
331+
} `json:"currentUser"`
332+
} `json:"data"`
333+
}
334+
if ok, err := svc.client.NewRequest(usernameQuery, nil).DoRaw(ctx, &resp); err != nil || !ok {
335+
return "", errors.WithMessage(err, "failed to resolve namespace: no user logged in")
336+
}
337+
338+
if resp.Data.CurrentUser.ID == "" {
339+
return "", errors.New("cannot resolve current user")
340+
}
341+
return resp.Data.CurrentUser.ID, nil
342+
}
343+
316344
var result struct {
317345
Data struct {
318346
User *struct{ ID string }
@@ -332,7 +360,7 @@ func (svc *Service) ResolveNamespace(ctx context.Context, namespace string) (str
332360
if result.Data.Organization != nil {
333361
return result.Data.Organization.ID, nil
334362
}
335-
return "", errors.New("no user or organization found")
363+
return "", fmt.Errorf("failed to resolve namespace %q: no user or organization found", namespace)
336364
}
337365

338366
func (svc *Service) ResolveRepositories(ctx context.Context, spec *CampaignSpec) ([]*graphql.Repository, error) {

0 commit comments

Comments
 (0)