Skip to content

Commit 31185dd

Browse files
authored
feat(gitlab): Add Branches (#6135)
#### Summary <!-- Explain what problem this PR addresses --> <!--
1 parent 15f1129 commit 31185dd

File tree

11 files changed

+177
-23
lines changed

11 files changed

+177
-23
lines changed

plugins/source/gitlab/client/client.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/cloudquery/cloudquery/plugins/source/gitlab/client/services"
87
"github.com/cloudquery/plugin-sdk/schema"
98
"github.com/cloudquery/plugin-sdk/specs"
109
"github.com/rs/zerolog"
@@ -15,17 +14,10 @@ type Client struct {
1514
// This is a client that you need to create and initialize in Configure
1615
// It will be passed for each resource fetcher.
1716
logger zerolog.Logger
18-
Gitlab Services
17+
Gitlab *gitlab.Client
1918
spec specs.Source
2019
BaseURL string
2120
}
22-
type Services struct {
23-
Users services.UsersClient
24-
Groups services.GroupsClient
25-
Projects services.ProjectsClient
26-
Settings services.SettingsClient
27-
Releases services.ReleasesClient
28-
}
2921

3022
func (c *Client) Logger() *zerolog.Logger {
3123
return &c.logger
@@ -56,18 +48,7 @@ func Configure(ctx context.Context, logger zerolog.Logger, s specs.Source) (sche
5648

5749
return &Client{
5850
logger: logger,
59-
Gitlab: NewServices(c),
51+
Gitlab: c,
6052
spec: s,
6153
}, nil
6254
}
63-
64-
// Take gitlab.Client as an argument and return an initialized Services struct
65-
func NewServices(c *gitlab.Client) Services {
66-
return Services{
67-
Users: c.Users,
68-
Groups: c.Groups,
69-
Projects: c.Projects,
70-
Settings: c.Settings,
71-
Releases: c.Releases,
72-
}
73-
}

plugins/source/gitlab/client/testing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func GitlabMockTestHelper(t *testing.T, table *schema.Table, createService func(
4848
}
4949
c := &Client{
5050
logger: l,
51-
Gitlab: NewServices(client),
51+
Gitlab: client,
5252
BaseURL: ts.URL,
5353
}
5454

plugins/source/gitlab/codegen/recipes/projects.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,20 @@ func Projects() []*Resource {
1313
SubService: "projects",
1414
PKColumns: []string{"base_url", "id"},
1515
Struct: &gitlab.Project{},
16-
Relations: []string{"ProjectsReleases()"},
16+
Relations: []string{"ProjectsReleases()", "ProjectBranches()"},
17+
},
18+
{
19+
Service: "projects",
20+
SubService: "project_branches",
21+
Struct: &gitlab.Branch{},
22+
PKColumns: []string{"project_id", "name"},
23+
ExtraColumns: []codegen.ColumnDefinition{
24+
{
25+
Name: "project_id",
26+
Type: schema.TypeInt,
27+
Resolver: `resolveProjectID`,
28+
},
29+
},
1730
},
1831
{
1932
Service: "projects",

plugins/source/gitlab/docs/tables/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
- [gitlab_group_members](gitlab_group_members.md)
77
- [gitlab_projects](gitlab_projects.md)
88
- [gitlab_projects_releases](gitlab_projects_releases.md)
9+
- [gitlab_project_branches](gitlab_project_branches.md)
910
- [gitlab_settings](gitlab_settings.md)
1011
- [gitlab_users](gitlab_users.md)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Table: gitlab_project_branches
2+
3+
The composite primary key for this table is (**project_id**, **name**).
4+
5+
## Relations
6+
7+
This table depends on [gitlab_projects](gitlab_projects.md).
8+
9+
## Columns
10+
11+
| Name | Type |
12+
| ------------- | ------------- |
13+
|_cq_source_name|String|
14+
|_cq_sync_time|Timestamp|
15+
|_cq_id|UUID|
16+
|_cq_parent_id|UUID|
17+
|base_url|String|
18+
|project_id (PK)|Int|
19+
|commit|JSON|
20+
|name (PK)|String|
21+
|protected|Bool|
22+
|merged|Bool|
23+
|default|Bool|
24+
|can_push|Bool|
25+
|developers_can_push|Bool|
26+
|developers_can_merge|Bool|
27+
|web_url|String|

plugins/source/gitlab/docs/tables/gitlab_projects.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The composite primary key for this table is (**base_url**, **id**).
66

77
The following tables depend on gitlab_projects:
88
- [gitlab_projects_releases](gitlab_projects_releases.md)
9+
- [gitlab_project_branches](gitlab_project_branches.md)
910

1011
## Columns
1112

plugins/source/gitlab/resources/services/projects/project_branches.go

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/source/gitlab/resources/services/projects/projects.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package projects
2+
3+
import (
4+
"context"
5+
6+
"github.com/cloudquery/cloudquery/plugins/source/gitlab/client"
7+
"github.com/cloudquery/plugin-sdk/schema"
8+
"github.com/xanzy/go-gitlab"
9+
)
10+
11+
func fetchProjectBranches(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error {
12+
c := meta.(*client.Client)
13+
project := parent.Item.(*gitlab.Project)
14+
opt := &gitlab.ListBranchesOptions{
15+
ListOptions: gitlab.ListOptions{
16+
PerPage: 1000,
17+
},
18+
}
19+
20+
for {
21+
// Get the first page with projects.
22+
branches, resp, err := c.Gitlab.Branches.ListBranches(project.ID, opt, gitlab.WithContext(ctx))
23+
if err != nil {
24+
return err
25+
}
26+
res <- branches
27+
// Exit the loop when we've seen all pages.
28+
if resp.NextPage == 0 {
29+
break
30+
}
31+
32+
// Update the page number to get the next page.
33+
opt.Page = resp.NextPage
34+
}
35+
36+
return nil
37+
}

plugins/source/gitlab/resources/services/projects/projects_mock_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ func buildProjects(mux *httprouter.Router) error {
5353
mux.GET("/api/v4/projects/:projectId/releases", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
5454
fmt.Fprint(w, string(releaseResp))
5555
})
56+
57+
var branch *gitlab.Branch
58+
if err := faker.FakeObject(&branch); err != nil {
59+
return err
60+
}
61+
62+
branchResp, err := json.Marshal([]*gitlab.Branch{branch})
63+
if err != nil {
64+
return err
65+
}
66+
67+
mux.GET("/api/v4/projects/:projectId/repository/branches", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
68+
fmt.Fprint(w, string(branchResp))
69+
})
5670
return nil
5771
}
5872

0 commit comments

Comments
 (0)