Skip to content

Commit

Permalink
Merge pull request #47 from tandr/accept-header
Browse files Browse the repository at this point in the history
Utilize Accept header for Get requests
  • Loading branch information
tandr authored Dec 10, 2019
2 parents cb44460 + ea99955 commit 13acf0f
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ script: cd
# in the next version of Go. Don't worry! Later we declare that test runs
# are allowed to fail on Go tip.
go:
- 1.9
- 1.12
- master

# Skip the install step. Don't `go get` dependencies. Only build with the
Expand Down
46 changes: 32 additions & 14 deletions hubapi/bom-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ package hubapi

import "time"

type bdJsonBomV6 struct{}

func (bdJsonBomV6) GetMimeType() string {
return "application/vnd.blackducksoftware.bill-of-materials-6+json"
}

type BomComponentList struct {
ItemsListBase
bdJsonBomV6
Items []BomComponent `json:"items"`
}

// BomComponent represents "application/vnd.blackducksoftware.bill-of-materials-6+json"
// We need to figure out what to do with content type here
type BomComponent struct {
bdJsonBomV6
ComponentName string `json:"componentName"`
ComponentVersionName string `json:"componentVersionName,omitempty"`
Component string `json:"component"`
Expand All @@ -33,7 +41,7 @@ type BomComponent struct {
ComponentModification string `json:"componentModification"`
ReleasedOn *time.Time `json:"releasedOn"`
ReviewStatus string `json:"reviewStatus"`
ReviewedDetails *BomReviewDetails `json:"reviewedDetails, omitempty"`
ReviewedDetails *BomReviewDetails `json:"reviewedDetails,omitempty"`
PolicyStatus string `json:"policyStatus"`
ApprovalStatus string `json:"approvalStatus"`
Ignored bool `json:"ignored"`
Expand All @@ -55,11 +63,13 @@ type BomComponent struct {
}

type BomVulnerableComponentList struct {
bdJsonBomV6
ItemsListBase
Items []BomVulnerableComponent `json:"items"`
}

type BomVulnerableComponent struct {
bdJsonBomV6
ComponentName string `json:"componentName"`
ComponentVersionName string `json:"componentVersionName"`
ComponentVersion string `json:"componentVersion"`
Expand All @@ -70,19 +80,6 @@ type BomVulnerableComponent struct {
Meta Meta `json:"_meta"`
}

type VulnerabilityBase struct {
VulnerabilityName string `json:"vulnerabilityName"`
Description string `json:"description"`
VulnerabilityPublishedDate string `json:"vulnerabilityPublishedDate"`
VulnerabilityUpdatedDate string `json:"vulnerabilityUpdatedDate"`
BaseScore float32 `json:"baseScore"`
ExploitabilitySubscore float32 `json:"exploitabilitySubscore"`
ImpactSubscore float32 `json:"impactSubscore"`
Source string `json:"source"`
Severity string `json:"severity"`
CweId string `json:"cweId,omitempty"`
}

type VulnerabilityWithRemediation struct {
VulnerabilityBase
RemediationStatus string `json:"remediationStatus"`
Expand Down Expand Up @@ -127,3 +124,24 @@ type BomReviewingUser struct {
LastName string `json:"lastName"`
User string `json:"user"`
}

// result of bom policy-status link under project's component version
type BomComponentPolicyStatus struct {
bdJsonBomV6
ApprovalStatus string `json:"approvalStatus"`
Meta Meta `json:"_meta"`
}

// result of bom policy-rules link under project's component version
type BomComponentPolicyRulesList struct {
bdJsonBomV6
ItemsListBase
Items []BomComponentPolicyRule `json:"items"`
}

type BomComponentPolicyRule struct {
bdJsonBomV6
PolicyRule
PolicyApprovalStatus string `json:"policyApprovalStatus"`
Comment string `json:"comment"`
}
38 changes: 37 additions & 1 deletion hubapi/component-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,26 @@ package hubapi

import "time"

type bdJsonComponentDetailV4 struct{}

func (bdJsonComponentDetailV4) GetMimeType() string {
return "application/vnd.blackducksoftware.component-detail-4+json"
}

type bdJsonComponentDetailV5 struct{}

func (bdJsonComponentDetailV5) GetMimeType() string {
return "application/vnd.blackducksoftware.component-detail-5+json"
}

type ComponentList struct {
bdJsonComponentDetailV4
ItemsListBase
Items []ComponentVariant `json:"items"`
}

type ComponentVariant struct {
bdJsonComponentDetailV4
ComponentName string `json:"componentName"`
VersionName string `json:"versionName,omitempty"`
OriginID string `json:"originId,omitempty"`
Expand All @@ -31,11 +45,13 @@ type ComponentVariant struct {
}

type ComponentVersionList struct {
bdJsonComponentDetailV5
ItemsListBase
Items []ComponentVersion `json:"items"`
}

type ComponentVersion struct {
bdJsonComponentDetailV5
VersionName string `json:"versionName,omitempty"`
ReleasedOn time.Time `json:"releasedOn,omitempty"`
License ComplexLicense `json:"license"`
Expand All @@ -48,6 +64,7 @@ type ComponentVersion struct {
}

type ComponentVersionOriginList struct {
bdJsonComponentDetailV5
ItemsListBase
Items []ComponentVersionOrigin `json:"items"`
}
Expand All @@ -59,6 +76,7 @@ type ComponentVersionOrigin struct {
}

type ComponentVersionVulnerabilityList struct {
bdJsonVulnerabilityV4
ItemsListBase
Items []ComponentVersionVulnerability `json:"items"`
}
Expand All @@ -76,6 +94,7 @@ type ComponentVersionVulnerability struct {

// returned by "references" component meta link
type ComponentProjectReferenceList struct {
bdJsonApplicationJson // TODO get the type, nothing in documentation
ItemsListBase
Items []ComponentProjectReference
}
Expand All @@ -91,6 +110,7 @@ type ComponentProjectReference struct {
}

type Component struct {
bdJsonComponentDetailV4
Name string `json:"name"`
Description string `json:"description,omitempty"`
ApprovalStatus string `json:"approvalStatus"`
Expand All @@ -104,10 +124,26 @@ type Component struct {
}

type ComponentRequest struct {
bdJsonComponentDetailV4
Name string `json:"name"`
Description string `json:"description"`
Homepage string `json:"url,omitempty"`
AdditionalHomepages []string `json:"additionalHomepages"`
ApprovalStatus string `json:"approvalStatus"`
ApprovalStatus string `json:"approvalStatus"` // [UNREVIEWED, IN_REVIEW, REVIEWED, APPROVED, LIMITED_APPROVAL, REJECTED, DEPRECATED]
Type string `json:"type"`
}

type ComponentRemediation struct {
bdJsonComponentDetailV5
FixesPreviousVulnerabilities *RemediationInfo `json:"fixesPreviousVulnerabilities,omitempty"`
NoVulnerabilities *RemediationInfo `json:"noVulnerabilities,omitempty"`
LatestAfterCurrent *RemediationInfo `json:"latestAfterCurrent,omitempty"`
Meta Meta `json:"_meta"`
}

type RemediationInfo struct {
Name string `json:"name"`
ComponentVersion string `json:"componentVersion"`
ReleasedOn *time.Time `json:"releasedOn"`
VulnerabilityCount int `json:"vulnerabilityCount"`
}
1 change: 1 addition & 0 deletions hubapi/license-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package hubapi

type ComplexLicense struct {
bdJsonComponentDetailV5
Name string `json:"name"`
Ownership string `json:"ownership"`
CodeSharing string `json:"codeSharing"`
Expand Down
86 changes: 86 additions & 0 deletions hubapi/mime-types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package hubapi

import (
"reflect"
)

type HasMimeType interface {
GetMimeType() string
}

// GetMimeType extracts a MIME type for the "Accept" header for requests
// The ways to embed a mime type
// First method
// type bdJsonCoolV7 struct {}
// func (bdJsonV7) GetMimeType() string { return "application/vnd.blackducksoftware.cool-7+json" }
// type SomeTypeWeRetrieve struct {
// bdJsonCoolV7
// ... // rest of the fields
// }
//
// 2.
// type bdJsonHackyCoolV4 struct {
// __mimetype struct{} `mimetype:"application/vnd.blackducksoftware.hackycool-4+json"`
// }
// type SomeTypeWeRetrieve struct {
// bdJsonHackyCoolV4
// ... // rest of the fields
// }
//
// 3. use both :)
// Note: known so far are
// "application/vnd.blackducksoftware.admin-4+json",
// "application/vnd.blackducksoftware.bill-of-materials-4+json",
// "application/vnd.blackducksoftware.bill-of-materials-5+json",
// "application/vnd.blackducksoftware.bill-of-materials-6+json",
// "application/vnd.blackducksoftware.component-detail-4+json",
// "application/vnd.blackducksoftware.component-detail-5+json",
// "application/vnd.blackducksoftware.journal-4+json",
// "application/vnd.blackducksoftware.notification-4+json",
// "application/vnd.blackducksoftware.policy-4+json",
// "application/vnd.blackducksoftware.policy-5+json",
// "application/vnd.blackducksoftware.project-detail-4+json",
// "application/vnd.blackducksoftware.project-detail-5+json",
// "application/vnd.blackducksoftware.report-4+json",
// "application/vnd.blackducksoftware.scan-4+json",
// "application/vnd.blackducksoftware.status-4+json",
// "application/vnd.blackducksoftware.user-4+json",
// "application/vnd.blackducksoftware.vulnerability-4+json",
func GetMimeType(v interface{}) string {

if typer, ok := v.(HasMimeType); ok {
return typer.GetMimeType()
}

return GetMimeTypeFromTag(v)
}

func GetMimeTypeFromTag(v interface{}) string {
const mimeType = "mimetype"

f := reflect.TypeOf(v)
if f.Kind() == reflect.Ptr {
f = f.Elem()
}

if specialField, hasField := f.FieldByName("__" + mimeType); hasField {
if tag, hasTag := specialField.Tag.Lookup(mimeType); hasTag {
return tag
}
}

for i := 0; i < f.NumField(); i++ {
if tag, hasTag := f.Field(i).Tag.Lookup(mimeType); hasTag {
return tag
}
}

return ""
}

// We have no documentation, so we use fallback
type bdJsonApplicationJson struct{}

func (bdJsonApplicationJson) GetMimeType() string {
return "application/json"
}
9 changes: 9 additions & 0 deletions hubapi/policyrules-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ import (
"time"
)

type bdJsonPolicyV5 struct{}

func (bdJsonPolicyV5) GetMimeType() string {
return "application/vnd.blackducksoftware.policy-5+json"
}

type PolicyRuleList struct {
bdJsonPolicyV5
ItemsListBase
Items []PolicyRule `json:"items"`
}

type PolicyRule struct {
bdJsonPolicyV5
Name string `json:"name"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
Expand Down Expand Up @@ -60,6 +68,7 @@ type ExpressionParameter struct {
}

type PolicyRuleRequest struct {
bdJsonPolicyV5
Name string `json:"name"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
Expand Down
Loading

0 comments on commit 13acf0f

Please sign in to comment.