Skip to content

Commit dd581f2

Browse files
authored
fix: allow CI pipeline to fail if the Change Request changes scm-engine configuration file (#80)
* fix: allow CI pipeline to fail if the Change Request changes scm-engine configuration file otherwise, continue to fail open * fix: MergeRequestIDUint() possible overflow
1 parent 5e4b5df commit dd581f2

File tree

8 files changed

+31
-8
lines changed

8 files changed

+31
-8
lines changed

cmd/shared.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ func ProcessMR(ctx context.Context, client scm.Client, cfg *config.Config, event
4242
// Track where we grab the configuration file from
4343
ctx = slogctx.With(ctx, slog.String("config_source_branch", "merge_request_branch"))
4444

45+
// Should we allow failing the CI pipeline?
46+
allowPipelineFailure := false
47+
4548
defer state.LockForProcessing(ctx)()
4649

4750
// Stop the pipeline when we leave this func
4851
defer func() {
49-
if stopErr := client.Stop(ctx, err); stopErr != nil {
52+
if stopErr := client.Stop(ctx, err, allowPipelineFailure); stopErr != nil {
5053
slogctx.Error(ctx, "Failed to update pipeline", slog.Any("error", stopErr))
5154
}
5255
}()
@@ -73,6 +76,9 @@ func ProcessMR(ctx context.Context, client scm.Client, cfg *config.Config, event
7376
return nil
7477
}
7578

79+
// Check if we are allowed to fail the CI pipeline
80+
allowPipelineFailure = evalContext.AllowPipelineFailure(ctx)
81+
7682
//
7783
// (Optional) Download the .scm-engine.yml configuration file from the GitLab HTTP API
7884
//

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
slogctx "github.com/veqryn/slog-context"
1818
)
1919

20-
// nolint: gochecknoglobals
20+
//nolint:gochecknoglobals
2121
var (
2222
commit = "unknown"
2323
date = "unknown"

pkg/scm/github/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (client *Client) Start(ctx context.Context) error {
6565
}
6666

6767
// Stop pipeline
68-
func (client *Client) Stop(ctx context.Context, err error) error {
68+
func (client *Client) Stop(ctx context.Context, err error, allowPipelineFailure bool) error {
6969
return nil
7070
}
7171

pkg/scm/github/context.go

+4
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,7 @@ func (c *Context) HasExecutedActionGroup(name string) bool {
109109

110110
return ok
111111
}
112+
113+
func (c *Context) AllowPipelineFailure(ctx context.Context) bool {
114+
return len(c.PullRequest.findModifiedFiles(state.ConfigFilePath(ctx))) == 1
115+
}

pkg/scm/gitlab/client.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (client *Client) Start(ctx context.Context) error {
185185
}
186186

187187
// Stop pipeline
188-
func (client *Client) Stop(ctx context.Context, evalError error) error {
188+
func (client *Client) Stop(ctx context.Context, evalError error, allowPipelineFailure bool) error {
189189
ok, pattern := state.ShouldUpdatePipeline(ctx)
190190
if !ok {
191191
return nil
@@ -210,7 +210,10 @@ func (client *Client) Stop(ctx context.Context, evalError error) error {
210210
)
211211

212212
if evalError != nil {
213-
status = go_gitlab.Success
213+
if allowPipelineFailure {
214+
status = go_gitlab.Failed
215+
}
216+
214217
message = evalError.Error()
215218
}
216219

pkg/scm/gitlab/context.go

+9
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ func (c *Context) CanUseConfigurationFileFromChangeRequest(ctx context.Context)
130130
return true
131131
}
132132

133+
// AllowPipelineFailure controls if the CI pipeline are allowed to fail
134+
//
135+
// We allow the pipeline to fail with an error if the SCM-Engine configuration file
136+
// is changed within the merge request, effectively allowing us to lint the configuration
137+
// file when changing it but failing "open" in all other cases.
138+
func (c *Context) AllowPipelineFailure(ctx context.Context) bool {
139+
return len(c.MergeRequest.findModifiedFiles(state.ConfigFilePath(ctx))) == 1
140+
}
141+
133142
func (c *Context) TrackActionGroupExecution(group string) {
134143
// Ungrouped actions shouldn't be tracked
135144
if len(group) == 0 {

pkg/scm/interfaces.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Client interface {
1313
Labels() LabelClient
1414
MergeRequests() MergeRequestClient
1515
Start(ctx context.Context) error
16-
Stop(ctx context.Context, err error) error
16+
Stop(ctx context.Context, err error, allowPipelineFailure bool) error
1717
}
1818

1919
type LabelClient interface {
@@ -29,6 +29,7 @@ type MergeRequestClient interface {
2929
}
3030

3131
type EvalContext interface {
32+
AllowPipelineFailure(ctx context.Context) bool
3233
CanUseConfigurationFileFromChangeRequest(ctx context.Context) bool
3334
GetDescription() string
3435
HasExecutedActionGroup(name string) bool

pkg/state/context.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ func MergeRequestIDInt(ctx context.Context) int {
156156
func MergeRequestIDUint(ctx context.Context) uint64 {
157157
val := MergeRequestID(ctx)
158158

159-
number, err := strconv.Atoi(val)
159+
number, err := strconv.ParseUint(val, 10, 64)
160160
if err != nil {
161161
panic(err)
162162
}
163163

164-
return uint64(number)
164+
return number
165165
}

0 commit comments

Comments
 (0)