code

package
v0.0.0-...-df5614f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2023 License: Apache-2.0 Imports: 45 Imported by: 0

Documentation

Overview

* © 2023 Khulnasoft Limited All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

Index

Constants

View Source
const (
	CreateBundleOperation           = "createBundle"
	ExtendBundleWithSourceOperation = "extendBundleWithSource"
	RunAnalysisOperation            = "runAnalysis"
	RunAutofixOperation             = "runAutofix"
	GetFiltersOperation             = "getFilters"
	FakeFileExtension               = ".java"
	// Helper constants to synchronize fake results and tests
	FakeAutofixSuggestionNewText = "FAKE_AUTOFIX_NEW_TEXT"
)

Variables

View Source
var (
	FakeKhulnasoftCodeApiServiceMutex = &sync.Mutex{}

	FakeCommand = khulnasoft.CommandData{
		Title:     "Code Flow blah blah fake",
		CommandId: khulnasoft.NavigateToRangeCommand,
		Arguments: []any{"path", fakeRange},
	}
	FakeFixCommand = khulnasoft.CommandData{
		Title:     "Code Flow blah blah fake",
		CommandId: khulnasoft.CodeFixCommand,
		Arguments: []any{"id", "path", fakeRange},
	}

	FakeIssue = khulnasoft.Issue{
		ID:               "KHULNASOFT-123",
		Range:            fakeRange,
		Severity:         khulnasoft.High,
		Product:          product.ProductCode,
		IssueType:        khulnasoft.CodeQualityIssue,
		Message:          "This is a dummy error (severity error)",
		CodelensCommands: []khulnasoft.CommandData{FakeCommand, FakeFixCommand},
		CodeActions:      []khulnasoft.CodeAction{FakeCodeAction},
		AdditionalData: khulnasoft.CodeIssueData{
			IsAutofixable: true,
		},
	}

	FakeCodeAction = khulnasoft.CodeAction{
		Title:   "FakeAction",
		Command: &FakeCommand,
	}

	FakeFilters        = []string{".cjs", ".ejs", ".es", ".es6", ".htm", ".html", ".js", ".jsx", ".mjs", ".ts", ".tsx", ".vue", ".java", ".erb", ".haml", ".rb", ".rhtml", ".slim", ".kt", ".swift", ".cls", ".config", ".pom", ".wxs", ".xml", ".xsd", ".aspx", ".cs", ".py", ".go", ".c", ".cc", ".cpp", ".cxx", ".h", ".hpp", ".hxx", ".php", ".phtml"}
	FakeAutofixFilters = []string{FakeFileExtension} // Main test scenario -- allowlist the fake file for autofix
)

Functions

func DecodePath

func DecodePath(encodedRelativePath string) (string, error)

func EncodePath

func EncodePath(relativePath string) string

func TempWorkdirWithVulnerabilities

func TempWorkdirWithVulnerabilities(t *testing.T) (filePath string, path string)

func ToAbsolutePath

func ToAbsolutePath(baseDir string, relativePath string) string

func ToRelativeUnixPath

func ToRelativeUnixPath(baseDir string, absoluteFilePath string) (string, error)

Types

type AnalysisOptions

type AnalysisOptions struct {
	// contains filtered or unexported fields
}

type AnalysisRequest

type AnalysisRequest struct {
	Key             AnalysisRequestKey `json:"key"`
	Severity        int                `json:"severity,omitempty"`
	Prioritized     bool               `json:"prioritized,omitempty"`
	Legacy          bool               `json:"legacy"`
	AnalysisContext codeRequestContext `json:"analysisContext"`
}

type AnalysisRequestKey

type AnalysisRequestKey struct {
	Type         string   `json:"type"`
	Hash         string   `json:"hash"`
	LimitToFiles []string `json:"limitToFiles,omitempty"`
	Shard        string   `json:"shard"`
}

type AnalysisStatus

type AnalysisStatus struct {
	// contains filtered or unexported fields
}

type AutofixFeedback

type AutofixFeedback struct {
	FixId           string             `json:"fixId"`
	Feedback        string             `json:"feedback"`
	AnalysisContext codeRequestContext `json:"analysisContext"`
}

type AutofixOptions

type AutofixOptions struct {
	// contains filtered or unexported fields
}

type AutofixRequest

type AutofixRequest struct {
	Key             AutofixRequestKey  `json:"key"`
	AnalysisContext codeRequestContext `json:"analysisContext"`
}

type AutofixRequestKey

type AutofixRequestKey struct {
	Type     string `json:"type"`
	Hash     string `json:"hash"`
	Shard    string `json:"shard"`
	FilePath string `json:"filePath"`
	RuleId   string `json:"ruleId"`
	// 1-based to comply with Sarif and Code API, see
	// https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html#_Ref493492556
	LineNum int `json:"lineNum"`
}

