Skip to content

Commit 8f5e050

Browse files
committed
Fix Bitbucket repository creation
1 parent bcb9950 commit 8f5e050

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

cli/application/bitbucket.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1529
func (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

213227
func (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"`

cli/main.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,23 @@ func main() {
322322
outputView.SetText("")
323323
pages.SwitchToPage(nextPage)
324324
go func() {
325+
index, _ := destTypeDropDown.GetCurrentOption()
326+
endpoint := application.EndpointUser
327+
if index != 0 {
328+
endpoint = application.EndpointOrganization
329+
}
330+
331+
owner := ""
332+
if endpoint == application.EndpointUser {
333+
owner = destUserInput.GetText()
334+
} else {
335+
if destOrgDropDown.GetOptionCount() > 1 {
336+
_, owner = destOrgDropDown.GetCurrentOption()
337+
} else {
338+
owner = destOrgInput.GetText()
339+
}
340+
}
341+
325342
for _, item := range repoItems {
326343
if !item.Selected {
327344
continue
@@ -334,8 +351,13 @@ func main() {
334351
}(repoFullName))
335352
outputView.ScrollToEnd()
336353

337-
// Simulate work
338-
time.Sleep(1 * time.Second)
354+
_, err := destApplication.CreateRepo(endpoint, owner, item.Repo)
355+
if err != nil {
356+
app.QueueUpdateDraw(func() {
357+
_, _ = outputView.Write([]byte("Repository creation failed: " + err.Error() + "\n"))
358+
})
359+
continue
360+
}
339361
}
340362
app.QueueUpdateDraw(func() {
341363
_, _ = outputView.Write([]byte("\n\nCompleted!"))

0 commit comments

Comments
 (0)