Class: JenkinsApi::Client::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins_cron/extentions/jenkins_api_client/job.rb

Instance Method Summary collapse

Instance Method Details

#build_freestyle_config(params) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jenkins_cron/extentions/jenkins_api_client/job.rb', line 6

def build_freestyle_config(params)
  # Supported SCM providers
  supported_scm = ["git", "subversion", "cvs"]

  # Set default values for params that are not specified.
  raise ArgumentError, "Job name must be specified" \
    unless params.is_a?(Hash) && params[:name]
  if params[:keep_dependencies].nil?
    params[:keep_dependencies] = false
  end
  if params[:block_build_when_downstream_building].nil?
    params[:block_build_when_downstream_building] = false
  end
  if params[:block_build_when_upstream_building].nil?
    params[:block_build_when_upstream_building] = false
  end
  params[:concurrent_build] = false if params[:concurrent_build].nil?
  if params[:notification_email]
    if params[:notification_email_for_every_unstable].nil?
      params[:notification_email_for_every_unstable] = false
    end
    if params[:notification_email_send_to_individuals].nil?
      params[:notification_email_send_to_individuals] ||= false
    end
  end
  # SCM configurations and Error handling.
  unless supported_scm.include?(params[:scm_provider]) ||
    params[:scm_provider].nil?
    raise "SCM #{params[:scm_provider]} is currently not supported"
  end
  if params[:scm_url].nil? && !params[:scm_provider].nil?
    raise 'SCM URL must be specified'
  end
  if params[:scm_branch].nil? && !params[:scm_provider].nil?
    params[:scm_branch] = "master"
  end
  if params[:scm_use_head_if_tag_not_found].nil?
    params[:scm_use_head_if_tag_not_found] = false
  end

  # Child projects configuration and Error handling
  if params[:child_threshold].nil? && !params[:child_projects].nil?
    params[:child_threshold] = 'failure'
  end

  @logger.debug "Creating a freestyle job with params: #{params.inspect}"

  # Build the Job xml file based on the parameters given
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') { |xml|
    xml.project {
      xml.actions
      xml.description
      xml.keepDependencies "#{params[:keep_dependencies]}"
      xml.properties
      # SCM related stuff
      if params[:scm_provider] == 'subversion'
        # Build subversion related XML portion
        scm_subversion(params, xml)
      elsif params[:scm_provider] == "cvs"
        # Build CVS related XML portion
        scm_cvs(params, xml)
      elsif params[:scm_provider] == "git"
        # Build Git related XML portion
        scm_git(params, xml)
      else
        xml.scm(:class => "hudson.scm.NullSCM")
      end
      # Restrict job to run in a specified node
      if params[:restricted_node]
        xml.assignedNode "#{params[:restricted_node]}"
        xml.canRoam "false"
      else
        xml.canRoam "true"
      end
      xml.disabled "false"
      xml.blockBuildWhenDownstreamBuilding(
        "#{params[:block_build_when_downstream_building]}")
      xml.blockBuildWhenUpstreamBuilding(
        "#{params[:block_build_when_upstream_building]}")
      if params[:timer]
        xml.triggers.vector {
          xml.send("hudson.triggers.TimerTrigger") {
            xml.spec params[:timer]
          }
        }
      else
        xml.triggers.vector
      end
      xml.concurrentBuild "#{params[:concurrent_build]}"
      # Shell command stuff
      xml.builders {
        if params[:shell_command]
          xml.send("hudson.tasks.Shell") {
            xml.command "#{params[:shell_command]}"
          }
        end
      }
      # Adding Downstream projects
      xml.publishers {
        # Build portion of XML that adds child projects
        child_projects(params, xml) if params[:child_projects]
        # Build portion of XML that adds email notification
        notification_email(params, xml) if params[:notification_email]
        # Build portion of XML that adds skype notification
        skype_notification(params, xml) if params[:skype_targets]
      }
      xml.buildWrappers
    }
  }
  builder.to_xml
end

#create_freestyle(params) ⇒ Object



118
119
120
121
# File 'lib/jenkins_cron/extentions/jenkins_api_client/job.rb', line 118

def create_freestyle(params)
  xml = build_freestyle_config(params)
  create(params[:name], xml)
end

#create_or_update_freestyle(params) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/jenkins_cron/extentions/jenkins_api_client/job.rb', line 128

def create_or_update_freestyle(params)
  if exists?(params[:name])
    update_freestyle(params)
  else
    create_freestyle(params)
  end
end

#update_freestyle(params) ⇒ Object



123
124
125
126
# File 'lib/jenkins_cron/extentions/jenkins_api_client/job.rb', line 123

def update_freestyle(params)
  xml = build_freestyle_config(params)
  post_config(params[:name], xml)
end