Skip to content

Commit 3453eb5

Browse files
committed
feat(upload): Configurable upload branch
1 parent 269a380 commit 3453eb5

File tree

7 files changed

+22
-11
lines changed

7 files changed

+22
-11
lines changed

cmd/fossa/cmd/upload/upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func Run(ctx *cli.Context) {
101101
log.Logger.Fatalf("Upload failed: %s", err.Error())
102102
}
103103
baseURL, err := url.Parse(c.Endpoint)
104-
reportURL, err := url.Parse("/projects/" + url.QueryEscape(locator.Fetcher+"+"+locator.Project) + "/refs/branch/master/" + url.QueryEscape(locator.Revision) + "/browse/dependencies")
104+
reportURL, err := url.Parse("/projects/" + url.QueryEscape(locator.Fetcher+"+"+locator.Project) + "/refs/branch/" + c.Branch + "/" + url.QueryEscape(locator.Revision) + "/browse/dependencies")
105105
log.Printf(`
106106
============================================================
107107

cmd/fossa/flags/flags.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ func WithAPIFlags(f []cli.Flag) []cli.Flag {
99
}
1010

1111
var (
12-
API = []cli.Flag{Endpoint, Fetcher, Project, Revision}
12+
API = []cli.Flag{Endpoint, Fetcher, Project, Revision, Branch}
1313
Endpoint = cli.StringFlag{Name: "e, endpoint", Usage: "the FOSSA server endpoint (default: 'https://app.fossa.io')"}
1414
Fetcher = cli.StringFlag{Name: "f, fetcher", Usage: "type of fetcher to use for fossa. (default: 'custom')"}
1515
Project = cli.StringFlag{Name: "p, project", Usage: "this repository's URL or VCS endpoint (default: VCS remote 'origin')"}
1616
Revision = cli.StringFlag{Name: "r, revision", Usage: "this repository's current revision hash (default: VCS hash HEAD)"}
17+
Branch = cli.StringFlag{Name: "b, branch", Usage: "this repository's current branch (default: current VCS branch)"}
1718
)
1819

1920
func WithGlobalFlags(f []cli.Flag) []cli.Flag {

cmd/fossa/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/fossas/fossa-cli/builders"
1313
"github.com/fossas/fossa-cli/cmd/fossa/cmd/update"
1414
"github.com/fossas/fossa-cli/cmd/fossa/cmd/upload"
15+
"github.com/fossas/fossa-cli/cmd/fossa/flags"
1516
"github.com/fossas/fossa-cli/cmd/fossa/version"
1617
"github.com/fossas/fossa-cli/config"
1718
"github.com/fossas/fossa-cli/log"
@@ -50,6 +51,7 @@ func main() {
5051
cli.StringFlag{Name: "p, project", Usage: projectUsage},
5152
cli.StringFlag{Name: "r, revision", Usage: revisionUsage},
5253
cli.StringFlag{Name: "e, endpoint", Usage: endpointUsage},
54+
flags.Branch,
5355
cli.StringSliceFlag{Name: "m, modules", Usage: "the modules to build and analyze"},
5456
cli.StringFlag{Name: "fetcher", Usage: fetcherUsage},
5557
// --strategy=pipreqs
@@ -60,7 +62,7 @@ func main() {
6062
// --no-ansi
6163
cli.BoolFlag{Name: "o, output", Usage: analyzeOutputUsage},
6264
cli.BoolFlag{Name: "allow-unresolved", Usage: analyzeAllowResolvedUsage},
63-
cli.BoolFlag{Name: "b, build", Usage: "run a default build in module directories if they have not been pre-built"},
65+
cli.BoolFlag{Name: "build", Usage: "run a default build in module directories if they have not been pre-built"},
6466
cli.BoolFlag{Name: "f, force", Usage: buildForceUsage},
6567
cli.BoolFlag{Name: "debug", Usage: debugUsage},
6668
}
@@ -98,6 +100,7 @@ func main() {
98100
cli.StringFlag{Name: "p, project", Usage: projectUsage},
99101
cli.StringFlag{Name: "r, revision", Usage: revisionUsage},
100102
cli.StringFlag{Name: "e, endpoint", Usage: endpointUsage},
103+
flags.Branch,
101104
cli.StringSliceFlag{Name: "m, modules", Usage: "the modules to analyze"},
102105
cli.BoolFlag{Name: "o, output", Usage: analyzeOutputUsage},
103106
cli.BoolFlag{Name: "allow-unresolved", Usage: analyzeAllowResolvedUsage},

cmd/fossa/upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func doUpload(conf config.CLIConfig, results []normalizedModule) (string, error)
184184
return "", fmt.Errorf("invalid response, but build was uploaded")
185185
}
186186
locParts := strings.Split(jsonResponse["locator"].(string), "$")
187-
getRef, _ := url.Parse("/projects/" + url.QueryEscape(locParts[0]) + "/refs/branch/master/" + url.QueryEscape(locParts[1]) + "/browse/dependencies")
187+
getRef, _ := url.Parse("/projects/" + url.QueryEscape(locParts[0]) + "/refs/branch/" + conf.Branch + "/" + url.QueryEscape(locParts[1]) + "/browse/dependencies")
188188
return fmt.Sprint(`
189189
============================================================
190190

config/file.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type configFileCLIV1 struct {
2323
Server string `yaml:"server,omitempty"`
2424
Project string `yaml:"project,omitempty"`
2525
Revision string `yaml:"revision,omitempty"`
26+
Branch string `yaml:"branch,omitempty"`
2627
Fetcher string `yaml:"fetcher,omitempty"` // fetcher defaults to custom
2728
}
2829

@@ -94,26 +95,30 @@ func setDefaultValues(c configFileV1) (configFileV1, error) {
9495
}
9596

9697
// Infer default locator and project from `git`.
97-
if c.CLI.Project == "" || c.CLI.Revision == "" {
98+
if c.CLI.Project == "" || c.CLI.Revision == "" || c.CLI.Branch == "" {
9899
// TODO: this needs to happen in the module directory, not the working
99100
// directory
100101
repo, err := git.PlainOpen(".")
101102
if err == nil {
102-
project := c.CLI.Project
103-
if project == "" {
103+
if c.CLI.Project == "" {
104104
origin, err := repo.Remote("origin")
105105
if err == nil && origin != nil {
106-
project = origin.Config().URLs[0]
107-
c.CLI.Project = project
106+
c.CLI.Project = origin.Config().URLs[0]
108107
}
109108
}
110-
revision := c.CLI.Revision
111-
if revision == "" {
109+
if c.CLI.Revision == "" {
112110
revision, err := repo.Head()
113111
if err == nil {
114112
c.CLI.Revision = revision.Hash().String()
115113
}
116114
}
115+
if c.CLI.Branch == "" {
116+
revision, err := repo.Head()
117+
if err == nil {
118+
c.CLI.Revision = revision.Hash().String()
119+
}
120+
c.CLI.Branch = revision.Name().String()
121+
}
117122
}
118123
}
119124

config/init.go

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func New(c *cli.Context) (CLIConfig, error) {
5252
Fetcher: c.String("fetcher"),
5353
Project: c.String("project"),
5454
Revision: c.String("revision"),
55+
Branch: c.String("branch"),
5556
Endpoint: c.String("endpoint"),
5657
Modules: modules,
5758

config/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type CLIConfig struct {
4444
Fetcher string
4545
Project string
4646
Revision string
47+
Branch string
4748
Endpoint string
4849
Modules []module.Config
4950

0 commit comments

Comments
 (0)