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

GetUser & GetAllUsers functionality. #313

Open
wants to merge 10 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
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module github.com/bndr/gojenkins

go 1.17
go 1.20

require (
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210924151903-3ad01bbaa167
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.12.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
github.com/stretchr/objx v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/net v0.0.0-20210924151903-3ad01bbaa167 h1:eDd+TJqbgfXruGQ5sJRU7tEtp/58OAx4+Ayjxg4SM+4=
golang.org/x/net v0.0.0-20210924151903-3ad01bbaa167/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand All @@ -16,3 +27,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14 changes: 8 additions & 6 deletions jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func (j *Jenkins) HasPlugin(ctx context.Context, name string) (*Plugin, error) {
return p.Contains(name), nil
}

//InstallPlugin with given version and name
// InstallPlugin with given version and name
func (j *Jenkins) InstallPlugin(ctx context.Context, name string, version string) error {
xml := fmt.Sprintf(`<jenkins><install plugin="%s@%s" /></jenkins>`, name, version)
resp, err := j.Requester.PostXML(ctx, "/pluginManager/installNecessaryPlugins", xml, j.Raw, map[string]string{})
Expand Down Expand Up @@ -555,11 +555,13 @@ func (j *Jenkins) GetAllViews(ctx context.Context) ([]*View, error) {
// First Parameter - name of the View
// Second parameter - Type
// Possible Types:
// gojenkins.LIST_VIEW
// gojenkins.NESTED_VIEW
// gojenkins.MY_VIEW
// gojenkins.DASHBOARD_VIEW
// gojenkins.PIPELINE_VIEW
//
// gojenkins.LIST_VIEW
// gojenkins.NESTED_VIEW
// gojenkins.MY_VIEW
// gojenkins.DASHBOARD_VIEW
// gojenkins.PIPELINE_VIEW
//
// Example: jenkins.CreateView("newView",gojenkins.LIST_VIEW)
func (j *Jenkins) CreateView(ctx context.Context, name string, viewType string) (*View, error) {
view := &View{Jenkins: j, Raw: new(ViewResponse), Base: "/view/" + name}
Expand Down
101 changes: 94 additions & 7 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,54 @@ const (
createUserContext = "/securityRealm/createAccountByAdmin"
)

// User is a Jenkins account
type User struct {
Jenkins *Jenkins
UserName string
FullName string
Email string
Raw *UserResponse
}

type Users struct {
Jenkins *Jenkins
UserName string
FullName string
Email string
ID string
Base string
Raw *UserResponse
}

type UserResponse struct {
Class string `json:"_class"`
AbsoluteURL string `json:"absoluteUrl"`
Description string `json:"description"`
FullName string `json:"fullName"`
ID string `json:"id"`
}

type AllUserResponse struct {
Class string `json:"_class"`
Users []struct {
LastChange int64 `json:"lastChange"`
Project struct {
Class string `json:"_class"`
Name string `json:"name"`
URL string `json:"url"`
} `json:"project"`
User struct {
AbsoluteURL string `json:"absoluteUrl"`
FullName string `json:"fullName"`
} `json:"user"`
} `json:"users"`
}

type AllUsers struct {
Jenkins *Jenkins
Base string
Raw *AllUserResponse
}

// ErrUser occurs when there is error creating or revoking Jenkins users
type ErrUser struct {
Message string
}
Expand All @@ -28,16 +67,19 @@ func (e *ErrUser) Error() string {
return e.Message
}

// CreateUser creates a new Jenkins account
// CreateUser creates a new Jenkins account.
func (j *Jenkins) CreateUser(ctx context.Context, userName, password, fullName, email string) (User, error) {
user := User{
// Set Jenkins client pointer to be able to delete user later
Jenkins: j,
UserName: userName,
FullName: fullName,
Email: email,
}
payload := "username=" + userName + "&password1=" + password + "&password2=" + password + "&fullname=" + fullName + "&email=" + email

// Create the payload string
payload := fmt.Sprintf("username=%s&password1=%s&password2=%s&fullname=%s&email=%s", userName, password, password, fullName, email)

// Send the POST request to create the user
response, err := j.Requester.Post(ctx, createUserContext, strings.NewReader(payload), nil, nil)
if err != nil {
return user, err
Expand All @@ -50,10 +92,12 @@ func (j *Jenkins) CreateUser(ctx context.Context, userName, password, fullName,
return user, nil
}

// DeleteUser deletes a Jenkins account
// DeleteUser deletes a Jenkins account.
func (j *Jenkins) DeleteUser(ctx context.Context, userName string) error {
deleteContext := "/securityRealm/user/" + userName + "/doDelete"
payload := "Submit=Yes"

// Send the POST request to delete the user
response, err := j.Requester.Post(ctx, deleteContext, strings.NewReader(payload), nil, nil)
if err != nil {
return err
Expand All @@ -66,7 +110,50 @@ func (j *Jenkins) DeleteUser(ctx context.Context, userName string) error {
return nil
}

// Delete deletes a Jenkins account
// Delete deletes a Jenkins account.
func (u *User) Delete() error {
return u.Jenkins.DeleteUser(context.Background(), u.UserName)
}

// GetUser retrieves information about a Jenkins user.
func (j *Jenkins) GetUser(ctx context.Context, userName string) (*Users, error) {
userInfo := Users{Jenkins: j, Raw: new(UserResponse), Base: "/user/" + userName}

// Poll for user information
_, err := userInfo.Poll(ctx)
if err != nil {
return nil, err
}
return &userInfo, nil
}

// GetAllUsers retrieves information about all Jenkins users.
// This operation may take a lot of time.
func (j *Jenkins) GetAllUsers(ctx context.Context) (*AllUsers, error) {
allUsers := AllUsers{Jenkins: j, Raw: new(AllUserResponse), Base: "/asynchPeople/"}

// Poll for all user information
_, err := allUsers.Poll(ctx)
if err != nil {
return nil, err
}
return &allUsers, nil
}

// Poll retrieves user information.
func (u *Users) Poll(ctx context.Context) (int, error) {
response, err := u.Jenkins.Requester.GetJSON(ctx, u.Base, u.Raw, nil)
if err != nil {
return 0, err
}
return response.StatusCode, nil
}

// Poll retrieves all user information.
func (u *AllUsers) Poll(ctx context.Context) (int, error) {
response, err := u.Jenkins.Requester.GetJSON(ctx, u.Base, u.Raw, nil)
if err != nil {
return 0, err
}
return response.StatusCode, nil
}