Skip to content

Commit 25b823f

Browse files
zakiskpipelines-as-code[bot]
authored andcommitted
chore: Refactor CreateStatus func in Bitbucket Data Center
refactored CreateStatus in Bitucket DataCenter with jenkins-x/go-scm. https://issues.redhat.com/browse/SRVKP-7291 Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
1 parent 25b92da commit 25b823f

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

pkg/provider/bitbucketdatacenter/bitbucketdatacenter.go

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func sanitizeTitle(s string) string {
7575
return strings.Split(s, "\n")[0]
7676
}
7777

78-
func (v *Provider) CreateStatus(_ context.Context, event *info.Event, statusOpts provider.StatusOpts) error {
78+
func (v *Provider) CreateStatus(ctx context.Context, event *info.Event, statusOpts provider.StatusOpts) error {
7979
detailsURL := event.Provider.URL
8080
switch statusOpts.Conclusion {
8181
case "skipped":
@@ -100,7 +100,7 @@ func (v *Provider) CreateStatus(_ context.Context, event *info.Event, statusOpts
100100
if statusOpts.DetailsURL != "" {
101101
detailsURL = statusOpts.DetailsURL
102102
}
103-
if v.Client == nil {
103+
if v.ScmClient == nil {
104104
return fmt.Errorf("no token has been set, cannot set status")
105105
}
106106

@@ -109,16 +109,18 @@ func (v *Provider) CreateStatus(_ context.Context, event *info.Event, statusOpts
109109
key = statusOpts.Conclusion
110110
}
111111

112-
_, err := v.Client.DefaultApi.SetCommitStatus(
113-
event.SHA,
114-
bbv1.BuildStatus{
115-
State: statusOpts.Conclusion,
116-
Name: v.pacInfo.ApplicationName,
117-
Key: key,
118-
Description: statusOpts.Title,
119-
Url: detailsURL,
120-
},
121-
)
112+
if v.pacInfo.ApplicationName != "" {
113+
key = fmt.Sprintf("%s/%s", v.pacInfo.ApplicationName, key)
114+
}
115+
116+
OrgAndRepo := fmt.Sprintf("%s/%s", event.Organization, event.Repository)
117+
opts := &scm.StatusInput{
118+
State: convertState(statusOpts.Conclusion),
119+
Label: key,
120+
Desc: statusOpts.Title,
121+
Link: detailsURL,
122+
}
123+
_, _, err := v.ScmClient.Repositories.CreateStatus(ctx, OrgAndRepo, event.SHA, opts)
122124
if err != nil {
123125
return err
124126
}
@@ -127,16 +129,14 @@ func (v *Provider) CreateStatus(_ context.Context, event *info.Event, statusOpts
127129
if statusOpts.OriginalPipelineRunName != "" {
128130
onPr = "/" + statusOpts.OriginalPipelineRunName
129131
}
130-
bbcomment := bbv1.Comment{
131-
Text: fmt.Sprintf("**%s%s** - %s\n\n%s", v.pacInfo.ApplicationName, onPr,
132-
statusOpts.Title, statusOpts.Text),
133-
}
132+
bbComment := fmt.Sprintf("**%s%s** - %s\n\n%s", v.pacInfo.ApplicationName, onPr, statusOpts.Title, statusOpts.Text)
134133

135134
if statusOpts.Conclusion == "SUCCESSFUL" && statusOpts.Status == "completed" &&
136-
statusOpts.Text != "" && event.EventType == triggertype.PullRequest.String() && v.pullRequestNumber > 0 {
137-
_, err := v.Client.DefaultApi.CreatePullRequestComment(
138-
v.projectKey, event.Repository, v.pullRequestNumber,
139-
bbcomment, []string{"application/json"})
135+
statusOpts.Text != "" && event.TriggerTarget == triggertype.PullRequest && event.PullRequestNumber > 0 {
136+
input := &scm.CommentInput{
137+
Body: bbComment,
138+
}
139+
_, _, err := v.ScmClient.PullRequests.CreateComment(ctx, OrgAndRepo, event.PullRequestNumber, input)
140140
if err != nil {
141141
return err
142142
}
@@ -146,6 +146,19 @@ func (v *Provider) CreateStatus(_ context.Context, event *info.Event, statusOpts
146146
return nil
147147
}
148148

149+
func convertState(from string) scm.State {
150+
switch from {
151+
case "FAILED":
152+
return scm.StateFailure
153+
case "INPROGRESS":
154+
return scm.StatePending
155+
case "SUCCESSFUL":
156+
return scm.StateSuccess
157+
default:
158+
return scm.StateUnknown
159+
}
160+
}
161+
149162
func (v *Provider) concatAllYamlFiles(ctx context.Context, objects []string, sha string, runevent *info.Event) (string, error) {
150163
var allTemplates string
151164
for _, value := range objects {

pkg/provider/bitbucketdatacenter/bitbucketdatacenter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func TestCreateStatus(t *testing.T) {
193193
for _, tt := range tests {
194194
t.Run(tt.name, func(t *testing.T) {
195195
ctx, _ := rtesting.SetupFakeContext(t)
196-
client, _, mux, tearDown, tURL := bbtest.SetupBBDataCenterClient(ctx)
196+
_, client, mux, tearDown, tURL := bbtest.SetupBBDataCenterClient(ctx)
197197
defer tearDown()
198198
if tt.nilClient {
199199
client = nil
@@ -203,7 +203,7 @@ func TestCreateStatus(t *testing.T) {
203203
event.Provider.Token = "token"
204204
v := &Provider{
205205
baseURL: tURL,
206-
Client: client,
206+
ScmClient: client,
207207
pullRequestNumber: pullRequestNumber,
208208
projectKey: event.Organization,
209209
run: &params.Run{},

0 commit comments

Comments
 (0)