Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-shell
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-shell
Commits
a36cf8de
Commit
a36cf8de
authored
May 20, 2019
by
Igor Drozdov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce gitlabnet.ParseJSON to DRY
parent
60280bbf
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
60 deletions
+48
-60
go/internal/gitlabnet/client.go
go/internal/gitlabnet/client.go
+12
-0
go/internal/gitlabnet/discover/client.go
go/internal/gitlabnet/discover/client.go
+15
-33
go/internal/gitlabnet/discover/client_test.go
go/internal/gitlabnet/discover/client_test.go
+13
-4
go/internal/gitlabnet/twofactorrecover/client.go
go/internal/gitlabnet/twofactorrecover/client.go
+8
-23
No files found.
go/internal/gitlabnet/client.go
View file @
a36cf8de
...
...
@@ -17,6 +17,10 @@ const (
secretHeaderName
=
"Gitlab-Shared-Secret"
)
var
(
ParsingError
=
fmt
.
Errorf
(
"Parsing failed"
)
)
type
ErrorResponse
struct
{
Message
string
`json:"message"`
}
...
...
@@ -120,3 +124,11 @@ func (c *GitlabClient) doRequest(method, path string, data interface{}) (*http.R
return
response
,
nil
}
func
ParseJSON
(
hr
*
http
.
Response
,
response
interface
{})
error
{
if
err
:=
json
.
NewDecoder
(
hr
.
Body
)
.
Decode
(
response
);
err
!=
nil
{
return
ParsingError
}
return
nil
}
go/internal/gitlabnet/discover/client.go
View file @
a36cf8de
package
discover
import
(
"encoding/json"
"fmt"
"net/http"
"net/url"
...
...
@@ -32,56 +31,39 @@ func NewClient(config *config.Config) (*Client, error) {
}
func
(
c
*
Client
)
GetByCommandArgs
(
args
*
commandargs
.
CommandArgs
)
(
*
Response
,
error
)
{
if
args
.
GitlabKeyId
!=
""
{
return
c
.
GetByKeyId
(
args
.
GitlabKeyId
)
}
else
if
args
.
GitlabUsername
!=
""
{
return
c
.
GetByUsername
(
args
.
GitlabUsername
)
params
:=
url
.
Values
{}
if
args
.
GitlabUsername
!=
""
{
params
.
Add
(
"username"
,
args
.
GitlabUsername
)
}
else
if
args
.
GitlabKeyId
!=
""
{
params
.
Add
(
"key_id"
,
args
.
GitlabKeyId
)
}
else
{
// There was no 'who' information, this matches the ruby error
// message.
return
nil
,
fmt
.
Errorf
(
"who='' is invalid"
)
}
}
func
(
c
*
Client
)
GetByKeyId
(
keyId
string
)
(
*
Response
,
error
)
{
params
:=
url
.
Values
{}
params
.
Add
(
"key_id"
,
keyId
)
return
c
.
getResponse
(
params
)
}
func
(
c
*
Client
)
GetByUsername
(
username
string
)
(
*
Response
,
error
)
{
params
:=
url
.
Values
{}
params
.
Add
(
"username"
,
username
)
return
c
.
getResponse
(
params
)
}
func
(
c
*
Client
)
parseResponse
(
resp
*
http
.
Response
)
(
*
Response
,
error
)
{
parsedResponse
:=
&
Response
{}
if
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
parsedResponse
);
err
!=
nil
{
return
nil
,
err
}
else
{
return
parsedResponse
,
nil
}
}
func
(
c
*
Client
)
getResponse
(
params
url
.
Values
)
(
*
Response
,
error
)
{
path
:=
"/discover?"
+
params
.
Encode
()
response
,
err
:=
c
.
client
.
Get
(
path
)
response
,
err
:=
c
.
client
.
Get
(
path
)
if
err
!=
nil
{
return
nil
,
err
}
defer
response
.
Body
.
Close
()
parsedResponse
,
err
:=
c
.
parseResponse
(
response
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Parsing failed"
)
return
parse
(
response
)
}
func
parse
(
hr
*
http
.
Response
)
(
*
Response
,
error
)
{
response
:=
&
Response
{}
if
err
:=
gitlabnet
.
ParseJSON
(
hr
,
response
);
err
!=
nil
{
return
nil
,
err
}
return
parsedR
esponse
,
nil
return
r
esponse
,
nil
}
func
(
r
*
Response
)
IsAnonymous
()
bool
{
...
...
go/internal/gitlabnet/discover/client_test.go
View file @
a36cf8de
...
...
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"testing"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
...
...
@@ -59,7 +60,9 @@ func TestGetByKeyId(t *testing.T) {
client
,
cleanup
:=
setup
(
t
)
defer
cleanup
()
result
,
err
:=
client
.
GetByKeyId
(
"1"
)
params
:=
url
.
Values
{}
params
.
Add
(
"key_id"
,
"1"
)
result
,
err
:=
client
.
getResponse
(
params
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
&
Response
{
UserId
:
2
,
Username
:
"alex-doe"
,
Name
:
"Alex Doe"
},
result
)
}
...
...
@@ -68,7 +71,9 @@ func TestGetByUsername(t *testing.T) {
client
,
cleanup
:=
setup
(
t
)
defer
cleanup
()
result
,
err
:=
client
.
GetByUsername
(
"jane-doe"
)
params
:=
url
.
Values
{}
params
.
Add
(
"username"
,
"jane-doe"
)
result
,
err
:=
client
.
getResponse
(
params
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
&
Response
{
UserId
:
1
,
Username
:
"jane-doe"
,
Name
:
"Jane Doe"
},
result
)
}
...
...
@@ -77,7 +82,9 @@ func TestMissingUser(t *testing.T) {
client
,
cleanup
:=
setup
(
t
)
defer
cleanup
()
result
,
err
:=
client
.
GetByUsername
(
"missing"
)
params
:=
url
.
Values
{}
params
.
Add
(
"username"
,
"missing"
)
result
,
err
:=
client
.
getResponse
(
params
)
assert
.
NoError
(
t
,
err
)
assert
.
True
(
t
,
result
.
IsAnonymous
())
}
...
...
@@ -110,7 +117,9 @@ func TestErrorResponses(t *testing.T) {
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
tc
.
desc
,
func
(
t
*
testing
.
T
)
{
resp
,
err
:=
client
.
GetByUsername
(
tc
.
fakeUsername
)
params
:=
url
.
Values
{}
params
.
Add
(
"username"
,
tc
.
fakeUsername
)
resp
,
err
:=
client
.
getResponse
(
params
)
assert
.
EqualError
(
t
,
err
,
tc
.
expectedError
)
assert
.
Nil
(
t
,
resp
)
...
...
go/internal/gitlabnet/twofactorrecover/client.go
View file @
a36cf8de
package
twofactorrecover
import
(
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/commandargs"
...
...
@@ -46,38 +44,25 @@ func (c *Client) GetRecoveryCodes(args *commandargs.CommandArgs) ([]string, erro
}
response
,
err
:=
c
.
client
.
Post
(
"/two_factor_recovery_codes"
,
requestBody
)
if
err
!=
nil
{
return
nil
,
err
}
defer
response
.
Body
.
Close
()
parsedResponse
,
err
:=
c
.
parseResponse
(
response
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Parsing failed"
)
}
if
parsedResponse
.
Success
{
return
parsedResponse
.
RecoveryCodes
,
nil
}
else
{
return
nil
,
errors
.
New
(
parsedResponse
.
Message
)
}
return
parse
(
response
)
}
func
(
c
*
Client
)
parseResponse
(
resp
*
http
.
Response
)
(
*
Response
,
error
)
{
parsedResponse
:=
&
Response
{}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
func
parse
(
hr
*
http
.
Response
)
([]
string
,
error
)
{
response
:=
&
Response
{}
if
err
:=
gitlabnet
.
ParseJSON
(
hr
,
response
);
err
!=
nil
{
return
nil
,
err
}
if
err
:=
json
.
Unmarshal
(
body
,
parsedResponse
);
err
!=
nil
{
return
nil
,
err
}
else
{
return
parsedResponse
,
nil
if
!
response
.
Success
{
return
nil
,
errors
.
New
(
response
.
Message
)
}
return
response
.
RecoveryCodes
,
nil
}
func
(
c
*
Client
)
getRequestBody
(
args
*
commandargs
.
CommandArgs
)
(
*
RequestBody
,
error
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment