Class: GhDraftIssueConverter::GhResoucesManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ GhResoucesManager

Returns a new instance of GhResoucesManager.



88
89
90
# File 'lib/gh_draft_issues_converter/gh_draft_issues_converter.rb', line 88

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



86
87
88
# File 'lib/gh_draft_issues_converter/gh_draft_issues_converter.rb', line 86

def client
  @client
end

Instance Method Details

#add_issue_projectv2(project_id, issue_id) ⇒ String

Returns Issue ID.

Returns:

  • (String)

    Issue ID



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gh_draft_issues_converter/gh_draft_issues_converter.rb', line 93

def add_issue_projectv2(project_id, issue_id)
  response = client.query <<~GRAPHQL
    mutation {
      addProjectV2ItemById(input: {projectId: "#{project_id}", contentId: "#{issue_id}"}) {
        item {
          id
        }
      }
    }
  GRAPHQL

  response.to_h["data"]["addProjectV2ItemById"]["item"]["id"]
end

#convert_field_nodes_to_objs(nodes) ⇒ Array<FieldValue>

Returns:



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/gh_draft_issues_converter/gh_draft_issues_converter.rb', line 167

def convert_field_nodes_to_objs(nodes)
  nodes.map do |node|
    next if node.empty?
    next if node["__typename"] != "ProjectV2ItemFieldSingleSelectValue"

    FieldValue.new(
      node["id"],
      node["name"],
      node["optionId"],
      node["field"]["id"],
      node["field"]["name"]
    )
  end.compact
end

#create_issue_from_draft(draft_issue, repository_id, project_id) ⇒ String

Returns:

  • (String)

    item_id



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/gh_draft_issues_converter/gh_draft_issues_converter.rb', line 259

def create_issue_from_draft(draft_issue, repository_id, project_id)
  response = client.query <<~GRAPHQL
    mutation {
      createIssue(input: {
        repositoryId: "#{repository_id}",
        assigneeIds: [#{draft_issue.user_ids_github_format}],
        title: "#{draft_issue.title}",
        body: "#{draft_issue.body.gsub("\n", "\\n").gsub('"', '\"')}"
      }) {
        issue {
          id
        }
      }
    }
  GRAPHQL

  response.original_hash["data"]["createIssue"]["issue"]["id"]
end

#delete_draft_issue(project_id, draft_issue) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/gh_draft_issues_converter/gh_draft_issues_converter.rb', line 278

def delete_draft_issue(project_id, draft_issue)
  response = client.query <<~GRAPHQL
  mutation {
    deleteProjectV2Item(input: {
      projectId: "#{project_id}",
      itemId: "#{draft_issue.id}"
    }) {
      clientMutationId
    }
  }
  GRAPHQL
end

#fetch_draft_issues(project_id) ⇒ Object



107
108
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/gh_draft_issues_converter/gh_draft_issues_converter.rb', line 107

def fetch_draft_issues(project_id)
  # https://docs.github.com/ja/graphql/reference/objects#user
  response = client.query <<~GRAPHQL
  query {
      node(id: "#{project_id}") {
        ... on ProjectV2 {
          items {
            nodes {
              id,
              content {
                ... on DraftIssue {
                  title
                  body
                  assignees(first: 10) {
                    nodes {
                      id
                    }
                  }
                }
              }
              fieldValues(first: 10) {
                nodes {
                  ... on ProjectV2ItemFieldSingleSelectValue {
                    id
                    optionId
                    name
                    field {
                      ... on ProjectV2SingleSelectField {
                        id
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
  }
  GRAPHQL

  nodes = response.to_h["data"]["node"]["items"]["nodes"]
  nodes.map do |node|
    next if node["content"]["__typename"] == "Issue"

    assign_user_ids = node["content"]["assignees"]["nodes"]
    assign_user_ids = assign_user_ids.map { |item| item["id"] }

    DraftIssue.new(
      node["id"],
      node["content"]["title"],
      node["content"]["body"],
      assign_user_ids,
      convert_field_nodes_to_objs(node["fieldValues"]["nodes"])
    )
  end.compact
end

#fetch_project_id(project_number, repository) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/gh_draft_issues_converter/gh_draft_issues_converter.rb', line 182

def fetch_project_id(project_number, repository)
  owner_is_user = owner_is_user?(repository)
  if owner_is_user
    response = client.query <<~GRAPHQL
    query {
      user(login: "#{repository.owner}") {
        projectV2(number: #{project_number}) {
          id,
          title
        }
      }
    }
    GRAPHQL
    response.to_h["data"]["user"]["projectV2"]["id"]
  else
    response = client.query <<~GRAPHQL
    query {
      organization(login: "#{repository.owner}") {
          projectV2(number: #{project_number}) {
            id,
            title
          }
      }
    }
    GRAPHQL
    response.to_h["data"]["organization"]["projectV2"]["id"]
  end
end

#fetch_repository_id(repository) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/gh_draft_issues_converter/gh_draft_issues_converter.rb', line 211

def fetch_repository_id(repository)
  owner_is_user = owner_is_user?(repository)
  if owner_is_user
    response = client.query <<~GRAPHQL
    query {
      repository(owner: "#{repository.owner}", name: "#{repository.name}") {
        id
      }
    }
    GRAPHQL

    response.to_h["data"]["repository"]["id"]
  else
    response = client.query <<~GRAPHQL
    query {
      organization(login: "#{repository.owner}") {
        repository(name: "#{repository.name}") {
          id
        }
      }
    }
    GRAPHQL

    response.to_h["data"]["organization"]["repository"]["id"]
  end
end

#owner_is_user?(repository) ⇒ Boolean

Returns:

  • (Boolean)


291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/gh_draft_issues_converter/gh_draft_issues_converter.rb', line 291

def owner_is_user?(repository)
  response = client.query <<~GRAPHQL
  query {
    repositoryOwner(login: "#{repository.owner}") {
      __typename
      login
    }
  }
  GRAPHQL

  response.to_h["data"]["repositoryOwner"]["__typename"] == "User"
end

#update_item_field_values(project_id, draft_issue, item_id) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/gh_draft_issues_converter/gh_draft_issues_converter.rb', line 238

def update_item_field_values(project_id, draft_issue, item_id)
  draft_issue.field_values.each do |field_value|
    response = client.query <<~GRAPHQL
    mutation {
      updateProjectV2ItemFieldValue(input: {
        projectId: "#{project_id}",
        itemId: "#{item_id}",
        fieldId: "#{field_value.field_id}",
        value: {singleSelectOptionId: "#{field_value.field_value_option_id}"}}
        ) {
        projectV2Item {
          id
        }
      }
    }
    GRAPHQL
  end
end