Skip to content

Commit

Permalink
feat: Added Projects list function (#4)
Browse files Browse the repository at this point in the history
* feat: Added Projects list function

* feat: Added ListProjects
  • Loading branch information
langecode authored Jan 6, 2025
1 parent 673407f commit 0d25776
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 114 deletions.
19 changes: 19 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module(
name = "go-bitbucket",
version = "1.0.0",
)

bazel_dep(name = "rules_go", version = "0.51.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "gazelle", version = "0.40.0", repo_name = "bazel_gazelle")

go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.23.4")

go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(
go_deps,
"com_github_google_go_querystring",
"com_github_julienschmidt_httprouter",
"com_github_stretchr_testify",
)
258 changes: 258 additions & 0 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

31 changes: 0 additions & 31 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,31 +0,0 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_go",
sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
go_rules_dependencies()
go_register_toolchains(version = "1.19.2")

http_archive(
name = "bazel_gazelle",
sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
],
)

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("//:deps.bzl", "go_dependencies")

# gazelle:repository_macro deps.bzl%go_dependencies
go_dependencies()

gazelle_dependencies()
1 change: 1 addition & 0 deletions bitbucket/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ go_test(
"projects_repos_prs_test.go",
"projects_repos_test.go",
"projects_repos_webhooks_test.go",
"projects_test.go",
"users_test.go",
"webhook_test.go",
],
Expand Down
25 changes: 25 additions & 0 deletions bitbucket/projects.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
package bitbucket

import (
"context"
)

type ProjectsService service

const projectsApiName = "api"

type Project struct {
ID uint64 `json:"id,omitempty"`
Key string `json:"key,omitempty"`
Name string `json:"name"`
Links map[string][]Link `json:"links,omitempty"`
}

type ProjectList struct {
ListResponse
Projects []*Project `json:"values"`
}

func (s *ProjectsService) ListProjects(ctx context.Context, opts *ListOptions) ([]*Project, *Response, error) {
var l ProjectList
resp, err := s.client.GetPaged(ctx, projectsApiName, "projects", &l, opts)
if err != nil {
return nil, resp, err
}
return l.Projects, resp, nil
}
7 changes: 0 additions & 7 deletions bitbucket/projects_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ type Repository struct {
Links map[string][]Link `json:"links,omitempty"`
}

type Project struct {
ID uint64 `json:"id,omitempty"`
Key string `json:"key,omitempty"`
Name string `json:"name"`
Links map[string][]Link `json:"links,omitempty"`
}

type Link struct {
Href string `json:"href"`
Name string `json:"name,omitempty"`
Expand Down
69 changes: 69 additions & 0 deletions bitbucket/projects_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package bitbucket

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestListProjects(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
assert.Equal(t, "GET", req.Method)
assert.Equal(t, "/api/latest/projects", req.URL.Path)
rw.Write([]byte(listProjectsResponse))
}))
defer server.Close()

client, _ := NewClient(server.URL, nil)
ctx := context.Background()
repos, resp, err := client.Projects.ListProjects(ctx, &ListOptions{})
assert.NoError(t, err)
assert.Len(t, repos, 2)
assert.Equal(t, uint64(363), repos[0].ID)
assert.True(t, resp.LastPage)
assert.Equal(t, uint(0), resp.Page.NextPageStart)
}

const listProjectsResponse = `{
"size": 2,
"limit": 25,
"isLastPage": true,
"start": 0,
"nextPageStart": 0,
"values": [
{
"key": "PRJ1",
"id": 363,
"name": "Project 1",
"description": "My project 1",
"public": false,
"type": "NORMAL",
"links": {
"self": [
{
"href": "https://git/projects/PRJ1"
}
]
}
},
{
"key": "PRJ2",
"id": 909,
"name": "Project 2",
"description": "My Project 2",
"public": false,
"type": "NORMAL",
"links": {
"self": [
{
"href": "https://git/projects/PRJ2"
}
]
}
}
]
}
`
75 changes: 0 additions & 75 deletions deps.bzl

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/neticdk/go-bitbucket

go 1.19
go 1.23

require github.com/stretchr/testify v1.8.1

Expand Down
1 change: 1 addition & 0 deletions mock/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
)

var (
ListProjects = EndpointPattern{Pattern: "/api/latest/projects", Method: "GET"}
SearchRepositories = EndpointPattern{Pattern: "/api/latest/repos", Method: "GET"}
ListRepositories = EndpointPattern{Pattern: "/api/latest/projects/:projectKey/repos", Method: "GET"}
GetRepository = EndpointPattern{Pattern: "/api/latest/projects/:projectKey/repos/:repositorySlug", Method: "GET"}
Expand Down

0 comments on commit 0d25776

Please sign in to comment.