Class: OssStats::BuildkiteClient
- Inherits:
-
Object
- Object
- OssStats::BuildkiteClient
- Defined in:
- lib/oss_stats/buildkite_client.rb
Overview
Client for interacting with the Buildkite GraphQL API.
Instance Attribute Summary collapse
-
#organization_slug ⇒ Object
readonly
Returns the value of attribute organization_slug.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #all_pipelines(org) ⇒ Object
- #get_pipeline(org, pipeline) ⇒ Object
-
#get_pipeline_builds(org, pipeline, from_date, to_date, branch = 'main') ⇒ Array<Hash>
Fetches builds for a given pipeline within a specified date range.
-
#initialize(token) ⇒ BuildkiteClient
constructor
Initializes a new BuildkiteClient.
- #pipelines_by_repo(org) ⇒ Object
Constructor Details
#initialize(token) ⇒ BuildkiteClient
Initializes a new BuildkiteClient.
17 18 19 20 |
# File 'lib/oss_stats/buildkite_client.rb', line 17 def initialize(token) @token = token @graphql_endpoint = URI('https://graphql.buildkite.com/v1') end |
Instance Attribute Details
#organization_slug ⇒ Object (readonly)
Returns the value of attribute organization_slug.
10 11 12 |
# File 'lib/oss_stats/buildkite_client.rb', line 10 def organization_slug @organization_slug end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
10 11 12 |
# File 'lib/oss_stats/buildkite_client.rb', line 10 def token @token end |
Instance Method Details
#all_pipelines(org) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/oss_stats/buildkite_client.rb', line 57 def all_pipelines(org) log.debug("Fetching all pipeline in #{org}") pipelines = [] after_cursor = nil has_next_page = true while has_next_page query = " query {\n organization(slug: \"\#{org}\") {\n pipelines(\n first: 50,\n after: \#{after_cursor ? \"\\\"\#{after_cursor}\\\"\" : 'null'}\n ) {\n edges {\n node {\n slug\n repository {\n url\n }\n visibility\n url\n }\n }\n pageInfo {\n endCursor\n hasNextPage\n }\n }\n }\n }\n GRAPHQL\n\n response_data = execute_graphql_query(query)\n current_pipelines = response_data.dig(\n 'data', 'organization', 'pipelines', 'edges'\n )\n if current_pipelines\n pipelines.concat(\n current_pipelines.map { |edge| edge['node'] },\n )\n end\n\n page_info = response_data.dig(\n 'data', 'organization', 'pipelines', 'pageInfo'\n )\n break unless page_info\n has_next_page = page_info['hasNextPage']\n after_cursor = page_info['endCursor']\n end\n\n pipelines\nend\n" |
#get_pipeline(org, pipeline) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/oss_stats/buildkite_client.rb', line 22 def get_pipeline(org, pipeline) log.debug("Fetching pipeline: #{org}/#{pipeline}") query = " query {\n pipeline(slug: \"\#{org}/\#{pipeline}\") {\n visibility\n url\n }\n }\n GRAPHQL\n\n response_data = execute_graphql_query(query)\n pipeline_data = response_data.dig('data', 'pipeline')\n if pipeline_data.nil? && response_data['data'].key?('pipeline')\n # The query returned, and the 'pipeline' key exists but is null,\n # meaning the pipeline was not found by Buildkite.\n log.debug(\n \"Pipeline \#{org}/\#{pipeline} not found.\",\n )\n elsif pipeline_data.nil? && response_data['errors']\n # Errors occurred, already logged by execute_graphql_query,\n # but we might want to note the slug it failed for.\n log.warn(\n \"Failed to fetch pipeline \#{org}/\#{pipeline}\" +\n 'due to API errors',\n )\n end\n pipeline_data\nrescue StandardError => e\n log.error(\n \"Error in get_pipeline for slug \#{org}/\#{pipeline}: \#{e.message}\",\n )\n nil\nend\n" |
#get_pipeline_builds(org, pipeline, from_date, to_date, branch = 'main') ⇒ Array<Hash>
Fetches builds for a given pipeline within a specified date range. Handles pagination to retrieve all relevant builds.
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/oss_stats/buildkite_client.rb', line 139 def get_pipeline_builds(org, pipeline, from_date, to_date, branch = 'main') log.debug("get_pipeline_builds: #{org}, #{pipeline}") all_build_edges = [] after_cursor = nil has_next_page = true while has_next_page query = " query {\n pipeline(slug: \"\#{org}/\#{pipeline}\") {\n builds(\n first: 50,\n after: \#{after_cursor ? \"\\\"\#{after_cursor}\\\"\" : 'null'},\n createdAtFrom: \"\#{from_date.to_datetime.rfc3339}\",\n createdAtTo: \"\#{to_date.to_datetime.rfc3339}\",\n branch: \"\#{branch}\",\n ) {\n edges {\n node {\n url\n id\n state\n createdAt\n message\n }\n }\n pageInfo { # For build pagination\n hasNextPage\n endCursor\n }\n }\n }\n }\n GRAPHQL\n\n response_data = execute_graphql_query(query)\n builds_data = response_data.dig('data', 'pipeline', 'builds')\n\n if builds_data && builds_data['edges']\n all_build_edges.concat(builds_data['edges'])\n page_info = builds_data['pageInfo']\n has_next_page = page_info['hasNextPage']\n after_cursor = page_info['endCursor']\n else\n # No builds data or error in structure, stop pagination\n has_next_page = false\n end\n end\n\n all_build_edges\nrescue StandardError => e\n log.error(\n \"Error in get_pipeline_builds for \#{org}/\#{pipeline}: \#{e.message}\",\n )\n [] # Return empty array on error\nend\n" |
#pipelines_by_repo(org) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/oss_stats/buildkite_client.rb', line 111 def pipelines_by_repo(org) log.debug("pipelines_by_repo: #{org}") pipelines_by_repo = Hash.new { |h, k| h[k] = [] } all_pipelines(org).each do |pipeline| repo_url = pipeline.dig('repository', 'url').gsub('.git', '') next unless repo_url pipelines_by_repo[repo_url] << { slug: pipeline['slug'], url: pipeline['url'], visibility: pipeline['visibility'], } end pipelines_by_repo end |