Skip to content

Commit

Permalink
Update endpoints for repository files to V4 API (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander van Harmelen committed Jul 12, 2017
1 parent 2135189 commit a0b1dcb
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 58 deletions.
52 changes: 26 additions & 26 deletions commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,59 +119,59 @@ type CommitAction struct {
Encoding string `url:"encoding,omitempty" json:"encoding,omitempty"`
}

// CreateCommitOptions represents the available options for a new commit.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
type CreateCommitOptions struct {
BranchName *string `url:"branch_name" json:"branch_name"`
CommitMessage *string `url:"commit_message" json:"commit_message"`
Actions []*CommitAction `url:"actions" json:"actions"`
AuthorEmail *string `url:"author_email,omitempty" json:"author_email,omitempty"`
AuthorName *string `url:"author_name,omitempty" json:"author_name,omitempty"`
}

// CreateCommit creates a commit with multiple files and actions.
// GetCommit gets a specific commit identified by the commit hash or name of a
// branch or tag.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
func (s *CommitsService) CreateCommit(pid interface{}, opt *CreateCommitOptions, options ...OptionFunc) (*Commit, *Response, error) {
// GitLab API docs: https://docs.gitlab.com/ce/api/commits.html#get-a-single-commit
func (s *CommitsService) GetCommit(pid interface{}, sha string, options ...OptionFunc) (*Commit, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/commits", url.QueryEscape(project))
u := fmt.Sprintf("projects/%s/repository/commits/%s", url.QueryEscape(project), sha)

req, err := s.client.NewRequest("POST", u, opt, options)
req, err := s.client.NewRequest("GET", u, nil, options)
if err != nil {
return nil, nil, err
}

var c *Commit
resp, err := s.client.Do(req, &c)
c := new(Commit)
resp, err := s.client.Do(req, c)
if err != nil {
return nil, resp, err
}

return c, resp, err
}

// GetCommit gets a specific commit identified by the commit hash or name of a
// branch or tag.
// CreateCommitOptions represents the available options for a new commit.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/commits.html#get-a-single-commit
func (s *CommitsService) GetCommit(pid interface{}, sha string, options ...OptionFunc) (*Commit, *Response, error) {
// GitLab API docs: https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
type CreateCommitOptions struct {
Branch *string `url:"branch" json:"branch"`
CommitMessage *string `url:"commit_message" json:"commit_message"`
Actions []*CommitAction `url:"actions" json:"actions"`
AuthorEmail *string `url:"author_email,omitempty" json:"author_email,omitempty"`
AuthorName *string `url:"author_name,omitempty" json:"author_name,omitempty"`
}

// CreateCommit creates a commit with multiple files and actions.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
func (s *CommitsService) CreateCommit(pid interface{}, opt *CreateCommitOptions, options ...OptionFunc) (*Commit, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/commits/%s", url.QueryEscape(project), sha)
u := fmt.Sprintf("projects/%s/repository/commits", url.QueryEscape(project))

req, err := s.client.NewRequest("GET", u, nil, options)
req, err := s.client.NewRequest("POST", u, opt, options)
if err != nil {
return nil, nil, err
}

c := new(Commit)
resp, err := s.client.Do(req, c)
var c *Commit
resp, err := s.client.Do(req, &c)
if err != nil {
return nil, resp, err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func pipelineExample() {
}

for _, pipeline := range pipelines {
log.Printf("Found pipeline: %s", pipeline)
log.Printf("Found pipeline: %v", pipeline)
}
}
10 changes: 4 additions & 6 deletions examples/repository_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,24 @@ func repositoryFileExample() {

// Create a new repository file
cf := &gitlab.CreateFileOptions{
FilePath: gitlab.String("file.go"),
BranchName: gitlab.String("master"),
Branch: gitlab.String("master"),
Encoding: gitlab.String("text"),
Content: gitlab.String("My file contents"),
CommitMessage: gitlab.String("Adding a test file"),
}
file, _, err := git.RepositoryFiles.CreateFile("myname/myproject", cf)
file, _, err := git.RepositoryFiles.CreateFile("myname/myproject", "file.go", cf)
if err != nil {
log.Fatal(err)
}

// Update a repository file
uf := &gitlab.UpdateFileOptions{
FilePath: gitlab.String(file.FilePath),
BranchName: gitlab.String("master"),
Branch: gitlab.String("master"),
Encoding: gitlab.String("text"),
Content: gitlab.String("My file content"),
CommitMessage: gitlab.String("Fixing typo"),
}
_, _, err = git.RepositoryFiles.UpdateFile("myname/myproject", uf)
_, _, err = git.RepositoryFiles.UpdateFile("myname/myproject", file.FilePath, uf)
if err != nil {
log.Fatal(err)
}
Expand Down
14 changes: 3 additions & 11 deletions repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,18 @@ func (s *RepositoriesService) ListTree(pid interface{}, opt *ListTreeOptions, op
return t, resp, err
}

// RawFileContentOptions represents the available RawFileContent() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repositories.html#raw-file-content
type RawFileContentOptions struct {
FilePath *string `url:"filepath,omitempty" json:"filepath,omitempty"`
}

// RawFileContent gets the raw file contents for a file by commit SHA and path
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repositories.html#raw-file-content
func (s *RepositoriesService) RawFileContent(pid interface{}, sha string, opt *RawFileContentOptions, options ...OptionFunc) ([]byte, *Response, error) {
func (s *RepositoriesService) RawFileContent(pid interface{}, sha string, options ...OptionFunc) ([]byte, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/blobs/%s", url.QueryEscape(project), sha)

req, err := s.client.NewRequest("GET", u, opt, options)
req, err := s.client.NewRequest("GET", u, nil, options)
if err != nil {
return nil, nil, err
}
Expand All @@ -122,7 +114,7 @@ func (s *RepositoriesService) RawBlobContent(pid interface{}, sha string, option
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/raw_blobs/%s", url.QueryEscape(project), sha)
u := fmt.Sprintf("projects/%s/repository/blobs/%s/raw", url.QueryEscape(project), sha)

req, err := s.client.NewRequest("GET", u, nil, options)
if err != nil {
Expand Down
59 changes: 45 additions & 14 deletions repository_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (r File) String() string {
// GetFileOptions represents the available GetFile() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repository_files.html#get-file-from-respository
// https://docs.gitlab.com/ce/api/repository_files.html#get-file-from-repository
type GetFileOptions struct {
Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
}
Expand All @@ -59,7 +59,7 @@ type GetFileOptions struct {
// name, size, content. Note that file content is Base64 encoded.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repository_files.html#get-file-from-respository
// https://docs.gitlab.com/ce/api/repository_files.html#get-file-from-repository
func (s *RepositoryFilesService) GetFile(pid interface{}, fileName string, opt *GetFileOptions, options ...OptionFunc) (*File, *Response, error) {
project, err := parseID(pid)
if err != nil {
Expand All @@ -81,6 +81,39 @@ func (s *RepositoryFilesService) GetFile(pid interface{}, fileName string, opt *
return f, resp, err
}

// GetRawFileOptions represents the available GetRawFile() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repository_files.html#get-raw-file-from-repository
type GetRawFileOptions struct {
Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
}

// GetRawFile allows you to receive the raw file in repository.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repository_files.html#get-raw-file-from-repository
func (s *RepositoryFilesService) GetRawFile(pid interface{}, fileName string, opt *GetRawFileOptions, options ...OptionFunc) (*File, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/files/%s/raw", url.QueryEscape(project), url.QueryEscape(fileName))

req, err := s.client.NewRequest("GET", u, opt, options)
if err != nil {
return nil, nil, err
}

f := new(File)
resp, err := s.client.Do(req, f)
if err != nil {
return nil, resp, err
}

return f, resp, err
}

// FileInfo represents file details of a GitLab repository file.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/repository_files.html
Expand All @@ -98,8 +131,7 @@ func (r FileInfo) String() string {
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repository_files.html#create-new-file-in-repository
type CreateFileOptions struct {
FilePath *string `url:"file_path,omitempty" json:"file_path,omitempty"`
BranchName *string `url:"branch_name,omitempty" json:"branch_name,omitempty"`
Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
Encoding *string `url:"encoding,omitempty" json:"encoding,omitempty"`
AuthorEmail *string `url:"author_email,omitempty" json:"author_email,omitempty"`
AuthorName *string `url:"author_name,omitempty" json:"author_name,omitempty"`
Expand All @@ -111,12 +143,12 @@ type CreateFileOptions struct {
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repository_files.html#create-new-file-in-repository
func (s *RepositoryFilesService) CreateFile(pid interface{}, opt *CreateFileOptions, options ...OptionFunc) (*FileInfo, *Response, error) {
func (s *RepositoryFilesService) CreateFile(pid interface{}, fileName string, opt *CreateFileOptions, options ...OptionFunc) (*FileInfo, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/files", url.QueryEscape(project))
u := fmt.Sprintf("projects/%s/repository/files/%s", url.QueryEscape(project), url.QueryEscape(fileName))

req, err := s.client.NewRequest("POST", u, opt, options)
if err != nil {
Expand All @@ -137,25 +169,25 @@ func (s *RepositoryFilesService) CreateFile(pid interface{}, opt *CreateFileOpti
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repository_files.html#update-existing-file-in-repository
type UpdateFileOptions struct {
FilePath *string `url:"file_path,omitempty" json:"file_path,omitempty"`
BranchName *string `url:"branch_name,omitempty" json:"branch_name,omitempty"`
Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
Encoding *string `url:"encoding,omitempty" json:"encoding,omitempty"`
AuthorEmail *string `url:"author_email,omitempty" json:"author_email,omitempty"`
AuthorName *string `url:"author_name,omitempty" json:"author_name,omitempty"`
Content *string `url:"content,omitempty" json:"content,omitempty"`
CommitMessage *string `url:"commit_message,omitempty" json:"commit_message,omitempty"`
LastCommitID *string `url:"last_commit_id,omitempty" json:"last_commit_id,omitempty"`
}

// UpdateFile updates an existing file in a repository
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repository_files.html#update-existing-file-in-repository
func (s *RepositoryFilesService) UpdateFile(pid interface{}, opt *UpdateFileOptions, options ...OptionFunc) (*FileInfo, *Response, error) {
func (s *RepositoryFilesService) UpdateFile(pid interface{}, fileName string, opt *UpdateFileOptions, options ...OptionFunc) (*FileInfo, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/files", url.QueryEscape(project))
u := fmt.Sprintf("projects/%s/repository/files/%s", url.QueryEscape(project), url.QueryEscape(fileName))

req, err := s.client.NewRequest("PUT", u, opt, options)
if err != nil {
Expand All @@ -176,8 +208,7 @@ func (s *RepositoryFilesService) UpdateFile(pid interface{}, opt *UpdateFileOpti
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repository_files.html#delete-existing-file-in-repository
type DeleteFileOptions struct {
FilePath *string `url:"file_path,omitempty" json:"file_path,omitempty"`
BranchName *string `url:"branch_name,omitempty" json:"branch_name,omitempty"`
Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
AuthorEmail *string `url:"author_email,omitempty" json:"author_email,omitempty"`
AuthorName *string `url:"author_name,omitempty" json:"author_name,omitempty"`
CommitMessage *string `url:"commit_message,omitempty" json:"commit_message,omitempty"`
Expand All @@ -187,12 +218,12 @@ type DeleteFileOptions struct {
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/repository_files.html#delete-existing-file-in-repository
func (s *RepositoryFilesService) DeleteFile(pid interface{}, opt *DeleteFileOptions, options ...OptionFunc) (*FileInfo, *Response, error) {
func (s *RepositoryFilesService) DeleteFile(pid interface{}, fileName string, opt *DeleteFileOptions, options ...OptionFunc) (*FileInfo, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/files", url.QueryEscape(project))
u := fmt.Sprintf("projects/%s/repository/files/%s", url.QueryEscape(project), url.QueryEscape(fileName))

req, err := s.client.NewRequest("DELETE", u, opt, options)
if err != nil {
Expand Down

0 comments on commit a0b1dcb

Please sign in to comment.