Skip to content

Commit 41c6ace

Browse files
aThorp96chmouel
authored andcommitted
refactor(providers): constantize provider StatusOpt conclusion options
The Conclusion field of StatusOpts has a discrete number of supported values. These being typed as strings has introduced bugs in the past and makes adding new values difficult. Defining the supported values as constant enums improves both the safety of the values and the ease of working with them.
1 parent 561d5e9 commit 41c6ace

File tree

29 files changed

+294
-246
lines changed

29 files changed

+294
-246
lines changed

pkg/adapter/sinker.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
1414
"github.com/openshift-pipelines/pipelines-as-code/pkg/pipelineascode"
1515
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
16+
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/status"
1617
"go.uber.org/zap"
1718
)
1819

@@ -156,9 +157,9 @@ func (s *sinker) setupClient(ctx context.Context, repo *v1alpha1.Repository) err
156157

157158
// createSkipCIStatus creates a neutral status check on the git provider when CI is skipped.
158159
func (s *sinker) createSkipCIStatus(ctx context.Context) error {
159-
statusOpts := provider.StatusOpts{
160+
statusOpts := status.StatusOpts{
160161
Status: "completed",
161-
Conclusion: "neutral",
162+
Conclusion: status.ConclusionNeutral,
162163
Title: "CI Skipped",
163164
Summary: fmt.Sprintf("%s - CI has been skipped", s.pacInfo.ApplicationName),
164165
Text: "Commit contains a skip CI command. Use /test or /retest to manually trigger CI if needed.",

pkg/formatting/pipelinerun.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package formatting
22

33
import (
4+
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/status"
45
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
56
corev1 "k8s.io/api/core/v1"
67
"knative.dev/pkg/apis"
78
)
89

910
// PipelineRunStatus return status of PR success failed or skipped.
10-
func PipelineRunStatus(pr *tektonv1.PipelineRun) string {
11+
func PipelineRunStatus(pr *tektonv1.PipelineRun) status.Conclusion {
1112
if len(pr.Status.Conditions) == 0 {
12-
return "neutral"
13+
return status.ConclusionNeutral
1314
}
1415
if pr.Status.GetCondition(apis.ConditionSucceeded).GetReason() == tektonv1.PipelineRunSpecStatusCancelled {
15-
return "cancelled"
16+
return status.ConclusionCancelled
1617
}
1718
if pr.Status.Conditions[0].Status == corev1.ConditionFalse {
18-
return "failure"
19+
return status.ConclusionFailure
1920
}
20-
return "success"
21+
return status.ConclusionSuccess
2122
}

pkg/formatting/pipelinerun_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestPipelineRunStatus(t *testing.T) {
7777
}
7878
for _, tt := range tests {
7979
t.Run(tt.name, func(t *testing.T) {
80-
output := PipelineRunStatus(tt.pr)
80+
output := string(PipelineRunStatus(tt.pr))
8181
assert.Equal(t, output, tt.name, "PipelineRunStatus() = %v, want %v", output, tt.name)
8282
})
8383
}

pkg/pipelineascode/errors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
1010
pacerrors "github.com/openshift-pipelines/pipelines-as-code/pkg/errors"
1111
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
12+
providerstatus "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/status"
1213
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
1314
"go.uber.org/zap"
1415
)
@@ -19,7 +20,7 @@ const (
1920

2021
var regexpIgnoreErrors = regexp.MustCompile(`.*no kind.*is registered for version.*in scheme.*`)
2122

22-
func (p *PacRun) checkAccessOrError(ctx context.Context, repo *v1alpha1.Repository, status provider.StatusOpts, viamsg string) (bool, error) {
23+
func (p *PacRun) checkAccessOrError(ctx context.Context, repo *v1alpha1.Repository, status providerstatus.StatusOpts, viamsg string) (bool, error) {
2324
p.debugf("checkAccessOrError: checking access for sender=%s via=%s", p.event.Sender, viamsg)
2425
allowed, err := p.vcx.IsAllowed(ctx, p.event)
2526
if err != nil {

pkg/pipelineascode/errors_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
pacerrors "github.com/openshift-pipelines/pipelines-as-code/pkg/errors"
1010
"github.com/openshift-pipelines/pipelines-as-code/pkg/events"
1111
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
12-
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
12+
providerstatus "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/status"
1313
testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients"
1414
testprovider "github.com/openshift-pipelines/pipelines-as-code/pkg/test/provider"
1515
"go.uber.org/zap"
@@ -94,7 +94,7 @@ func TestCheckAccessOrErrror(t *testing.T) {
9494

9595
// Call the function
9696
repo := &v1alpha1.Repository{}
97-
status := provider.StatusOpts{}
97+
status := providerstatus.StatusOpts{}
9898
allowed, err := p.checkAccessOrError(context.Background(), repo, status, "via test")
9999

100100
// Verify results

pkg/pipelineascode/match.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/openshift-pipelines/pipelines-as-code/pkg/matcher"
1414
"github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
1515
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
16-
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
16+
providerstatus "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/status"
1717
"github.com/openshift-pipelines/pipelines-as-code/pkg/resolve"
1818
"github.com/openshift-pipelines/pipelines-as-code/pkg/secrets"
1919
"github.com/openshift-pipelines/pipelines-as-code/pkg/templates"
@@ -92,10 +92,10 @@ func (p *PacRun) verifyRepoAndUser(ctx context.Context) (*v1alpha1.Repository, e
9292
// trigger CI on the repository, as any user is able to comment on a pushed commit in open-source repositories.
9393
if p.event.TriggerTarget == triggertype.Push && opscomments.IsAnyOpsEventType(p.event.EventType) {
9494
p.debugf("verifyRepoAndUser: checking access for gitops comment on push")
95-
status := provider.StatusOpts{
95+
status := providerstatus.StatusOpts{
9696
Status: CompletedStatus,
9797
Title: "Permission denied",
98-
Conclusion: failureConclusion,
98+
Conclusion: providerstatus.ConclusionFailure,
9999
DetailsURL: p.event.URL,
100100
AccessDenied: true,
101101
}
@@ -109,10 +109,10 @@ func (p *PacRun) verifyRepoAndUser(ctx context.Context) (*v1alpha1.Repository, e
109109
// on comment we skip it for now, we are going to check later on
110110
if p.event.TriggerTarget != triggertype.Push && p.event.EventType != opscomments.NoOpsCommentEventType.String() {
111111
p.debugf("verifyRepoAndUser: checking access for trigger target=%s event_type=%s", p.event.TriggerTarget, p.event.EventType)
112-
status := provider.StatusOpts{
112+
status := providerstatus.StatusOpts{
113113
Status: queuedStatus,
114114
Title: "Pending approval, waiting for an /ok-to-test",
115-
Conclusion: pendingConclusion,
115+
Conclusion: providerstatus.ConclusionPending,
116116
DetailsURL: p.event.URL,
117117
AccessDenied: true,
118118
}
@@ -122,10 +122,10 @@ func (p *PacRun) verifyRepoAndUser(ctx context.Context) (*v1alpha1.Repository, e
122122
// When /ok-to-test is approved, update the parent "Pipelines as Code CI" status to success
123123
// to indicate the approval was successful before pipelines start running.
124124
if p.event.EventType == opscomments.OkToTestCommentEventType.String() {
125-
approvalStatus := provider.StatusOpts{
125+
approvalStatus := providerstatus.StatusOpts{
126126
Status: CompletedStatus,
127127
Title: "Approved",
128-
Conclusion: successConclusion,
128+
Conclusion: providerstatus.ConclusionSuccess,
129129
DetailsURL: p.event.URL,
130130
}
131131
if err := p.vcx.CreateStatus(ctx, p.event, approvalStatus); err != nil {
@@ -282,10 +282,10 @@ func (p *PacRun) getPipelineRunsFromRepo(ctx context.Context, repo *v1alpha1.Rep
282282
// if the event is a comment event, but we don't have any match from the keys.OnComment then do the ACL checks again
283283
// we skipped previously so we can get the match from the event to the pipelineruns
284284
if p.event.EventType == opscomments.NoOpsCommentEventType.String() || p.event.EventType == opscomments.OnCommentEventType.String() {
285-
status := provider.StatusOpts{
285+
status := providerstatus.StatusOpts{
286286
Status: queuedStatus,
287287
Title: "Pending approval, waiting for an /ok-to-test",
288-
Conclusion: pendingConclusion,
288+
Conclusion: providerstatus.ConclusionPending,
289289
DetailsURL: p.event.URL,
290290
AccessDenied: true,
291291
}
@@ -484,11 +484,11 @@ func (p *PacRun) checkNeedUpdate(_ string) (string, bool) {
484484

485485
func (p *PacRun) createNeutralStatus(ctx context.Context, title, text string) error {
486486
p.debugf("createNeutralStatus: title=%s", title)
487-
status := provider.StatusOpts{
487+
status := providerstatus.StatusOpts{
488488
Status: CompletedStatus,
489489
Title: title,
490490
Text: text,
491-
Conclusion: neutralConclusion,
491+
Conclusion: providerstatus.ConclusionNeutral,
492492
DetailsURL: p.event.URL,
493493
}
494494
if err := p.vcx.CreateStatus(ctx, p.event, status); err != nil {

pkg/pipelineascode/pipelineascode.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
2020
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
2121
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
22+
providerstatus "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/status"
2223
"github.com/openshift-pipelines/pipelines-as-code/pkg/secrets"
2324
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
2425
"go.uber.org/zap"
@@ -27,14 +28,10 @@ import (
2728
)
2829

2930
const (
30-
tektonDir = ".tekton"
31-
CompletedStatus = "completed"
32-
inProgressStatus = "in_progress"
33-
queuedStatus = "queued"
34-
successConclusion = "success"
35-
failureConclusion = "failure"
36-
pendingConclusion = "pending"
37-
neutralConclusion = "neutral"
31+
tektonDir = ".tekton"
32+
CompletedStatus = "completed"
33+
inProgressStatus = "in_progress"
34+
queuedStatus = "queued"
3835
)
3936

4037
type PacRun struct {
@@ -87,9 +84,9 @@ func (p *PacRun) Run(ctx context.Context) error {
8784

8885
matchedPRs, repo, err := p.matchRepoPR(ctx)
8986
if err != nil {
90-
createStatusErr := p.vcx.CreateStatus(ctx, p.event, provider.StatusOpts{
87+
createStatusErr := p.vcx.CreateStatus(ctx, p.event, providerstatus.StatusOpts{
9188
Status: CompletedStatus,
92-
Conclusion: failureConclusion,
89+
Conclusion: providerstatus.ConclusionFailure,
9390
Text: fmt.Sprintf("There was an issue validating the commit: %q", err),
9491
DetailsURL: p.run.Clients.ConsoleUI().URL(),
9592
})
@@ -161,13 +158,13 @@ func (p *PacRun) Run(ctx context.Context) error {
161158
if prName == "" {
162159
prName = match.PipelineRun.GetGenerateName()
163160
}
164-
createStatusErr := p.vcx.CreateStatus(ctx, p.event, provider.StatusOpts{
161+
createStatusErr := p.vcx.CreateStatus(ctx, p.event, providerstatus.StatusOpts{
165162
PipelineRunName: prName,
166163
PipelineRun: match.PipelineRun,
167164
OriginalPipelineRunName: match.PipelineRun.GetAnnotations()[keys.OriginalPRName],
168165
Status: CompletedStatus,
169-
Conclusion: failureConclusion,
170166
Title: "pipelinerun start failure",
167+
Conclusion: providerstatus.ConclusionFailure,
171168
Text: errMsgM,
172169
DetailsURL: p.run.Clients.ConsoleUI().URL(),
173170
InstanceCountForCheckRun: i,
@@ -313,9 +310,9 @@ func (p *PacRun) startPR(ctx context.Context, match matcher.Match) (*tektonv1.Pi
313310
if err != nil {
314311
return nil, fmt.Errorf("cannot create message template: %w", err)
315312
}
316-
status := provider.StatusOpts{
313+
status := providerstatus.StatusOpts{
317314
Status: inProgressStatus,
318-
Conclusion: pendingConclusion,
315+
Conclusion: providerstatus.ConclusionPending,
319316
Text: msg,
320317
DetailsURL: consoleURL,
321318
PipelineRunName: pr.GetName(),

pkg/provider/bitbucketcloud/bitbucket.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
1818
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketcloud/types"
1919
providerMetrics "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/providermetrics"
20+
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/status"
2021
"go.uber.org/zap"
2122
)
2223

@@ -82,26 +83,30 @@ func (v *Provider) GetConfig() *info.ProviderConfig {
8283
}
8384
}
8485

85-
func (v *Provider) CreateStatus(_ context.Context, event *info.Event, statusopts provider.StatusOpts) error {
86+
func (v *Provider) CreateStatus(_ context.Context, event *info.Event, statusopts status.StatusOpts) error {
87+
var state types.CommitStatusState
88+
8689
switch statusopts.Conclusion {
87-
case "skipped":
88-
statusopts.Conclusion = "STOPPED"
90+
case status.ConclusionSkipped:
91+
state = types.StateStopped
8992
statusopts.Title = "➖ Skipping this commit"
90-
case "neutral":
91-
statusopts.Conclusion = "STOPPED"
93+
case status.ConclusionNeutral:
94+
state = types.StateStopped
9295
statusopts.Title = "➖ CI has stopped"
93-
case "failure":
94-
statusopts.Conclusion = "FAILED"
96+
case status.ConclusionFailure:
97+
state = types.StateFailed
9598
statusopts.Title = "❌ Failed"
96-
case "pending":
97-
statusopts.Conclusion = "INPROGRESS"
99+
case status.ConclusionPending:
100+
state = types.StateInProgress
98101
statusopts.Title = "⚡ CI has started"
99-
case "success":
100-
statusopts.Conclusion = "SUCCESSFUL"
102+
case status.ConclusionSuccess:
103+
state = types.StateSuccessful
101104
statusopts.Title = "✅ Commit has been validated"
102-
case "completed":
103-
statusopts.Conclusion = "SUCCESSFUL"
105+
case status.ConclusionCompleted:
106+
state = types.StateSuccessful
104107
statusopts.Title = "✅ Completed"
108+
case status.ConclusionCancelled:
109+
// TODO
105110
}
106111
detailsURL := event.Provider.URL
107112
if statusopts.DetailsURL != "" {
@@ -111,7 +116,7 @@ func (v *Provider) CreateStatus(_ context.Context, event *info.Event, statusopts
111116
cso := &bitbucket.CommitStatusOptions{
112117
Key: provider.GetCheckName(statusopts, v.pacInfo),
113118
Url: detailsURL,
114-
State: statusopts.Conclusion,
119+
State: string(state),
115120
Description: statusopts.Title,
116121
}
117122
cmo := &bitbucket.CommitsOptions{
@@ -133,7 +138,7 @@ func (v *Provider) CreateStatus(_ context.Context, event *info.Event, statusopts
133138
}
134139

135140
eventType := triggertype.IsPullRequestType(event.EventType)
136-
if statusopts.Conclusion != "STOPPED" && statusopts.Status == "completed" &&
141+
if state != types.StateStopped && statusopts.Status == "completed" &&
137142
statusopts.Text != "" &&
138143
(eventType == triggertype.PullRequest || event.TriggerTarget == triggertype.PullRequest) {
139144
onPr := ""

0 commit comments

Comments
 (0)