Update gitlab API to version v4

This commit is contained in:
Ivan Chernov 2017-08-10 09:24:35 +03:00
parent 3de3662d29
commit 6074a0164e
5 changed files with 39 additions and 44 deletions

View file

@ -1,17 +1,16 @@
package client
import (
"encoding/base64"
"encoding/json"
"strconv"
"strings"
)
const (
searchUrl = "/projects/search/:query"
projectsUrl = "/projects"
projectUrl = "/projects/:id"
repoUrlRawFile = "/projects/:id/repository/blobs/:sha"
repoUrlRawFileRef = "/projects/:id/repository/files"
repoUrlRawFileRef = "/projects/:id/repository/files/:filepath"
commitStatusUrl = "/projects/:id/statuses/:sha"
)
@ -47,6 +46,7 @@ func (c *Client) Projects(page int, per_page int, hide_archives bool) ([]*Projec
projectsOptions := QMap{
"page": strconv.Itoa(page),
"per_page": strconv.Itoa(per_page),
"membership": "true",
}
if hide_archives {
@ -79,39 +79,31 @@ func (c *Client) Project(id string) (*Project, error) {
return project, err
}
// Get Raw file content
func (c *Client) RepoRawFile(id, sha, filepath string) ([]byte, error) {
url, opaque := c.ResourceUrl(
repoUrlRawFile,
QMap{
":id": id,
":sha": sha,
},
QMap{
"filepath": filepath,
},
)
contents, err := c.Do("GET", url, opaque, nil)
return contents, err
}
func (c *Client) RepoRawFileRef(id, ref, filepath string) ([]byte, error) {
var fileRef FileRef
url, opaque := c.ResourceUrl(
repoUrlRawFileRef,
QMap{
":id": id,
":id": id,
":filepath": filepath,
},
QMap{
"filepath": filepath,
"ref": ref,
"ref": ref,
},
)
contents, err := c.Do("GET", url, opaque, nil)
if err != nil {
return nil, err
}
return contents, err
err = json.Unmarshal(contents, &fileRef)
if err != nil {
return nil, err
}
fileRawContent, err := base64.StdEncoding.DecodeString(fileRef.Content)
return fileRawContent, err
}
//
@ -138,8 +130,9 @@ func (c *Client) SetStatus(id, sha, state, desc, ref, link string) error {
// Get a list of projects by query owned by the authenticated user.
func (c *Client) SearchProjectId(namespace string, name string) (id int, err error) {
url, opaque := c.ResourceUrl(searchUrl, nil, QMap{
":query": strings.ToLower(name),
url, opaque := c.ResourceUrl(projectsUrl, nil, QMap{
"query": strings.ToLower(name),
"membership": "true",
})
var projects []*Project

View file

@ -136,3 +136,15 @@ type HookPayload struct {
ObjectKind string `json:"object_kind,omitempty"`
ObjectAttributes *HookObjAttr `json:"object_attributes,omitempty"`
}
type FileRef struct {
FileName string `json:"file_name,omitempty"`
FilePath string `json:"file_path,omitempty"`
Size int `json:"size,omitempty"`
Encoding string `json:"encoding,omitempty"`
Content string `json:"content"`
Ref string `json:"ref,omitempty"`
BlobId string `json:"blob_id,omitempty"`
CommitId string `json:"commit_id,omitempty"`
LastCommitId string `json:"last_commit_id,omitempty"`
}

View file

@ -311,17 +311,7 @@ func (g *Gitlab) Perm(u *model.User, owner, name string) (*model.Perm, error) {
// File fetches a file from the remote repository and returns in string format.
func (g *Gitlab) File(user *model.User, repo *model.Repo, build *model.Build, f string) ([]byte, error) {
var client = NewClient(g.URL, user.Token, g.SkipVerify)
id, err := GetProjectId(g, client, repo.Owner, repo.Name)
if err != nil {
return nil, err
}
out, err := client.RepoRawFile(id, build.Commit, f)
if err != nil {
return nil, err
}
return out, err
return g.FileRef(user, repo, build.Commit, f)
}
// FileRef fetches the file from the GitHub repository and returns its contents.

View file

@ -18,7 +18,7 @@ const (
// NewClient is a helper function that returns a new GitHub
// client using the provided OAuth token.
func NewClient(url, accessToken string, skipVerify bool) *client.Client {
client := client.New(url, "/api/v3", accessToken, skipVerify)
client := client.New(url, "/api/v4", accessToken, skipVerify)
return client
}

View file

@ -15,7 +15,7 @@ func NewServer() *httptest.Server {
//println(r.URL.Path + " " + r.Method)
// evaluate the path to serve a dummy data file
switch r.URL.Path {
case "/api/v3/projects":
case "/api/v4/projects":
if r.URL.Query().Get("archived") == "false" {
w.Write(notArchivedProjectsPayload)
} else {
@ -23,13 +23,13 @@ func NewServer() *httptest.Server {
}
return
case "/api/v3/projects/diaspora/diaspora-client":
case "/api/v4/projects/diaspora/diaspora-client":
w.Write(project4Paylod)
return
case "/api/v3/projects/brightbox/puppet":
case "/api/v4/projects/brightbox/puppet":
w.Write(project6Paylod)
return
case "/api/v3/projects/diaspora/diaspora-client/services/drone-ci":
case "/api/v4/projects/diaspora/diaspora-client/services/drone-ci":
switch r.Method {
case "PUT":
if r.FormValue("token") == "" {
@ -45,7 +45,7 @@ func NewServer() *httptest.Server {
case "/oauth/token":
w.Write(accessTokenPayload)
return
case "/api/v3/user":
case "/api/v4/user":
w.Write(currentUserPayload)
return
}