Module: ActiveProject::Adapters::GithubProject::Helpers

Included in:
Issues, Projects
Defined in:
lib/active_project/adapters/github_project/helpers.rb

Instance Method Summary collapse

Instance Method Details

#fetch_all_pages(query, variables:, connection_path:) {|vars| ... } ⇒ Array<Hash>

Cursor-based pagination wrapper for GraphQL connections.

Parameters:

  • query (String)

    the GraphQL query with $after variable

  • variables (Hash)

    initial variables (without :after)

  • connection_path (Array<String>)

    JSON path to the connection hash

Yields:

  • (vars)

    yields the variables hash for each page so caller can execute the request

Returns:

  • (Array<Hash>)

    all nodes from every page



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_project/adapters/github_project/helpers.rb', line 16

def fetch_all_pages(query, variables:, connection_path:, &request_block)
  # turn the (possibly-nil) block into something callable
  request_fn =
    request_block ||
    ->(v) { request_gql(query: query, variables: v) }

  after = nil
  nodes = []

  loop do
    data = request_fn.call(variables.merge(after: after))
    conn = data.dig(*connection_path)
    nodes.concat(conn["nodes"])
    break unless conn["pageInfo"]["hasNextPage"]

    after = conn["pageInfo"]["endCursor"]
  end

  nodes
end

#map_user(u) ⇒ Object

Convert a compact user hash returned by GraphQL into Resources::User.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_project/adapters/github_project/helpers.rb', line 63

def map_user(u)
  return nil unless u

  Resources::User.new(
    self,
    id: u["id"] || u["login"],
    name: u["login"],
    email: u["email"],
    adapter_source: :github,
    raw_data: u
  )
end

#owner_node_id(login) ⇒ Object

Resolve a user/org login → GraphQL node-ID (memoised).



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_project/adapters/github_project/helpers.rb', line 40

def owner_node_id()
  @owner_id_cache ||= {}
  return @owner_id_cache[] if @owner_id_cache.key?()

  q = <<~GQL
    query($login:String!){
      organization(login:$login){ id }
      user(login:$login){ id }
    }
  GQL

  data = request_gql(query: q, variables: { login:  })
  id = data.dig("organization", "id") || data.dig("user", "id")
  raise ActiveProject::NotFoundError, "GitHub owner “#{}” not found" unless id

  id = upgraded_id(id) if respond_to?(:upgraded_id)

  @owner_id_cache[] = id
end

#project_field_ids(project_id) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_project/adapters/github_project/helpers.rb', line 76

def project_field_ids(project_id)
  @field_cache ||= {}
  @field_cache[project_id] ||= begin
    q = <<~GQL
      query($id:ID!){
        node(id:$id){
          ... on ProjectV2{
            fields(first:50){
              nodes{
                ... on ProjectV2FieldCommon{ id name }
              }
            }
          }
        }
      }
    GQL
    nodes = request_gql(query: q, variables: { id: project_id })
            .dig("node", "fields", "nodes")
    nodes.to_h { |f| [ f["name"], f["id"] ] }
  end
end