Skip to content

Commit 53eb83b

Browse files
authored
Merge pull request #36 from sleclercq/feature-edit-project
feat(project) Edit project
2 parents f514f2a + 90314f3 commit 53eb83b

File tree

4 files changed

+88
-28
lines changed

4 files changed

+88
-28
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ go-gitlab-client is a simple client written in golang to consume gitlab API.
1111
*
1212
### Projects [gitlab api doc](http://doc.gitlab.com/ce/api/projects.html)
1313
* list projects
14-
* get single project
15-
* remove project
16-
14+
* add/get/edit/rm single project
1715
*
1816
### Repositories [gitlab api doc](http://doc.gitlab.com/ce/api/repositories.html)
1917
* list repository branches

examples/projects/main.go

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func main() {
3535
var method string
3636
flag.StringVar(&method, "m", "", "Specify method to retrieve projects infos, available methods:\n"+
3737
" > -m projects\n"+
38-
" > -m project -id PROJECT_ID\n"+
38+
" > -m project -id PROJECT_ID [-o <edit> -desc PROJECT_DESCRIPTION]\n"+
3939
" > -m hooks -id PROJECT_ID\n"+
4040
" > -m branches -id PROJECT_ID\n"+
4141
" > -m team -id PROJECT_ID\n"+
@@ -53,6 +53,12 @@ func main() {
5353
var sort string
5454
flag.StringVar(&sort, "sort", "", "Specify merge request sort")
5555

56+
var operation string
57+
flag.StringVar(&operation, "o", "", "Specify operation")
58+
59+
var desc string
60+
flag.StringVar(&desc, "desc", "", "Specify description")
61+
5662
flag.Usage = func() {
5763
fmt.Printf("Usage:\n")
5864
flag.PrintDefaults()
@@ -84,38 +90,56 @@ func main() {
8490
}
8591

8692
case "project":
87-
fmt.Println("Fetching project…")
8893

8994
if id == "" {
9095
flag.Usage()
9196
return
9297
}
9398

94-
project, err := gitlab.Project(id)
95-
if err != nil {
96-
fmt.Println(err.Error())
97-
return
99+
if operation == "" {
100+
fmt.Println("Fetching project…")
101+
102+
project, err := gitlab.Project(id)
103+
if err != nil {
104+
fmt.Println(err.Error())
105+
return
106+
}
107+
108+
format := "> %-23s: %s\n"
109+
110+
fmt.Printf("%s\n", project.Name)
111+
fmt.Printf(format, "id", strconv.Itoa(project.Id))
112+
fmt.Printf(format, "name", project.Name)
113+
fmt.Printf(format, "description", project.Description)
114+
fmt.Printf(format, "default branch", project.DefaultBranch)
115+
fmt.Printf(format, "owner.name", project.Owner.Username)
116+
fmt.Printf(format, "public", strconv.FormatBool(project.Public))
117+
fmt.Printf(format, "path", project.Path)
118+
fmt.Printf(format, "path with namespace", project.PathWithNamespace)
119+
fmt.Printf(format, "issues enabled", strconv.FormatBool(project.IssuesEnabled))
120+
fmt.Printf(format, "merge requests enabled", strconv.FormatBool(project.MergeRequestsEnabled))
121+
fmt.Printf(format, "wall enabled", strconv.FormatBool(project.WallEnabled))
122+
fmt.Printf(format, "wiki enabled", strconv.FormatBool(project.WikiEnabled))
123+
fmt.Printf(format, "shared runners enabled", strconv.FormatBool(project.SharedRunners))
124+
fmt.Printf(format, "created at", project.CreatedAtRaw)
125+
//fmt.Printf(format, "namespace", project.Namespace)
126+
}
127+
128+
switch operation {
129+
case "edit":
130+
fmt.Println("Edit project...")
131+
132+
project := gogitlab.Project{
133+
Description: desc,
134+
}
135+
136+
_, err := gitlab.UpdateProject(id, &project)
137+
if err != nil {
138+
fmt.Println(err.Error())
139+
return
140+
}
98141
}
99142

100-
format := "> %-23s: %s\n"
101-
102-
fmt.Printf("%s\n", project.Name)
103-
fmt.Printf(format, "id", strconv.Itoa(project.Id))
104-
fmt.Printf(format, "name", project.Name)
105-
fmt.Printf(format, "description", project.Description)
106-
fmt.Printf(format, "default branch", project.DefaultBranch)
107-
fmt.Printf(format, "owner.name", project.Owner.Username)
108-
fmt.Printf(format, "public", strconv.FormatBool(project.Public))
109-
fmt.Printf(format, "path", project.Path)
110-
fmt.Printf(format, "path with namespace", project.PathWithNamespace)
111-
fmt.Printf(format, "issues enabled", strconv.FormatBool(project.IssuesEnabled))
112-
fmt.Printf(format, "merge requests enabled", strconv.FormatBool(project.MergeRequestsEnabled))
113-
fmt.Printf(format, "wall enabled", strconv.FormatBool(project.WallEnabled))
114-
fmt.Printf(format, "wiki enabled", strconv.FormatBool(project.WikiEnabled))
115-
fmt.Printf(format, "created at", project.CreatedAtRaw)
116-
fmt.Printf(format, "shared runners enabled", strconv.FormatBool(project.SharedRunners))
117-
//fmt.Printf(format, "namespace", project.Namespace)
118-
119143
case "branches":
120144
fmt.Println("Fetching project branches…")
121145

projects.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,33 @@ func (g *Gitlab) Project(id string) (*Project, error) {
124124
return project, err
125125
}
126126

127+
/*
128+
Update a specific project, identified by project ID or NAME,
129+
which is owned by the authentication user.
130+
Namespaced project may be retrieved by specifying the namespace
131+
and its project name like this:
132+
133+
`namespace%2Fproject-name`
134+
135+
*/
136+
func (g *Gitlab) UpdateProject(id string, project *Project) (*Project, error) {
137+
138+
url := g.ResourceUrl(project_url, map[string]string{":id": id})
139+
140+
encodedRequest, err := json.Marshal(project)
141+
if err != nil {
142+
return nil, err
143+
}
144+
var result *Project
145+
146+
contents, err := g.buildAndExecRequest("PUT", url, encodedRequest)
147+
if err == nil {
148+
err = json.Unmarshal(contents, &result)
149+
}
150+
151+
return result, err
152+
}
153+
127154
/*
128155
Lists all branches of a project.
129156
*/

projects_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ func TestProject(t *testing.T) {
2626
defer ts.Close()
2727
}
2828

29+
func TestUpdateProject(t *testing.T) {
30+
ts, gitlab := Stub("stubs/projects/show.json")
31+
project := Project{
32+
Description: "Project Description",
33+
}
34+
35+
_, err := gitlab.UpdateProject("1", &project)
36+
assert.Equal(t, err, nil)
37+
defer ts.Close()
38+
}
39+
2940
func TestProjectBranches(t *testing.T) {
3041
ts, gitlab := Stub("stubs/projects/branches/index.json")
3142
branches, err := gitlab.ProjectBranches("1")

0 commit comments

Comments
 (0)