Skip to content

Commit

Permalink
Added Jira project key and Project URL flags (#237)
Browse files Browse the repository at this point in the history
Added Jira Project Key and Project URL flags to fossa analyze
  • Loading branch information
anuccio1 authored and elldritch committed Aug 31, 2018
1 parent efa8f60 commit e290faf
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 38 deletions.
31 changes: 20 additions & 11 deletions api/fossa/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ var (
ErrRevisionDoesNotExist = errors.New("revision does not exist (are the project and revision correct and published in FOSSA?)")
)

// UploadOptions are optional keys that provide extra metadata for an upload.
type UploadOptions struct {
Branch string
ProjectURL string
JIRAProjectKey string
}

// Upload uploads a project's analysis.
func Upload(fetcher, project, revision, title, branch string, data []SourceUnit) (Locator, error) {
func Upload(title string, locator Locator, options UploadOptions, data []SourceUnit) (Locator, error) {
// Check preconditions
if fetcher != "custom" && revision == "" {
if locator.Fetcher != "custom" && locator.Revision == "" {
log.Fatal("Could not infer revision name from `git` remote named `origin`. To submit a custom project, set Fetcher to `custom` in `.fossa.yml`")
}
if project == "" {
if locator.Project == "" {
log.Fatal("Could not infer project name from either `.fossa.yml` or `git` remote named `origin`")
}
if len(data) == 0 {
Expand All @@ -45,22 +52,24 @@ func Upload(fetcher, project, revision, title, branch string, data []SourceUnit)
"payload": string(payload),
}).Debug("uploading build")

locator := Locator{Fetcher: fetcher, Project: project, Revision: revision}

q := url.Values{}
q.Add("locator", locator.String())
q.Add("v", version.ShortString())

if fetcher == "custom" {
if locator.Fetcher == "custom" {
q.Add("managedBuild", "true")
q.Add("title", title)
}
if branch != "" {
q.Add("branch", branch)

if options.Branch != "" {
q.Add("branch", options.Branch)
}
if options.ProjectURL != "" {
q.Add("projectURL", options.ProjectURL)
}
if revision != "" {
q.Add("revision", revision)
if options.JIRAProjectKey != "" {
q.Add("jiraProjectKey", options.JIRAProjectKey)
}

endpoint, err := url.Parse("/api/builds/custom?" + q.Encode())
if err != nil {
log.Fatal("Failed to generate upload URL")
Expand Down
14 changes: 13 additions & 1 deletion cmd/fossa/cmd/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,19 @@ func Do(modules []module.Module) (analyzed []module.Module, err error) {

func uploadAnalysis(normalized []fossa.SourceUnit) error {
display.InProgress("Uploading analysis...")
locator, err := fossa.Upload(config.Fetcher(), config.Project(), config.Revision(), config.Title(), config.Branch(), normalized)
locator, err := fossa.Upload(
config.Title(),
fossa.Locator{
Fetcher: config.Fetcher(),
Project: config.Project(),
Revision: config.Revision(),
},
fossa.UploadOptions{
Branch: config.Branch(),
ProjectURL: config.ProjectURL(),
JIRAProjectKey: config.JIRAProjectKey(),
},
normalized)
display.ClearProgress()
if err != nil {
log.Fatalf("Error during upload: %s", err.Error())
Expand Down
14 changes: 13 additions & 1 deletion cmd/fossa/cmd/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,17 @@ func Do(data []fossa.SourceUnit) (fossa.Locator, error) {
log.Fatalf("No data to upload")
}

return fossa.Upload(config.Fetcher(), config.Project(), config.Revision(), config.Title(), config.Branch(), data)
return fossa.Upload(
config.Title(),
fossa.Locator{
Fetcher: config.Fetcher(),
Project: config.Project(),
Revision: config.Revision(),
},
fossa.UploadOptions{
Branch: config.Branch(),
ProjectURL: config.ProjectURL(),
JIRAProjectKey: config.JIRAProjectKey(),
},
data)
}
30 changes: 17 additions & 13 deletions cmd/fossa/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,23 @@ func WithAPIFlags(f []cli.Flag) []cli.Flag {
}

var (
API = []cli.Flag{EndpointF, TitleF, FetcherF, ProjectF, RevisionF, BranchF}
Endpoint = "endpoint"
EndpointF = cli.StringFlag{Name: Short(Endpoint), Usage: "the FOSSA server endpoint (default: 'https://app.fossa.io')"}
Title = "title"
TitleF = cli.StringFlag{Name: Short(Title), Usage: "the title of the FOSSA project. (default: the project name)"}
Fetcher = "fetcher"
FetcherF = cli.StringFlag{Name: Short(Fetcher), Usage: "type of fetcher to use for fossa. (default: 'custom')"}
Project = "project"
ProjectF = cli.StringFlag{Name: Short(Project), Usage: "this repository's URL or VCS endpoint (default: VCS remote 'origin')"}
Revision = "revision"
RevisionF = cli.StringFlag{Name: Short(Revision), Usage: "this repository's current revision hash (default: VCS hash HEAD)"}
Branch = "branch"
BranchF = cli.StringFlag{Name: Short(Branch), Usage: "this repository's current branch (default: current VCS branch)"}
API = []cli.Flag{EndpointF, TitleF, FetcherF, ProjectF, RevisionF, BranchF, ProjectURLF, JIRAProjectKeyF}
Endpoint = "endpoint"
EndpointF = cli.StringFlag{Name: Short(Endpoint), Usage: "the FOSSA server endpoint (default: 'https://app.fossa.io')"}
Title = "title"
TitleF = cli.StringFlag{Name: Short(Title), Usage: "the title of the FOSSA project. (default: the project name)"}
Fetcher = "fetcher"
FetcherF = cli.StringFlag{Name: Short(Fetcher), Usage: "type of fetcher to use for fossa. (default: 'custom')"}
Project = "project"
ProjectF = cli.StringFlag{Name: Short(Project), Usage: "this repository's URL or VCS endpoint (default: VCS remote 'origin')"}
Revision = "revision"
RevisionF = cli.StringFlag{Name: Short(Revision), Usage: "this repository's current revision hash (default: VCS hash HEAD)"}
Branch = "branch"
BranchF = cli.StringFlag{Name: Short(Branch), Usage: "this repository's current branch (default: current VCS branch)"}
ProjectURL = "project-url"
ProjectURLF = cli.StringFlag{Name: ShortUpper(ProjectURL), Usage: "this repository's home page"}
JIRAProjectKey = "jira-project-key"
JIRAProjectKeyF = cli.StringFlag{Name: Short(JIRAProjectKey), Usage: "this repository's JIRA project key"}
)

func WithGlobalFlags(f []cli.Flag) []cli.Flag {
Expand Down
10 changes: 10 additions & 0 deletions config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type File interface {
Project() string
Branch() string
Revision() string
ProjectURL() string
JIRAProjectKey() string

Modules() []module.Module
}
Expand Down Expand Up @@ -57,6 +59,14 @@ func (_ NoFile) Branch() string {
return ""
}

func (_ NoFile) ProjectURL() string {
return ""
}

func (_ NoFile) JIRAProjectKey() string {
return ""
}

func (_ NoFile) Revision() string {
return ""
}
Expand Down
24 changes: 17 additions & 7 deletions config/file.v1/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ type File struct {

type CLIProperties struct {
// Upload configuration.
APIKey string `yaml:"api_key,omitempty"`
Server string `yaml:"server,omitempty"`
Fetcher string `yaml:"fetcher,omitempty"` // Defaults to custom
Project string `yaml:"project,omitempty"`
Title string `yaml:"title,omitempty"`
Revision string `yaml:"revision,omitempty"`
Branch string `yaml:"branch,omitempty"` // Only used with custom fetcher
APIKey string `yaml:"api_key,omitempty"`
Server string `yaml:"server,omitempty"`
Fetcher string `yaml:"fetcher,omitempty"` // Defaults to custom
Project string `yaml:"project,omitempty"`
Title string `yaml:"title,omitempty"`
Revision string `yaml:"revision,omitempty"`
Branch string `yaml:"branch,omitempty"` // Only used with custom fetcher
ProjectURL string `yaml:"project_url,omitempty"` // Only used with custom fetcher
JIRAProjectKey string `yaml:"jira_project_key,omitempty"` // Only used with custom fetcher
}

type AnalyzeProperties struct {
Expand Down Expand Up @@ -121,6 +123,14 @@ func (file File) Branch() string {
return file.CLI.Branch
}

func (file File) ProjectURL() string {
return file.CLI.ProjectURL
}

func (file File) JIRAProjectKey() string {
return file.CLI.JIRAProjectKey
}

func (file File) Revision() string {
return file.CLI.Revision
}
Expand Down
12 changes: 7 additions & 5 deletions config/file.v2/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
type File struct {
Version int `yaml:"version"`

Endpoint string `yaml:"server,omitempty"`
Project string `yaml:"project,omitempty"`
Revision string `yaml:"revision,omitempty"`
Branch string `yaml:"branch,omitempty"`
ImportedVCS bool `yaml:"imported-vcs,omitempty"`
Endpoint string `yaml:"server,omitempty"`
Project string `yaml:"project,omitempty"`
Revision string `yaml:"revision,omitempty"`
Branch string `yaml:"branch,omitempty"`
ProjectURL string `yaml:"project_url,omitempty"`
JiraProjectKey string `yaml:"jira_project_key,omitempty"`
ImportedVCS bool `yaml:"imported-vcs,omitempty"`

Modules []module.Module
}
Expand Down
8 changes: 8 additions & 0 deletions config/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ func Branch() string {
return TryStrings(MockBranch, StringFlag(flags.Branch), file.Branch(), inferred, "master")
}

func ProjectURL() string {
return TryStrings(StringFlag(flags.ProjectURL), file.ProjectURL(), "")
}

func JIRAProjectKey() string {
return TryStrings(StringFlag(flags.JIRAProjectKey), file.JIRAProjectKey(), "")
}

/**** Analysis configuration keys ****/

func Options() (map[string]interface{}, error) {
Expand Down

0 comments on commit e290faf

Please sign in to comment.