Skip to content

Commit

Permalink
feat: add feature to get repostiory file
Browse files Browse the repository at this point in the history
  • Loading branch information
rizkyekoputra committed Oct 18, 2022
1 parent c5db75a commit 3d432ca
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
76 changes: 76 additions & 0 deletions pkg/gitlab/gitlab_mock/repository_file.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions pkg/gitlab/repository_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gitlab

import (
"github.com/gopaytech/go-commons/pkg/ptr"
gl "github.com/xanzy/go-gitlab"
)

type RepositoryFile interface {
GetFileByPath(pid NameOrId, path, ref string) (*gl.File, error)
GetRawFileByPath(pid NameOrId, path, ref string) ([]byte, error)
}

type repositoryFile struct {
client *gl.Client
}

func (f *repositoryFile) GetFileByPath(pid NameOrId, path, ref string) (*gl.File, error) {
file, _, err := f.client.RepositoryFiles.GetFile(pid.ID, path, &gl.GetFileOptions{
Ref: ptr.String(ref),
})
if err != nil {
return nil, err
}

return file, nil
}

func (f *repositoryFile) GetRawFileByPath(pid NameOrId, path, ref string) ([]byte, error) {
fileBytes, _, err := f.client.RepositoryFiles.GetRawFile(pid.ID, path, &gl.GetRawFileOptions{
Ref: ptr.String(ref),
})
if err != nil {
return nil, err
}

return fileBytes, nil
}

func NewRepositoryFile(client *gl.Client) RepositoryFile {
return &repositoryFile{client: client}
}
40 changes: 40 additions & 0 deletions pkg/gitlab/repository_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gitlab_test

import (
"testing"

"github.com/gopaytech/go-commons/pkg/encoding"
"github.com/gopaytech/go-commons/pkg/gitlab"
"github.com/stretchr/testify/assert"
)

func Test_repositoryFile_GetFileByPath(t *testing.T) {
t.Run("Get file from branch testing should success", func(t *testing.T) {
client, err := gitlab.NewClient("https://gitlab.com", "")
assert.NoError(t, err)

projectClient := gitlab.NewRepositoryFile(client)

file, err := projectClient.GetFileByPath(gitlab.NameOrId{ID: 12967633}, "i<3Gitlab", "testing")
assert.NoError(t, err)
assert.NotNil(t, file)

content, err := encoding.Base64Decode(file.Content)
assert.NoError(t, err)
assert.Equal(t, "God is a woman.", content)
})
}

func Test_repositoryFile_GetRawFileByPath(t *testing.T) {
t.Run("Get raw file from branch testing should success", func(t *testing.T) {
client, err := gitlab.NewClient("https://gitlab.com", "")
assert.NoError(t, err)

projectClient := gitlab.NewRepositoryFile(client)

fileByte, err := projectClient.GetRawFileByPath(gitlab.NameOrId{ID: 12967633}, "i<3Gitlab", "testing")
assert.NoError(t, err)
assert.NotNil(t, fileByte)
assert.Equal(t, []byte("God is a woman."), fileByte)
})
}

0 comments on commit 3d432ca

Please sign in to comment.