Class: Issuetype

Inherits:
Object
  • Object
show all
Defined in:
lib/jirarest2/issuetype.rb

Overview

Keep track of one Issuetype

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionString (readonly)

Returns The description for this issuetype.

Returns:

  • (String)

    The description for this issuetype



25
26
27
# File 'lib/jirarest2/issuetype.rb', line 25

def description
  @description
end

#fieldsArray<Hash> (readonly)

Returns All the fields in this issuetype.

Returns:

  • (Array<Hash>)

    All the fields in this issuetype



27
28
29
# File 'lib/jirarest2/issuetype.rb', line 27

def fields
  @fields
end

#nameString (readonly)

Returns The name of the issuetype.

Returns:

  • (String)

    The name of the issuetype



23
24
25
# File 'lib/jirarest2/issuetype.rb', line 23

def name
  @name
end

#required_fieldsArray (readonly)

Returns All the required fields in this issuetype.

Returns:

  • (Array)

    All the required fields in this issuetype



29
30
31
# File 'lib/jirarest2/issuetype.rb', line 29

def required_fields
  @required_fields
end

Instance Method Details

#createmeta(issuetype) ⇒ Object

TODO:

As the name and not the id of the field is used here some fields might get lost. There should be some way around while still enabling the use of names for external calls

Interpret the result of createmeta for one issuetype

Raises:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/jirarest2/issuetype.rb', line 114

def createmeta(issuetype)
  if issuetype.nil? || issuetype["name"].nil? then
    raise Jirarest2::WrongIssuetypeException
  else
    @name = issuetype["name"]
  end
  @description = issuetype["description"]
  @fields = Hash.new
  @required_fields = Array.new
  @field_name_id = Hash.new
  issuetype["fields"].each { |id,structure|
    name = structure["name"]
    required = structure["required"]
    type = decipher_schema(structure["schema"])
    field = Jirarest2Field::const_get(type).new(id,name,{:required => required, :createmeta => structure})
    @fields[id]  = field
    @field_name_id[name] = id
    @required_fields << field if required
  }
end

#decipher_schema(schema) ⇒ String

TODO:

timetracking will probably not work

TODO:

attachment is not realy good either

TODO:

hiddenjobswitch is unsure

TODO:

jobcheckbox is unsure

TODO:

check priority type

TODO:

how to handle readonlyfield?

Get the correct Fieldtype based on the schema from createmeta Strings are a little bit strange as the representation differs - some just go as values others go as hashes

Returns:

  • (String)

    Name of the Fieldtype to use



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
# File 'lib/jirarest2/issuetype.rb', line 41

def decipher_schema(schema) 
  case schema["type"]
  when "number"
    return "NumberField"
  when "user"
    return "UserField"
  when "version"
    return "VersionField"
  when "project"
    return "ProjectField"
  when "issuetype"
    return "HashField"
  when "datetime"
    return "DateTimeField"
  when "date"
    return "DateField"
  when "priority"
    return "HashField"
  when "group"
    return "UserField"
  when "resolution"
    return "HashField"
  when "timetracking"
    return "TimetrackingField"
  when "array"
    case schema["items"]
    when "version"
      return "MultiVersionField"
    when "group"
      return "MultiUserField"
    when "attachment"
      return "MultiStringField"
    when "user"
      return "MultiUserField"
    when "component"
      return "MultiHashField"
    when "string"
      return "MultiStringField" if schema["system"] && schema["system"] == "labels" # This is the only field with the "system" attribute
      case schema["custom"] 
      when /.*:multicheckboxes$/
        return "MultiHashField"
      when /.*:multiselect$/
        return "MultiHashField"
      when /.*:cascadingselect$/
        return "CascadingField"
      else
        raise Jirarest2::CouldNotDetermineFieldtypeException, schema
      end
    else
      raise Jirarest2::CouldNotDetermineFieldtypeException, schema
    end
  when "string"
    # Strings should be easy but unfortunately there are some strings that result in hashes
    return "TextField" if schema["system"]
    schema["custom"] =~ /.*:(\w*)$/ 
    case $1
    when "radiobuttons"
      return "HashField"
    when "select"
      return "HashField"
    else 
      return "TextField"
    end
  else
    raise Jirarest2::CouldNotDetermineFieldtypeException, schema
  end

