From dc4b03d701bbf51880406787f73433d8868c9b6a Mon Sep 17 00:00:00 2001 From: Rob Warner Date: Thu, 6 Aug 2015 17:23:38 -0400 Subject: [PATCH 1/5] Add session so you can log in and get token --- session.go | 54 ++++++++++++++++++++++++++++++++++++++++++ session_test.go | 32 +++++++++++++++++++++++++ stubs/session/new.json | 20 ++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 session.go create mode 100644 session_test.go create mode 100644 stubs/session/new.json diff --git a/session.go b/session.go new file mode 100644 index 0000000..23bb3f1 --- /dev/null +++ b/session.go @@ -0,0 +1,54 @@ +package gogitlab + +import ( + "encoding/json" + "net/url" +) + +const ( + session_url = "/session" // Create a new session +) + +type Session struct { + Id int `json:"id,omitempty"` + Username string `json:"username,omitempty"` + Email string `json:"email,omitempty"` + Name string `json:"name,omitempty"` + PrivateToken string `json:"private_token,omitempty"` + Blocked bool `json:"blocked,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + Bio string `json:"bio,omitempty"` + Skype string `json:"skype,omitempty"` + LinkedIn string `json:"linkedin,omitempty"` + Twitter string `json:"twitter,omitempty"` + WebsiteUrl string `json:"website_url,omitempty"` + DarkScheme bool `json:"dark_scheme,omitempty"` + ThemeId int `json:"theme_id,omitempty"` + IsAdmin bool `json:"is_admin,omitempty"` + CanCreateGroup bool `json:"can_create_group,omitempty"` + CanCreateTeam bool `json:"can_create_team,omitempty"` + CanCreateProject bool `json:"can_create_project,omitempty"` +} + +func (g *Gitlab) NewSession(login string, email string, password string) (*Session, error) { + path := g.ResourceUrl(session_url, nil) + + v := url.Values{} + if login != "" { + v.Set("login", login) + } + if email != "" { + v.Set("email", email) + } + v.Set("password", password) + + body := v.Encode() + + var session *Session + contents, err := g.buildAndExecRequest("POST", path, []byte(body)) + if err == nil { + err = json.Unmarshal(contents, &session) + } + + return session, err +} diff --git a/session_test.go b/session_test.go new file mode 100644 index 0000000..4f9c69a --- /dev/null +++ b/session_test.go @@ -0,0 +1,32 @@ +package gogitlab + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewSession(t *testing.T) { + ts, gitlab := Stub("stubs/session/new.json") + session, err := gitlab.NewSession("john_smith", "", "pw") + + assert.Equal(t, err, nil) + assert.IsType(t, new(Session), session) + assert.Equal(t, session.Id, 1) + assert.Equal(t, session.Username, "john_smith") + assert.Equal(t, session.Name, "John Smith") + assert.Equal(t, session.PrivateToken, "dd34asd13as") + assert.Equal(t, session.Blocked, false) + assert.Equal(t, session.CreatedAt, "2012-05-23T08:00:58Z") + assert.Equal(t, session.Bio, "") + assert.Equal(t, session.Skype, "") + assert.Equal(t, session.LinkedIn, "") + assert.Equal(t, session.Twitter, "") + assert.Equal(t, session.ThemeId, 1) + assert.Equal(t, session.DarkScheme, false) + assert.Equal(t, session.IsAdmin, false) + assert.Equal(t, session.CanCreateGroup, true) + assert.Equal(t, session.CanCreateTeam, true) + assert.Equal(t, session.CanCreateProject, true) + defer ts.Close() +} diff --git a/stubs/session/new.json b/stubs/session/new.json new file mode 100644 index 0000000..39bc123 --- /dev/null +++ b/stubs/session/new.json @@ -0,0 +1,20 @@ +{ + "id": 1, + "username": "john_smith", + "email": "john@example.com", + "name": "John Smith", + "private_token": "dd34asd13as", + "blocked": false, + "created_at": "2012-05-23T08:00:58Z", + "bio": null, + "skype": "", + "linkedin": "", + "twitter": "", + "website_url": "", + "dark_scheme": false, + "theme_id": 1, + "is_admin": false, + "can_create_group": true, + "can_create_team": true, + "can_create_project": true +} From da2969fb8790f947ce81b96211a8995870ffc100 Mon Sep 17 00:00:00 2001 From: Rob Warner Date: Wed, 12 Aug 2015 08:30:35 -0400 Subject: [PATCH 2/5] Update README to include information on Session API --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7f2b2f7..30210c7 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,11 @@ go-gitlab-client is a simple client written in golang to consume gitlab API. * list project deploy keys * add/get/rm project deploy key +* + ###Session [gitlab api doc](http://api.gitlab.org/session.html) + * create session +Note: In most cases, OAuth2 is the preferred method of authentication, and you can find GitLab OAuth2 documentation [here](http://api.gitlab.org/oauth2.html). Access to the GitLab Session API is provided as a convenience when OAuth2 doesn't fit your use case. ##Installation @@ -69,4 +73,4 @@ Visit the docs at http://godoc.org/github.com/plouc/go-gitlab-client You can play with the examples located in the `examples` directory * [projects](https://github.com/plouc/go-gitlab-client/tree/master/examples/projects) -* [repositories](https://github.com/plouc/go-gitlab-client/tree/master/examples/repositories) \ No newline at end of file +* [repositories](https://github.com/plouc/go-gitlab-client/tree/master/examples/repositories) From 2ffc87c34c2df7c71fa9b49e9086388c75fbe696 Mon Sep 17 00:00:00 2001 From: Rob Warner Date: Wed, 12 Aug 2015 08:36:28 -0400 Subject: [PATCH 3/5] Fix README for Session --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 30210c7..6ff37d0 100644 --- a/README.md +++ b/README.md @@ -36,9 +36,11 @@ go-gitlab-client is a simple client written in golang to consume gitlab API. ###Session [gitlab api doc](http://api.gitlab.org/session.html) * create session -Note: In most cases, OAuth2 is the preferred method of authentication, and you can find GitLab OAuth2 documentation [here](http://api.gitlab.org/oauth2.html). Access to the GitLab Session API is provided as a convenience when OAuth2 doesn't fit your use case. + +Note: In most cases, OAuth2 is the preferred method of authentication, and you can find GitLab OAuth2 documentation [here](http://api.gitlab.org/oauth2.html). Access to the GitLab Session API is provided as a convenience when OAuth2 doesn't fit your use case. + ##Installation To install go-gitlab-client, use `go get`: From 05b8706abec30a2bf01a940ec48d5add2404920c Mon Sep 17 00:00:00 2001 From: Rob Warner Date: Wed, 12 Aug 2015 08:41:08 -0400 Subject: [PATCH 4/5] Fix README formatting for Session API --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 6ff37d0..8c6e3dd 100644 --- a/README.md +++ b/README.md @@ -32,13 +32,10 @@ go-gitlab-client is a simple client written in golang to consume gitlab API. * list project deploy keys * add/get/rm project deploy key -* +* ###Session [gitlab api doc](http://api.gitlab.org/session.html) * create session - - - Note: In most cases, OAuth2 is the preferred method of authentication, and you can find GitLab OAuth2 documentation [here](http://api.gitlab.org/oauth2.html). Access to the GitLab Session API is provided as a convenience when OAuth2 doesn't fit your use case. ##Installation From 3904f07061a616df2c4f2109035ee80005afbe16 Mon Sep 17 00:00:00 2001 From: Rob Warner Date: Wed, 12 Aug 2015 08:30:35 -0400 Subject: [PATCH 5/5] Update README to include information on Session API --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7f2b2f7..8c6e3dd 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,11 @@ go-gitlab-client is a simple client written in golang to consume gitlab API. * list project deploy keys * add/get/rm project deploy key +* + ###Session [gitlab api doc](http://api.gitlab.org/session.html) + * create session - +Note: In most cases, OAuth2 is the preferred method of authentication, and you can find GitLab OAuth2 documentation [here](http://api.gitlab.org/oauth2.html). Access to the GitLab Session API is provided as a convenience when OAuth2 doesn't fit your use case. ##Installation @@ -69,4 +72,4 @@ Visit the docs at http://godoc.org/github.com/plouc/go-gitlab-client You can play with the examples located in the `examples` directory * [projects](https://github.com/plouc/go-gitlab-client/tree/master/examples/projects) -* [repositories](https://github.com/plouc/go-gitlab-client/tree/master/examples/repositories) \ No newline at end of file +* [repositories](https://github.com/plouc/go-gitlab-client/tree/master/examples/repositories)