Skip to content

Commit

Permalink
Refine ParameterDefinition definition according upstream source code (#…
Browse files Browse the repository at this point in the history
…39)

Signed-off-by: John Niang <[email protected]>
  • Loading branch information
JohnNiang authored Nov 24, 2021
1 parent af6c56c commit 9597d81
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,14 @@ type ParameterDefinition struct {
Value string `json:"value"`
Filepath string `json:"file,omitempty"`
DefaultParameterValue *ParameterValue `json:"defaultParameterValue,omitempty"`

// Reference: https://github.com/jenkinsci/jenkins/blob/65b9f1cf51c3b3cf44ecb7d51d3f30d7dbe6b3bd/core/src/main/java/hudson/model/RunParameterDefinition.java#L103-L106
Choices []string `json:"choices,omitempty"`

// Reference: https://github.com/jenkinsci/jenkins/blob/65b9f1cf51c3b3cf44ecb7d51d3f30d7dbe6b3bd/core/src/main/java/hudson/model/RunParameterDefinition.java#L103-L106
ProjectName string `json:"projectName,omitempty"`
// Reference: https://github.com/jenkinsci/jenkins/blob/65b9f1cf51c3b3cf44ecb7d51d3f30d7dbe6b3bd/core/src/main/java/hudson/model/RunParameterDefinition.java#L116-L121
Filter string `json:"filter,omitempty"`
}

// ParameterValue represents the value for param
Expand Down
103 changes: 103 additions & 0 deletions pkg/job/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package job

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"testing"

"github.com/jenkins-zh/jenkins-client/pkg/core"
Expand Down Expand Up @@ -527,3 +529,104 @@ func TestParsePipelinePath(t *testing.T) {
})
}
}

func TestParameterDefinition(t *testing.T) {
type args struct {
parameterJSON string
}

tests := []struct {
name string
args args
want ParameterDefinition
}{{
name: "String parametere definition",
args: args{
// Test data from https://github.com/jenkinsci/blueocean-plugin/tree/master/blueocean-rest#parameterized-pipeline
parameterJSON: `
{
"_class" : "hudson.model.StringParameterDefinition",
"defaultParameterValue" : {
"_class" : "hudson.model.StringParameterValue",
"name" : "param1",
"value" : "xyz"
},
"description" : "string param",
"name" : "param1",
"type" : "StringParameterDefinition"
}`,
},
want: ParameterDefinition{
Name: "param1",
Type: "StringParameterDefinition",
Description: "string param",
DefaultParameterValue: &ParameterValue{
Name: "param1",
Value: "xyz",
},
},
}, {
name: "Choice parameter definition",
args: args{
parameterJSON: `
{
"defaultParameterValue": {
"name": "choice",
"value": "a"
},
"description": "choice description",
"name": "choice",
"type": "ChoiceParameterDefinition",
"choices": ["a", "b", "c", "d"]
}`,
},
want: ParameterDefinition{
Name: "choice",
Type: "ChoiceParameterDefinition",
Description: "choice description",
Choices: []string{"a", "b", "c", "d"},
DefaultParameterValue: &ParameterValue{
Name: "choice",
Value: "a",
},
},
}, {
name: "Run parameter definition",
args: args{
parameterJSON: `
{
"defaultParameterValue": {
"name": "rpd",
"value": true
},
"description": "desc",
"name": "rpd",
"projectName": "project",
"filter": "stable",
"type": "RunParameterDefinition"
}`,
},
want: ParameterDefinition{
Name: "rpd",
ProjectName: "project",
Filter: "stable",
Type: "RunParameterDefinition",
Description: "desc",
DefaultParameterValue: &ParameterValue{
Name: "rpd",
Value: true,
},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parameter := ParameterDefinition{}
if err := json.Unmarshal([]byte(tt.args.parameterJSON), &parameter); err != nil {
t.Fatal("faile to unmarshal JSON", tt.args.parameterJSON, err)
}
if !reflect.DeepEqual(parameter, tt.want) {
t.Errorf("parsePipelinePath() = \n%+v, want \n%+v", parameter, tt.want)
}
})
}
}

0 comments on commit 9597d81

Please sign in to comment.