Commit bf71009e authored by Alexander Hansen Færøy's avatar Alexander Hansen Færøy
Browse files

Merge branch 'support-status' into 'main'

add function to return issue status

Closes #24

See merge request !38
parents efb85959 319f5cef
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -91,6 +91,47 @@ module TorPlugin
      .max
  end

  def execute_graphql_query(query_string, variables)
    graphql_adapter = network.graphql.adapter
    parsed_query = graphql_adapter.parse(query_string)

    # this is what the code should look like if gitlab-triage-1.46.0/lib/gitlab/triage/network_adapters/graphql_adapter.rb wasn't buggy
    #response = graphql_adapter.query(parsed_query, variables: variables)

    # this is the heinous crime we have to commit, bypassing visibility, to remove the "parsed_response.nodes?" which throws because nodes? doesn't exists (?!)
    response = graphql_adapter.send(:client).query(parsed_query, variables: variables, context: { token: graphql_adapter.options.token })
    parsed_response = graphql_adapter.send(:parse_response, response, [])

    headers = response.extensions.fetch('headers', {})

    graphql_response = {
      ratelimit_remaining: headers['ratelimit-remaining'].to_i,
      ratelimit_reset_at: Time.at(headers['ratelimit-reset'].to_i)
    }

    graphql_response.merge(results: parsed_response.to_h)
  end

  def status
    get_status_query = %{
      query ($workItemId: WorkItemID!) {
        workItem(id: $workItemId) {
          widgets(onlyTypes: [STATUS]) {
            ... on WorkItemWidgetStatus {
              status {
                name
              }
            }
          }
        }
      }
    }

    variables = {workItemId: "gid://gitlab/WorkItem/#{resource[:id]}"}
    res = execute_graphql_query(get_status_query, variables)
    res.dig(:results, 'workItem', 'widgets', 0, 'status', 'name')
  end

  # Returns the author of a given issue or MR.
  def author
    resource[:author]