github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/syz-cluster/pkg/service/base_finding.go (about)

     1  // Copyright 2025 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package service
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	"fmt"
    10  
    11  	"github.com/google/syzkaller/syz-cluster/pkg/api"
    12  	"github.com/google/syzkaller/syz-cluster/pkg/app"
    13  	"github.com/google/syzkaller/syz-cluster/pkg/db"
    14  )
    15  
    16  type BaseFindingService struct {
    17  	baseFindingRepo *db.BaseFindingRepository
    18  	buildRepo       *db.BuildRepository
    19  }
    20  
    21  func NewBaseFindingService(env *app.AppEnvironment) *BaseFindingService {
    22  	return &BaseFindingService{
    23  		baseFindingRepo: db.NewBaseFindingRepository(env.Spanner),
    24  		buildRepo:       db.NewBuildRepository(env.Spanner),
    25  	}
    26  }
    27  
    28  var ErrBuildNotFound = errors.New("build not found")
    29  
    30  func (s *BaseFindingService) Upload(ctx context.Context, info *api.BaseFindingInfo) error {
    31  	finding, err := s.makeBaseFinding(ctx, info)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	return s.baseFindingRepo.Save(ctx, finding)
    36  }
    37  
    38  func (s *BaseFindingService) Status(ctx context.Context, info *api.BaseFindingInfo) (
    39  	*api.BaseFindingStatus, error) {
    40  	finding, err := s.makeBaseFinding(ctx, info)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	exists, err := s.baseFindingRepo.Exists(ctx, finding)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return &api.BaseFindingStatus{
    49  		Observed: exists,
    50  	}, nil
    51  }
    52  
    53  func (s *BaseFindingService) makeBaseFinding(ctx context.Context, info *api.BaseFindingInfo) (*db.BaseFinding, error) {
    54  	build, err := s.buildRepo.GetByID(ctx, info.BuildID)
    55  	if err != nil {
    56  		return nil, fmt.Errorf("failed to query build: %w", err)
    57  	} else if build == nil {
    58  		return nil, ErrBuildNotFound
    59  	}
    60  	return &db.BaseFinding{
    61  		CommitHash: build.CommitHash,
    62  		Config:     build.ConfigName,
    63  		Arch:       build.Arch,
    64  		Title:      info.Title,
    65  	}, nil
    66  }