| package ingestion |
| |
| import ( |
| "context" |
| "fmt" |
| "sort" |
| "time" |
| |
| "go.skia.org/infra/go/depot_tools" |
| "go.skia.org/infra/go/vcsinfo" |
| ) |
| |
| // TODO(stephana): Make the GetFile function support lookup with a combination |
| // of fileName and commitHash to truly reflect the functionality of the VCS interface. |
| |
| type MockVCSImpl struct { |
| commits []*vcsinfo.LongCommit |
| depsFileMap map[string]string |
| pathContentMap map[string]string |
| secondaryVCS vcsinfo.VCS |
| secondaryExtractor depot_tools.DEPSExtractor |
| } |
| |
| // MockVCS returns an instance of VCS that returns the commits passed as |
| // arguments. |
| // To control the GetFile function use these two parameters: |
| // depsContentMap maps commits from a hash to a dependency file. |
| // pathContentMap maps file names to string content. |
| // Currently the GetFile function will only consider the fileName or the hash |
| // but not a combination of both. The fileName has priority. |
| func MockVCS(commits []*vcsinfo.LongCommit, depsContentMap map[string]string, pathContentMap map[string]string) vcsinfo.VCS { |
| return MockVCSImpl{ |
| commits: commits, |
| depsFileMap: depsContentMap, |
| pathContentMap: pathContentMap, |
| } |
| } |
| |
| func (m MockVCSImpl) Update(ctx context.Context, pull, allBranches bool) error { return nil } |
| func (m MockVCSImpl) LastNIndex(N int) []*vcsinfo.IndexCommit { return nil } |
| func (m MockVCSImpl) Range(begin, end time.Time) []*vcsinfo.IndexCommit { return nil } |
| func (m MockVCSImpl) IndexOf(ctx context.Context, hash string) (int, error) { |
| return 0, nil |
| } |
| func (m MockVCSImpl) From(start time.Time) []string { |
| idx := sort.Search(len(m.commits), func(i int) bool { return m.commits[i].Timestamp.Unix() >= start.Unix() }) |
| |
| ret := make([]string, 0, len(m.commits)-idx) |
| for _, commit := range m.commits[idx:] { |
| ret = append(ret, commit.Hash) |
| } |
| return ret |
| } |
| |
| func (m MockVCSImpl) Details(ctx context.Context, hash string, getBranches bool) (*vcsinfo.LongCommit, error) { |
| for _, commit := range m.commits { |
| if commit.Hash == hash { |
| return commit, nil |
| } |
| } |
| return nil, fmt.Errorf("Unable to find commit") |
| } |
| |
| func (m MockVCSImpl) ByIndex(ctx context.Context, N int) (*vcsinfo.LongCommit, error) { |
| return nil, nil |
| } |
| |
| func (m MockVCSImpl) GetFile(ctx context.Context, fileName, commitHash string) (string, error) { |
| // fileName must be non-empty to be considered. |
| if ret, ok := m.pathContentMap[fileName]; (fileName != "") && ok { |
| return ret, nil |
| } |
| // gitHash must be non-empty to be considered. |
| if ret, ok := m.depsFileMap[commitHash]; (commitHash != "") && ok { |
| return ret, nil |
| } |
| return "", fmt.Errorf("Unable to find file '%s' for commit '%s'", fileName, commitHash) |
| } |
| |
| func (m MockVCSImpl) ResolveCommit(ctx context.Context, commitHash string) (string, error) { |
| if m.secondaryVCS == nil { |
| return "", nil |
| } |
| foundCommit, err := m.secondaryExtractor.ExtractCommit(m.secondaryVCS.GetFile(ctx, "DEPS", commitHash)) |
| if err != nil { |
| return "", err |
| } |
| return foundCommit, nil |
| } |
| |
| func (m MockVCSImpl) SetSecondaryRepo(secVCS vcsinfo.VCS, extractor depot_tools.DEPSExtractor) { |
| m.secondaryVCS = secVCS |
| m.secondaryExtractor = extractor |
| } |