Skip to content

Commit

Permalink
Move build vcs code into a package so it can be reused. (#500)
Browse files Browse the repository at this point in the history
Signed-off-by: Caleb Brown <[email protected]>
  • Loading branch information
calebbrown authored Dec 13, 2023
1 parent da9dc22 commit b3aca0a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/collect_signals/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/ossf/criticality_score/cmd/collect_signals/vcs"
"github.com/ossf/criticality_score/internal/collector"
log "github.com/ossf/criticality_score/internal/log"
)
Expand Down Expand Up @@ -93,7 +94,7 @@ func main() {
defer logger.Sync()

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

Expand Down
26 changes: 18 additions & 8 deletions cmd/collect_signals/vcs.go → cmd/collect_signals/vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package vcs

import (
"runtime/debug"
)

const commitIDKey = "vcs.revision"
const (
MissingCommitID = "-missing-"
commitIDKey = "vcs.revision"
)

var commitID = getCommitID()
var commitID string

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

for _, setting := range info.Settings {
Expand All @@ -36,5 +37,14 @@ func getCommitID() string {
}
}

return ""
return MissingCommitID
}

// CommitID returns the vcs commit ID embedded in the binary when the
// -buildvcs flag is set while building.
func CommitID() string {
if commitID == "" {
commitID = fetchCommitID()
}
return commitID
}
5 changes: 3 additions & 2 deletions cmd/collect_signals/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"go.opencensus.io/stats/view"
"go.uber.org/zap"

"github.com/ossf/criticality_score/cmd/collect_signals/vcs"
"github.com/ossf/criticality_score/internal/collector"
"github.com/ossf/criticality_score/internal/scorer"
"github.com/ossf/criticality_score/internal/signalio"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (w *collectWorker) Process(ctx context.Context, req *data.ScorecardBatchReq
extras = append(extras, w.scoreColumnName)
}
extras = append(extras, collectionDateColumnName)
if commitID != "" {
if commitID := vcs.CommitID(); commitID != vcs.MissingCommitID {
extras = append(extras, commitIDColumnName)
}

Expand Down Expand Up @@ -111,7 +112,7 @@ func (w *collectWorker) Process(ctx context.Context, req *data.ScorecardBatchReq

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

0 comments on commit b3aca0a

Please sign in to comment.