add jenkins api

Signed-off-by: soulseen <sunzhu@yunify.com>
This commit is contained in:
sunzhu
2019-04-17 11:04:04 +08:00
committed by zryfish
parent addf11c38b
commit e64e8bb93b
10 changed files with 697 additions and 0 deletions

196
pkg/models/devops/devops.go Normal file
View File

@@ -0,0 +1,196 @@
/*
Copyright 2019 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package devops
import (
"compress/gzip"
"encoding/json"
"flag"
"fmt"
log "github.com/golang/glog"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
)
var JenkinsUrl string
func init() {
flag.StringVar(&JenkinsUrl, "jenkins-url", "http://ks-jenkins.kubesphere-devops-system.svc.cluster.local:80", "jenkins server host")
}
func GetPipeline(projectName, pipelineName string, req *http.Request) (*Pipeline, error) {
baseUrl := fmt.Sprintf(JenkinsUrl+GetPipelineUrl, projectName, pipelineName)
log.Infof("Jenkins-url: " + baseUrl)
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
return nil, err
}
var res = new(Pipeline)
err = json.Unmarshal(resBody, &res)
if err != nil {
log.Error(err)
return nil, err
}
return res, err
}
func SearchPipelines(req *http.Request) ([]interface{}, error) {
baseUrl := JenkinsUrl + SearchPipelineUrl + req.URL.RawQuery
log.Infof("Jenkins-url: " + baseUrl)
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
return nil, err
}
var res []interface{}
err = json.Unmarshal(resBody, &res)
if err != nil {
log.Error(err)
return nil, err
}
return res, err
}
func SearchPipelineRuns(projectName, pipelineName string, req *http.Request) ([]interface{}, error) {
baseUrl := fmt.Sprintf(JenkinsUrl+SearchPipelineRunUrl+req.URL.RawQuery, projectName, pipelineName)
log.Infof("Jenkins-url: " + baseUrl)
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
return nil, err
}
var res []interface{}
err = json.Unmarshal(resBody, &res)
if err != nil {
log.Error(err)
return nil, err
}
return res, err
}
func GetPipelineRun(projectName, pipelineName, branchName, runId string, req *http.Request) (*Pipeline, error) {
baseUrl := fmt.Sprintf(JenkinsUrl+GetPipelineRunUrl, projectName, pipelineName, branchName, runId)
log.Infof("Jenkins-url: " + baseUrl)
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
return nil, err
}
var res = new(Pipeline)
err = json.Unmarshal(resBody, &res)
if err != nil {
log.Error(err)
return nil, err
}
return res, err
}
func GetPipelineRunNodes(projectName, pipelineName, branchName, runId string, req *http.Request) ([]interface{}, error) {
baseUrl := fmt.Sprintf(JenkinsUrl+GetPipelineRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
log.Infof("Jenkins-url: " + baseUrl)
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
return nil, err
}
var res []interface{}
err = json.Unmarshal(resBody, &res)
if err != nil {
log.Error(err)
return nil, err
}
return res, err
}
func GetStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) {
baseUrl := fmt.Sprintf(JenkinsUrl+GetStepLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId)
log.Infof("Jenkins-url: " + baseUrl)
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
return nil, err
}
return resBody, err
}
// create jenkins request
func jenkinsClient(baseUrl string, req *http.Request) ([]byte, error) {
newReqUrl, err := url.Parse(baseUrl)
if err != nil {
log.Error(err)
return nil, err
}
client := &http.Client{Timeout: 30 * time.Second}
newRequest := &http.Request{
Method: req.Method,
URL: newReqUrl,
Header: req.Header,
Body: req.Body,
}
resp, err := client.Do(newRequest)
if err != nil {
log.Error(err)
return nil, err
}
defer resp.Body.Close()
resBody, _ := getRespBody(resp)
if resp.StatusCode >= http.StatusBadRequest {
jkerr := new(JkError)
_ = json.Unmarshal(resBody, jkerr)
log.Error(nil, jkerr)
return nil, jkerr
}
return resBody, err
}
// Decompress response.body of JenkinsAPIResponse
func getRespBody(resp *http.Response) ([]byte, error) {
var reader io.ReadCloser
if resp.Header.Get("Content-Encoding") == "gzip" {
reader, _ = gzip.NewReader(resp.Body)
} else {
reader = resp.Body
}
resBody, err := ioutil.ReadAll(reader)
if err != nil {
log.Error(err)
return nil, err
}
return resBody, err
}

View File

@@ -0,0 +1,34 @@
/*
Copyright 2019 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package devops
type JkError struct {
Message string `json:"message"`
Code int `json:"code"`
Errors []Errors `json:"errors"`
}
type Errors struct {
Message string `json:"message"`
Code string `json:"code"`
Field string `json:"field"`
}
func (err *JkError) Error() string {
return err.Message
}

164
pkg/models/devops/json.go Normal file
View File

@@ -0,0 +1,164 @@
/*
Copyright 2019 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package devops
type Pipeline struct {
Class string `json:"_class,omitempty"`
Links Links `json:"_links,omitempty"`
Actions []interface{} `json:"actions,omitempty"`
DisplayName string `json:"displayName,omitempty"`
FullDisplayName string `json:"fullDisplayName,omitempty"`
FullName string `json:"fullName,omitempty"`
Name interface{} `json:"name,omitempty"`
Organization string `json:"organization,omitempty"`
Parameters interface{} `json:"parameters,omitempty"`
Permissions Permissions `json:"permissions,omitempty"`
EstimatedDurationInMillis int `json:"estimatedDurationInMillis,omitempty"`
NumberOfFolders int `json:"numberOfFolders,omitempty"`
NumberOfPipelines int `json:"numberOfPipelines,omitempty"`
PipelineFolderNames []interface{} `json:"pipelineFolderNames,omitempty"`
WeatherScore int `json:"weatherScore,omitempty"`
BranchNames []string `json:"branchNames,omitempty"`
NumberOfFailingBranches int `json:"numberOfFailingBranches,omitempty"`
NumberOfFailingPullRequests int `json:"numberOfFailingPullRequests,omitempty"`
NumberOfSuccessfulBranches int `json:"numberOfSuccessfulBranches,omitempty"`
NumberOfSuccessfulPullRequests int `json:"numberOfSuccessfulPullRequests,omitempty"`
ScmSource ScmSource `json:"scmSource,omitempty"`
TotalNumberOfBranches int `json:"totalNumberOfBranches,omitempty"`
TotalNumberOfPullRequests int `json:"totalNumberOfPullRequests,omitempty"`
ArtifactsZipFile interface{} `json:"artifactsZipFile,omitempty"`
CauseOfBlockage interface{} `json:"causeOfBlockage,omitempty"`
Causes []Causes `json:"causes,omitempty"`
ChangeSet []interface{} `json:"changeSet,omitempty"`
Description interface{} `json:"description,omitempty"`
DurationInMillis int `json:"durationInMillis,omitempty"`
EnQueueTime string `json:"enQueueTime,omitempty"`
EndTime string `json:"endTime,omitempty"`
ID string `json:"id,omitempty"`
Pipeline string `json:"pipeline,omitempty"`
Replayable bool `json:"replayable,omitempty"`
Result string `json:"result,omitempty"`
RunSummary string `json:"runSummary,omitempty"`
StartTime string `json:"startTime,omitempty"`
State string `json:"state,omitempty"`
Type string `json:"type,omitempty"`
Branch Branch `json:"branch,omitempty"`
CommitID string `json:"commitId,omitempty"`
CommitURL interface{} `json:"commitUrl,omitempty"`
PullRequest interface{} `json:"pullRequest,omitempty"`
}
type Self struct {
Class string `json:"_class,omitempty"`
Href string `json:"href,omitempty"`
}
type Scm struct {
Class string `json:"_class,omitempty"`
Href string `json:"href,omitempty"`
}
type Branches struct {
Class string `json:"_class,omitempty"`
Href string `json:"href,omitempty"`
}
type Actions struct {
Class string `json:"_class,omitempty"`
Href string `json:"href,omitempty"`
}
type Runs struct {
Class string `json:"_class,omitempty"`
Href string `json:"href,omitempty"`
}
type Trends struct {
Class string `json:"_class,omitempty"`
Href string `json:"href,omitempty"`
}
type Queue struct {
Class string `json:"_class,omitempty"`
Href string `json:"href,omitempty"`
}
type Links struct {
Self Self `json:"self,omitempty"`
Scm Scm `json:"scm,omitempty"`
Branches Branches `json:"branches,omitempty"`
Actions Actions `json:"actions,omitempty"`
Runs Runs `json:"runs,omitempty"`
Trends Trends `json:"trends,omitempty"`
Queue Queue `json:"queue,omitempty"`
PrevRun PrevRun `json:"prevRun"`
Parent Parent `json:"parent"`
Tests Tests `json:"tests"`
Nodes Nodes `json:"nodes"`
Log Log `json:"log"`
BlueTestSummary BlueTestSummary `json:"blueTestSummary"`
Steps Steps `json:"steps"`
Artifacts Artifacts `json:"artifacts"`
}
type Permissions struct {
Create bool `json:"create,omitempty"`
Configure bool `json:"configure,omitempty"`
Read bool `json:"read,omitempty"`
Start bool `json:"start,omitempty"`
Stop bool `json:"stop,omitempty"`
}
type ScmSource struct {
Class string `json:"_class,omitempty"`
APIURL interface{} `json:"apiUrl,omitempty"`
ID string `json:"id,omitempty"`
}
type PrevRun struct {
Class string `json:"_class"`
Href string `json:"href"`
}
type Parent struct {
Class string `json:"_class"`
Href string `json:"href"`
}
type Tests struct {
Class string `json:"_class"`
Href string `json:"href"`
}
type Nodes struct {
Class string `json:"_class"`
Href string `json:"href"`
}
type Log struct {
Class string `json:"_class"`
Href string `json:"href"`
}
type BlueTestSummary struct {
Class string `json:"_class"`
Href string `json:"href"`
}
type Steps struct {
Class string `json:"_class"`
Href string `json:"href"`
}
type Artifacts struct {
Class string `json:"_class"`
Href string `json:"href"`
}
type Causes struct {
Class string `json:"_class"`
ShortDescription string `json:"shortDescription"`
UserID string `json:"userId,omitempty"`
UserName string `json:"userName,omitempty"`
}
type Branch struct {
IsPrimary bool `json:"isPrimary"`
Issues []interface{} `json:"issues"`
URL string `json:"url"`
}

View File

@@ -0,0 +1,28 @@
/*
Copyright 2019 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package devops
// Some apis for Jenkins.
const (
GetPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/"
SearchPipelineUrl = "/blue/rest/search/?"
SearchPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/?"
GetPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/"
GetPipelineRunNodesUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/?"
GetStepLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/%s/log/?"
)