Skip to content

Commit

Permalink
feat: add __ID__ pipeline marker for the eval id
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Aug 30, 2024
1 parent 1949473 commit f8322c0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func ProcessMR(ctx context.Context, client scm.Client, cfg *config.Config, event
ctx = state.WithStartTime(ctx, time.Now())

// Attach unique eval id to the logs so they are easy to filter on later
ctx = slogctx.With(ctx, slog.String("eval_id", sid.MustGenerate()))
ctx = state.WithEvaluationID(ctx, sid.MustGenerate())

// Track where we grab the configuration file from
ctx = slogctx.With(ctx, slog.String("config_source_branch", "merge_request_branch"))
Expand Down
6 changes: 4 additions & 2 deletions pkg/scm/gitlab/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ func (client *Client) Start(ctx context.Context) error {

if len(pattern) != 0 {
link := pattern
link = strings.ReplaceAll(link, "__PROJECT_ID__", state.ProjectID(ctx))
link = strings.ReplaceAll(link, "__ID__", state.EvaluationID(ctx))
link = strings.ReplaceAll(link, "__MR_ID__", state.MergeRequestID(ctx))
link = strings.ReplaceAll(link, "__PROJECT_ID__", state.ProjectID(ctx))
link = strings.ReplaceAll(link, "__START_TS_MS__", strconv.FormatInt(state.StartTime(ctx).UnixMilli(), 10))
link = strings.ReplaceAll(link, "__STOP_TS_MS__", "")

Expand Down Expand Up @@ -158,8 +159,9 @@ func (client *Client) Stop(ctx context.Context, err error) error {

if len(pattern) != 0 {
link := pattern
link = strings.ReplaceAll(link, "__PROJECT_ID__", state.ProjectID(ctx))
link = strings.ReplaceAll(link, "__ID__", state.EvaluationID(ctx))
link = strings.ReplaceAll(link, "__MR_ID__", state.MergeRequestID(ctx))
link = strings.ReplaceAll(link, "__PROJECT_ID__", state.ProjectID(ctx))
link = strings.ReplaceAll(link, "__START_TS_MS__", strconv.FormatInt(state.StartTime(ctx).UnixMilli(), 10))
link = strings.ReplaceAll(link, "__STOP_TS_MS__", strconv.FormatInt(time.Now().UnixMilli(), 10))

Expand Down
13 changes: 12 additions & 1 deletion pkg/state/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ const (
mergeRequestID
projectID
provider
startTime
token
updatePipeline
updatePipelineURL
startTime
evaluationID
)

func ProjectID(ctx context.Context) string {
return ctx.Value(projectID).(string) //nolint:forcetypeassert
}

func EvaluationID(ctx context.Context) string {
return ctx.Value(evaluationID).(string) //nolint:forcetypeassert
}

func CommitSHA(ctx context.Context) string {
return ctx.Value(commitSha).(string) //nolint:forcetypeassert
}
Expand Down Expand Up @@ -62,6 +67,12 @@ func StartTime(ctx context.Context) time.Time {
return ctx.Value(startTime).(time.Time) //nolint:forcetypeassert
}

func WithEvaluationID(ctx context.Context, id string) context.Context {
ctx = slogctx.With(ctx, slog.String("eval_id", id))

return context.WithValue(ctx, evaluationID, id)
}

func WithStartTime(ctx context.Context, now time.Time) context.Context {
return context.WithValue(ctx, startTime, now)
}
Expand Down

0 comments on commit f8322c0

Please sign in to comment.