-
Notifications
You must be signed in to change notification settings - Fork 450
/
main.go
158 lines (141 loc) · 4.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// 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 main implements the PubSub controller.
package main
import (
"context"
"fmt"
"os"
"time"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/timestamppb"
"sigs.k8s.io/release-utils/version"
"github.com/ossf/scorecard/v4/clients"
"github.com/ossf/scorecard/v4/cron/internal/config"
"github.com/ossf/scorecard/v4/cron/internal/data"
"github.com/ossf/scorecard/v4/cron/internal/pubsub"
)
var headSHA = clients.HeadSHA
func publishToRepoRequestTopic(iter data.Iterator, topicPublisher pubsub.Publisher,
shardSize int, datetime time.Time,
) (int32, error) {
var shardNum int32
request := data.ScorecardBatchRequest{
JobTime: timestamppb.New(datetime),
ShardNum: &shardNum,
}
// Create and send batch requests of repoURLs of size `ShardSize`:
// * Iterate through incoming repoURLs until `request` has len(Repos) of size `ShardSize`.
// * Publish request to PubSub topic.
// * Clear request.Repos and increment shardNum.
for iter.HasNext() {
repoURL, err := iter.Next()
if err != nil {
return shardNum, fmt.Errorf("error reading repoURL: %w", err)
}
request.Repos = append(request.GetRepos(), &data.Repo{
Url: &repoURL.Repo,
// TODO(controller): pass in non-HEAD commitSHA here.
Commit: &headSHA,
Metadata: repoURL.Metadata.ToString(),
})
if len(request.GetRepos()) < shardSize {
continue
}
if err := topicPublisher.Publish(&request); err != nil {
return shardNum, fmt.Errorf("error running topicPublisher.Publish: %w", err)
}
request.Repos = nil
shardNum++
}
// Check if more repoURLs are pending to be sent in `request`.
if len(request.GetRepos()) > 0 {
if err := topicPublisher.Publish(&request); err != nil {
return shardNum, fmt.Errorf("error running topicPublisher.Publish: %w", err)
}
} else {
// shardNum incremented for an empty shard, decrement.
shardNum--
}
if err := topicPublisher.Close(); err != nil {
return shardNum, fmt.Errorf("error running topicPublisher.Close: %w", err)
}
return shardNum, nil
}
func main() {
ctx := context.Background()
t := time.Now()
if len(os.Args) != 2 {
panic("must provide a single argument")
}
inFile, err := os.OpenFile(os.Args[1], os.O_RDONLY, 0o644)
if err != nil {
panic(err)
}
reader, err := data.MakeIteratorFrom(inFile)
if err != nil {
panic(err)
}
topic, err := config.GetRequestTopicURL()
if err != nil {
panic(err)
}
topicPublisher, err := pubsub.CreatePublisher(ctx, topic)
if err != nil {
panic(err)
}
shardSize, err := config.GetShardSize()
if err != nil {
panic(err)
}
bucket, err := config.GetResultDataBucketURL()
if err != nil {
panic(err)
}
rawBucket, err := config.GetRawResultDataBucketURL()
if err != nil {
panic(err)
}
shardNum, err := publishToRepoRequestTopic(reader, topicPublisher, shardSize, t)
if err != nil {
panic(err)
}
// Populate `.shard_metadata` file.
metadata := data.ShardMetadata{
NumShard: new(int32),
ShardLoc: new(string),
CommitSha: new(string),
}
*metadata.NumShard = shardNum + 1
*metadata.ShardLoc = bucket + "/" + data.GetBlobFilename("", t)
*metadata.CommitSha = version.GetVersionInfo().GitCommit
metadataJSON, err := protojson.Marshal(&metadata)
if err != nil {
panic(fmt.Errorf("error during protojson.Marshal: %w", err))
}
err = data.WriteToBlobStore(ctx, bucket, data.GetShardMetadataFilename(t), metadataJSON)
if err != nil {
panic(fmt.Errorf("error writing to BlobStore: %w", err))
}
// Raw data.
*metadata.ShardLoc = rawBucket + "/" + data.GetBlobFilename("", t)
metadataJSON, err = protojson.Marshal(&metadata)
if err != nil {
panic(fmt.Errorf("error during protojson.Marshal raw: %w", err))
}
err = data.WriteToBlobStore(ctx, rawBucket, data.GetShardMetadataFilename(t), metadataJSON)
if err != nil {
panic(fmt.Errorf("error writing to BlobStore raw: %w", err))
}
}