Skip to content

Commit

Permalink
Add the Git commit ID to the data produced by the worker. (#261)
Browse files Browse the repository at this point in the history
* Add the Git commit ID to the data produced by the worker.

Signed-off-by: Caleb Brown <calebbrown@google.com>

* Small refactor to cache the commitID once per execution.

Signed-off-by: Caleb Brown <calebbrown@google.com>

Signed-off-by: Caleb Brown <calebbrown@google.com>
  • Loading branch information
calebbrown committed Nov 28, 2022
1 parent e2ee6f1 commit 14585f3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/collect_signals/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ COPY . ./
FROM base AS collect_signals
ARG TARGETOS
ARG TARGETARCH
RUN CGO_ENABLED=0 go build ./cmd/collect_signals
RUN CGO_ENABLED=0 go build -buildvcs ./cmd/collect_signals
RUN chmod -R 0775 /src/config/scorer/*

FROM gcr.io/distroless/base:nonroot@sha256:533c15ef2acb1d3b1cd4e58d8aa2740900cae8f579243a53c53a6e28bcac0684
Expand Down
5 changes: 5 additions & 0 deletions cmd/collect_signals/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func main() {
}
defer logger.Sync()

// Embed the commitID with all log messages.
if commitID != "" {
logger = logger.With(zap.String("commit_id", commitID))
}

// Extract the GCP project ID.
gcpProjectID, err := config.GetProjectID()
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions cmd/collect_signals/vcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2022 Criticality Score 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
//
// https://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

import (
"runtime/debug"
)

const commitIDKey = "vcs.revision"

var commitID = getCommitID()

// getCommitID returns the vcs commit ID embedded in the binary when the
// -buildvcs flag is set while building.
func getCommitID() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}

for _, setting := range info.Settings {
if setting.Key == commitIDKey {
return setting.Value
}
}

return ""
}
17 changes: 16 additions & 1 deletion cmd/collect_signals/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (
"github.com/ossf/criticality_score/internal/signalio"
)

const collectionDateColumnName = "collection_date"
const (
collectionDateColumnName = "collection_date"
commitIDColumnName = "worker_commit_id"
)

type collectWorker struct {
logger *zap.Logger
Expand Down Expand Up @@ -51,6 +54,9 @@ func (w *collectWorker) Process(ctx context.Context, req *data.ScorecardBatchReq
extras = append(extras, w.scoreColumnName)
}
extras = append(extras, collectionDateColumnName)
if commitID != "" {
extras = append(extras, commitIDColumnName)
}

var jsonOutput bytes.Buffer
jsonOut := signalio.JSONWriter(&jsonOutput)
Expand Down Expand Up @@ -102,6 +108,15 @@ func (w *collectWorker) Process(ctx context.Context, req *data.ScorecardBatchReq
Value: jobTime,
})

// Ensure the commit ID is included with each record for helping
// identify which Git commit is associated with this record.
if commitID != "" {
extras = append(extras, signalio.Field{
Key: commitIDColumnName,
Value: commitID,
})
}

// Write the signals to storage.
if err := jsonOut.WriteSignals(ss, extras...); err != nil {
return fmt.Errorf("failed writing signals: %w", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/criticality_score/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ $ export GITHUB_TOKEN=ghp_abc,ghp_123
BigQuery access requires the "BigQuery User" (`roles/bigquery.user`) role added
to the account used, or be an "Owner".

##### Option 1: `gcloud login`
##### Option 1: `gcloud auth login`

This option is useful during development. Run `gcloud login --update-adc` to
login to GCP and prepare application default credentials.
This option is useful during development. Run `gcloud auth login --update-adc`
to login to GCP and prepare application default credentials.

##### Option 2: GCE Service Worker

Expand Down

0 comments on commit 14585f3

Please sign in to comment.