Class: GHX::ProjectItem

Inherits:
Object
  • Object
show all
Defined in:
lib/ghx/project_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_configuration:, data:) ⇒ ProjectItem

Returns a new instance of ProjectItem.



5
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
# File 'lib/ghx/project_item.rb', line 5

def initialize(field_configuration:, data:)
  _setup_field_configuration(field_configuration)

  GHX.logger.debug data

  node = data
  content = node["content"]

  # These are fields common to all Project Items:
  @id = node["id"]
  @project_id = node["project"]["id"]
  @issue_number = content["number"]
  @issue_title = content["title"]
  @issue_url = content["url"]
  @issue_state = content["state"]

  field_values = node["fieldValues"]["nodes"]

  field_values.each do |field|
    GHX.logger.debug "Field: #{field}"
    next unless field["field"]
    next if field["field"]["name"].to_s.empty?

    name = normalized_field_value_name(field["field"]["name"])

    public_send(:"#{name}=", extracted_field_value(field))
  rescue NoMethodError
    GHX.logger.warn "Could not find field name: #{field["field"]["name"]} in the field configuration for project item #{id}"
  end
end

Instance Attribute Details

#field_mapObject

Returns the value of attribute field_map.



3
4
5
# File 'lib/ghx/project_item.rb', line 3

def field_map
  @field_map
end

#field_valuesObject

Returns the value of attribute field_values.



3
4
5
# File 'lib/ghx/project_item.rb', line 3

def field_values
  @field_values
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/ghx/project_item.rb', line 3

def id
  @id
end

#issue_numberObject

Returns the value of attribute issue_number.



3
4
5
# File 'lib/ghx/project_item.rb', line 3

def issue_number
  @issue_number
end

#issue_stateObject

Returns the value of attribute issue_state.



3
4
5
# File 'lib/ghx/project_item.rb', line 3

def issue_state
  @issue_state
end

#issue_titleObject

Returns the value of attribute issue_title.



3
4
5
# File 'lib/ghx/project_item.rb', line 3

def issue_title
  @issue_title
end

#issue_urlObject

Returns the value of attribute issue_url.



3
4
5
# File 'lib/ghx/project_item.rb', line 3

def issue_url
  @issue_url
end

#project_idObject

Returns the value of attribute project_id.



3
4
5
# File 'lib/ghx/project_item.rb', line 3

def project_id
  @project_id
end

Instance Method Details

#update(**fields) ⇒ Object

Updates the given fields to the given values. Makes a GraphQL call per field to do the update.

Parameters:

  • fields (Hash)

    A hash of field names to values.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ghx/project_item.rb', line 39

def update(**fields)
  fields.each do |field, value|
    field_id = field_id(field)
    raise "Field not found: #{field}" unless field_id

    field_type = field_type(field)

    case field_type
    when "DATE"
      update_date_field(field_id, value)
    when "SINGLE_SELECT"
      update_single_select_field(field_id, value)
    when "TEXT"
      update_text_field(field_id, value)
    when "TITLE"
      update_title_field(field_id, value)
    else
      GHX.logger.warn "Unknown field type in update: #{field_type}"
    end
  end
end

#update_date_field(field_id, value) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ghx/project_item.rb', line 85

def update_date_field(field_id, value)
  gql_query = "    mutation {\n      updateProjectV2ItemFieldValue(input: {\n        fieldId: \"\#{field_id}\",\n        itemId: \"\#{id}\",\n        projectId: \"\#{project_id}\",\n        value: { \n          date: \"\#{value}\"\n        }\n      }) {\n        projectV2Item {\n          id  \n        }\n      }\n    }\n  GQL\n\n  client = GraphqlClient.new(ENV[\"GITHUB_TOKEN\"])\n  res = client.query(gql_query)\n  GHX.logger.debug \"Update date field result\"\n  GHX.logger.debug res\nend\n"

#update_single_select_field(field_id, value) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ghx/project_item.rb', line 109

def update_single_select_field(field_id, value)
  field_options = field_options(field_id)
  raise "No options found for #{field_id}" unless field_options

  option_id = field_options.find { |option| option[:name] == value }&.fetch(:id)
  raise "Option not found: #{value}" unless option_id

  gql_query = "    mutation {\n      updateProjectV2ItemFieldValue(input: {\n        fieldId: \"\#{field_id}\",\n        itemId: \"\#{id}\",\n        projectId: \"\#{project_id}\",\n        value: { \n          singleSelectOptionId: \"\#{option_id}\"\n        }\n      }) {\n        projectV2Item {\n          id  \n        }\n      }\n    }\n  GQL\n\n  client = GraphqlClient.new(ENV[\"GITHUB_TOKEN\"])\n  res = client.query(gql_query)\n  GHX.logger.debug \"Update single select field result\"\n  GHX.logger.debug res.body\nend\n"

#update_text_field(field_id, value) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ghx/project_item.rb', line 61

def update_text_field(field_id, value)
  gql_query = "    mutation {\n      updateProjectV2ItemFieldValue(input: {\n        fieldId: \"\#{field_id}\",\n        itemId: \"\#{id}\",\n        projectId: \"\#{project_id}\",\n        value: { \n          text: \"\#{value}\"\n        }\n      }) {\n        projectV2Item {\n          id  \n        }\n      }\n    }\n  GQL\n\n  client = GraphqlClient.new(ENV[\"GITHUB_TOKEN\"])\n  res = client.query(gql_query)\n  GHX.logger.debug \"Update text field result\"\n  GHX.logger.debug res\nend\n"