type AutofixResponse

type AutofixResponse struct {
	Status             string                     `json:"status"`
	AutofixSuggestions []autofixResponseSingleFix `json:"fixes"`
}

AutofixResponse is the json-based structure to which we can translate the results of the HTTP request to Autofix upstream.

type AutofixStatus

type AutofixStatus struct {
	// contains filtered or unexported fields
}

type AutofixSuggestion

type AutofixSuggestion struct {
	FixId       string
	AutofixEdit khulnasoft.WorkspaceEdit
}

AutofixSuggestion models a fix returned by autofix service

type Bundle

type Bundle struct {
	KhulnasoftCode KhulnasoftCodeClient
	BundleHash     string
	UploadBatches  []*UploadBatch
	Files          map[string]BundleFile
	// contains filtered or unexported fields
}

func (*Bundle) FetchDiagnosticsData

func (b *Bundle) FetchDiagnosticsData(
	ctx context.Context,
) ([]khulnasoft.Issue, error)

func (*Bundle) Upload

func (b *Bundle) Upload(ctx context.Context, uploadBatch *UploadBatch) error

type BundleFile

type BundleFile struct {
	Hash    string `json:"hash"`
	Content string `json:"content"`
}

type BundleUploader

type BundleUploader struct {
	KhulnasoftCode KhulnasoftCodeClient
	// contains filtered or unexported fields
}

func NewBundler

func NewBundler(KhulnasoftCode KhulnasoftCodeClient, instrumentor performance.Instrumentor) *BundleUploader

func (*BundleUploader) Upload

func (b *BundleUploader) Upload(ctx context.Context, bundle Bundle, files map[string]BundleFile) (Bundle, error)

type FakeKhulnasoftCodeClient

type FakeKhulnasoftCodeClient struct {
	Calls               map[string][][]any
	HasCreatedNewBundle bool
	HasExtendedBundle   bool
	ExtendBundleFiles   map[string]BundleFile
	TotalBundleCount    int
	ExtendedBundleCount int
	AnalysisDuration    time.Duration
	FailOnCreateBundle  bool
	ConfigFiles         []string

	NoFixSuggestions bool
	// contains filtered or unexported fields
}

func (*FakeKhulnasoftCodeClient) Clear

func (f *FakeKhulnasoftCodeClient) Clear()

func (*FakeKhulnasoftCodeClient) CreateBundle

func (f *FakeKhulnasoftCodeClient) CreateBundle(_ context.Context,
	files map[string]string,
) (bundleHash string, missingFiles []string, err error)

func (*FakeKhulnasoftCodeClient) ExtendBundle

func (f *FakeKhulnasoftCodeClient) ExtendBundle(
	_ context.Context,
	bundleHash string,
	files map[string]BundleFile,
	removedFiles []string,
) (string, []string, error)

func (*FakeKhulnasoftCodeClient) GetAllCalls

func (f *FakeKhulnasoftCodeClient) GetAllCalls(op string) [][]any

func (*FakeKhulnasoftCodeClient) GetCallParams

func (f *FakeKhulnasoftCodeClient) GetCallParams(callNo int, op string) []any

func (*FakeKhulnasoftCodeClient) GetFilters

func (f *FakeKhulnasoftCodeClient) GetFilters(_ context.Context) (
	filters FiltersResponse,
	err error,
)

func (*FakeKhulnasoftCodeClient) RunAnalysis

func (*FakeKhulnasoftCodeClient) RunAutofix

func (*FakeKhulnasoftCodeClient) SubmitAutofixFeedback

func (f *FakeKhulnasoftCodeClient) SubmitAutofixFeedback(ctx context.Context, fixId string, positive bool) error

type FiltersResponse

type FiltersResponse struct {
	ConfigFiles []string `json:"configFiles" pact:"min=1"`
	Extensions  []string `json:"extensions" pact:"min=1"`
}

type KhulnasoftAnalysisFailedError

type KhulnasoftAnalysisFailedError struct {
	Msg string
}

func (KhulnasoftAnalysisFailedError) Error

type KhulnasoftAutofixFailedError

type KhulnasoftAutofixFailedError struct {
	Msg string
}

Should implement `error` interface

func (KhulnasoftAutofixFailedError) Error

type KhulnasoftCodeClient

