Skip to content

Commit

Permalink
Version hint flag (#213)
Browse files Browse the repository at this point in the history
* Add version hints flag
  • Loading branch information
filip-debricked authored Mar 21, 2024
1 parent 0cf4149 commit 2d07f2e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
9 changes: 9 additions & 0 deletions internal/cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var jsonFilePath string
var exclusions = file.Exclusions()
var verbose bool
var regenerate int
var versionHint bool
var noResolve bool
var noFingerprint bool
var passOnDowntime bool
Expand All @@ -40,6 +41,7 @@ const (
IntegrationFlag = "integration"
ExclusionFlag = "exclusion"
VerboseFlag = "verbose"
VersionHintFlag = "version-hint"
RegenerateFlag = "regenerate"
NoResolveFlag = "no-resolve"
FingerprintFlag = "fingerprint"
Expand Down Expand Up @@ -110,6 +112,12 @@ $ debricked scan . `+exampleFlags)
"\nExample:\n$ debricked resolve . --regenerate=1",
}, "\n")
cmd.Flags().IntVar(&regenerate, RegenerateFlag, 0, regenerateDoc)
versionHintDoc := strings.Join(
[]string{
"Toggles version hinting, i.e using manifest versions to help manifestless resolution.\n",
"\nExample:\n$ debricked scan . --version-hint=false",
}, "\n")
cmd.Flags().BoolVar(&versionHint, VersionHintFlag, true, versionHintDoc)
verboseDoc := strings.Join(
[]string{
"This flag allows you to reduce error output for resolution.",
Expand Down Expand Up @@ -156,6 +164,7 @@ func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error {
Exclusions: viper.GetStringSlice(ExclusionFlag),
Verbose: viper.GetBool(VerboseFlag),
Regenerate: viper.GetInt(RegenerateFlag),
VersionHint: viper.GetBool(VersionHintFlag),
RepositoryName: viper.GetString(RepositoryFlag),
CommitName: viper.GetString(CommitFlag),
BranchName: viper.GetString(BranchFlag),
Expand Down
2 changes: 2 additions & 0 deletions internal/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type DebrickedOptions struct {
Exclusions []string
Verbose bool
Regenerate int
VersionHint bool
RepositoryName string
CommitName string
BranchName string
Expand Down Expand Up @@ -206,6 +207,7 @@ func (dScanner *DebrickedScanner) scan(options DebrickedOptions, gitMetaObject g
GitMetaObject: gitMetaObject,
IntegrationsName: options.IntegrationName,
CallGraphUploadTimeout: options.CallGraphUploadTimeout,
VersionHint: options.VersionHint,
}
result, err := (*dScanner.uploader).Upload(uploaderOptions)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/upload/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ type uploadBatch struct {
integrationName string
ciUploadId int
callGraphTimeout int
versionHint bool
}

func newUploadBatch(client *client.IDebClient, fileGroups file.Groups, gitMetaObject *git.MetaObject, integrationName string, callGraphTimeout int) *uploadBatch {
return &uploadBatch{client: client, fileGroups: fileGroups, gitMetaObject: gitMetaObject, integrationName: integrationName, ciUploadId: 0, callGraphTimeout: callGraphTimeout}
func newUploadBatch(client *client.IDebClient, fileGroups file.Groups, gitMetaObject *git.MetaObject, integrationName string, callGraphTimeout int, versionHint bool) *uploadBatch {
return &uploadBatch{client: client, fileGroups: fileGroups, gitMetaObject: gitMetaObject, integrationName: integrationName, ciUploadId: 0, callGraphTimeout: callGraphTimeout, versionHint: versionHint}
}

// upload concurrently posts all file groups to Debricked
Expand Down Expand Up @@ -126,7 +127,6 @@ func (uploadBatch *uploadBatch) uploadFile(filePath string, timeout int) error {
if uploadBatch.initialized() {
_ = writer.WriteField("ciUploadId", strconv.Itoa(uploadBatch.ciUploadId))
}

response, err := (*uploadBatch.client).Post(
"/api/1.0/open/uploads/dependencies/files",
writer.FormDataContentType(),
Expand All @@ -136,11 +136,9 @@ func (uploadBatch *uploadBatch) uploadFile(filePath string, timeout int) error {
if err != nil {
return err
}

if !uploadBatch.initialized() {
data, _ := io.ReadAll(response.Body)
defer response.Body.Close()

uFile := uploadedFile{}
_ = json.Unmarshal(data, &uFile)
if uFile.CiUploadId == 0 {
Expand All @@ -163,6 +161,7 @@ func (uploadBatch *uploadBatch) initAnalysis() error {
IntegrationName: uploadBatch.integrationName,
CommitName: uploadBatch.gitMetaObject.CommitName,
Author: uploadBatch.gitMetaObject.Author,
VersionHint: uploadBatch.versionHint,
DebrickedIntegration: "cli",
})

Expand Down Expand Up @@ -277,6 +276,7 @@ type uploadFinish struct {
CommitName string `json:"commitName"`
Author string `json:"author"`
DebrickedIntegration string `json:"debrickedIntegration"`
VersionHint bool `json:"versionHint"`
}

func getRelativeFilePath(filePath string) string {
Expand Down
10 changes: 5 additions & 5 deletions internal/upload/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestUploadWithBadFiles(t *testing.T) {
clientMock.AddMockResponse(mockRes)
clientMock.AddMockResponse(mockRes)
c = clientMock
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60)
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, true)
var buf bytes.Buffer
log.SetOutput(&buf)
err = batch.upload()
Expand All @@ -48,7 +48,7 @@ func TestUploadWithBadFiles(t *testing.T) {
}

func TestInitAnalysisWithoutAnyFiles(t *testing.T) {
batch := newUploadBatch(nil, file.Groups{}, nil, "CLI", 10*60)
batch := newUploadBatch(nil, file.Groups{}, nil, "CLI", 10*60, true)
err := batch.initAnalysis()

assert.ErrorContains(t, err, "failed to find dependency files")
Expand All @@ -71,7 +71,7 @@ func TestWaitWithPollingTerminatedError(t *testing.T) {
}
clientMock.AddMockResponse(mockRes)
c = clientMock
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60)
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, true)

uploadResult, err := batch.wait()

Expand All @@ -96,7 +96,7 @@ func TestInitUploadBadFile(t *testing.T) {
clientMock.AddMockResponse(mockRes)

var c client.IDebClient = clientMock
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60)
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, true)

files, err := batch.initUpload()

Expand All @@ -123,7 +123,7 @@ func TestInitUpload(t *testing.T) {
clientMock.AddMockResponse(mockRes)

var c client.IDebClient = clientMock
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60)
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, true)

files, err := batch.initUpload()

Expand Down
3 changes: 2 additions & 1 deletion internal/upload/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type DebrickedOptions struct {
GitMetaObject git.MetaObject
IntegrationsName string
CallGraphUploadTimeout int
VersionHint bool
}

type IUploader interface {
Expand All @@ -35,7 +36,7 @@ func NewUploader(c client.IDebClient) (*Uploader, error) {

func (uploader *Uploader) Upload(o IOptions) (*UploadResult, error) {
dOptions := o.(DebrickedOptions)
batch := newUploadBatch(uploader.client, dOptions.FileGroups, &dOptions.GitMetaObject, dOptions.IntegrationsName, dOptions.CallGraphUploadTimeout)
batch := newUploadBatch(uploader.client, dOptions.FileGroups, &dOptions.GitMetaObject, dOptions.IntegrationsName, dOptions.CallGraphUploadTimeout, dOptions.VersionHint)

err := batch.upload()
if err != nil {
Expand Down

0 comments on commit 2d07f2e

Please sign in to comment.