Skip to content

Commit

Permalink
add more types
Browse files Browse the repository at this point in the history
  • Loading branch information
jgramoll committed Jul 16, 2019
1 parent a5d34f1 commit cf1a857
Show file tree
Hide file tree
Showing 74 changed files with 2,612 additions and 309 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ resource "jenkins_job_git_scm_user_remote_config" "premerge" {
}
resource "jenkins_job_git_scm_branch" "premerge" {
job = "${jenkins_job.premerge.id}"
scm = "${jenkins_job_git_scm.premerge.id}"
name = "FETCH_HEAD"
}
resource "jenkins_job_git_scm_clean_before_checkout_extention" "premerge" {
job = "${jenkins_job.premerge.id}"
resource "jenkins_job_git_scm_clean_before_checkout_extension" "premerge" {
scm = "${jenkins_job_git_scm.premerge.id}"
}
resource "jenkins_job_build_discard_property" "main" {
Expand Down Expand Up @@ -127,3 +127,10 @@ resource "jenkins_job_gerrit_branch" "main" {
}
```

## TODO

1. rename job
1. import resources
1. use job name as part of id + guid
1. fix reads to set state from remote
9 changes: 0 additions & 9 deletions client/branch.go

This file was deleted.

20 changes: 20 additions & 0 deletions client/compare_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package client

import (
"encoding/xml"
)

type CompareType int

const (
CompareTypePlain CompareType = iota
CompareTypeRegExp
)

func (t CompareType) String() string {
return [...]string{"PLAIN", "REG_EXP"}[t]
}

func (t CompareType) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(t.String(), start)
}
6 changes: 0 additions & 6 deletions client/cps_scm_flow_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,3 @@ func NewCpsScmFlowDefinition() *CpsScmFlowDefinition {
Class: "org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition",
}
}

// func (definition *CpsScmFlowDefinition) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// start.Name.Local = "asdf"
// e.
// return nil
// }
31 changes: 31 additions & 0 deletions client/git_scm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package client

import "encoding/xml"

type GitSCM struct {
XMLName xml.Name `xml:"scm"`
Class string `xml:"class,attr"`
ConfigVersion string `xml:"configVersion"`
UserRemoteConfigs *GitUserRemoteConfigs `xml:"userRemoteConfigs"`
Branches *GitScmBranches `xml:"branches"`

DoGenerateSubmoduleConfigurations bool `xml:"doGenerateSubmoduleConfigurations"`
// submoduleCfg
// extensions
}

func NewGitScm() *GitSCM {
return &GitSCM{
Class: "hudson.plugins.git.GitSCM",
Branches: NewGitScmBranches(),
UserRemoteConfigs: NewGitUserRemoteConfigs(),
}
}

func (scm *GitSCM) AppendUserRemoteConfig(config *GitUserRemoteConfig) *GitUserRemoteConfigs {
return scm.UserRemoteConfigs.Append(config)
}

func (scm *GitSCM) AppendBranch(branch *GitScmBranchSpec) *GitScmBranches {
return scm.Branches.Append(branch)
}
10 changes: 10 additions & 0 deletions client/git_scm_branch_spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package client

type GitScmBranchSpec struct {
Id string `xml:"id,attr"`
Name string `xml:"name"`
}

func NewGitScmBranchSpec() *GitScmBranchSpec {
return &GitScmBranchSpec{}
}
28 changes: 28 additions & 0 deletions client/git_scm_branches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package client

import (
"encoding/xml"
)

type GitScmBranches struct {
XMLName xml.Name `xml:"branches"`
Items *[]*GitScmBranchSpec `xml:"hudson.plugins.git.BranchSpec"`
}

func NewGitScmBranches() *GitScmBranches {
return &GitScmBranches{
Items: &[]*GitScmBranchSpec{},
}
}

func (branches *GitScmBranches) Append(branch *GitScmBranchSpec) *GitScmBranches {
var newBranchItems []*GitScmBranchSpec
if branches.Items != nil {
newBranchItems = append(*branches.Items, branch)
} else {
newBranchItems = append(newBranchItems, branch)
}
newBranches := NewGitScmBranches()
newBranches.Items = &newBranchItems
return newBranches
}
14 changes: 7 additions & 7 deletions client/git_user_remote_config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package client

type GitUserRemoteConfig struct {
Refspec string `xml:"refspec"`
Url string `xml:"url"`
CredentialsId string `xml:"credentialsId"`
}

type GitUserRemoteConfigs struct {
Items *[]*GitUserRemoteConfig `xml:"hudson.plugins.git.UserRemoteConfig"`
}
Expand All @@ -12,7 +18,7 @@ func NewGitUserRemoteConfigs() *GitUserRemoteConfigs {

func (configs *GitUserRemoteConfigs) Append(config *GitUserRemoteConfig) *GitUserRemoteConfigs {
var newConfigItems []*GitUserRemoteConfig
if (configs.Items != nil) {
if configs.Items != nil {
newConfigItems = append(*configs.Items, config)
} else {
newConfigItems = append(newConfigItems, config)
Expand All @@ -21,9 +27,3 @@ func (configs *GitUserRemoteConfigs) Append(config *GitUserRemoteConfig) *GitUse
newConfigs.Items = &newConfigItems
return newConfigs
}

type GitUserRemoteConfig struct {
Refspec string `xml:"refspec"`
Url string `xml:"url"`
CredentialsId string `xml:"credentialsId"`
}
16 changes: 9 additions & 7 deletions client/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ var ErrJobPropertyNotFound = errors.New("Could not find job property")

// Job
type Job struct {
Id string
Name string
Disabled bool
Description string
Properties *JobProperties
Definition JobDefinition
Id string
Name string
Disabled bool
Description string
KeepDependencies bool
Properties *JobProperties
Definition JobDefinition
}

// NewJob return Job object with default values
func NewJob() *Job {
return &Job{
Properties: NewJobProperties(),
KeepDependencies: false,
Properties: NewJobProperties(),
}
}

Expand Down
23 changes: 12 additions & 11 deletions client/job_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ import (

type jobConfig struct {
XMLName xml.Name `xml:"flow-definition"`
Id string `xml:"id,attr"`
Plugin string `xml:"plugin,attr"`
Id string `xml:"id,attr,omitempty"`
Plugin string `xml:"plugin,attr,omitempty"`

// actions
Description string `xml:"description"`
// keepDependencies
Properties *JobProperties `xml:"properties"`
Definition *JobDefinitionXml `xml:"definition"`
// Triggers *[]*Trigger `xml:"triggers"`
Description string `xml:"description"`
KeepDependencies bool `xml:"keepDependencies"`
Properties *JobProperties `xml:"properties"`
Definition *JobDefinitionXml `xml:"definition"`
// triggers
Disabled bool `xml:"disabled"`
}

func JobConfigFromJob(job *Job) *jobConfig {
return &jobConfig{
Id: job.Id,
Description: job.Description,
Disabled: job.Disabled,
Properties: job.Properties,
Id: job.Id,
Description: job.Description,
KeepDependencies: job.KeepDependencies,
Disabled: job.Disabled,
Properties: job.Properties,
Definition: &JobDefinitionXml{
Item: job.Definition,
},
Expand Down
5 changes: 0 additions & 5 deletions client/job_config_property.go

This file was deleted.

123 changes: 119 additions & 4 deletions client/job_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,137 @@ package client
import (
"encoding/xml"
"testing"

"github.com/sergi/go-diff/diffmatchpatch"
)

func TestJobConfigSerialize(t *testing.T) {
job := NewJob()
job.Id = "job-id"
job.Description = "my-desc"

definition := NewCpsScmFlowDefinition()
definition.SCM = NewGitScm()
definition.SCM.ConfigVersion = "my-version"

branchSpec := NewGitScmBranchSpec()
branchSpec.Name = "branchspec"
definition.Id = "definition-id"
definition.SCM.Branches = definition.SCM.Branches.Append(branchSpec)
//userRemoteConfigs
//extensions
job.Definition = definition

gerritBranch := NewJobGerritTriggerBranch()
gerritBranch.CompareType = CompareTypeRegExp
gerritBranch.Pattern = "my-branch"
gerritProject := NewJobGerritTriggerProject()
gerritProject.CompareType = CompareTypePlain
gerritProject.Pattern = "my-project"
gerritProject.Branches = gerritProject.Branches.Append(gerritBranch)
gerritTrigger := NewJobGerritTrigger()
gerritTrigger.Projects = gerritTrigger.Projects.Append(gerritProject)
gerritTriggerPatchsetEvent := NewJobGerritTriggerPluginPatchsetCreatedEvent()
gerritTrigger.TriggerOnEvents = gerritTrigger.TriggerOnEvents.Append(gerritTriggerPatchsetEvent)
gerritTriggerDraftEvent := NewJobGerritTriggerPluginDraftPublishedEvent()
gerritTrigger.TriggerOnEvents = gerritTrigger.TriggerOnEvents.Append(gerritTriggerDraftEvent)
triggerJobProperty := NewJobPipelineTriggersProperty()
triggerJobProperty.Id = "trigger-id"
triggerJobProperty.Triggers = triggerJobProperty.Triggers.Append(gerritTrigger)
job.Properties = job.Properties.Append(triggerJobProperty)

discardPropertyStrategy := NewJobPipelineBuildDiscarderPropertyStrategyLogRotator()
discardPropertyStrategy.DaysToKeep = 1
discardPropertyStrategy.NumToKeep = 2
discardPropertyStrategy.ArtifactDaysToKeep = 3
discardPropertyStrategy.ArtifactNumToKeep = 4
discardProperty := NewJobPipelineBuildDiscarderProperty()
discardProperty.Id = "discard-id"
discardProperty.Strategy = discardPropertyStrategy
job.Properties = job.Properties.Append(discardProperty)

config := JobConfigFromJob(job)
resultBytes, err := xml.MarshalIndent(config, "", "\t")
if err != nil {
t.Fatalf("failed to serialize xml %s", err)
}
result := string(resultBytes)
expected := `<flow-definition id="" plugin="workflow-job@2.33">
<description></description>
<properties></properties>
expected := `<flow-definition id="job-id">
<description>my-desc</description>
<keepDependencies>false</keepDependencies>
<properties>
<org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty id="trigger-id">
<triggers>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger>
<gerritProjects>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject>
<compareType>PLAIN</compareType>
<pattern>my-project</pattern>
<branches>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.Branch>
<compareType>REG_EXP</compareType>
<pattern>my-branch</pattern>
</com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.Branch>
</branches>
<disableStrictForbiddenFileVerification>false</disableStrictForbiddenFileVerification>
</com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject>
</gerritProjects>
<skipVote>
<onSuccessful>false</onSuccessful>
<onFailed>false</onFailed>
<onUnstable>false</onUnstable>
<onNotBuilt>false</onNotBuilt>
</skipVote>
<silentMode>false</silentMode>
<silentStartMode>false</silentStartMode>
<escapeQuotes>true</escapeQuotes>
<nameAndEmailParameterMode>PLAIN</nameAndEmailParameterMode>
<commitMessageParameterMode>BASE64</commitMessageParameterMode>
<changeSubjectParameterMode>PLAIN</changeSubjectParameterMode>
<commentTextParameterMode>BASE64</commentTextParameterMode>
<serverName>__ANY__</serverName>
<triggerOnEvents class="linked-list">
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent>
<excludeDrafts>false</excludeDrafts>
<excludeTrivialRebase>false</excludeTrivialRebase>
<excludeNoCodeChange>false</excludeNoCodeChange>
<excludePrivateState>false</excludePrivateState>
<excludeWipState>false</excludeWipState>
</com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginDraftPublishedEvent></com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginDraftPublishedEvent>
</triggerOnEvents>
<dynamicTriggerConfiguration>false</dynamicTriggerConfiguration>
</com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger>
</triggers>
</org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
<jenkins.model.BuildDiscarderProperty id="discard-id">
<strategy class="hudson.tasks.LogRotator">
<daysToKeep>1</daysToKeep>
<numToKeep>2</numToKeep>
<artifactDaysToKeep>3</artifactDaysToKeep>
<artifactNumToKeep>4</artifactNumToKeep>
</strategy>
</jenkins.model.BuildDiscarderProperty>
</properties>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" id="definition-id">
<scm class="hudson.plugins.git.GitSCM">
<configVersion>my-version</configVersion>
<userRemoteConfigs></userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec id="">
<name>branchspec</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
</scm>
<scriptPath></scriptPath>
<lightweight>false</lightweight>
</definition>
<disabled>false</disabled>
</flow-definition>`
if result != expected {
t.Fatalf("job definition should be %s, was %s", expected, result)
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(expected, result, true)
t.Fatalf("job definition not expected: %s", dmp.DiffPrettyText(diffs))
}
}
4 changes: 2 additions & 2 deletions client/job_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type jobDetails struct {
// lastSuccessfulBuild
// lastUnstableBuild
// lastUnsuccessfulBuild
NextBuildNumber int64 `xml:"nextBuildNumber"`
Property *[]*JobConfigProperty `xml:"property"`
NextBuildNumber int64 `xml:"nextBuildNumber"`
// property
// queueItem
ConcurrentBuild bool `xml:"concurrentBuild"`
ResumeBlocked bool `xml:"resumeBlocked"`
Expand Down
Loading

0 comments on commit cf1a857

Please sign in to comment.