2022-12-02 02:08:48 +03:00
|
|
|
// Copyright 2021 OpenSSF Scorecard Authors
|
2021-05-15 00:32:23 +03:00
|
|
|
//
|
2021-05-24 08:01:56 +03:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2021-05-15 00:32:23 +03:00
|
|
|
// 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.
|
2021-05-24 08:01:56 +03:00
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-05-15 00:32:23 +03:00
|
|
|
|
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-09-14 04:57:31 +03:00
|
|
|
"reflect"
|
2021-05-15 00:32:23 +03:00
|
|
|
|
|
|
|
"github.com/jszwec/csvutil"
|
2021-05-24 06:51:52 +03:00
|
|
|
|
2024-04-13 00:51:50 +03:00
|
|
|
"github.com/ossf/scorecard/v5/clients/githubrepo"
|
|
|
|
"github.com/ossf/scorecard/v5/clients/gitlabrepo"
|
2021-05-15 00:32:23 +03:00
|
|
|
)
|
|
|
|
|
2021-08-01 01:31:34 +03:00
|
|
|
// Iterator interface is used to iterate through list of input repos for the cron job.
|
2021-05-15 00:32:23 +03:00
|
|
|
type Iterator interface {
|
|
|
|
HasNext() bool
|
2021-10-29 19:07:46 +03:00
|
|
|
Next() (RepoFormat, error)
|
2021-05-15 00:32:23 +03:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:31:34 +03:00
|
|
|
// MakeIteratorFrom returns an implementation of Iterator interface.
|
|
|
|
// Currently returns an instance of csvIterator.
|
2021-05-20 04:00:44 +03:00
|
|
|
func MakeIteratorFrom(reader io.Reader) (Iterator, error) {
|
2021-07-19 03:33:45 +03:00
|
|
|
csvReader := csv.NewReader(reader)
|
|
|
|
csvReader.Comment = '#'
|
2022-09-14 04:57:31 +03:00
|
|
|
header, err := csvutil.Header(RepoFormat{}, "csv")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error in csvutil.Header: %w", err)
|
|
|
|
}
|
|
|
|
dec, err := csvutil.NewDecoder(csvReader, header...)
|
2021-05-15 00:32:23 +03:00
|
|
|
if err != nil {
|
2021-05-20 04:00:44 +03:00
|
|
|
return nil, fmt.Errorf("error in csvutil.NewDecoder: %w", err)
|
2021-05-15 00:32:23 +03:00
|
|
|
}
|
|
|
|
return &csvIterator{decoder: dec}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type csvIterator struct {
|
2022-09-14 04:57:31 +03:00
|
|
|
decoder *csvutil.Decoder
|
|
|
|
err error
|
|
|
|
next RepoFormat
|
|
|
|
afterHeader bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true on the first call if the most recently decoded record is a header.
|
|
|
|
// always returns false on subsequent calls, as this is only intended to evaluate the first line.
|
|
|
|
func (reader *csvIterator) isHeader() bool {
|
|
|
|
if reader.afterHeader {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
header, err := csvutil.Header(RepoFormat{}, "csv")
|
|
|
|
if err != nil {
|
|
|
|
reader.err = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
lastRead := reader.decoder.Record()
|
|
|
|
reader.afterHeader = true
|
|
|
|
return reflect.DeepEqual(header, lastRead)
|
2021-05-15 00:32:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (reader *csvIterator) HasNext() bool {
|
|
|
|
reader.err = reader.decoder.Decode(&reader.next)
|
2022-09-14 04:57:31 +03:00
|
|
|
if reader.isHeader() {
|
|
|
|
reader.err = reader.decoder.Decode(&reader.next)
|
|
|
|
}
|
2021-05-30 07:21:43 +03:00
|
|
|
return !errors.Is(reader.err, io.EOF)
|
2021-05-15 00:32:23 +03:00
|
|
|
}
|
|
|
|
|
2021-10-29 19:07:46 +03:00
|
|
|
func (reader *csvIterator) Next() (RepoFormat, error) {
|
2021-05-15 00:32:23 +03:00
|
|
|
if reader.err != nil {
|
2021-10-29 19:07:46 +03:00
|
|
|
return reader.next, fmt.Errorf("reader has error: %w", reader.err)
|
2021-05-15 00:32:23 +03:00
|
|
|
}
|
2023-05-25 00:43:36 +03:00
|
|
|
|
|
|
|
repoURI := reader.next.Repo
|
|
|
|
|
|
|
|
// validate gitlab or github url
|
|
|
|
if _, err := gitlabrepo.MakeGitlabRepo(repoURI); err != nil {
|
|
|
|
if _, err := githubrepo.MakeGithubRepo(repoURI); err != nil {
|
|
|
|
return reader.next, fmt.Errorf("invalid URL, neither github nor gitlab: %w", err)
|
|
|
|
}
|
2021-10-21 00:18:42 +03:00
|
|
|
}
|
2021-10-29 19:07:46 +03:00
|
|
|
return reader.next, nil
|
2021-05-15 00:32:23 +03:00
|
|
|
}
|
2022-09-08 14:32:52 +03:00
|
|
|
|
|
|
|
func MakeNestedIterator(iterators []Iterator) (Iterator, error) {
|
|
|
|
return &nestedIterator{iterators: iterators}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type nestedIterator struct {
|
|
|
|
iterators []Iterator
|
|
|
|
current int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *nestedIterator) HasNext() bool {
|
|
|
|
for i.current < len(i.iterators) {
|
|
|
|
if i.iterators[i.current].HasNext() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
i.current++
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *nestedIterator) Next() (RepoFormat, error) {
|
|
|
|
r, err := i.iterators[i.current].Next()
|
|
|
|
if err != nil {
|
|
|
|
return RepoFormat{}, fmt.Errorf("nestedIterator.Next(): %w", err)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|