Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add BigQuery loader function for static analysis #943

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions function/loader/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,56 @@ func Load(ctx context.Context, m PubSubMessage) error {
log.Printf("Job created: %s", job.ID())
return nil
}

func LoadStaticAnalysis(ctx context.Context, m PubSubMessage) error {
project := os.Getenv("GCP_PROJECT")
bucket := os.Getenv("OSSF_MALWARE_STATIC_ANALYSIS_RESULTS")

bq, err := bigquery.NewClient(ctx, project)
if err != nil {
return fmt.Errorf("failed to create BigQuery client: %w", err)

}
defer bq.Close()

schema, err := bigquery.SchemaFromJSON(staticAnalysisSchemaJSON)
if err != nil {
return fmt.Errorf("failed to decode schema: %w", err)
}

gcsRef := bigquery.NewGCSReference(fmt.Sprintf("gs://%s/*.json", bucket))
gcsRef.Schema = schema
gcsRef.SourceFormat = bigquery.JSON
gcsRef.MaxBadRecords = 10000

dataset := bq.Dataset("packages")
loader := dataset.Table("staticanalysis").LoaderFrom(gcsRef)
loader.WriteDisposition = bigquery.WriteTruncate
loader.TimePartitioning = &bigquery.TimePartitioning{
Type: bigquery.DayPartitioningType,
Field: "created",
}

job, err := loader.Run(ctx)
if err != nil {
return fmt.Errorf("failed to create load job: %v", err)
}

fmt.Printf("load job created: %s\n", job.ID())

status, err := job.Wait(ctx)
if err != nil {
return fmt.Errorf("error waiting for job: %w", err)
}

if status.Err() != nil {
fmt.Printf("job completed with %d errors\n", len(status.Errors))
for idx, err := range status.Errors {
fmt.Printf("error %d: %v\n", idx, err)
}

return status.Err()
}

return nil
}