Skip to content

Commit 4b39ad9

Browse files
committed
Check JSON result type after parsing, bump version
1 parent 75420bc commit 4b39ad9

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lib/project_pull_mover/gh_cli.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class GhCli
1212

1313
class NoJsonError < StandardError; end
1414
class GraphqlApiError < StandardError; end
15+
class UnexpectedJsonStructureError < StandardError; end
1516

1617
class JsonParseError < StandardError
1718
attr_reader :cause
@@ -86,12 +87,18 @@ def check_auth_status
8687
def get_project_items
8788
project_items_cmd = "#{gh_path} project item-list #{@options.project_number} " \
8889
"--owner #{@options.project_owner} --format json --limit #{@options.proj_items_limit}"
89-
json = T.let(`#{project_items_cmd}`, T.nilable(String))
90-
if json.nil? || json == ""
90+
json_str = T.let(`#{project_items_cmd}`, T.nilable(String))
91+
if json_str.nil? || json_str == ""
9192
raise NoJsonError, "Error: no JSON results for project items; command: #{project_items_cmd}"
9293
end
9394

94-
all_project_items = parse_json(json)["items"]
95+
json_obj = parse_json(json_str)
96+
unless json_obj.is_a?(Hash)
97+
raise UnexpectedJsonStructureError, "Error: expected a hash with 'items', got #{json_obj.class.name}: " \
98+
"#{json_obj.inspect}"
99+
end
100+
101+
all_project_items = json_obj["items"]
95102
unless quiet_mode?
96103
units = all_project_items.size == 1 ? "item" : "items"
97104
@logger.info("Found #{all_project_items.size} #{units} in project")
@@ -131,6 +138,11 @@ def make_graphql_api_query(graphql_query)
131138
json_str = `#{gh_path} api graphql -f query='#{graphql_query}'`
132139
graphql_resp = parse_json(json_str)
133140

141+
unless graphql_resp.is_a?(Hash)
142+
raise UnexpectedJsonStructureError, "Error: expected a hash from GraphQL API, got " \
143+
"#{graphql_resp.class.name}: #{graphql_resp.inspect}"
144+
end
145+
134146
unless graphql_resp["data"]
135147
graphql_error_msg = if graphql_resp["errors"]
136148
graphql_resp["errors"].map { |err| err["message"] }.join("\n")
@@ -153,13 +165,19 @@ def get_pulls_by_author_in_project
153165

154166
pulls_by_author_in_project_cmd = "#{gh_path} search prs --author \"#{@options.author}\" --project " \
155167
"\"#{@options.project_owner}/#{@options.project_number}\" --json \"number,repository\" --limit #{@options.proj_items_limit} --state open"
156-
json = T.let(`#{pulls_by_author_in_project_cmd}`, T.nilable(String))
157-
if json.nil? || json == ""
168+
json_str = T.let(`#{pulls_by_author_in_project_cmd}`, T.nilable(String))
169+
if json_str.nil? || json_str == ""
158170
raise NoJsonError, "Error: no JSON results for pull requests by author in project; " \
159171
"command: #{pulls_by_author_in_project_cmd}"
160172
end
161173

162-
parse_json(json)
174+
json_obj = parse_json(json_str)
175+
unless json_obj.nil? || json_obj.is_a?(Array)
176+
raise UnexpectedJsonStructureError, "Error: expected an array of pull requests, got " \
177+
"#{json_obj.class.name}: #{json_obj.inspect}"
178+
end
179+
180+
json_obj
163181
end
164182

165183
sig { params(input: String).returns(T.untyped) }

lib/project_pull_mover/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
# frozen_string_literal: true
33

44
module ProjectPullMover
5-
VERSION = "0.0.6"
5+
VERSION = "0.0.7"
66
end

0 commit comments

Comments
 (0)