1- package forges
1+ package bitbucket
22
33import (
44 "bytes"
55 "context"
66 "encoding/json"
77 "errors"
88 "fmt"
9+ forge "github.com/git-pkgs/forge"
910 "io"
1011 "net/http"
1112 "time"
@@ -21,7 +22,8 @@ type bitbucketForge struct {
2122 httpClient * http.Client
2223}
2324
24- func newBitbucketForge (token string , hc * http.Client ) * bitbucketForge {
25+ // New creates a Bitbucket forge backend.
26+ func New (token string , hc * http.Client ) forge.Forge {
2527 if hc == nil {
2628 hc = http .DefaultClient
2729 }
@@ -33,7 +35,7 @@ type bitbucketRepoService struct {
3335 httpClient * http.Client
3436}
3537
36- func (f * bitbucketForge ) Repos () RepoService {
38+ func (f * bitbucketForge ) Repos () forge. RepoService {
3739 return & bitbucketRepoService {token : f .token , httpClient : f .httpClient }
3840}
3941
@@ -121,14 +123,14 @@ func (s *bitbucketRepoService) doJSON(ctx context.Context, method, url string, b
121123 defer func () { _ = resp .Body .Close () }()
122124
123125 if resp .StatusCode == http .StatusNotFound {
124- return ErrNotFound
126+ return forge . ErrNotFound
125127 }
126128 if resp .StatusCode == http .StatusNoContent {
127129 return nil
128130 }
129131 if resp .StatusCode >= 400 {
130132 respBody , _ := io .ReadAll (resp .Body )
131- return & HTTPError {StatusCode : resp .StatusCode , URL : url , Body : string (respBody )}
133+ return & forge. HTTPError {StatusCode : resp .StatusCode , URL : url , Body : string (respBody )}
132134 }
133135
134136 if v != nil {
@@ -141,8 +143,8 @@ func (s *bitbucketRepoService) getJSON(ctx context.Context, url string, v any) e
141143 return s .doJSON (ctx , http .MethodGet , url , nil , v )
142144}
143145
144- func convertBitbucketRepo (bb bbRepository ) Repository {
145- result := Repository {
146+ func convertBitbucketRepo (bb bbRepository ) forge. Repository {
147+ result := forge. Repository {
146148 FullName : bb .FullName ,
147149 Name : bb .Slug ,
148150 Description : bb .Description ,
@@ -187,7 +189,7 @@ func convertBitbucketRepo(bb bbRepository) Repository {
187189 return result
188190}
189191
190- func (s * bitbucketRepoService ) Get (ctx context.Context , owner , repo string ) (* Repository , error ) {
192+ func (s * bitbucketRepoService ) Get (ctx context.Context , owner , repo string ) (* forge. Repository , error ) {
191193 url := fmt .Sprintf ("%s/repositories/%s/%s" , bitbucketAPI , owner , repo )
192194 var bb bbRepository
193195 if err := s .getJSON (ctx , url , & bb ); err != nil {
@@ -198,20 +200,20 @@ func (s *bitbucketRepoService) Get(ctx context.Context, owner, repo string) (*Re
198200 return & result , nil
199201}
200202
201- func (s * bitbucketRepoService ) List (ctx context.Context , owner string , opts ListRepoOpts ) ([]Repository , error ) {
203+ func (s * bitbucketRepoService ) List (ctx context.Context , owner string , opts forge. ListRepoOpts ) ([]forge. Repository , error ) {
202204 perPage := opts .PerPage
203205 if perPage <= 0 {
204206 perPage = 100
205207 }
206208
207- var all []Repository
209+ var all []forge. Repository
208210 url := fmt .Sprintf ("%s/repositories/%s?pagelen=%d" , bitbucketAPI , owner , perPage )
209211
210212 for url != "" {
211213 var page bbReposResponse
212214 if err := s .getJSON (ctx , url , & page ); err != nil {
213- if errors .Is (err , ErrNotFound ) {
214- return nil , ErrOwnerNotFound
215+ if errors .Is (err , forge . ErrNotFound ) {
216+ return nil , forge . ErrOwnerNotFound
215217 }
216218 return nil , err
217219 }
@@ -221,10 +223,10 @@ func (s *bitbucketRepoService) List(ctx context.Context, owner string, opts List
221223 url = page .Next
222224 }
223225
224- return FilterRepos (all , opts ), nil
226+ return forge . FilterRepos (all , opts ), nil
225227}
226228
227- func (s * bitbucketRepoService ) Create (ctx context.Context , opts CreateRepoOpts ) (* Repository , error ) {
229+ func (s * bitbucketRepoService ) Create (ctx context.Context , opts forge. CreateRepoOpts ) (* forge. Repository , error ) {
228230 owner := opts .Owner
229231 if owner == "" {
230232 return nil , fmt .Errorf ("bitbucket: owner is required for repo creation" )
@@ -236,7 +238,7 @@ func (s *bitbucketRepoService) Create(ctx context.Context, opts CreateRepoOpts)
236238 if opts .Description != "" {
237239 body ["description" ] = opts .Description
238240 }
239- if opts .Visibility == VisibilityPrivate {
241+ if opts .Visibility == forge . VisibilityPrivate {
240242 body ["is_private" ] = true
241243 } else {
242244 body ["is_private" ] = false
@@ -251,7 +253,7 @@ func (s *bitbucketRepoService) Create(ctx context.Context, opts CreateRepoOpts)
251253 return & result , nil
252254}
253255
254- func (s * bitbucketRepoService ) Edit (ctx context.Context , owner , repo string , opts EditRepoOpts ) (* Repository , error ) {
256+ func (s * bitbucketRepoService ) Edit (ctx context.Context , owner , repo string , opts forge. EditRepoOpts ) (* forge. Repository , error ) {
255257 body := map [string ]any {}
256258
257259 if opts .Description != nil {
@@ -265,9 +267,9 @@ func (s *bitbucketRepoService) Edit(ctx context.Context, owner, repo string, opt
265267 }
266268
267269 switch opts .Visibility {
268- case VisibilityPrivate :
270+ case forge . VisibilityPrivate :
269271 body ["is_private" ] = true
270- case VisibilityPublic :
272+ case forge . VisibilityPublic :
271273 body ["is_private" ] = false
272274 }
273275
@@ -290,7 +292,7 @@ func (s *bitbucketRepoService) Delete(ctx context.Context, owner, repo string) e
290292 return s .doJSON (ctx , http .MethodDelete , url , nil , nil )
291293}
292294
293- func (s * bitbucketRepoService ) Fork (ctx context.Context , owner , repo string , opts ForkRepoOpts ) (* Repository , error ) {
295+ func (s * bitbucketRepoService ) Fork (ctx context.Context , owner , repo string , opts forge. ForkRepoOpts ) (* forge. Repository , error ) {
294296 body := map [string ]any {}
295297 if opts .Name != "" {
296298 body ["name" ] = opts .Name
@@ -309,8 +311,8 @@ func (s *bitbucketRepoService) Fork(ctx context.Context, owner, repo string, opt
309311 return & result , nil
310312}
311313
312- func (s * bitbucketRepoService ) ListTags (ctx context.Context , owner , repo string ) ([]Tag , error ) {
313- var allTags []Tag
314+ func (s * bitbucketRepoService ) ListTags (ctx context.Context , owner , repo string ) ([]forge. Tag , error ) {
315+ var allTags []forge. Tag
314316 url := fmt .Sprintf ("%s/repositories/%s/%s/refs/tags?pagelen=100" , bitbucketAPI , owner , repo )
315317
316318 for url != "" {
@@ -319,7 +321,7 @@ func (s *bitbucketRepoService) ListTags(ctx context.Context, owner, repo string)
319321 return nil , err
320322 }
321323 for _ , t := range page .Values {
322- allTags = append (allTags , Tag {
324+ allTags = append (allTags , forge. Tag {
323325 Name : t .Name ,
324326 Commit : t .Target .Hash ,
325327 })
@@ -329,8 +331,8 @@ func (s *bitbucketRepoService) ListTags(ctx context.Context, owner, repo string)
329331 return allTags , nil
330332}
331333
332- func (s * bitbucketRepoService ) Search (ctx context.Context , opts SearchRepoOpts ) ([]Repository , error ) {
334+ func (s * bitbucketRepoService ) Search (ctx context.Context , opts forge. SearchRepoOpts ) ([]forge. Repository , error ) {
333335 // Bitbucket doesn't have a global repo search API.
334336 // The closest is searching within a workspace, which requires an owner.
335- return nil , ErrNotSupported
337+ return nil , forge . ErrNotSupported
336338}
0 commit comments