Skip to content

Commit cbfe918

Browse files
committed
fix: Add workflow mock test, fix resolveContents
1 parent c7d7029 commit cbfe918

5 files changed

Lines changed: 59 additions & 13 deletions

File tree

plugins/source/github/codegen/recipes/actions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func Actions() []*Resource {
1818
ExtraColumns: append(orgColumns, idColumn,
1919
codegen.ColumnDefinition{
2020
Name: "contents",
21-
Type: schema.TypeJSON,
21+
Type: schema.TypeString,
2222
Resolver: `resolveContents`,
2323
},
2424
),

plugins/source/github/docs/tables/github_workflows.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The composite primary key for this table is (**org**, **id**).
1515
|_cq_parent_id|UUID|
1616
|org (PK)|String|
1717
|id (PK)|Int|
18-
|contents|JSON|
18+
|contents|String|
1919
|node_id|String|
2020
|name|String|
2121
|path|String|

plugins/source/github/resources/services/actions/workflows.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/source/github/resources/services/actions/workflows_fetch.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/cloudquery/cloudquery/plugins/source/github/client"
99
"github.com/cloudquery/plugin-sdk/schema"
1010
"github.com/google/go-github/v48/github"
11-
"sigs.k8s.io/yaml"
1211
)
1312

1413
func fetchWorkflows(ctx context.Context, meta schema.ClientMeta, _ *schema.Resource, res chan<- interface{}) error {
@@ -45,7 +44,7 @@ func resolveContents(ctx context.Context, meta schema.ClientMeta, resource *sche
4544
cl := meta.(*client.Client)
4645
workflow := resource.Item.(*github.Workflow)
4746

48-
parsedUrl, err := url.Parse(*workflow.URL)
47+
parsedUrl, err := url.Parse(*workflow.HTMLURL)
4948
if err != nil {
5049
return err
5150
}
@@ -54,20 +53,20 @@ func resolveContents(ctx context.Context, meta schema.ClientMeta, resource *sche
5453
if len(pathParts) < 2 {
5554
return nil
5655
}
57-
opts := github.RepositoryContentGetOptions{}
56+
owner := pathParts[1]
57+
repo := pathParts[2]
58+
ref := pathParts[4]
59+
path := *workflow.Path
60+
opts := github.RepositoryContentGetOptions{Ref: ref}
5861

59-
fileContent, _, _, err := cl.Github.Repositories.GetContents(ctx, cl.Org, pathParts[2], *workflow.Path, &opts)
62+
fileContent, _, _, err := cl.Github.Repositories.GetContents(ctx, owner, repo, path, &opts)
6063
if err != nil {
6164
// This is not actually an error, it means that a workflow file has been deleted
62-
return err
65+
return resource.Set(c.Name, nil)
6366
}
6467
content, err := fileContent.GetContent()
6568
if err != nil {
6669
return err
6770
}
68-
jsonContent, err := yaml.YAMLToJSON([]byte(content))
69-
if err != nil {
70-
return err
71-
}
72-
return resource.Set(c.Name, jsonContent)
71+
return resource.Set(c.Name, content)
7372
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package actions
2+
3+
import (
4+
"testing"
5+
6+
"github.com/cloudquery/cloudquery/plugins/source/github/client"
7+
"github.com/cloudquery/cloudquery/plugins/source/github/client/mocks"
8+
"github.com/cloudquery/plugin-sdk/faker"
9+
"github.com/golang/mock/gomock"
10+
"github.com/google/go-github/v48/github"
11+
)
12+
13+
func buildWorkflows(t *testing.T, ctrl *gomock.Controller) client.GithubServices {
14+
repositoriesMock := mocks.NewMockRepositoriesService(ctrl)
15+
workflowsMock := mocks.NewMockActionsService(ctrl)
16+
17+
// create mock for repositories
18+
var repository github.Repository
19+
if err := faker.FakeObject(&repository); err != nil {
20+
t.Fatal(err)
21+
}
22+
23+
var workflow github.Workflow
24+
if err := faker.FakeObject(&workflow); err != nil {
25+
t.Fatal(err)
26+
}
27+
workflow.HTMLURL = github.String("https://github.com/testorg/repo/blob/master/.github/workflows/161335")
28+
workflow.Path = github.String(".github/workflows/ci.yml")
29+
count := 1
30+
workflows := github.Workflows{Workflows: []*github.Workflow{&workflow}, TotalCount: &count}
31+
32+
workflowContent := github.RepositoryContent{}
33+
if err := faker.FakeObject(&workflowContent); err != nil {
34+
t.Fatal(err)
35+
}
36+
workflowContent.Encoding = github.String("")
37+
opts := github.RepositoryContentGetOptions{Ref: "master"}
38+
39+
repositoriesMock.EXPECT().ListByOrg(gomock.Any(), "testorg", gomock.Any()).Return([]*github.Repository{&repository}, &github.Response{}, nil)
40+
repositoriesMock.EXPECT().GetContents(gomock.Any(), "testorg", "repo", *workflow.Path, &opts).Return(&workflowContent, nil, nil, nil)
41+
workflowsMock.EXPECT().ListWorkflows(gomock.Any(), *repository.Owner.Login, *repository.Name, gomock.Any()).Return(&workflows, &github.Response{}, nil)
42+
return client.GithubServices{Actions: workflowsMock, Repositories: repositoriesMock}
43+
}
44+
45+
func TestActionBillings(t *testing.T) {
46+
client.GithubMockTestHelper(t, Workflows(), buildWorkflows, client.TestOptions{})
47+
}

0 commit comments

Comments
 (0)