scorecard/cron/data/writer.go
Azeem Shaikh c06f89af83
Script to add new projects to projects.csv file (#567)
Co-authored-by: Azeem Shaikh <azeems@google.com>
2021-06-10 13:24:33 -07:00

77 lines
2.0 KiB
Go

// Copyright 2021 Security Scorecard Authors
//
// 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.
package data
import (
"encoding/csv"
"fmt"
"io"
"sort"
"github.com/jszwec/csvutil"
"github.com/ossf/scorecard/repos"
)
type repoEntry struct {
Repo string `csv:"repo"`
Metadata string `csv:"metadata"`
}
func repoEntryFromRepoURL(repoURLs []repos.RepoURL) []repoEntry {
repoentries := make([]repoEntry, 0)
for _, repoURL := range repoURLs {
repoentry := repoEntry{
Repo: repoURL.URL(),
Metadata: repoURL.Metadata,
}
repoentries = append(repoentries, repoentry)
}
return repoentries
}
func SortAndAppendTo(out io.Writer, oldRepos, newRepos []repos.RepoURL) error {
repoentries := repoEntryFromRepoURL(oldRepos)
repoentries = append(repoentries, repoEntryFromRepoURL(newRepos)...)
sort.SliceStable(repoentries, func(i, j int) bool {
return repoentries[i].Repo < repoentries[j].Repo
})
csvWriter := csv.NewWriter(out)
enc := csvutil.NewEncoder(csvWriter)
if err := enc.Encode(repoentries); err != nil {
return fmt.Errorf("error during Encode: %w", err)
}
csvWriter.Flush()
return nil
}
func SortAndAppend(out io.Writer, newRepos []repos.RepoURL) error {
iter, err := MakeIterator()
if err != nil {
return fmt.Errorf("error during MakeIterator: %w", err)
}
oldRepos := make([]repos.RepoURL, 0)
for iter.HasNext() {
repo, err := iter.Next()
if err != nil {
return fmt.Errorf("error during iter.Next: %w", err)
}
oldRepos = append(oldRepos, repo)
}
return SortAndAppendTo(out, oldRepos, newRepos)
}