Skip to content

Commit

Permalink
Use querystring package to build urls & rename resource urls to path
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Jul 11, 2018
1 parent 287e4dd commit b84bf94
Show file tree
Hide file tree
Showing 36 changed files with 345 additions and 494 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ GO_IMAGE = golang:1.10.3
MODD_VERSION = 0.5
GO_PKG_SRC_PATH = "github.com/plouc/go-gitlab-client"

SHA1 = $(shell git rev-parse HEAD)
OS = $(shell uname)

GIT_SHA = $(shell git rev-parse HEAD)
GIT_REF ?= $(shell git symbolic-ref -q --short HEAD)

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
# HELP
Expand Down
26 changes: 6 additions & 20 deletions gitlab/badges.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package gitlab

import (
"encoding/json"
"strconv"
)

const (
projectBadgesUrl = "/projects/:id/badges"
projectBadgeUrl = "/projects/:id/badges/:badge_id"
ProjectBadgesApiPath = "/projects/:id/badges"
ProjectBadgeApiPath = "/projects/:id/badges/:badge_id"
)

type Badge struct {
Expand All @@ -20,20 +19,7 @@ type Badge struct {
}

func (g *Gitlab) ProjectBadges(projectId string, o *PaginationOptions) ([]*Badge, *ResponseMeta, error) {
u := g.ResourceUrl(projectBadgesUrl, map[string]string{":id": projectId})

if o != nil {
q := u.Query()

if o.Page != 1 {
q.Set("page", strconv.Itoa(o.Page))
}
if o.PerPage != 0 {
q.Set("per_page", strconv.Itoa(o.PerPage))
}

u.RawQuery = q.Encode()
}
u := g.ResourceUrlQ(ProjectBadgesApiPath, map[string]string{":id": projectId}, o)

var badges []*Badge

Expand All @@ -46,7 +32,7 @@ func (g *Gitlab) ProjectBadges(projectId string, o *PaginationOptions) ([]*Badge
}

func (g *Gitlab) ProjectBadge(projectId, badgeId string) (*Badge, *ResponseMeta, error) {
u := g.ResourceUrl(projectBadgeUrl, map[string]string{
u := g.ResourceUrl(ProjectBadgeApiPath, map[string]string{
":id": projectId,
":badge_id": badgeId,
})
Expand All @@ -62,7 +48,7 @@ func (g *Gitlab) ProjectBadge(projectId, badgeId string) (*Badge, *ResponseMeta,
}

func (g *Gitlab) AddProjectBadge(projectId string, badge *Badge) (*Badge, *ResponseMeta, error) {
u := g.ResourceUrl(projectBadgesUrl, map[string]string{":id": projectId})
u := g.ResourceUrl(ProjectBadgesApiPath, map[string]string{":id": projectId})

badgeJson, err := json.Marshal(badge)
if err != nil {
Expand All @@ -79,7 +65,7 @@ func (g *Gitlab) AddProjectBadge(projectId string, badge *Badge) (*Badge, *Respo
}

func (g *Gitlab) RemoveProjectBadge(projectId, badgeId string) (*ResponseMeta, error) {
u := g.ResourceUrl(projectBadgeUrl, map[string]string{
u := g.ResourceUrl(ProjectBadgeApiPath, map[string]string{
":id": projectId,
":badge_id": badgeId,
})
Expand Down
39 changes: 13 additions & 26 deletions gitlab/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package gitlab

import (
"encoding/json"
"strconv"
)

const (
projectBranchesUrl = "/projects/:id/repository/branches" // List repository branches.
projectBranchUrl = "/projects/:id/repository/branches/:branch" // Get a specific branch of a project.
projectMergedBranchesUrl = "/projects/:id/repository/merged_branches"
ProjectBranchesApiPath = "/projects/:id/repository/branches"
ProjectBranchApiPath = "/projects/:id/repository/branches/:branch"
ProjectMergedBranchesApiPath = "/projects/:id/repository/merged_branches"
)

type BranchCommit struct {
Expand All @@ -32,27 +31,14 @@ type Branch struct {

type BranchesOptions struct {
PaginationOptions
Search string // Return list of branches matching the search criteria
SortOptions

// Return list of branches matching the search criteria
Search string `url:"search,omitempty"`
}

func (g *Gitlab) ProjectBranches(projectId string, o *BranchesOptions) ([]*Branch, *ResponseMeta, error) {
u := g.ResourceUrl(projectBranchesUrl, map[string]string{":id": projectId})

if o != nil {
q := u.Query()

if o.Page != 1 {
q.Set("page", strconv.Itoa(o.Page))
}
if o.PerPage != 0 {
q.Set("per_page", strconv.Itoa(o.PerPage))
}
if o.Search != "" {
q.Set("search", o.Search)
}

u.RawQuery = q.Encode()
}
u := g.ResourceUrlQ(ProjectBranchesApiPath, map[string]string{":id": projectId}, o)

var branches []*Branch

Expand All @@ -65,7 +51,7 @@ func (g *Gitlab) ProjectBranches(projectId string, o *BranchesOptions) ([]*Branc
}

func (g *Gitlab) ProjectBranch(projectId, branchName string) (*Branch, *ResponseMeta, error) {
u := g.ResourceUrl(projectBranchUrl, map[string]string{
u := g.ResourceUrl(ProjectBranchApiPath, map[string]string{
":id": projectId,
":branch": branchName,
})
Expand All @@ -81,7 +67,8 @@ func (g *Gitlab) ProjectBranch(projectId, branchName string) (*Branch, *Response
}

func (g *Gitlab) AddProjectBranch(projectId string, branchName, ref string) (*Branch, *ResponseMeta, error) {
u := g.ResourceUrl(projectBranchesUrl, map[string]string{":id": projectId})
u := g.ResourceUrl(ProjectBranchesApiPath, map[string]string{":id": projectId})

q := u.Query()
q.Set("branch", branchName)
q.Set("ref", ref)
Expand All @@ -97,7 +84,7 @@ func (g *Gitlab) AddProjectBranch(projectId string, branchName, ref string) (*Br
}

func (g *Gitlab) RemoveProjectBranch(projectId, branchName string) (*ResponseMeta, error) {
u := g.ResourceUrl(projectBranchUrl, map[string]string{
u := g.ResourceUrl(ProjectBranchApiPath, map[string]string{
":id": projectId,
":branch": branchName,
})
Expand All @@ -109,7 +96,7 @@ func (g *Gitlab) RemoveProjectBranch(projectId, branchName string) (*ResponseMet
}

func (g *Gitlab) RemoveProjectMergedBranches(projectId string) (string, *ResponseMeta, error) {
u := g.ResourceUrl(projectMergedBranchesUrl, map[string]string{":id": projectId})
u := g.ResourceUrl(ProjectMergedBranchesApiPath, map[string]string{":id": projectId})

var responseWithMessage *ResponseWithMessage
contents, meta, err := g.buildAndExecRequest("DELETE", u.String(), nil)
Expand Down
8 changes: 4 additions & 4 deletions gitlab/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

const (
projectCommitBuildsUrl = "/projects/:id/repository/commits/:sha/builds"
projectBuildArtifactsUrl = "/projects/:id/builds/:build_id/artifacts"
ProjectCommitBuildsApiPath = "/projects/:id/repository/commits/:sha/builds"
ProjectBuildArtifactsApiPath = "/projects/:id/builds/:build_id/artifacts"
)

type ArtifactsFile struct {
Expand All @@ -34,7 +34,7 @@ type Build struct {
}

func (g *Gitlab) ProjectCommitBuilds(id, sha1 string) ([]*Build, *ResponseMeta, error) {
u := g.ResourceUrl(projectCommitBuildsUrl, map[string]string{
u := g.ResourceUrl(ProjectCommitBuildsApiPath, map[string]string{
":id": id,
":sha": sha1,
})
Expand All @@ -52,7 +52,7 @@ func (g *Gitlab) ProjectCommitBuilds(id, sha1 string) ([]*Build, *ResponseMeta,
}

func (g *Gitlab) ProjectBuildArtifacts(id, buildId string) (io.ReadCloser, error) {
u := g.ResourceUrl(projectBuildArtifactsUrl, map[string]string{
u := g.ResourceUrl(ProjectBuildArtifactsApiPath, map[string]string{
":id": id,
":build_id": buildId,
})
Expand Down
4 changes: 2 additions & 2 deletions gitlab/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const (
projectCommitStatusesUrl = "/projects/:id/repository/commits/:sha/statuses"
ProjectCommitStatusesApiPath = "/projects/:id/repository/commits/:sha/statuses"
)

type CommitStatus struct {
Expand All @@ -25,7 +25,7 @@ type CommitStatus struct {
}

func (g *Gitlab) ProjectCommitStatuses(id, sha1 string) ([]*CommitStatus, *ResponseMeta, error) {
u := g.ResourceUrl(projectCommitStatusesUrl, map[string]string{
u := g.ResourceUrl(ProjectCommitStatusesApiPath, map[string]string{
":id": id,
":sha": sha1,
})
Expand Down
12 changes: 6 additions & 6 deletions gitlab/deploy_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

const (
projectDeployKeysUrl = "/projects/:id/keys"
projectDeployKeyUrl = "/projects/:id/keys/:key_id"
ProjectDeployKeysApiPath = "/projects/:id/keys"
ProjectDeployKeyApiPath = "/projects/:id/keys/:key_id"
)

/*
Expand All @@ -21,7 +21,7 @@ Parameters:
*/
func (g *Gitlab) ProjectDeployKeys(id string) ([]*PublicKey, *ResponseMeta, error) {
u := g.ResourceUrl(projectDeployKeysUrl, map[string]string{":id": id})
u := g.ResourceUrl(ProjectDeployKeysApiPath, map[string]string{":id": id})

var deployKeys []*PublicKey

Expand All @@ -45,7 +45,7 @@ Parameters:
*/
func (g *Gitlab) ProjectDeployKey(id, keyId string) (*PublicKey, *ResponseMeta, error) {
u := g.ResourceUrl(projectDeployKeyUrl, map[string]string{
u := g.ResourceUrl(ProjectDeployKeyApiPath, map[string]string{
":id": id,
":key_id": keyId,
})
Expand Down Expand Up @@ -73,7 +73,7 @@ Parameters:
*/
func (g *Gitlab) AddProjectDeployKey(id, title, key string) (*ResponseMeta, error) {
u := g.ResourceUrl(projectDeployKeysUrl, map[string]string{":id": id})
u := g.ResourceUrl(ProjectDeployKeysApiPath, map[string]string{":id": id})

var err error

Expand All @@ -100,7 +100,7 @@ Parameters:
*/
func (g *Gitlab) RemoveProjectDeployKey(id, keyId string) (*ResponseMeta, error) {
u := g.ResourceUrl(projectDeployKeyUrl, map[string]string{
u := g.ResourceUrl(ProjectDeployKeyApiPath, map[string]string{
":id": id,
":key_id": keyId,
})
Expand Down
3 changes: 1 addition & 2 deletions gitlab/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ type FeedCommit struct {
}

func (g *Gitlab) Activity() (ActivityFeed, error) {
url := g.BaseUrl + dashboardFeedPath + "?private_token=" + g.Token
fmt.Println(url)
url := g.BaseUrl + DashboardFeedPath + "?private_token=" + g.Token

contents, _, err := g.buildAndExecRequest("GET", url, nil)
if err != nil {
Expand Down
44 changes: 37 additions & 7 deletions gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"net/url"
"strconv"
"strings"

"github.com/google/go-querystring/query"
)

const (
dashboardFeedPath = "/dashboard.atom"
DashboardFeedPath = "/dashboard.atom"
)

type Gitlab struct {
Expand All @@ -26,8 +28,8 @@ type Gitlab struct {
}

type PaginationOptions struct {
Page int
PerPage int
Page int `url:"page,omitempty"`
PerPage int `url:"per_page,omitempty"`
}

type SortDirection string
Expand All @@ -37,6 +39,11 @@ const (
SortDirectionDesc SortDirection = "desc"
)

type SortOptions struct {
OrderBy string `url:"order_by,omitempty"`
Sort SortDirection `url:"sort,omitempty"`
}

type ResponseWithMessage struct {
Message string `json:"message"`
}
Expand Down Expand Up @@ -64,6 +71,7 @@ var (
`If set to true, gitlab client will skip certificate checking for https, possibly exposing your system to MITM attack.`)
)

// NewGitlab generates a new gitlab service
func NewGitlab(baseUrl, apiPath, token string) *Gitlab {
config := &tls.Config{InsecureSkipVerify: *skipCertVerify}
tr := &http.Transport{
Expand All @@ -80,16 +88,38 @@ func NewGitlab(baseUrl, apiPath, token string) *Gitlab {
}
}

func (g *Gitlab) ResourceUrl(p string, params map[string]string) *url.URL {
// ResourceUrl builds an url for given resource path.
//
// It replaces path placeholders with values from `params`:
//
// /whatever/:id => /whatever/1
//
func (g *Gitlab) ResourceUrl(path string, params map[string]string) *url.URL {
if params != nil {
for key, val := range params {
p = strings.Replace(p, key, val, -1)
path = strings.Replace(path, key, val, -1)
}
}

u, err := url.Parse(g.BaseUrl + g.ApiPath + p)
u, err := url.Parse(g.BaseUrl + g.ApiPath + path)
if err != nil {
panic("Error while building gitlab url")
panic("Error while building gitlab url, unable to parse generated url")
}

return u
}

// ResourceUrlQ generates an url and appends a query string to it if available
func (g *Gitlab) ResourceUrlQ(path string, params map[string]string, qs interface{}) *url.URL {
u := g.ResourceUrl(path, params)

if qs != nil {
v, err := query.Values(qs)
if err != nil {
panic("Error while building gitlab url, unable to set query string")
}

u.RawQuery = v.Encode()
}

return u
Expand Down
4 changes: 2 additions & 2 deletions gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestResourceUrl(t *testing.T) {

assert.Equal(
t,
gitlab.ResourceUrl(projectsUrl, nil).String(),
gitlab.ResourceUrl(ProjectsApiPath, nil).String(),
"http://base_url/api_path/projects",
)
assert.Equal(
t,
gitlab.ResourceUrl(projectUrl, map[string]string{":id": "123"}).String(),
gitlab.ResourceUrl(ProjectApiPath, map[string]string{":id": "123"}).String(),
"http://base_url/api_path/projects/123",
)
}
Loading

0 comments on commit b84bf94

Please sign in to comment.