Skip to content

Commit 07490d1

Browse files
committed
tool/redmine-backporter.rb: Authenticate GET requests
bugs.ruby-lang.org now rejects anonymous JSON API requests with 403 Forbidden. The backporter already requires a Redmine API key and uses it for write requests, but the read-only issue and journal lookups were still sent without credentials. Send the existing X-Redmine-API-Key header for those GET requests too, so ls/show/done/last can keep using the JSON API.
1 parent fdad6b5 commit 07490d1

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

tool/redmine-backporter.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
BACKPORT_CF_KEY = 'cf_5'
3434
STATUS_CLOSE = 5
3535
REDMINE_API_KEY = api_key || ENV['REDMINE_API_KEY'] || (puts opts.help; raise 'need to specify REDMINE_API_KEY')
36+
REDMINE_API_KEY_HEADER = {'X-Redmine-API-Key' => REDMINE_API_KEY}.freeze
3637
REDMINE_BASE = 'https://bugs.ruby-lang.org'
3738

3839
@query = {
@@ -164,8 +165,16 @@ def has_commit(commit, branch)
164165
system("git", *base, "merge-base", "--is-ancestor", commit, branch)
165166
end
166167

168+
def redmine_read_json(uri)
169+
JSON(uri.read($openuri_options.merge(REDMINE_API_KEY_HEADER)))
170+
end
171+
172+
def redmine_get(http, uri)
173+
http.get(uri.respond_to?(:request_uri) ? uri.request_uri : uri, REDMINE_API_KEY_HEADER)
174+
end
175+
167176
def show_last_journal(http, uri)
168-
res = http.get("#{uri.path}?include=journals")
177+
res = redmine_get(http, "#{uri.path}?include=journals")
169178
res.value
170179
h = JSON(res.body)
171180
x = h["issue"]
@@ -219,7 +228,7 @@ class CommandSyntaxError < RuntimeError; end
219228
raise CommandSyntaxError unless /\A(\d+)?\z/ =~ args
220229
uri = URI(REDMINE_BASE+'/projects/ruby-master/issues.json?'+URI.encode_www_form(@query.dup.merge('page' => ($1 ? $1.to_i : 1))))
221230
# puts uri
222-
res = JSON(uri.read($openuri_options))
231+
res = redmine_read_json(uri)
223232
@issues = issues = res["issues"]
224233
from = res["offset"] + 1
225234
total = res["total_count"]
@@ -244,7 +253,7 @@ class CommandSyntaxError < RuntimeError; end
244253
end
245254
uri = "#{REDMINE_BASE}/issues/#{id}"
246255
uri = URI(uri+".json?include=children,attachments,relations,changesets,journals")
247-
res = JSON(uri.read($openuri_options))
256+
res = redmine_read_json(uri)
248257
i = res["issue"]
249258
unless i["changesets"]
250259
abort "You don't have view_changesets permission"
@@ -375,7 +384,8 @@ class << @changesets
375384

376385
uri = URI("#{REDMINE_BASE}/issues/#{@issue}.json")
377386
Net::HTTP.start(uri.host, uri.port, http_options) do |http|
378-
res = http.get(uri.path)
387+
res = redmine_get(http, uri)
388+
res.value
379389
data = JSON(res.body)
380390
h = data["issue"]["custom_fields"].find{|x|x["id"]==5}
381391
if h and val = h["value"] and val != ""

0 commit comments

Comments
 (0)