Skip to content

Commit 20f7d73

Browse files
committed
Use types for Bitbucket JSON
1 parent 8f5e050 commit 20f7d73

File tree

1 file changed

+49
-46
lines changed

1 file changed

+49
-46
lines changed

cli/application/bitbucket.go

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,40 @@ type BitbucketApp struct {
1212
config AppConfig
1313
}
1414

15+
// LinkPayload represents the JSON payload for a link on Bitbucket.
16+
type LinkPayload struct {
17+
Href string `json:"href"`
18+
Name string `json:"name"`
19+
}
20+
21+
// RepositoryPayload represents the JSON payload for a repository on Bitbucket.
22+
type RepositoryPayload struct {
23+
Scm string `json:"scm"`
24+
Name string `json:"name"`
25+
FullName string `json:"full_name,omitempty"`
26+
Description string `json:"description"`
27+
IsPrivate bool `json:"is_private"`
28+
HasWiki bool `json:"has_wiki"`
29+
HasIssues bool `json:"has_issues"`
30+
Owner struct {
31+
DisplayName string `json:"display_name"`
32+
UUID string `json:"uuid"`
33+
} `json:"owner"`
34+
Links struct {
35+
Self LinkPayload `json:"self"`
36+
Clone []LinkPayload `json:"clone"`
37+
} `json:"links"`
38+
}
39+
40+
// RepositoryListPayload represents the JSON payload for a repository list on Bitbucket.
41+
type RepositoryListPayload struct {
42+
Page int `json:"page"`
43+
PageLen int `json:"pagelen"`
44+
Next string `json:"next"`
45+
Previous string `json:"previous"`
46+
Values []RepositoryPayload `json:"values"`
47+
}
48+
1549
// Slugify a string.
1650
func slugify(s string) string {
1751
s = strings.ToLower(s)
@@ -124,28 +158,13 @@ func (g *BitbucketApp) GetRepositories(endpoint ApiEndpoint, owner string, authU
124158

125159
body, _ := io.ReadAll(resp.Body)
126160

127-
// Bitbucket repositories response structure
128-
var response struct {
129-
Values []struct {
130-
Name string `json:"name"`
131-
FullName string `json:"full_name"`
132-
Description string `json:"description"`
133-
IsPrivate bool `json:"is_private"`
134-
HasWiki bool `json:"has_wiki"`
135-
HasIssues bool `json:"has_issues"`
136-
Owner struct {
137-
Username string `json:"username"`
138-
} `json:"owner"`
139-
Links struct {
140-
Clone []struct {
141-
Name string `json:"name"`
142-
Href string `json:"href"`
143-
} `json:"clone"`
144-
} `json:"links"`
145-
} `json:"values"`
146-
Next string `json:"next"`
161+
if resp.StatusCode != http.StatusOK {
162+
return nil, fmt.Errorf("failed to get repositories: %s", resp.Status)
147163
}
148164

165+
// Bitbucket repositories response structure
166+
var response RepositoryListPayload
167+
149168
if err := json.Unmarshal(body, &response); err != nil {
150169
return nil, err
151170
}
@@ -159,7 +178,7 @@ func (g *BitbucketApp) GetRepositories(endpoint ApiEndpoint, owner string, authU
159178
HasWiki: item.HasWiki,
160179
HasIssues: item.HasIssues,
161180
Owner: User{
162-
Login: item.Owner.Username,
181+
Login: item.Owner.DisplayName,
163182
},
164183
}
165184

@@ -227,13 +246,13 @@ func (g *BitbucketApp) GetIssues(repo Repository) ([]Issue, error) {
227246
func (g *BitbucketApp) CreateRepo(endpoint ApiEndpoint, owner string, source Repository) (Repository, error) {
228247
url := g.config.ApiUrl + "/2.0/repositories/" + owner + "/" + slugify(source.Name)
229248

230-
payload := map[string]any{
231-
"scm": "git",
232-
"name": source.Name,
233-
"description": source.Description,
234-
"is_private": source.Private,
235-
"has_wiki": source.HasWiki,
236-
"has_issues": source.HasIssues,
249+
payload := RepositoryPayload{
250+
Scm: "git",
251+
Name: source.Name,
252+
Description: source.Description,
253+
IsPrivate: source.Private,
254+
HasWiki: source.HasWiki,
255+
HasIssues: source.HasIssues,
237256
}
238257

239258
jsonData, err := json.Marshal(payload)
@@ -258,23 +277,7 @@ func (g *BitbucketApp) CreateRepo(endpoint ApiEndpoint, owner string, source Rep
258277
}
259278

260279
// Parse response
261-
var response struct {
262-
Name string `json:"name"`
263-
FullName string `json:"full_name"`
264-
Description string `json:"description"`
265-
IsPrivate bool `json:"is_private"`
266-
HasWiki bool `json:"has_wiki"`
267-
HasIssues bool `json:"has_issues"`
268-
Owner struct {
269-
Username string `json:"username"`
270-
} `json:"owner"`
271-
Links struct {
272-
Clone []struct {
273-
Name string `json:"name"`
274-
Href string `json:"href"`
275-
} `json:"clone"`
276-
} `json:"links"`
277-
}
280+
var response RepositoryPayload
278281

279282
if err := json.Unmarshal(body, &response); err != nil {
280283
return Repository{}, err
@@ -288,7 +291,7 @@ func (g *BitbucketApp) CreateRepo(endpoint ApiEndpoint, owner string, source Rep
288291
HasWiki: response.HasWiki,
289292
HasIssues: response.HasIssues,
290293
Owner: User{
291-
Login: response.Owner.Username,
294+
Login: response.Owner.DisplayName,
292295
},
293296
}
294297

0 commit comments

Comments
 (0)