Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(project) list/add/edit/rm project variables #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ go-gitlab-client is a simple client written in golang to consume gitlab API.
*
### Projects [gitlab api doc](http://doc.gitlab.com/ce/api/projects.html)
* list projects
* add/get/edit/rm single project
* get single project
* remove project
* list project variables
* add/get/edit/rm project variable
*
### Repositories [gitlab api doc](http://doc.gitlab.com/ce/api/repositories.html)
* list repository branches
Expand Down
103 changes: 102 additions & 1 deletion examples/projects/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func main() {
" > -m hooks -id PROJECT_ID\n"+
" > -m branches -id PROJECT_ID\n"+
" > -m team -id PROJECT_ID\n"+
" > -m merge_requests -id PROJECT_ID [-state <all|merged|opened|closed>] [-order <created_at|updated_at>] [-sort <asc|desc>]")
" > -m merge_requests -id PROJECT_ID [-state <all|merged|opened|closed>] [-order <created_at|updated_at>] [-sort <asc|desc>]\n"+
" > -m variables -id PROJECT_ID [-o <add|get|edit|rm>] [-key VARIABLE_KEY] [-value VARIABLE_VALUE]")

var id string
flag.StringVar(&id, "id", "", "Specify repository id")
Expand All @@ -56,6 +57,12 @@ func main() {
var operation string
flag.StringVar(&operation, "o", "", "Specify operation")

var key string
flag.StringVar(&key, "key", "", "Specify key")

var value string
flag.StringVar(&value, "value", "", "Specify value")

var desc string
flag.StringVar(&desc, "desc", "", "Specify description")

Expand Down Expand Up @@ -223,5 +230,99 @@ func main() {
fmt.Printf("> [%d] %s (+%d) by %s on %s.\n", mr.Id, mr.Title, mr.Upvotes, mr.Author.Name, mr.CreatedAt)
}

case "variables":

if id == "" {
flag.Usage()
return
}

if operation == "" {
fmt.Println("Fetching project variables...")

variables, err := gitlab.ProjectVariables(id)
if err != nil {
fmt.Println(err.Error())
return
}

for _, variable := range variables {
fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)
}
return
}

switch operation {
case "get":
fmt.Println("Fetching project variable...")
if key == "" {
flag.Usage()
return
}

variable, err := gitlab.ProjectVariable(id, key)
if err != nil {
fmt.Println(err.Error())
return
}

fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)

case "add":
fmt.Println("Add project variable...")
if key == "" || value == "" {
flag.Usage()
return
}

req := gogitlab.Variable{
Key: key,
Value: value,
}

variable, err := gitlab.AddProjectVariable(id, &req)
if err != nil {
fmt.Println(err.Error())
return
}

fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)

case "edit":
fmt.Println("Edit project variable...")
if key == "" || value == "" {
flag.Usage()
return
}

req := gogitlab.Variable{
Key: key,
Value: value,
}

variable, err := gitlab.UpdateProjectVariable(id, &req)
if err != nil {
fmt.Println(err.Error())
return
}

fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)

case "rm":
fmt.Println("Delete project variable...")
if key == "" {
flag.Usage()
return
}

variable, err := gitlab.DeleteProjectVariable(id, key)
if err != nil {
fmt.Println(err.Error())
return
}

fmt.Printf("> %s -> %s.\n", variable.Key, variable.Value)

}
}
}
8 changes: 4 additions & 4 deletions hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestHook(t *testing.T) {
ts, gitlab := Stub("stubs/hooks/show.json")
hook, err := gitlab.ProjectHook("1", "2")

assert.Equal(t, err, nil)
assert.NoError(t, err)
assert.IsType(t, new(Hook), hook)
assert.Equal(t, hook.Url, "http://example.com/hook")
defer ts.Close()
Expand All @@ -20,7 +20,7 @@ func TestParsePushHook(t *testing.T) {
stub, _ := ioutil.ReadFile("stubs/hooks/push.json")
p, err := ParseHook([]byte(stub))

assert.Equal(t, err, nil)
assert.NoError(t, err)
assert.IsType(t, new(HookPayload), p)
assert.Equal(t, p.After, "da1560886d4f094c3e6c9ef40349f7d38b5d27d7")
assert.Equal(t, p.Repository.URL, "git@localhost:diaspora.git")
Expand All @@ -35,7 +35,7 @@ func TestParseIssueHook(t *testing.T) {
stub, _ := ioutil.ReadFile("stubs/hooks/issue.json")
p, err := ParseHook([]byte(stub))

assert.Equal(t, err, nil)
assert.NoError(t, err)
assert.Equal(t, p.ObjectKind, "issue")
assert.Equal(t, p.ObjectAttributes.Id, 301)
}
Expand All @@ -44,7 +44,7 @@ func TestParseMergeRequestHook(t *testing.T) {
stub, _ := ioutil.ReadFile("stubs/hooks/merge_request.json")
p, err := ParseHook([]byte(stub))

assert.Equal(t, err, nil)
assert.NoError(t, err)
assert.Equal(t, p.ObjectKind, "merge_request")
assert.Equal(t, p.ObjectAttributes.TargetBranch, "master")
assert.Equal(t, p.ObjectAttributes.SourceProjectId, p.ObjectAttributes.TargetProjectId)
Expand Down
2 changes: 1 addition & 1 deletion issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestAddIssue(t *testing.T) {
}
issue, err := gitlab.AddIssue("1", req)

assert.Equal(t, err, nil)
assert.NoError(t, err)
assert.Equal(t, issue.Id, 1)
assert.Equal(t, issue.IId, 1)
assert.Equal(t, issue.ProjectId, 1)
Expand Down
16 changes: 8 additions & 8 deletions merge_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestProjectMergeRequests(t *testing.T) {
ts, gitlab := Stub("stubs/merge_requests/index.json")
mrs, err := gitlab.ProjectMergeRequests("3", nil)

assert.Equal(t, err, nil)
assert.NoError(t, err)
assert.Equal(t, len(mrs), 1)
defer ts.Close()
}
Expand All @@ -19,7 +19,7 @@ func TestProjectMergeRequest(t *testing.T) {
ts, gitlab := Stub("stubs/merge_requests/show.json")
mr, err := gitlab.ProjectMergeRequest("3", "1")

assert.Equal(t, err, nil)
assert.NoError(t, err)
assert.Equal(t, mr.TargetBranch, "master")
assert.Equal(t, mr.MergeStatus, "can_be_merged")
assert.Equal(t, mr.SourceProjectID, 2)
Expand All @@ -31,7 +31,7 @@ func TestProjectMergeRequestCommits(t *testing.T) {
ts, gitlab := Stub("stubs/merge_requests/commits.json")
commits, err := gitlab.ProjectMergeRequestCommits("3", "1")

assert.Equal(t, err, nil)
assert.NoError(t, err)
assert.Equal(t, len(commits), 2)
defer ts.Close()
}
Expand All @@ -40,7 +40,7 @@ func TestProjectMergeRequestChanges(t *testing.T) {
ts, gitlab := Stub("stubs/merge_requests/changes.json")
mr, err := gitlab.ProjectMergeRequestChanges("3", "1")

assert.Equal(t, err, nil)
assert.NoError(t, err)
assert.Equal(t, len(mr.Changes), 1)
defer ts.Close()
}
Expand All @@ -52,7 +52,7 @@ func TestAddMergeRequest(t *testing.T) {
}
_, err := gitlab.AddMergeRequest(&req)

assert.Equal(t, err, nil)
assert.NoError(t, err)
defer ts.Close()
}

Expand All @@ -64,21 +64,21 @@ func TestEditMergeRequest(t *testing.T) {
}
err := gitlab.EditMergeRequest(&req)

assert.Equal(t, err, nil)
assert.NoError(t, err)
defer ts.Close()
}

func TestProjectMergeRequestAccept(t *testing.T) {
ts, gitlab := Stub("stubs/merge_requests/show.json")
req := AcceptMergeRequestRequest{}
_, err := gitlab.ProjectMergeRequestAccept("3", "1", &req)
assert.Equal(t, err, nil)
assert.NoError(t, err)
defer ts.Close()
}

func TestProjectMergeRequestCancelMerge(t *testing.T) {
ts, gitlab := Stub("stubs/merge_requests/show.json")
_, err := gitlab.ProjectMergeRequestCancelMerge("3", "1")
assert.Equal(t, err, nil)
assert.NoError(t, err)
defer ts.Close()
}
113 changes: 105 additions & 8 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
)

const (
projects_url = "/projects" // Get a list of projects owned by the authenticated user
projects_all = "/projects/all" // Get a list of all GitLab projects (admin only)
projects_search_url = "/projects/search/:query" // Search for projects by name
project_url = "/projects/:id" // Get a specific project, identified by project ID or NAME
project_url_events = "/projects/:id/events" // Get project events
project_url_branches = "/projects/:id/repository/branches" // Lists all branches of a project
project_url_members = "/projects/:id/members" // List project team members
project_url_member = "/projects/:id/members/:user_id" // Get project team member
projects_url = "/projects" // Get a list of projects owned by the authenticated user
projects_all = "/projects/all" // Get a list of all GitLab projects (admin only)
projects_search_url = "/projects/search/:query" // Search for projects by name
project_url = "/projects/:id" // Get a specific project, identified by project ID or NAME
project_url_events = "/projects/:id/events" // Get project events
project_url_branches = "/projects/:id/repository/branches" // Lists all branches of a project
project_url_members = "/projects/:id/members" // List project team members
project_url_member = "/projects/:id/members/:user_id" // Get project team member
project_url_variables = "/projects/:id/variables" // List project variables or add one
project_url_variable = "/projects/:id/variables/:variable_key" // Get or Update project variable
)

type Member struct {
Expand All @@ -36,6 +38,11 @@ type Namespace struct {
Updated_At string
}

type Variable struct {
Key string `json:"key"`
Value string `json:"value"`
}

// A gitlab project
type Project struct {
Id int `json:"id,omitempty"`
Expand Down Expand Up @@ -180,3 +187,93 @@ func (g *Gitlab) ProjectMembers(id string) ([]*Member, error) {

return members, err
}

/*
Lists all variables of a project
*/
func (g *Gitlab) ProjectVariables(id string) ([]*Variable, error) {
url, opaque := g.ResourceUrlRaw(project_url_variables, map[string]string{":id": id})

var variables []*Variable

contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
if err == nil {
err = json.Unmarshal(contents, &variables)
}

return variables, err
}

/*
Shows a project variable
*/
func (g *Gitlab) ProjectVariable(id string, key string) (*Variable, error) {
url, opaque := g.ResourceUrlRaw(project_url_variable, map[string]string{":id": id, ":variable_key": key})

var result *Variable

contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
if err == nil {
err = json.Unmarshal(contents, &result)
}

return result, err
}

/*
Adds a project variable
*/
func (g *Gitlab) AddProjectVariable(id string, variable *Variable) (*Variable, error) {
url, opaque := g.ResourceUrlRaw(project_url_variables, map[string]string{":id": id})

encodedRequest, err := json.Marshal(variable)
if err != nil {
return nil, err
}

var result *Variable

contents, err := g.buildAndExecRequestRaw("POST", url, opaque, encodedRequest)
if err == nil {
err = json.Unmarshal(contents, &result)
}

return result, err
}

/*
Updates a project variable
*/
func (g *Gitlab) UpdateProjectVariable(id string, variable *Variable) (*Variable, error) {
url := g.ResourceUrl(project_url_variable, map[string]string{":id": id, ":variable_key": variable.Key})

encodedRequest, err := json.Marshal(variable)
if err != nil {
return nil, err
}
var result *Variable

contents, err := g.buildAndExecRequest("PUT", url, encodedRequest)

if err == nil {
err = json.Unmarshal(contents, &result)
}

return result, err
}

/*
Deletes a project variable
*/
func (g *Gitlab) DeleteProjectVariable(id string, key string) (*Variable, error) {
url, opaque := g.ResourceUrlRaw(project_url_variable, map[string]string{":id": id, ":variable_key": key})

var result *Variable

contents, err := g.buildAndExecRequestRaw("DELETE", url, opaque, nil)
if err == nil {
err = json.Unmarshal(contents, &result)
}

return result, err
}
Loading