end

#decode_issue(json) ⇒ Object

TODO:

Is this not really Issue instead of Issuetype?

TODO:

I need name of the issuetype if possible

Interpret the result to the request for an existing issue

Raises:

  • (Jirarest2::FieldsUnknownError)

    Raised if the fields to this issuetype is unknown



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/jirarest2/issuetype.rb', line 141

def decode_issue(json)
  if (@fields.nil? or @fields == {}) then 
    # Prepare the fields
    if json.has_key?("schema") then
      createmeta(json["schema"])
    elsif  json.has_key?("editmeta") then
      createmeta(json["editmeta"])
    else
      # We need to know what to fill and how
      raise Jirearesr2::FieldsUnknownError, json["self"]
    end
  end
  @issuekey = json["key"]
  json["fields"].each{ |field_id,content|
    @fields["field_id"].parse_value(content)
  }
end

#fieldtype(fieldname) ⇒ String

Return the fieldtype (Multitype as “array” nostly for backwards compability)

Returns:

  • (String)

    The fieldtype as String. MultiField types and CascadingField are returned as “array”



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/jirarest2/issuetype.rb', line 217

def fieldtype(fieldname)
  ftype =  get_field(fieldname,:name).class.to_s
  ftype =~ /^.*::(\w+)Field$/
  ftshort = $1
  case ftshort
  when /Multi.*/
    return "array"
  when "Cascading"
    return "array"
  else
    return ftshort
  end
end

#get_value(id, denom = :name) ⇒ String, Array

Get the value of a field

Parameters:

  • id (String)

    The name of the field

  • denom (Symbol) (defaults to: :name)

    Field identifier to use (:name or :id)

Returns:

  • (String, Array)

    value The value of the field



173
174
175
176
# File 'lib/jirarest2/issuetype.rb', line 173

def get_value(id,denom = :name)
  field = get_field(id,denom)
  return field.value
end

#new_ticket_hashHash

TODO:

NOT finished

Build up a hash to give to jira to create a new ticket

Returns:

  • (Hash)

    Hash to be sent to the server

Raises:



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/jirarest2/issuetype.rb', line 196

def new_ticket_hash
  missing_fields = required_by_name(true)
  if missing_fields == [] then
    fields = Hash.new
    @fields.each { |id,field| 
      fields = fields.merge!(field.to_j) if ! field.to_j.nil? #Sending empty fields with a new ticket will not work
    }
    # Work out some difference between different versions of JIRA(tm). Sometimes the issuetype is there as a field sometimes it isn't.
    if !fields.has_key?("issuetype") then
      fields["issuetype"] = {"name" => @name}
    end
    h = {"fields" => fields}
    return h
  else
    raise Jirarest2::RequiredFieldNotSetException, missing_fields
  end
end

#required_by_name(only_empty = false) ⇒ Array

check if all the required fields have values The following fields are not seen as required in this method because JIRA (tm) sets it’s own defaults: project, issuetype, reporter

Parameters:

  • only_empty (Boolean) (defaults to: false)

    If set to true will only return those Names where the value is empty

Returns:

  • (Array)

    Names of all the required_fields that have no value assigned, empty if all fields have a value



184
185
186
187
188
189
190
# File 'lib/jirarest2/issuetype.rb', line 184

def required_by_name(only_empty = false)
  empty = Array.new
  @required_fields.each{ |field|
    empty << field.name if (field.value.nil? || ! only_empty ) && !field.name.nil? && field.id != "issuetype" && field.id != "reporter" 
  }
  return empty
end

#set_value(id, value, denom = :name) ⇒ Object

Set the value of a field

Parameters:

  • id (String)

    The name of the field

  • value (String, Array)

    The value of the field

  • denom (Symbol) (defaults to: :name)

    Field identifier to use (:name or :id)



164
165
166
167
# File 'lib/jirarest2/issuetype.rb', line 164

def set_value(id,value,denom = :name)
  field = get_field(id,denom)
  field.value = value
end