Skip to content
Merged
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
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ 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
* get single project
* remove project

* add/get/edit/rm single project
*
### Repositories [gitlab api doc](http://doc.gitlab.com/ce/api/repositories.html)
* list repository branches
Expand Down
74 changes: 49 additions & 25 deletions examples/projects/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
var method string
flag.StringVar(&method, "m", "", "Specify method to retrieve projects infos, available methods:\n"+
" > -m projects\n"+
" > -m project -id PROJECT_ID\n"+
" > -m project -id PROJECT_ID [-o <edit> -desc PROJECT_DESCRIPTION]\n"+
" > -m hooks -id PROJECT_ID\n"+
" > -m branches -id PROJECT_ID\n"+
" > -m team -id PROJECT_ID\n"+
Expand All @@ -53,6 +53,12 @@ func main() {
var sort string
flag.StringVar(&sort, "sort", "", "Specify merge request sort")

var operation string
flag.StringVar(&operation, "o", "", "Specify operation")

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

flag.Usage = func() {
fmt.Printf("Usage:\n")
flag.PrintDefaults()
Expand Down Expand Up @@ -84,38 +90,56 @@ func main() {
}

case "project":
fmt.Println("Fetching project…")

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

project, err := gitlab.Project(id)
if err != nil {
fmt.Println(err.Error())
return
if operation == "" {
fmt.Println("Fetching project…")

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

format := "> %-23s: %s\n"

fmt.Printf("%s\n", project.Name)
fmt.Printf(format, "id", strconv.Itoa(project.Id))
fmt.Printf(format, "name", project.Name)
fmt.Printf(format, "description", project.Description)
fmt.Printf(format, "default branch", project.DefaultBranch)
fmt.Printf(format, "owner.name", project.Owner.Username)
fmt.Printf(format, "public", strconv.FormatBool(project.Public))
fmt.Printf(format, "path", project.Path)
fmt.Printf(format, "path with namespace", project.PathWithNamespace)
fmt.Printf(format, "issues enabled", strconv.FormatBool(project.IssuesEnabled))
fmt.Printf(format, "merge requests enabled", strconv.FormatBool(project.MergeRequestsEnabled))
fmt.Printf(format, "wall enabled", strconv.FormatBool(project.WallEnabled))
fmt.Printf(format, "wiki enabled", strconv.FormatBool(project.WikiEnabled))
fmt.Printf(format, "shared runners enabled", strconv.FormatBool(project.SharedRunners))
fmt.Printf(format, "created at", project.CreatedAtRaw)
//fmt.Printf(format, "namespace", project.Namespace)
}

switch operation {
case "edit":
fmt.Println("Edit project...")

project := gogitlab.Project{
Description: desc,
}

_, err := gitlab.UpdateProject(id, &project)
if err != nil {
fmt.Println(err.Error())
return
}
}

format := "> %-23s: %s\n"

fmt.Printf("%s\n", project.Name)
fmt.Printf(format, "id", strconv.Itoa(project.Id))
fmt.Printf(format, "name", project.Name)
fmt.Printf(format, "description", project.Description)
fmt.Printf(format, "default branch", project.DefaultBranch)
fmt.Printf(format, "owner.name", project.Owner.Username)
fmt.Printf(format, "public", strconv.FormatBool(project.Public))
fmt.Printf(format, "path", project.Path)
fmt.Printf(format, "path with namespace", project.PathWithNamespace)
fmt.Printf(format, "issues enabled", strconv.FormatBool(project.IssuesEnabled))
fmt.Printf(format, "merge requests enabled", strconv.FormatBool(project.MergeRequestsEnabled))
fmt.Printf(format, "wall enabled", strconv.FormatBool(project.WallEnabled))
fmt.Printf(format, "wiki enabled", strconv.FormatBool(project.WikiEnabled))
fmt.Printf(format, "created at", project.CreatedAtRaw)
fmt.Printf(format, "shared runners enabled", strconv.FormatBool(project.SharedRunners))
//fmt.Printf(format, "namespace", project.Namespace)

case "branches":
fmt.Println("Fetching project branches…")

Expand Down
27 changes: 27 additions & 0 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ func (g *Gitlab) Project(id string) (*Project, error) {
return project, err
}

/*
Update a specific project, identified by project ID or NAME,
which is owned by the authentication user.
Namespaced project may be retrieved by specifying the namespace
and its project name like this:

`namespace%2Fproject-name`

*/
func (g *Gitlab) UpdateProject(id string, project *Project) (*Project, error) {

url := g.ResourceUrl(project_url, map[string]string{":id": id})

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

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

return result, err
}

/*
Lists all branches of a project.
*/
Expand Down
11 changes: 11 additions & 0 deletions projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ func TestProject(t *testing.T) {
defer ts.Close()
}

func TestUpdateProject(t *testing.T) {
ts, gitlab := Stub("stubs/projects/show.json")
project := Project{
Description: "Project Description",
}

_, err := gitlab.UpdateProject("1", &project)
assert.Equal(t, err, nil)
defer ts.Close()
}

func TestProjectBranches(t *testing.T) {
ts, gitlab := Stub("stubs/projects/branches/index.json")
branches, err := gitlab.ProjectBranches("1")
Expand Down