type KhulnasoftCodeClient interface {
	GetFilters(ctx context.Context) (
		filters FiltersResponse,
		err error)

	CreateBundle(
		ctx context.Context,
		files map[string]string,
	) (newBundleHash string, missingFiles []string, err error)

	ExtendBundle(
		ctx context.Context,
		bundleHash string,
		files map[string]BundleFile,
		removedFiles []string,
	) (newBundleHash string, missingFiles []string, err error)

	RunAnalysis(
		ctx context.Context,
		options AnalysisOptions,
		baseDir string,
	) (
		[]khulnasoft.Issue,
		AnalysisStatus,
		error,
	)

	RunAutofix(
		ctx context.Context,
		options AutofixOptions,
		baseDir string,
	) ([]AutofixSuggestion,
		AutofixStatus,
		error,
	)

	SubmitAutofixFeedback(ctx context.Context, fixId string, positive bool) error
}

type KhulnasoftCodeHTTPClient

type KhulnasoftCodeHTTPClient struct {
	// contains filtered or unexported fields
}

func NewHTTPRepository

func NewHTTPRepository(
	instrumentor performance2.Instrumentor,
	errorReporter error_reporting.ErrorReporter,
	client func() *http.Client,
) *KhulnasoftCodeHTTPClient

func (*KhulnasoftCodeHTTPClient) CreateBundle

func (s *KhulnasoftCodeHTTPClient) CreateBundle(
	ctx context.Context,
	filesToFilehashes map[string]string,
) (string, []string, error)

func (*KhulnasoftCodeHTTPClient) ExtendBundle

func (s *KhulnasoftCodeHTTPClient) ExtendBundle(
	ctx context.Context,
	bundleHash string,
	files map[string]BundleFile,
	removedFiles []string,
) (string, []string, error)

func (*KhulnasoftCodeHTTPClient) GetFilters

func (s *KhulnasoftCodeHTTPClient) GetFilters(ctx context.Context) (
	filters FiltersResponse,
	err error,
)

func (*KhulnasoftCodeHTTPClient) RunAnalysis

func (s *KhulnasoftCodeHTTPClient) RunAnalysis(
	ctx context.Context,
	options AnalysisOptions,
	baseDir string,
) ([]khulnasoft.Issue, AnalysisStatus, error)

func (*KhulnasoftCodeHTTPClient) RunAutofix

func (s *KhulnasoftCodeHTTPClient) RunAutofix(
	ctx context.Context,
	options AutofixOptions,
	baseDir string,
) ([]AutofixSuggestion,
	AutofixStatus,
	error,
)

func (*KhulnasoftCodeHTTPClient) SubmitAutofixFeedback

func (s *KhulnasoftCodeHTTPClient) SubmitAutofixFeedback(ctx context.Context, fixId string, positive bool) error

type SarifResponse

type SarifResponse struct {
	Type     string  `json:"type"`
	Progress float64 `json:"progress"`
	Status   string  `json:"status"`
	Timing   struct {
		FetchingCode int `json:"fetchingCode"`
		Queue        int `json:"queue"`
		Analysis     int `json:"analysis"`
	} `json:"timing"`
	Coverage []struct {
		Files       int    `json:"files"`
		IsSupported bool   `json:"isSupported"`
		Lang        string `json:"lang"`
	} `json:"coverage"`
	Sarif struct {
		Schema  string `json:"$schema"`
		Version string `json:"version"`
		Runs    []run  `json:"runs"`
	} `json:"sarif"`
}

type ScanMetrics

type ScanMetrics struct {
	// contains filtered or unexported fields
}

type ScanStatus

type ScanStatus struct {
	// contains filtered or unexported fields
}

func NewScanStatus

func NewScanStatus() *ScanStatus

type Scanner

type Scanner struct {
	BundleUploader      *BundleUploader
	KhulnasoftApiClient khulnasoft_api.KhulnasoftApiClient
	// contains filtered or unexported fields
}

func New

func New(bundleUploader *BundleUploader,
	apiClient khulnasoft_api.KhulnasoftApiClient,
	reporter error_reporting.ErrorReporter,
	analytics ux2.Analytics,
	learnService learn.Service,
	notifier notification.Notifier,
) *Scanner

func (*Scanner) IsEnabled

func (sc *Scanner) IsEnabled() bool

func (*Scanner) Product

func (sc *Scanner) Product() product.Product

func (*Scanner) Scan

func (sc *Scanner) Scan(ctx context.Context, path string, folderPath string) (issues []khulnasoft.Issue, err error)

func (*Scanner) SupportedCommands

func (sc *Scanner) SupportedCommands() []khulnasoft.CommandName

func (*Scanner) UploadAndAnalyze

func (sc *Scanner) UploadAndAnalyze(ctx context.Context,
	files <-chan string,
	path string,
	scanMetrics *ScanMetrics,
	changedFiles map[string]bool,
) (issues []khulnasoft.Issue, err error)

type UploadBatch

type UploadBatch struct {
	// contains filtered or unexported fields
}

func NewUploadBatch

func NewUploadBatch() *UploadBatch

type UploadStatus

type UploadStatus struct {
	UploadedFiles int
	TotalFiles    int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL