Skip to content

Commit 09c0f55

Browse files
committed
backport(api): Backport branch flag to 0.5.x
1 parent 64ddd93 commit 09c0f55

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

cmd/fossa/main.go

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
configUsage = "path to config file (default: .fossa.{yml,yaml})"
2828
fetcherUsage = "type of fetcher to use for fossa. Default's to custom"
2929
projectUsage = "this repository's URL or VCS endpoint (default: VCS remote 'origin')"
30+
branchUsage = "this repository's branch (default: current VCS branch)"
3031
revisionUsage = "this repository's current revision hash (default: VCS hash HEAD)"
3132
endpointUsage = "the FOSSA server endpoint (default: https://app.fossa.io)"
3233
buildForceUsage = "ignore cached build artifacts"
@@ -45,6 +46,7 @@ func main() {
4546
app.Flags = []cli.Flag{
4647
cli.StringFlag{Name: "c, config", Usage: configUsage},
4748
cli.StringFlag{Name: "p, project", Usage: projectUsage},
49+
cli.StringFlag{Name: "branch", Usage: branchUsage},
4850
cli.StringFlag{Name: "r, revision", Usage: revisionUsage},
4951
cli.StringFlag{Name: "e, endpoint", Usage: endpointUsage},
5052
cli.StringFlag{Name: "m, modules", Usage: "the modules to build and analyze"},
@@ -87,6 +89,7 @@ func main() {
8789
cli.StringFlag{Name: "c, config", Usage: configUsage},
8890
cli.StringFlag{Name: "fetcher", Usage: fetcherUsage},
8991
cli.StringFlag{Name: "p, project", Usage: projectUsage},
92+
cli.StringFlag{Name: "branch", Usage: branchUsage},
9093
cli.StringFlag{Name: "r, revision", Usage: revisionUsage},
9194
cli.StringFlag{Name: "e, endpoint", Usage: endpointUsage},
9295
cli.StringFlag{Name: "m, modules", Usage: "the modules to analyze"},
@@ -103,6 +106,7 @@ func main() {
103106
cli.StringFlag{Name: "c, config", Usage: configUsage},
104107
cli.StringFlag{Name: "fetcher", Usage: fetcherUsage},
105108
cli.StringFlag{Name: "p, project", Usage: projectUsage},
109+
cli.StringFlag{Name: "branch", Usage: branchUsage},
106110
cli.StringFlag{Name: "r, revision", Usage: revisionUsage},
107111
cli.StringFlag{Name: "e, endpoint", Usage: endpointUsage},
108112
cli.IntFlag{Name: "t, timeout", Usage: "timeout for waiting for build status in seconds", Value: 60 * 10},
@@ -117,6 +121,7 @@ func main() {
117121
cli.StringFlag{Name: "c, config", Usage: configUsage},
118122
cli.StringFlag{Name: "fetcher", Usage: fetcherUsage},
119123
cli.StringFlag{Name: "p, project", Usage: projectUsage},
124+
cli.StringFlag{Name: "branch", Usage: branchUsage},
120125
cli.StringFlag{Name: "r, revision", Usage: revisionUsage},
121126
cli.StringFlag{Name: "e, endpoint", Usage: endpointUsage},
122127
cli.BoolFlag{Name: "l, locators", Usage: "upload data in locator format instead of JSON"},
@@ -140,6 +145,7 @@ func main() {
140145
cli.StringFlag{Name: "c, config", Usage: configUsage},
141146
cli.StringFlag{Name: "fetcher", Usage: fetcherUsage},
142147
cli.StringFlag{Name: "p, project", Usage: projectUsage},
148+
cli.StringFlag{Name: "branch", Usage: branchUsage},
143149
cli.StringFlag{Name: "r, revision", Usage: revisionUsage},
144150
cli.StringFlag{Name: "e, endpoint", Usage: endpointUsage},
145151
cli.BoolFlag{Name: "allow-unresolved", Usage: analyzeAllowResolvedUsage},

cmd/fossa/upload.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,19 @@ func doUpload(conf config.CLIConfig, results []normalizedModule) (string, error)
152152

153153
analysisLogger.Debugf("Uploading build data from (%#v) modules: %#v", len(results), string(buildData))
154154

155-
fossaEndpoint := "/api/builds/custom?locator=" + url.QueryEscape(string(module.MakeLocator(conf.Fetcher, conf.Project, conf.Revision))) + "&v=" + version
155+
branch := "master"
156+
if conf.Branch != "" {
157+
branch = conf.Branch
158+
}
159+
160+
fossaEndpoint := "/api/builds/custom?locator=" + url.QueryEscape(string(module.MakeLocator(conf.Fetcher, conf.Project, conf.Revision))) + "&branch=" + url.QueryEscape(branch) + "&v=" + url.QueryEscape(version)
156161
if conf.Fetcher == "custom" {
157162
defaultProjectTitle := results[0].Name
158163
cwd, _ := filepath.Abs(".")
159164
if cwd != "" {
160165
defaultProjectTitle = filepath.Base(cwd)
161166
}
162-
fossaEndpoint += fmt.Sprintf("&managedBuild=true&title=%s", url.PathEscape(defaultProjectTitle))
167+
fossaEndpoint += fmt.Sprintf("&managedBuild=true&title=%s", url.QueryEscape(defaultProjectTitle))
163168
}
164169

165170
postRef, _ := url.Parse(fossaEndpoint)
@@ -185,7 +190,7 @@ func doUpload(conf config.CLIConfig, results []normalizedModule) (string, error)
185190
return "", fmt.Errorf("invalid response, but build was uploaded")
186191
}
187192
locParts := strings.Split(jsonResponse["locator"].(string), "$")
188-
getRef, _ := url.Parse("/projects/" + url.QueryEscape(locParts[0]) + "/refs/branch/master/" + url.QueryEscape(locParts[1]) + "/browse/dependencies")
193+
getRef, _ := url.Parse("/projects/" + url.QueryEscape(locParts[0]) + "/refs/branch/" + url.QueryEscape(conf.Branch) + "/" + url.QueryEscape(locParts[1]) + "/browse/dependencies")
189194
return fmt.Sprint(`
190195
============================================================
191196

config/file.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"fmt"
55
"io/ioutil"
66
"os"
7+
"strings"
78

8-
"github.com/fossas/fossa-cli/module"
99
git "gopkg.in/src-d/go-git.v4"
1010
yaml "gopkg.in/yaml.v2"
11+
12+
"github.com/fossas/fossa-cli/module"
1113
)
1214

1315
type configFileV1 struct {
@@ -22,6 +24,7 @@ type configFileCLIV1 struct {
2224
APIKey string `yaml:"api_key,omitempty"`
2325
Server string `yaml:"server,omitempty"`
2426
Project string `yaml:"project,omitempty"`
27+
Branch string `yaml:"branch,omitempty"`
2528
Revision string `yaml:"revision,omitempty"`
2629
Fetcher string `yaml:"fetcher,omitempty"` // fetcher defaults to custom
2730
}
@@ -94,26 +97,29 @@ func setDefaultValues(c configFileV1) (configFileV1, error) {
9497
}
9598

9699
// Infer default locator and project from `git`.
97-
if c.CLI.Project == "" || c.CLI.Revision == "" {
100+
if c.CLI.Project == "" || c.CLI.Revision == "" || c.CLI.Branch == "" {
98101
// TODO: this needs to happen in the module directory, not the working
99102
// directory
100103
repo, err := git.PlainOpen(".")
101104
if err == nil {
102-
project := c.CLI.Project
103-
if project == "" {
105+
if c.CLI.Project == "" {
104106
origin, err := repo.Remote("origin")
105107
if err == nil && origin != nil {
106-
project = origin.Config().URLs[0]
107-
c.CLI.Project = project
108+
c.CLI.Project = origin.Config().URLs[0]
108109
}
109110
}
110-
revision := c.CLI.Revision
111-
if revision == "" {
111+
if c.CLI.Revision == "" {
112112
revision, err := repo.Head()
113113
if err == nil {
114114
c.CLI.Revision = revision.Hash().String()
115115
}
116116
}
117+
if c.CLI.Branch == "" {
118+
revision, err := repo.Head()
119+
if err == nil {
120+
c.CLI.Branch = strings.TrimPrefix(string(revision.Name()), "refs/heads/")
121+
}
122+
}
117123
}
118124
}
119125

config/init.go

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func New(c *cli.Context) (CLIConfig, error) {
4545
APIKey: c.String("api_key"),
4646
Fetcher: c.String("fetcher"),
4747
Project: c.String("project"),
48+
Branch: c.String("branch"),
4849
Revision: c.String("revision"),
4950
Endpoint: c.String("endpoint"),
5051
Modules: modules,
@@ -88,6 +89,9 @@ func New(c *cli.Context) (CLIConfig, error) {
8889
if config.Project == "" {
8990
config.Project = configFile.CLI.Project
9091
}
92+
if config.Branch == "" {
93+
config.Branch = configFile.CLI.Branch
94+
}
9195
if config.Revision == "" {
9296
config.Revision = configFile.CLI.Revision
9397
}

config/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type CLIConfig struct {
4747
APIKey string
4848
Fetcher string
4949
Project string
50+
Branch string
5051
Revision string
5152
Endpoint string
5253
Modules []module.Config

0 commit comments

Comments
 (0)