Skip to content

Commit 98abe1d

Browse files
authored
Merge pull request #131 from github/add-domain-change-link.rb
Update the domain name for links in Issue & PR comments
2 parents cceaf5b + df5f9dc commit 98abe1d

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Script to update the domain name for links in issue & pr comments.
2+
require 'octokit'
3+
require 'optparse'
4+
require 'ostruct'
5+
6+
## Check for environment variables
7+
begin
8+
access_token = ENV.fetch("GITHUB_TOKEN")
9+
hostname = ENV.fetch("GITHUB_HOSTNAME")
10+
rescue KeyError
11+
puts
12+
puts "To run this script, please set the following environment variables:"
13+
puts "- GITHUB_TOKEN: A valid access token"
14+
puts "- GITHUB_HOSTNAME: A valid GitHub Enterprise hostname"
15+
exit 1
16+
end
17+
18+
# Set up Octokit
19+
Octokit.configure do |kit|
20+
kit.api_endpoint = "https://#{hostname}/api/v3"
21+
kit.access_token = access_token
22+
kit.auto_paginate = true
23+
end
24+
25+
unless ARGV.length >= 2
26+
puts "Specify domain names to change using the following format:"
27+
puts "- change-domains.rb old_domain new_domain"
28+
exit 1
29+
end
30+
31+
options = OpenStruct.new
32+
options.noop = false
33+
34+
OptionParser.new do |parser|
35+
parser.on("-n", "--noop", "Find the links, but don't update the content.", "Pipe this to a CSV file for a report", "of all links that will be changed.") do |v|
36+
options.noop = v
37+
end
38+
end.parse!
39+
40+
41+
# Extract links to attached files using regexp
42+
# Looks for the raw markdown formatted image link formatted like this:
43+
# [image description](https://media.octodemo.com/user/267/files/e014c3e4-889c-11e6-8637-1f16c810cfe3)
44+
# example pattern = /\[[^\]]*\]\((https:\/\/media.octodemo.com[^\)]*\/files\/[^\)]*)\)/
45+
old_domain = ARGV[0]
46+
new_domain = ARGV[1]
47+
media_pattern = /\[[^\]]*\]\((https:\/\/media.#{old_domain}[^\)]*\/user\/\d*\/files\/[^\)]*)\)/
48+
49+
Octokit.repositories.map{|repo| repo.full_name}.each do |r|
50+
# Extract issues containing links to attached files
51+
issues = Octokit.issues(r, {state: :all}).select do |i|
52+
unless i.body.nil?
53+
i.body.match(media_pattern)
54+
end
55+
end
56+
issues.each do |issue|
57+
# Extract the link pattern from issues' body
58+
matched_links = issue.body.scan(media_pattern)
59+
matched_links.each do |file|
60+
puts "#{issue.html_url},#{file[0]}"
61+
# Rewrite link with "media" subdomain to "/storage" on the new domain
62+
new_link = file[0].gsub("media.#{old_domain}", "#{new_domain}/storage")
63+
new_body = issue.body.gsub(file[0], new_link)
64+
unless options.noop == true
65+
Octokit.update_issue(r, issue.number, :body => new_body)
66+
puts "Updated Issue/PR: #{issue.html_url}"
67+
end
68+
end
69+
end
70+
71+
# Issue comments as well (including pull request comments)
72+
issue_comments = Octokit.issues_comments(r).select do |ic|
73+
unless ic.body.nil?
74+
ic.body.match(media_pattern)
75+
end
76+
end
77+
unless issue_comments.nil?
78+
issue_comments.each do |issue_comment|
79+
matched_links = issue_comment.body.scan(media_pattern)
80+
matched_links.each do |file|
81+
puts "#{issue_comment.html_url},#{file[0]}"
82+
# Rewrite link with "media" subdomain to "/storage" on the new domain
83+
new_link = file[0].gsub("media.#{old_domain}", "#{new_domain}/storage")
84+
new_comment = issue_comment.body.gsub(file[0], new_link)
85+
unless options.noop == true
86+
Octokit.update_comment(r, issue_comment.id, new_comment)
87+
puts "Updated Issue/PR Comment: #{issue_comment.html_url}"
88+
end
89+
end
90+
end
91+
end
92+
93+
# Pull request review comments as well
94+
#
95+
# > Disabled >= v2.8. Issues/PRs and associated comments are included in the above methods.
96+
# > Will need to add Review comments with the next release of Octokit.
97+
# > See https://github.com/octokit/octokit.rb/pull/860 for PR that implements
98+
# > the Preview version of the Review API.
99+
#
100+
# pr_comments = Octokit.pulls_comments(r).select do |prc|
101+
# unless prc.body.nil?
102+
# prc.body.match(media_pattern)
103+
# end
104+
# end
105+
# unless pr_comments.nil?
106+
# pr_comments.each do |pr_comment|
107+
# matched_links = pr_comment.body.scan(media_pattern)
108+
# matched_links.each do |file|
109+
# puts "#{pr_comment.html_url},#{file[0]}"
110+
# end
111+
# end
112+
# end
113+
end

0 commit comments

Comments
 (0)