-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add client/job_parameter_definition and job_parameter_definition_choice * add provider/job_parameter_definition.go * add importer/resource_job_parameter_definition.go * fix gerrit trigger import index * refactor importer ids to have full tree propertyId_triggerId_projectId_branchId to avoid any conflicts
- Loading branch information
Showing
42 changed files
with
1,653 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package client | ||
|
||
type JobParameterDefinition interface { | ||
GetId() string | ||
SetId(string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package client | ||
|
||
import "encoding/xml" | ||
|
||
func init() { | ||
parametersDefinitionUnmarshalFunc["hudson.model.ChoiceParameterDefinition"] = unmarshalJobParameterDefinitionChoice | ||
} | ||
|
||
type JobParameterDefinitionChoice struct { | ||
Id string `xml:"id,attr,omitempty"` | ||
XMLName xml.Name `xml:"hudson.model.ChoiceParameterDefinition"` | ||
|
||
Name string `xml:"name"` | ||
Description string `xml:"description"` | ||
|
||
Choices *JobParameterDefinitionChoiceList `xml:"choices"` | ||
} | ||
|
||
func NewJobParameterDefinitionChoice() *JobParameterDefinitionChoice { | ||
return &JobParameterDefinitionChoice{ | ||
Choices: NewJobParameterDefinitionChoiceList(), | ||
} | ||
} | ||
|
||
func (d *JobParameterDefinitionChoice) GetId() string { | ||
return d.Id | ||
} | ||
|
||
func (d *JobParameterDefinitionChoice) SetId(id string) { | ||
d.Id = id | ||
} | ||
|
||
func unmarshalJobParameterDefinitionChoice(d *xml.Decoder, start xml.StartElement) (JobParameterDefinition, error) { | ||
definition := NewJobParameterDefinitionChoice() | ||
err := d.DecodeElement(definition, &start) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return definition, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package client | ||
|
||
import "encoding/xml" | ||
|
||
type JobParameterDefinitionChoiceList struct { | ||
XMLName xml.Name `xml:"choices"` | ||
Class string `xml:"class,attr,omitempty"` | ||
Items *JobParameterDefinitionChoiceListArray `xml:"a"` | ||
} | ||
|
||
func NewJobParameterDefinitionChoiceList() *JobParameterDefinitionChoiceList { | ||
return &JobParameterDefinitionChoiceList{ | ||
Class: "java.util.Arrays$ArrayList", | ||
Items: NewJobParameterDefinitionChoiceListArray(), | ||
} | ||
} | ||
|
||
func (choices *JobParameterDefinitionChoiceList) Append(choice string) *JobParameterDefinitionChoiceList { | ||
newChoices := NewJobParameterDefinitionChoiceList() | ||
if choices != nil { | ||
newChoices.Items = choices.Items.Append(choice) | ||
} else { | ||
newChoices.Items = NewJobParameterDefinitionChoiceListArray().Append(choice) | ||
} | ||
return newChoices | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package client | ||
|
||
import "encoding/xml" | ||
|
||
type JobParameterDefinitionChoiceListArray struct { | ||
XMLName xml.Name `xml:"a"` | ||
Class string `xml:"class,attr,omitempty"` | ||
Items *[]string `xml:"string"` | ||
} | ||
|
||
func NewJobParameterDefinitionChoiceListArray() *JobParameterDefinitionChoiceListArray { | ||
return &JobParameterDefinitionChoiceListArray{ | ||
Class: "string-array", | ||
Items: &[]string{}, | ||
} | ||
} | ||
|
||
func (choices *JobParameterDefinitionChoiceListArray) Append(choice string) *JobParameterDefinitionChoiceListArray { | ||
newChoices := NewJobParameterDefinitionChoiceListArray() | ||
if choices != nil { | ||
*newChoices.Items = append(*choices.Items, choice) | ||
} else { | ||
*newChoices.Items = []string{choice} | ||
} | ||
return newChoices | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/xml" | ||
"testing" | ||
|
||
"github.com/sergi/go-diff/diffmatchpatch" | ||
) | ||
|
||
var paramterDefinitionChoice *JobParameterDefinitionChoice | ||
|
||
func init() { | ||
paramterDefinitionChoice = NewJobParameterDefinitionChoice() | ||
paramterDefinitionChoice.Name = "ENV" | ||
paramterDefinitionChoice.Choices = paramterDefinitionChoice.Choices.Append("edge") | ||
paramterDefinitionChoice.Choices = paramterDefinitionChoice.Choices.Append("staging") | ||
paramterDefinitionChoice.Choices = paramterDefinitionChoice.Choices.Append("prod") | ||
} | ||
|
||
func TestJobParametersDefinitionPropertyDeserialize(t *testing.T) { | ||
var job Job | ||
err := xml.Unmarshal([]byte(expectedParameterDefinitionChoiceJson), &job) | ||
if err != nil { | ||
t.Fatalf("failed to deserialize xml %s", err) | ||
} | ||
if job.Properties == nil || len(*job.Properties.Items) == 0 { | ||
t.Fatalf("failed to deserialize properties") | ||
} | ||
property, ok := (*job.Properties.Items)[0].(*JobParametersDefinitionProperty) | ||
if !ok { | ||
t.Fatalf("failed to deserialize JobParametersDefinitionProperty") | ||
} | ||
if property.ParameterDefinitions == nil || property.ParameterDefinitions.Items == nil || len(*property.ParameterDefinitions.Items) == 0 { | ||
t.Fatalf("failed to deserialize property items") | ||
} | ||
resultChoiceProperty, ok := (*property.ParameterDefinitions.Items)[0].(*JobParameterDefinitionChoice) | ||
if !ok { | ||
t.Fatalf("failed to deserialize JobParameterDefinitionChoice") | ||
} | ||
if resultChoiceProperty.Name != paramterDefinitionChoice.Name { | ||
t.Fatalf("failed to deserialize JobParameterDefinitionChoice Name") | ||
} | ||
} | ||
|
||
func TestJobParametersDefinitionPropertySerialize(t *testing.T) { | ||
job := NewJob() | ||
jobParametersDefinitionProperty := NewJobParametersDefinitionProperty() | ||
jobParametersDefinitionProperty.ParameterDefinitions = | ||
jobParametersDefinitionProperty.ParameterDefinitions.Append(paramterDefinitionChoice) | ||
job.Properties = job.Properties.Append(jobParametersDefinitionProperty) | ||
|
||
config := JobConfigFromJob(job) | ||
resultBytes, err := xml.MarshalIndent(config, "", "\t") | ||
if err != nil { | ||
t.Fatalf("failed to serialize xml %s", err) | ||
} | ||
result := string(resultBytes) | ||
|
||
if result != expectedParameterDefinitionChoiceJson { | ||
dmp := diffmatchpatch.New() | ||
diffs := dmp.DiffMain(expectedParameterDefinitionChoiceJson, result, true) | ||
t.Fatalf("job definition not expected: %s", dmp.DiffPrettyText(diffs)) | ||
} | ||
} | ||
|
||
var expectedParameterDefinitionChoiceJson = `<flow-definition> | ||
<actions></actions> | ||
<description></description> | ||
<keepDependencies>false</keepDependencies> | ||
<properties> | ||
<hudson.model.ParametersDefinitionProperty> | ||
<parameterDefinitions> | ||
<hudson.model.ChoiceParameterDefinition> | ||
<name>ENV</name> | ||
<description></description> | ||
<choices class="java.util.Arrays$ArrayList"> | ||
<a class="string-array"> | ||
<string>edge</string> | ||
<string>staging</string> | ||
<string>prod</string> | ||
</a> | ||
</choices> | ||
</hudson.model.ChoiceParameterDefinition> | ||
</parameterDefinitions> | ||
</hudson.model.ParametersDefinitionProperty> | ||
</properties> | ||
<triggers></triggers> | ||
<disabled>false</disabled> | ||
</flow-definition>` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/xml" | ||
"errors" | ||
) | ||
|
||
func init() { | ||
propertyUnmarshalFunc["hudson.model.ParametersDefinitionProperty"] = unmarshalJobParametersDefinitionProperty | ||
} | ||
|
||
// ErrJobParameterDefinitionNotFound job parameter definition not found | ||
var ErrJobParameterDefinitionNotFound = errors.New("Could not find job parameter definition") | ||
|
||
type JobParametersDefinitionProperty struct { | ||
XMLName xml.Name `xml:"hudson.model.ParametersDefinitionProperty"` | ||
Id string `xml:"id,attr,omitempty"` | ||
Plugin string `xml:"plugin,attr,omitempty"` | ||
|
||
ParameterDefinitions *JobParameterDefinitions `xml:"parameterDefinitions"` | ||
} | ||
|
||
func NewJobParametersDefinitionProperty() *JobParametersDefinitionProperty { | ||
return &JobParametersDefinitionProperty{} | ||
} | ||
|
||
func (property *JobParametersDefinitionProperty) GetId() string { | ||
return property.Id | ||
} | ||
|
||
func (p *JobParametersDefinitionProperty) SetId(id string) { | ||
p.Id = id | ||
} | ||
|
||
func unmarshalJobParametersDefinitionProperty(d *xml.Decoder, start xml.StartElement) (JobProperty, error) { | ||
property := NewJobParametersDefinitionProperty() | ||
err := d.DecodeElement(property, &start) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return property, nil | ||
} | ||
|
||
func (property *JobParametersDefinitionProperty) GetParameterDefinition( | ||
definitionId string, | ||
) (JobParameterDefinition, error) { | ||
definitions := *(property.ParameterDefinitions).Items | ||
for _, definition := range definitions { | ||
if definition.GetId() == definitionId { | ||
return definition, nil | ||
} | ||
} | ||
return nil, ErrJobParameterDefinitionNotFound | ||
} | ||
|
||
func (property *JobParametersDefinitionProperty) UpdateParameterDefinition( | ||
newDefinition JobParameterDefinition, | ||
) error { | ||
definitionId := newDefinition.GetId() | ||
definitions := *(property.ParameterDefinitions).Items | ||
for i, definition := range definitions { | ||
if definition.GetId() == definitionId { | ||
definitions[i] = newDefinition | ||
return nil | ||
} | ||
} | ||
return ErrJobParameterDefinitionNotFound | ||
} | ||
|
||
func (property *JobParametersDefinitionProperty) DeleteParameterDefinition(definitionId string) error { | ||
definitions := *(property.ParameterDefinitions).Items | ||
for i, definition := range definitions { | ||
if definition.GetId() == definitionId { | ||
*property.ParameterDefinitions.Items = append(definitions[:i], definitions[i+1:]...) | ||
return nil | ||
} | ||
} | ||
return ErrJobParameterDefinitionNotFound | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package client | ||
|
||
import "encoding/xml" | ||
|
||
type parametersDefinitionUnmarshaler func(*xml.Decoder, xml.StartElement) (JobParameterDefinition, error) | ||
|
||
var parametersDefinitionUnmarshalFunc = map[string]parametersDefinitionUnmarshaler{} | ||
|
||
type JobParameterDefinitions struct { | ||
XMLName xml.Name `xml:"parameterDefinitions"` | ||
Items *[]JobParameterDefinition | ||
} | ||
|
||
func NewJobParameterDefinitions() *JobParameterDefinitions { | ||
return &JobParameterDefinitions{ | ||
Items: &[]JobParameterDefinition{}, | ||
} | ||
} | ||
|
||
func (defs *JobParameterDefinitions) Append(def JobParameterDefinition) *JobParameterDefinitions { | ||
newDefs := NewJobParameterDefinitions() | ||
if defs != nil { | ||
*newDefs.Items = append(*defs.Items, def) | ||
} else { | ||
*newDefs.Items = []JobParameterDefinition{def} | ||
} | ||
return newDefs | ||
} | ||
|
||
func (defs *JobParameterDefinitions) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { | ||
var tok xml.Token | ||
var err error | ||
*defs = *NewJobParameterDefinitions() | ||
for tok, err = d.Token(); err == nil; tok, err = d.Token() { | ||
if elem, ok := tok.(xml.StartElement); ok { | ||
if unmarshalXML, ok := parametersDefinitionUnmarshalFunc[elem.Name.Local]; ok { | ||
def, err := unmarshalXML(d, elem) | ||
if err != nil { | ||
return err | ||
} | ||
*defs.Items = append(*(defs).Items, def) | ||
} | ||
} | ||
if end, ok := tok.(xml.EndElement); ok { | ||
if end.Name.Local == "parameterDefinitions" { | ||
break | ||
} | ||
} | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.