Merge branch 'master' into s2ibin-upload

Signed-off-by: runzexia <runzexia@yunify.com>

# Conflicts:
#	vendor/modules.txt
This commit is contained in:
runzexia
2019-09-02 15:21:38 +08:00
23 changed files with 1195 additions and 432 deletions

View File

@@ -135,6 +135,55 @@ func GetStepLog(projectName, pipelineName, runId, nodeId, stepId string, req *ht
}
func GetSCMServers(scmId string, req *http.Request) ([]byte, error) {
baseUrl := fmt.Sprintf(jenkins.Server+GetSCMServersUrl, scmId)
log.Info("Jenkins-url: " + baseUrl)
req.Method = http.MethodGet
resBody, err := sendJenkinsRequest(baseUrl, req)
if err != nil {
log.Error(err)
return nil, err
}
return resBody, err
}
func CreateSCMServers(scmId string, req *http.Request) ([]byte, error) {
requestBody, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Error(err)
return nil, err
}
createReq := &CreateScmServerReq{}
err = json.Unmarshal(requestBody, createReq)
if err != nil {
log.Error(err)
return nil, err
}
req.Body = nil
byteServers, err := GetSCMServers(scmId, req)
if err != nil {
log.Error(err)
return nil, err
}
var servers []*SCMServer
_ = json.Unmarshal(byteServers, &servers)
for _, server := range servers {
if server.ApiURL == createReq.ApiURL {
return json.Marshal(server)
}
}
req.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
baseUrl := fmt.Sprintf(jenkins.Server+CreateSCMServersUrl, scmId)
log.Info("Jenkins-url: " + baseUrl)
req.Method = http.MethodPost
resBody, err := sendJenkinsRequest(baseUrl, req)
if err != nil {
log.Error(err)
return nil, err
}
return resBody, err
}
func Validate(scmId string, req *http.Request) ([]byte, error) {
baseUrl := fmt.Sprintf(jenkins.Server+ValidateUrl, scmId)
log.Info("Jenkins-url: " + baseUrl)
@@ -731,7 +780,7 @@ func jenkinsClient(baseUrl string, req *http.Request) ([]byte, http.Header, erro
log.Errorf("%+v", string(resBody))
jkerr := new(JkError)
jkerr.Code = resp.StatusCode
jkerr.Message = http.StatusText(resp.StatusCode)
jkerr.Message = string(resBody)
return nil, nil, jkerr
}

View File

@@ -260,6 +260,19 @@ type SCMOrg struct {
Name string `json:"name,omitempty" description:"organization name"`
}
type SCMServer struct {
Class string `json:"_class,omitempty" description:"Its a fully qualified name and is an identifier of the producer of this resource's capability."`
Links struct {
Self struct {
Class string `json:"_class,omitempty" description:"Its a fully qualified name and is an identifier of the producer of this resource's capability."`
Href string `json:"href,omitempty" description:"self url in api"`
} `json:"self,omitempty" description:"scm server self info"`
} `json:"_links,omitempty" description:"references the reachable path to this resource"`
ID string `json:"id,omitempty" description:"server id of scm server"`
Name string `json:"name,omitempty" description:"name of scm server"`
ApiURL string `json:"apiUrl,omitempty" description:"url of scm server"`
}
// GetOrgRepo
type OrgRepo struct {
Class string `json:"_class,omitempty" description:"Its a fully qualified name and is an identifier of the producer of this resource's capability."`
@@ -721,6 +734,11 @@ type CheckPlayload struct {
Abort bool `json:"abort,omitempty" description:"abort or not"`
}
type CreateScmServerReq struct {
Name string `json:"name,omitempty" description:"name of scm server"`
ApiURL string `json:"apiUrl,omitempty" description:"url of scm server"`
}
type CheckPlayloadParameters struct {
Name string `json:"name,omitempty" description:"name"`
Value string `json:"value,omitempty" description:"value"`

File diff suppressed because it is too large Load Diff

View File

@@ -110,7 +110,7 @@ func Test_NoScmPipelineConfig_Param(t *testing.T) {
Name: "",
Description: "for test",
Jenkinsfile: "node{echo 'hello'}",
Parameters: []*Parameter{
Parameters: &Parameters{
&Parameter{
Name: "d",
DefaultValue: "a\nb",
@@ -123,7 +123,7 @@ func Test_NoScmPipelineConfig_Param(t *testing.T) {
Name: "",
Description: "for test",
Jenkinsfile: "node{echo 'hello'}",
Parameters: []*Parameter{
Parameters: &Parameters{
&Parameter{
Name: "a",
DefaultValue: "abc",
@@ -355,7 +355,28 @@ func Test_MultiBranchPipelineConfig_Source(t *testing.T) {
ApiUri: "https://api.github.com",
DiscoverBranches: 1,
DiscoverPRFromOrigin: 2,
DiscoverPRFromForks: &GithubDiscoverPRFromForks{
DiscoverPRFromForks: &DiscoverPRFromForks{
Strategy: 1,
Trust: 1,
},
},
},
&MultiBranchPipeline{
Name: "",
Description: "for test",
ScriptPath: "Jenkinsfile",
SourceType: "bitbucket_server",
TimerTrigger: &TimerTrigger{
Interval: "12345566",
},
BitbucketServerSource: &BitbucketServerSource{
Owner: "kubesphere",
Repo: "devops",
CredentialId: "github",
ApiUri: "https://api.github.com",
DiscoverBranches: 1,
DiscoverPRFromOrigin: 2,
DiscoverPRFromForks: &DiscoverPRFromForks{
Strategy: 1,
Trust: 1,
},
@@ -439,7 +460,7 @@ func Test_MultiBranchPipelineCloneConfig(t *testing.T) {
ApiUri: "https://api.github.com",
DiscoverBranches: 1,
DiscoverPRFromOrigin: 2,
DiscoverPRFromForks: &GithubDiscoverPRFromForks{
DiscoverPRFromForks: &DiscoverPRFromForks{
Strategy: 1,
Trust: 1,
},
@@ -496,7 +517,7 @@ func Test_MultiBranchPipelineRegexFilter(t *testing.T) {
ApiUri: "https://api.github.com",
DiscoverBranches: 1,
DiscoverPRFromOrigin: 2,
DiscoverPRFromForks: &GithubDiscoverPRFromForks{
DiscoverPRFromForks: &DiscoverPRFromForks{
Strategy: 1,
Trust: 1,
},

View File

@@ -44,6 +44,8 @@ const (
CheckPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/nodes/%s/steps/%s/"
GetBranchNodeStepsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/nodes/%s/steps/?"
GetNodeStepsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/nodes/%s/steps/?"
GetSCMServersUrl = "/blue/rest/organizations/jenkins/scm/%s/servers/"
CreateSCMServersUrl = "/blue/rest/organizations/jenkins/scm/%s/servers/"
ValidateUrl = "/blue/rest/organizations/jenkins/scm/%s/validate"
GetSCMOrgUrl = "/blue/rest/organizations/jenkins/scm/%s/organizations/?"
GetOrgRepoUrl = "/blue/rest/organizations/jenkins/scm/%s/organizations/%s/repositories/?"

View File

@@ -22,6 +22,7 @@ import (
"github.com/emicklei/go-restful"
"github.com/gocraft/dbr"
"github.com/golang/glog"
"k8s.io/klog"
"kubesphere.io/kubesphere/pkg/db"
"kubesphere.io/kubesphere/pkg/gojenkins"
"kubesphere.io/kubesphere/pkg/gojenkins/utils"
@@ -105,6 +106,34 @@ func ListDevopsProjects(workspace, username string, conditions *params.Condition
return &models.PageableResponse{Items: result, TotalCount: int(count)}, nil
}
func GetDevOpsProjectsCount(username string) (uint32, error) {
dbconn := devops_mysql.OpenDatabase()
query := dbconn.Select(devops.GetColumnsFromStructWithPrefix(devops.DevOpsProjectTableName, devops.DevOpsProject{})...).
From(devops.DevOpsProjectTableName)
var sqconditions []dbr.Builder
if username != devops.KS_ADMIN {
onCondition := fmt.Sprintf("%s = %s", devops.DevOpsProjectMembershipProjectIdColumn, devops.DevOpsProjectIdColumn)
query.Join(devops.DevOpsProjectMembershipTableName, onCondition)
sqconditions = append(sqconditions, db.Eq(devops.DevOpsProjectMembershipUsernameColumn, username))
sqconditions = append(sqconditions, db.Eq(
devops.DevOpsProjectMembershipTableName+"."+devops.StatusColumn, devops.StatusActive))
}
sqconditions = append(sqconditions, db.Eq(
devops.DevOpsProjectTableName+"."+devops.StatusColumn, devops.StatusActive))
if len(sqconditions) > 0 {
query.Where(db.And(sqconditions...))
}
count, err := query.Count()
if err != nil {
klog.Errorf("%+v", err)
return 0, restful.NewError(http.StatusInternalServerError, err.Error())
}
return count, nil
}
func DeleteDevOpsProject(projectId, username string) error {
err := devops.CheckProjectUserInRole(username, projectId, []string{devops.ProjectOwner})
if err != nil {