-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add feature to get repostiory file
- Loading branch information
1 parent
c5db75a
commit 3d432ca
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |