Fix CreateOrUpdateJob function
This commit is contained in:
parent
1de7a05278
commit
f3093cdbbc
|
|
@ -2,15 +2,17 @@ package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bndr/gojenkins"
|
"github.com/bndr/gojenkins"
|
||||||
"errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var errorNotFound = errors.New("404")
|
||||||
|
|
||||||
// Jenkins defines Jenkins API
|
// Jenkins defines Jenkins API
|
||||||
type Jenkins interface {
|
type Jenkins interface {
|
||||||
GenerateToken(userName, tokenName string) (*UserToken, error)
|
GenerateToken(userName, tokenName string) (*UserToken, error)
|
||||||
|
|
@ -30,7 +32,7 @@ type Jenkins interface {
|
||||||
GetLabel(name string) (*gojenkins.Label, error)
|
GetLabel(name string) (*gojenkins.Label, error)
|
||||||
GetBuild(jobName string, number int64) (*gojenkins.Build, error)
|
GetBuild(jobName string, number int64) (*gojenkins.Build, error)
|
||||||
GetJob(id string, parentIDs ...string) (*gojenkins.Job, error)
|
GetJob(id string, parentIDs ...string) (*gojenkins.Job, error)
|
||||||
GetSubJob(parentId string, childId string) (*gojenkins.Job, error)
|
GetSubJob(parentID string, childID string) (*gojenkins.Job, error)
|
||||||
GetFolder(id string, parents ...string) (*gojenkins.Folder, error)
|
GetFolder(id string, parents ...string) (*gojenkins.Folder, error)
|
||||||
GetAllNodes() ([]*gojenkins.Node, error)
|
GetAllNodes() ([]*gojenkins.Node, error)
|
||||||
GetAllBuildIds(job string) ([]gojenkins.JobBuild, error)
|
GetAllBuildIds(job string) ([]gojenkins.JobBuild, error)
|
||||||
|
|
@ -64,21 +66,22 @@ func (jenkins *jenkins) CreateOrUpdateJob(config string, options ...interface{})
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("error creating job, job name is missing")
|
return nil, errors.New("error creating job, job name is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
// create or update
|
// create or update
|
||||||
job, err := jenkins.GetJob(qr["name"])
|
job, err := jenkins.GetJob(qr["name"])
|
||||||
if jobNotExists(err) {
|
if isNotFoundError(err) {
|
||||||
_, err := jenkins.CreateJob(config, options)
|
return jenkins.CreateJob(config, options...)
|
||||||
return nil, err
|
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
err := job.UpdateConfig(config)
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = job.UpdateConfig(config)
|
||||||
return job, err
|
return job, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func jobNotExists(err error) bool {
|
func isNotFoundError(err error) bool {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error() == errors.New("404").Error()
|
return err.Error() == errorNotFound.Error()
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue