Skip to content

Commit faba1a9

Browse files
authored
Merge branch 'main' into fix/gitlab-commit-status-pipeline-id-caching
2 parents f57af59 + 658e9d4 commit faba1a9

File tree

7 files changed

+380
-10
lines changed

7 files changed

+380
-10
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ GOLANGCI_LINT=golangci-lint
55
GOFUMPT=gofumpt
66
TKN_BINARY_NAME := tkn
77
TKN_BINARY_URL := https://tekton.dev/docs/cli/\#installation
8+
DEFAULT_GOTESTSUM_FORMAT ?= testdox
89
LDFLAGS=
910
OUTPUT_DIR=bin
1011
GO = go
@@ -68,7 +69,7 @@ test-unit: ## Run unit tests
6869
@mkdir -p tmp/
6970
@echo "Running unit tests..."
7071
@if command -v gotestsum >/dev/null 2>&1; then \
71-
gotestsum --format testdox -- $(DEFAULT_GO_TEST_FLAGS) $(GO_TEST_FLAGS) -timeout $(TIMEOUT_UNIT) ./pkg/...; \
72+
gotestsum --format ${DEFAULT_GOTESTSUM_FORMAT} -- $(DEFAULT_GO_TEST_FLAGS) $(GO_TEST_FLAGS) -timeout $(TIMEOUT_UNIT) ./pkg/...; \
7273
else \
7374
$(GO) test $(DEFAULT_GO_TEST_FLAGS) $(GO_TEST_FLAGS) -timeout $(TIMEOUT_UNIT) ./pkg/...; \
7475
fi

