Skip to content

Commit

Permalink
Remove unused code lines (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored Aug 24, 2022
1 parent 9367e7a commit f45b22d
Show file tree
Hide file tree
Showing 21 changed files with 465 additions and 1,054 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,5 @@ replace (
github.com/kubesphere/sonargo => github.com/kubesphere/sonargo v0.0.2
golang.org/x/text => golang.org/x/text v0.3.7
)

replace github.com/jenkins-zh/jenkins-client => github.com/linuxsuren/jenkins-client v0.0.0-20220823083928-d2b2288b88d8
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,6 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i
github.com/jenkins-x/go-scm v1.11.19 h1:H4CzaM/C/0QcCVLDh603Q6Bv4hqU4G3De2yQntWubqg=
github.com/jenkins-x/go-scm v1.11.19/go.mod h1:eIcty4+tf6E7ycGOg0cUqnaLP+1LH1Z8zncQFQqRa3E=
github.com/jenkins-zh/jenkins-cli v0.0.32/go.mod h1:uE1mH9PNITrg0sugv6HXuM/CSddg0zxXoYu3w57I3JY=
github.com/jenkins-zh/jenkins-client v0.0.13 h1:MIBq36M9RYaJTVOE9sHmD/NOXzf4el9qZ4RRr/1/Lxw=
github.com/jenkins-zh/jenkins-client v0.0.13/go.mod h1:ICBk7OOoTafVP//f/VfKZ34c0ff8vJwVnOsF9btiMYU=
github.com/jenkins-zh/jenkins-client/pkg/k8s v0.0.0-20220811083317-bb6f87a3f866 h1:MQwSe0z1hju5X9c5BEfQAvkb4Fub1ZoHLo+ZLJ7mAyo=
github.com/jenkins-zh/jenkins-client/pkg/k8s v0.0.0-20220811083317-bb6f87a3f866/go.mod h1:zetE8JfNmxkot7OCNXJyS/QwxFnfcUFzrs/jg4U2gAw=
github.com/jenkins-zh/jenkins-formulas v0.0.5/go.mod h1:zS8fm8u5L6FcjZM0QznXsLV9T2UtSVK+hT6Sm76iUZ4=
Expand Down Expand Up @@ -448,6 +446,8 @@ github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec/go.mo
github.com/linuxsuren/http-downloader v0.0.6/go.mod h1:xxgh2OE7WGL9TwDE9L8Gh7Lqq9fFPuHbh5tofUitEfE=
github.com/linuxsuren/http-downloader v0.0.29 h1:GJUoybR4SViarPfX2xipq0unUssiuB9qrSSoDDW1jao=
github.com/linuxsuren/http-downloader v0.0.29/go.mod h1:bniEqpLyyydtCTDyjo3nco6LD2F90ruPH87fuqAznQg=
github.com/linuxsuren/jenkins-client v0.0.0-20220823083928-d2b2288b88d8 h1:9wEvfOXaL0Ip2+I0zUlUGvcUAjqJMueFZWzdRUqaPbs=
github.com/linuxsuren/jenkins-client v0.0.0-20220823083928-d2b2288b88d8/go.mod h1:T8M/g0p7jjx0NBwN6gNqsAAmT1P3B70GBBJJgsX9kLw=
github.com/linuxsuren/unstructured v0.0.1 h1:ilUA8MUYbR6l9ebo/YPV2bKqlf62bzQursDSE+j00iU=
github.com/linuxsuren/unstructured v0.0.1/go.mod h1:KH6aTj+FegzGBzc1vS6mzZx3/duhTUTEVyW5sO7p4as=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down
13 changes: 3 additions & 10 deletions pkg/client/devops/fake/fakedevops.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,6 @@ func (d *Devops) CheckScriptCompile(projectName, pipelineName string, httpParame
func (d *Devops) CheckCron(projectName string, httpParameters *devops.HttpParameters) (*devops.CheckCronRes, error) {
return nil, nil
}
func (d *Devops) ToJenkinsfile(httpParameters *devops.HttpParameters) (*devops.ResJenkinsfile, error) {
return nil, nil
}
func (d *Devops) ToJSON(httpParameters *devops.HttpParameters) (map[string]interface{}, error) {
return nil, nil
}

// CredentialOperator
func (d *Devops) CreateCredentialInProject(projectId string, credential *v1.Secret) (string, error) {
Expand Down Expand Up @@ -395,9 +389,6 @@ func (d *Devops) GetCredentialInProject(projectId, id string) (*devops.Credentia
}
return &devops.Credential{Id: id}, nil
}
func (d *Devops) GetCredentialsInProject(projectId string) ([]*devops.Credential, error) {
return nil, nil
}
func (d *Devops) DeleteCredentialInProject(projectId, id string) (string, error) {
if _, ok := d.Credentials[projectId][id]; !ok {
err := &devops.ErrorResponse{
Expand Down Expand Up @@ -450,7 +441,9 @@ func (d *Devops) CreateProjectPipeline(projectId string, pipeline *devopsv1alpha
err := fmt.Errorf("pipeline name [%s] has been used", pipeline.Name)
return "", restful.NewError(http.StatusConflict, err.Error())
}
d.Pipelines[projectId][pipeline.Name] = pipeline
if d.Pipelines[projectId] != nil {
d.Pipelines[projectId][pipeline.Name] = pipeline
}
return "", nil
}

Expand Down
251 changes: 251 additions & 0 deletions pkg/client/devops/fake/fakedevops_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
/*
Copyright 2022 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 fake

import (
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
devopsv1alpha3 "kubesphere.io/devops/pkg/api/devops/v1alpha3"
)

func TestCredential(t *testing.T) {
secret := &v1.Secret{}
secret.SetName("secret")
secret.SetNamespace("project")

secret1 := secret.DeepCopy()
secret1.SetName("secret1")

client := NewWithCredentials("project", secret.DeepCopy())

// find credential by a correct name
cre, err := client.GetCredentialInProject("project", "secret")
assert.NotNil(t, cre)
assert.Nil(t, err)

// not found
cre, err = client.GetCredentialInProject("fake", "fake")
assert.NotNil(t, err)
assert.Nil(t, cre)

// create a secert that already exist
var id string
id, err = client.CreateCredentialInProject("project", secret.DeepCopy())
assert.NotNil(t, err)
assert.Empty(t, id)

// delete an existing
id, err = client.DeleteCredentialInProject("project", "secret")
assert.Nil(t, err)
assert.Equal(t, "", id)

// delete a non-exsting
id, err = client.DeleteCredentialInProject("fake", "fake")
assert.NotNil(t, err)
assert.Empty(t, id)

// create it once it was deleted
id, err = client.CreateCredentialInProject("project", secret.DeepCopy())
assert.Nil(t, err)
assert.Equal(t, secret.DeepCopy().GetName(), id)

// update
id, err = client.UpdateCredentialInProject("project", secret.DeepCopy())
assert.Nil(t, err)
assert.Equal(t, secret.DeepCopy().GetName(), id)

// update a non-existing
id, err = client.UpdateCredentialInProject("project", secret1.DeepCopy())
assert.NotNil(t, err)
assert.Empty(t, id)
}

func TestPipeline(t *testing.T) {
pip := &devopsv1alpha3.Pipeline{}
pip.SetName("pip")
pip.SetNamespace("project")

pip1 := pip.DeepCopy()
pip1.SetName("pip1")

client := NewWithPipelines("project", pip.DeepCopy())

var id string
var err error

// create a non-exsiting project
id, err = client.CreateDevOpsProject("project1")
assert.Nil(t, err)
assert.Equal(t, "project1", id)

// create an exsiting project
id, err = client.CreateDevOpsProject("project")
assert.Nil(t, err)
assert.Equal(t, "project", id)

// delete a non-exsiting project
assert.NotNil(t, client.DeleteDevOpsProject("project2"))

// delete an exsiting project
assert.Nil(t, client.DeleteDevOpsProject("project1"))

// get an exsiting project
id, err = client.GetDevOpsProject("project")
assert.Nil(t, err)
assert.Equal(t, "project", id)

// get a non-existing project
id, err = client.GetDevOpsProject("project4")
assert.NotNil(t, err)
assert.Empty(t, id)

// create an existing pipeline
id, err = client.CreateProjectPipeline("project", pip.DeepCopy())
assert.NotNil(t, err)
assert.Empty(t, id)

// create a non-existing pipeline
id, err = client.CreateProjectPipeline("project", pip1.DeepCopy())
assert.Nil(t, err)
assert.Empty(t, id)

// delete a non-existing pipeline
id, err = client.DeleteProjectPipeline("project", "pip2")
assert.NotNil(t, err)
assert.Empty(t, id)

// delete an existing pipeline
id, err = client.DeleteProjectPipeline("project", "pip1")
assert.Nil(t, err)
assert.Empty(t, id)

// update an existing pipeline
id, err = client.UpdateProjectPipeline("project", pip.DeepCopy())
assert.Nil(t, err)
assert.Empty(t, id)

// update a non-existing pipeline
id, err = client.UpdateProjectPipeline("project", pip1.DeepCopy())
assert.NotNil(t, err)
assert.Empty(t, id)

// get an existing pipeline
var obj interface{}
obj, err = client.GetProjectPipelineConfig("project", "pip")
assert.Nil(t, err)
assert.NotNil(t, obj)

// get a non-existing pipeline
obj, err = client.GetProjectPipelineConfig("project", "pip3")
assert.NotNil(t, err)
assert.Nil(t, obj)
}

func TestNotImplement(t *testing.T) {
client := New("fake")
assert.NotNil(t, client)
client = NewFakeDevops(nil)
assert.Nil(t, client.ReloadConfiguration())
assert.Nil(t, client.ApplyNewSource("fake"))

var (
o1 interface{}
o2 interface{}
o3 interface{}
)

o1, o2 = client.GetProjectPipelineBuildByType("", "", "")
assertNils(t, o1, o2)
o1, o2 = client.GetMultiBranchPipelineBuildByType("", "", "", "")
assertNils(t, o1, o2)
o1, o2 = client.CheckCron("", nil)
assertNils(t, o1, o2)
o1, o2 = client.CheckScriptCompile("", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.GetPipeline("", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.ListPipelines(nil)
assertNils(t, o1, o2)
o1, o2 = client.GetPipelineRun("", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.StopPipeline("", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.ReplayPipeline("", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.GetArtifacts("", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.DownloadArtifact("", "", "", "")
assertNils(t, o1, o2)
o1, o2 = client.GetRunLog("", "", "", nil)
assertNils(t, o1, o2)
o1, o2, o3 = client.GetStepLog("", "", "", "", "", nil)
assertNils(t, o1, o2, o3)
o1, o2 = client.RunPipeline("", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.ListPipelineRuns("", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.SubmitInputStep("", "", "", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.GetBranchPipeline("", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.GetBranchPipelineRun("", "", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.StopBranchPipeline("", "", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.ReplayBranchPipeline("", "", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.RunBranchPipeline("", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.GetBranchArtifacts("", "", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.GetBranchRunLog("", "", "", "", nil)
assertNils(t, o1, o2)
o1, o2, o3 = client.GetBranchStepLog("", "", "", "", "", "", nil)
assertNils(t, o1, o2, o3)
o1, o2 = client.SubmitBranchInputStep("", "", "", "", "", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.GetPipelineBranch("", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.ScanBranch("", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.GetConsoleLog("", "", nil)
assertNils(t, o1, o2)
o1, o2 = client.GetCrumb(nil)
assertNils(t, o1, o2)
o1, o2 = client.GetSCMServers("", nil)
assertNils(t, o1, o2)
o1, o2 = client.GetSCMOrg("", nil)
assertNils(t, o1, o2)
o1, o2 = client.CreateSCMServers("", nil)
assertNils(t, o1, o2)
o1, o2 = client.Validate("", nil)
assertNils(t, o1, o2)
o1, o2 = client.GetNotifyCommit(nil)
assertNils(t, o1, o2)
o1, o2 = client.GithubWebhook(nil)
assertNils(t, o1, o2)
o1, o2 = client.GenericWebhook(nil)
assertNils(t, o1, o2)
}

func assertNils(t *testing.T, obj ...interface{}) {
for _, item := range obj {
assert.Nil(t, item)
}
}
25 changes: 20 additions & 5 deletions pkg/client/devops/jclient/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,28 @@ limitations under the License.
package jclient

import (
jcredential "github.com/jenkins-zh/jenkins-client/pkg/credential"
v1 "k8s.io/api/core/v1"
"kubesphere.io/devops/pkg/client/devops"
"kubesphere.io/devops/pkg/client/devops/util"
)

// CreateCredentialInProject creates a credential, then returns the ID
func (j *JenkinsClient) CreateCredentialInProject(projectID string, credential *v1.Secret) (string, error) {
return j.jenkins.CreateCredentialInProject(projectID, credential)
func (j *JenkinsClient) CreateCredentialInProject(projectID string, credential *v1.Secret) (id string, err error) {
client := j.getClient()

var cre interface{}
if cre, err = util.ConvertSecretToCredential(credential); err != nil {
return "", err
}
return "", client.CreateInFolder(projectID, cre)
}

// UpdateCredentialInProject updates a credential
func (j *JenkinsClient) UpdateCredentialInProject(projectID string, credential *v1.Secret) (string, error) {
return j.jenkins.UpdateCredentialInProject(projectID, credential)
func (j *JenkinsClient) UpdateCredentialInProject(projectID string, credential *v1.Secret) (id string, err error) {
client := j.getClient()
err = client.UpdateInFolder(projectID, credential.GetName(), credential)
return
}

// GetCredentialInProject returns a credential
Expand All @@ -38,5 +48,10 @@ func (j *JenkinsClient) GetCredentialInProject(projectID, id string) (*devops.Cr

// DeleteCredentialInProject deletes a credential
func (j *JenkinsClient) DeleteCredentialInProject(projectID, id string) (string, error) {
return j.jenkins.DeleteCredentialInProject(projectID, id)
client := j.getClient()
return id, client.DeleteInFolder(projectID, id)
}

func (j *JenkinsClient) getClient() *jcredential.CredentialsManager {
return &jcredential.CredentialsManager{JenkinsCore: j.Core}
}
Loading

0 comments on commit f45b22d

Please sign in to comment.