@@ -12,6 +12,20 @@ type BitbucketApp struct {
1212 config AppConfig
1313}
1414
15+ // Slugify a string.
16+ func slugify (s string ) string {
17+ s = strings .ToLower (s )
18+ s = strings .ReplaceAll (s , " " , "-" )
19+ var result strings.Builder
20+ result .Grow (len (s ))
21+ for _ , r := range s {
22+ if (r >= 'a' && r <= 'z' ) || (r >= '0' && r <= '9' ) || r == '_' || r == '-' || r == '.' {
23+ result .WriteRune (r )
24+ }
25+ }
26+ return result .String ()
27+ }
28+
1529func (g * BitbucketApp ) GetOrganizations () ([]Organization , error ) {
1630 req , _ := http .NewRequest ("GET" , g .config .ApiUrl + "/2.0/workspaces" , nil )
1731 req .SetBasicAuth (g .config .Username , g .config .Password )
@@ -211,14 +225,10 @@ func (g *BitbucketApp) GetIssues(repo Repository) ([]Issue, error) {
211225}
212226
213227func (g * BitbucketApp ) CreateRepo (endpoint ApiEndpoint , owner string , source Repository ) (Repository , error ) {
214- var url string
215- if endpoint == EndpointOrganization {
216- url = g .config .ApiUrl + "/2.0/repositories/" + owner + "/" + source .Name
217- } else {
218- url = g .config .ApiUrl + "/2.0/repositories/" + owner + "/" + source .Name
219- }
228+ url := g .config .ApiUrl + "/2.0/repositories/" + owner + "/" + slugify (source .Name )
220229
221- payload := map [string ]interface {}{
230+ payload := map [string ]any {
231+ "scm" : "git" ,
222232 "name" : source .Name ,
223233 "description" : source .Description ,
224234 "is_private" : source .Private ,
@@ -234,18 +244,19 @@ func (g *BitbucketApp) CreateRepo(endpoint ApiEndpoint, owner string, source Rep
234244 req , _ := http .NewRequest ("POST" , url , strings .NewReader (string (jsonData )))
235245 req .SetBasicAuth (g .config .Username , g .config .Password )
236246 req .Header .Set ("Content-Type" , "application/json" )
247+ req .Header .Set ("Accept" , "application/json" )
237248 resp , err := http .DefaultClient .Do (req )
238249 if err != nil {
239250 return Repository {}, err
240251 }
241252 defer resp .Body .Close ()
242253
243- if resp .StatusCode != http .StatusCreated {
254+ body , _ := io .ReadAll (resp .Body )
255+
256+ if resp .StatusCode != http .StatusOK {
244257 return Repository {}, fmt .Errorf ("failed to create repo: %s" , resp .Status )
245258 }
246259
247- body , _ := io .ReadAll (resp .Body )
248-
249260 // Parse response
250261 var response struct {
251262 Name string `json:"name"`
0 commit comments