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 = "    query($login:String!){\n      organization(login:$login){ id }\n      user(login:$login){ id }\n    }\n  GQL\n\n  data = request_gql(query: q, variables: { login: login })\n  id = data.dig(\"organization\", \"id\") || data.dig(\"user\", \"id\")\n  raise ActiveProject::NotFoundError, \"GitHub owner \u201C\#{login}\u201D not found\" unless id\n\n  id = upgraded_id(id) if respond_to?(:upgraded_id)\n\n  @owner_id_cache[login] = id\nend\n"

#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 = "      query($id:ID!){\n        node(id:$id){\n          ... on ProjectV2{\n            fields(first:50){\n              nodes{\n                ... on ProjectV2FieldCommon{ id name }\n              }\n            }\n          }\n        }\n      }\n    GQL\n    nodes = request_gql(query: q, variables: { id: project_id })\n            .dig(\"node\", \"fields\", \"nodes\")\n    nodes.to_h { |f| [ f[\"name\"], f[\"id\"] ] }\n  end\nend\n"