pkg/adapter/sinker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (s *sinker) setupClient(ctx context.Context, repo *v1alpha1.Repository) err
159159
func (s *sinker) createSkipCIStatus(ctx context.Context) error {
160160
statusOpts := status.StatusOpts{
161161
Status: "completed",
162-
Conclusion: status.ConclusionNeutral,
162+
Conclusion: status.ConclusionSkipped,
163163
Title: "CI Skipped",
164164
Summary: fmt.Sprintf("%s - CI has been skipped", s.pacInfo.ApplicationName),
165165
Text: "Commit contains a skip CI command. Use /test or /retest to manually trigger CI if needed.",

pkg/provider/gitea/gitea.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,36 @@ func (v *Provider) createStatusCommit(ctx context.Context, event *info.Event, pa
369369
return nil
370370
}
371371

372-
func (v *Provider) GetCommitStatuses(_ context.Context, _ *info.Event) ([]provider.CommitStatusInfo, error) {
373-
return nil, nil
372+
func (v *Provider) GetCommitStatuses(_ context.Context, event *info.Event) ([]provider.CommitStatusInfo, error) {
373+
if v.giteaClient == nil {
374+
return nil, fmt.Errorf("no gitea client has been initialized")
375+
}
376+
377+
statuses, _, err := v.Client().ListStatuses(
378+
event.Organization, event.Repository, event.SHA,
379+
forgejo.ListStatusesOption{},
380+
)
381+
if err != nil {
382+
return nil, err
383+
}
384+
385+
var (
386+
result []provider.CommitStatusInfo
387+
seen = map[string]struct{}{}
388+
)
389+
for _, s := range statuses {
390+
key := fmt.Sprintf("%s\x00%s", s.Context, string(s.State))
391+
if _, ok := seen[key]; ok {
392+
continue
393+
}
394+
seen[key] = struct{}{}
395+
result = append(result, provider.CommitStatusInfo{
396+
Name: s.Context,
397+
Status: string(s.State),
398+
})
399+
}
400+
401+
return result, nil
374402
}
375403

376404
func (v *Provider) GetTektonDir(_ context.Context, event *info.Event, path, provenance string) (string, error) {

pkg/provider/gitea/gitea_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,3 +1219,112 @@ func TestGetCommitInfoPRLookupPopulatesURLs(t *testing.T) {
12191219
assert.Equal(t, "https://gitea.com/fork-owner/repo", event.HeadURL, "HeadURL should be populated from PR lookup")
12201220
assert.Equal(t, "https://gitea.com/owner/repo", event.BaseURL, "BaseURL should be populated from PR lookup")
12211221
}
1222+
1223+
func TestGetCommitStatuses(t *testing.T) {
1224+
tests := []struct {
1225+
name string
1226+
event *info.Event
1227+
nilClient bool
1228+
mockHandler func(http.ResponseWriter, *http.Request)
1229+
want []provider.CommitStatusInfo
1230+
wantErr string
1231+
}{
1232+
{
1233+
name: "happy path with multiple statuses",
1234+
event: &info.Event{
1235+
Organization: "org",
1236+
Repository: "repo",
1237+
SHA: "abc123",
1238+
},
1239+
mockHandler: func(rw http.ResponseWriter, _ *http.Request) {
1240+
fmt.Fprint(rw, `[
1241+
{"context":"Pipelines as Code CI / pr-one","status":"success"},
1242+
{"context":"Pipelines as Code CI / pr-two","status":"failure"}
1243+
]`)
1244+
},
1245+
want: []provider.CommitStatusInfo{
1246+
{Name: "Pipelines as Code CI / pr-one", Status: "success"},
1247+
{Name: "Pipelines as Code CI / pr-two", Status: "failure"},
1248+
},
1249+
},
1250+
{
1251+
name: "deduplicates identical statuses",
1252+
event: &info.Event{
1253+
Organization: "org",
1254+
Repository: "repo",
1255+
SHA: "abc123",
1256+
},
1257+
mockHandler: func(rw http.ResponseWriter, _ *http.Request) {
1258+
fmt.Fprint(rw, `[
1259+
{"context":"CI / build","status":"success"},
1260+
{"context":"CI / build","status":"success"},
1261+
{"context":"CI / build","status":"failure"}
1262+
]`)
1263+
},
1264+
want: []provider.CommitStatusInfo{
1265+
{Name: "CI / build", Status: "success"},
1266+
{Name: "CI / build", Status: "failure"},
1267+
},
1268+
},
1269+
{
1270+
name: "empty response",
1271+
event: &info.Event{
1272+
Organization: "org",
1273+
Repository: "repo",
1274+
SHA: "abc123",
1275+
},
1276+
mockHandler: func(rw http.ResponseWriter, _ *http.Request) {
1277+
fmt.Fprint(rw, `[]`)
1278+
},
1279+
},
1280+
{
1281+
name: "nil client returns error",
1282+
nilClient: true,
1283+
event: &info.Event{
1284+
Organization: "org",
1285+
Repository: "repo",
1286+
SHA: "abc123",
1287+
},
1288+
wantErr: "no gitea client has been initialized",
1289+
},
1290+
{
1291+
name: "API error",
1292+
event: &info.Event{
1293+
Organization: "org",
1294+
Repository: "repo",
1295+
SHA: "abc123",
1296+
},
1297+
mockHandler: func(rw http.ResponseWriter, _ *http.Request) {
1298+
rw.WriteHeader(http.StatusInternalServerError)
1299+
},
1300+
wantErr: "500",
1301+
},
1302+
}
1303+
1304+
for _, tt := range tests {
1305+
t.Run(tt.name, func(t *testing.T) {
1306+
var p *Provider
1307+
if tt.nilClient {
1308+
p = &Provider{}
1309+
} else {
1310+
fakeclient, mux, teardown := tgitea.Setup(t)
1311+
defer teardown()
1312+
1313+
mux.HandleFunc(
1314+
fmt.Sprintf("/repos/%s/%s/commits/%s/statuses",
1315+
tt.event.Organization, tt.event.Repository, tt.event.SHA),
1316+
tt.mockHandler,
1317+
)
1318+
p = &Provider{giteaClient: fakeclient}
1319+
}
1320+
1321+
got, err := p.GetCommitStatuses(context.Background(), tt.event)
1322+
if tt.wantErr != "" {
1323+
assert.ErrorContains(t, err, tt.wantErr)
1324+
return
1325+
}
1326+
assert.NilError(t, err)
1327+
assert.DeepEqual(t, got, tt.want)
1328+
})
1329+
}
1330+
}

pkg/provider/gitlab/gitlab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func (v *Provider) CreateStatus(ctx context.Context, event *info.Event, statusOp
313313

314314
switch statusOpts.Conclusion {
315315
case providerstatus.ConclusionSkipped:
316-
state = gitlab.Canceled
316+
state = gitlab.Skipped
317317
statusOpts.Title = "skipped validating this commit"
318318
case providerstatus.ConclusionNeutral:
319319
state = gitlab.Canceled

pkg/provider/gitlab/parse_payload.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (v *Provider) ParsePayload(ctx context.Context, run *params.Run, request *h
8383
// GitLab sends same event for both Tag creation and deletion i.e. "Tag Push Hook".
8484
// if gitEvent.After is containing all zeros and gitEvent.CheckoutSHA is empty
8585
// it is Delete "Tag Push Hook".
86-
if isZeroSHA(gitEvent.After) && gitEvent.CheckoutSHA == "" {
86+
if provider.IsZeroSHA(gitEvent.After) && gitEvent.CheckoutSHA == "" {
8787
return nil, fmt.Errorf("event Delete %s is not supported", event)
8888
}
8989

@@ -319,7 +319,3 @@ func (v *Provider) handleCommitCommentEvent(ctx context.Context, event *gitlab.C
319319
v.Logger.Infof("gitlab commit_comment: pipelinerun %s has been requested on %s/%s#%s", action, processedEvent.Organization, processedEvent.Repository, processedEvent.SHA)
320320
return processedEvent, nil
321321
}
322-
323-
func isZeroSHA(sha string) bool {
324-
return sha == "0000000000000000000000000000000000000000"
325-
}

0 commit comments

Comments
 (0)