Skip to content

Commit

Permalink
alias params (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelee2012 authored Dec 2, 2021
1 parent 978af68 commit b85fe57
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 40 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
"log"
"time"

"github.com/imroc/req"
"github.com/joelee2012/go-jenkins/jenkins"
"github.com/joelee2012/go-jenkins"
)

func main() {
Expand Down Expand Up @@ -48,7 +47,7 @@ func main() {
if err := j.CreateJob("workflowjob1", xml); err != nil {
log.Fatalln(err)
}
qitem, err := J.BuildJob("workflowjob1", req.Param{})
qitem, err := J.BuildJob("workflowjob1", jenkins.ReqParams{})
if err != nil {
t.Errorf("expect build job successful, but got error:\n %v", err)
}
Expand Down
9 changes: 2 additions & 7 deletions build.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package jenkins

import (
"github.com/imroc/req"
)

type Build struct {
Item
ID int
Expand All @@ -21,7 +17,7 @@ func NewBuild(url, class string, jenkins *Jenkins) *Build {
}

func (b *Build) GetConsoleText() (string, error) {
resp, err := b.Request("GET", "consoleText", req.Param{})
resp, err := b.Request("GET", "consoleText", ReqParams{})
if err != nil {
return "", err
}
Expand All @@ -30,7 +26,6 @@ func (b *Build) GetConsoleText() (string, error) {

func (b *Build) IsBuilding() (bool, error) {
var status BuildShortJson
err := b.BindAPIJson(req.Param{"tree": "building"}, &status)
err := b.BindAPIJson(ReqParams{"tree": "building"}, &status)
return status.Building, err
}

4 changes: 2 additions & 2 deletions credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Credentials struct {

func (cs *Credentials) Get(name string) (*Credential, error) {
var credsJson CredentialsJson
if err := cs.BindAPIJson(req.Param{"depth": "1"}, &credsJson); err != nil {
if err := cs.BindAPIJson(ReqParams{"depth": "1"}, &credsJson); err != nil {
return nil, err
}
for _, cred := range credsJson.Credentials {
Expand All @@ -26,7 +26,7 @@ func (cs *Credentials) Create(xml string) error {

func (cs *Credentials) List() ([]*Credential, error) {
var credsJson CredentialsJson
if err := cs.BindAPIJson(req.Param{"depth": "1"}, &credsJson); err != nil {
if err := cs.BindAPIJson(ReqParams{"depth": "1"}, &credsJson); err != nil {
return nil, err
}
var creds []*Credential
Expand Down
4 changes: 3 additions & 1 deletion item.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ type Item struct {
jenkins *Jenkins
}

type ReqParams = req.Param

func NewItem(url, class string, jenkins *Jenkins) *Item {
url = appendSlash(url)
return &Item{URL: url, Class: parseClass(class), jenkins: jenkins}
}
func (i *Item) BindAPIJson(param req.Param, v interface{}) error {
func (i *Item) BindAPIJson(param ReqParams, v interface{}) error {
return doBindAPIJson(i, param, v)
}

Expand Down
2 changes: 1 addition & 1 deletion jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (j *Jenkins) GetVersion() (string, error) {
return resp.Response().Header.Get("X-Jenkins"), nil
}

func (j *Jenkins) BuildJob(fullName string, params req.Param) (*QueueItem, error) {
func (j *Jenkins) BuildJob(fullName string, params ReqParams) (*QueueItem, error) {
job, err := j.GetJob(fullName)
if err != nil {
return nil, err
Expand Down
6 changes: 1 addition & 5 deletions jenkins_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package jenkins

import (
"fmt"
"log"
"os"
"strings"
"testing"
"time"

"github.com/imroc/req"
)

var J *Jenkins
Expand Down Expand Up @@ -169,7 +166,7 @@ func TestBuildJob(t *testing.T) {
t.Errorf("expect create job successful, but got error:\n %v", err)
}
defer J.DeleteJob("go-test1")
qitem, err := J.BuildJob("go-test1", req.Param{})
qitem, err := J.BuildJob("go-test1", ReqParams{})
if err != nil {
t.Errorf("expect build job successful, but got error:\n %v", err)
}
Expand All @@ -185,7 +182,6 @@ func TestBuildJob(t *testing.T) {
}
}
// waiting build to finish
fmt.Println(build)
for {
building, err := build.IsBuilding()
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewJob(url, class string, jenkins *Jenkins) *Job {
}

func (j *Job) Rename(name string) error {
resp, err := j.Request("POST", "confirmRename", req.Param{"newName": name})
resp, err := j.Request("POST", "confirmRename", ReqParams{"newName": name})
if err != nil {
return err
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func (j *Job) IsBuildable() (bool, error) {
Class string `json:"_class"`
Buildable bool `json:"buildable"`
}
err := j.BindAPIJson(req.Param{"tree": "buildable"}, &apiJson)
err := j.BindAPIJson(ReqParams{"tree": "buildable"}, &apiJson)
return apiJson.Buildable, err
}

Expand All @@ -90,7 +90,7 @@ func (j *Job) SetDescription(description string) error {
return doSetDescription(j, description)
}

func (j *Job) Build(param req.Param) (*QueueItem, error) {
func (j *Job) Build(param ReqParams) (*QueueItem, error) {
buildable, err := j.IsBuildable()
if err != nil {
return nil, err
Expand Down Expand Up @@ -131,7 +131,7 @@ type JobShortJson struct {

func (j *Job) GetBuild(number int) (*Build, error) {
var jobJson JobShortJson
err := j.BindAPIJson(req.Param{"tree": "builds[number,url]"}, &jobJson)
err := j.BindAPIJson(ReqParams{"tree": "builds[number,url]"}, &jobJson)
if err != nil {
return nil, err
}
Expand All @@ -146,7 +146,7 @@ func (j *Job) GetBuild(number int) (*Build, error) {

func (j *Job) Get(name string) (*Job, error) {
var folderJson JobShortJson
if err := j.BindAPIJson(req.Param{"tree": "jobs[url,name]"}, &folderJson); err != nil {
if err := j.BindAPIJson(ReqParams{"tree": "jobs[url,name]"}, &folderJson); err != nil {
return nil, err
}
for _, job := range folderJson.Jobs {
Expand All @@ -158,7 +158,7 @@ func (j *Job) Get(name string) (*Job, error) {
}

func (j *Job) Create(name, xml string) error {
_, err := j.Request("POST", "createItem", req.Param{"name": name}, req.BodyXML(xml))
_, err := j.Request("POST", "createItem", ReqParams{"name": name}, req.BodyXML(xml))
return err
}

Expand All @@ -172,7 +172,7 @@ func (j *Job) List(depth int) ([]*Job, error) {
query = fmt.Sprintf(qf, query)
}
var folderJson JobShortJson
if err := j.BindAPIJson(req.Param{"tree": query}, &folderJson); err != nil {
if err := j.BindAPIJson(ReqParams{"tree": query}, &folderJson); err != nil {
return nil, err
}
var jobs []*Job
Expand Down
4 changes: 2 additions & 2 deletions mix.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ func doSetDescription(r Requester, description string) error {

func doGetDescription(r Requester) (string, error) {
data := make(map[string]string)
if err := doBindAPIJson(r, req.Param{"tree": "description"}, data); err != nil {
if err := doBindAPIJson(r, ReqParams{"tree": "description"}, data); err != nil {
return "", err
}
return data["description"], nil
}

func doBindAPIJson(r Requester, param req.Param, v interface{}) error {
func doBindAPIJson(r Requester, param ReqParams, v interface{}) error {
resp, err := r.Request("GET", "api/json", param)
if err != nil {
return err
Expand Down
6 changes: 1 addition & 5 deletions node.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package jenkins

import (
"github.com/imroc/req"
)

type ComputerSet struct {
Item
}
Expand All @@ -15,7 +11,7 @@ func NewComputerSet(url string, jenkins *Jenkins) *ComputerSet {
func (cs *ComputerSet) GetBuilds() ([]*Build, error) {
var csJson ComputerSetJson
var builds []*Build
if err := cs.BindAPIJson(req.Param{"tree": "computer[oneOffExecutors[currentExecutable[url]]]"}, &csJson); err != nil {
if err := cs.BindAPIJson(ReqParams{"tree": "computer[oneOffExecutors[currentExecutable[url]]]"}, &csJson); err != nil {
return builds, err
}
for _, c := range csJson.Computers {
Expand Down
9 changes: 2 additions & 7 deletions queue.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package jenkins

import (
"github.com/imroc/req"
)

type QueueItem struct {
Item
ID int
Expand All @@ -23,7 +19,7 @@ func (q *QueueItem) GetBuild() (*Build, error) {
return q.build, nil
}
var queueJson QueueItemJson
if err := q.BindAPIJson(req.Param{}, &queueJson); err != nil {
if err := q.BindAPIJson(ReqParams{}, &queueJson); err != nil {
return nil, err
}
var err error
Expand All @@ -47,7 +43,7 @@ func (q *QueueItem) getWaitingBuild() (*Build, error) {
QueueId int `json:"queueId"`
}
for _, build := range builds {
if err := build.BindAPIJson(req.Param{"tree": "queueId"}, &buildJson); err != nil {
if err := build.BindAPIJson(ReqParams{"tree": "queueId"}, &buildJson); err != nil {
return nil, err
}
if buildJson.QueueId == q.ID {
Expand All @@ -56,4 +52,3 @@ func (q *QueueItem) getWaitingBuild() (*Build, error) {
}
return nil, nil
}

0 comments on commit b85fe57

Please sign in to comment.