Skip to content

Commit ed64ddf

Browse files
authored
Add --noop option
Enables a command line option to make this a read-only operation. Can be piped to a CSV to generate a report containing the Issue/PR URLs and all links that matched the specified regex.
1 parent 9518b8c commit ed64ddf

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

api/ruby/enterprise/change-domains-in-links.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Script to update the domain name for links in issue & pr comments.
22
require 'octokit'
3+
require 'optparse'
4+
require 'ostruct'
35

46
## Check for environment variables
57
begin
@@ -20,12 +22,22 @@
2022
kit.auto_paginate = true
2123
end
2224

23-
unless ARGV.length == 2
25+
unless ARGV.length >= 2
2426
puts "Specify domain names to change using the following format:"
2527
puts "- change-domains.rb old_domain new_domain"
2628
exit 1
2729
end
2830

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. \n\t\t\t\t\s\s\s\s\sPipe 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+
2941
# Extract links to attached files using regexp
3042
# Looks for the raw markdown formatted image link formatted like this:
3143
# [image description](https://media.octodemo.com/user/267/files/e014c3e4-889c-11e6-8637-1f16c810cfe3)
@@ -49,8 +61,10 @@
4961
# Rewrite link with "media" subdomain to "/storage" on the new domain
5062
new_link = file[0].gsub("media.#{old_domain}", "#{new_domain}/storage")
5163
new_body = issue.body.gsub(file[0], new_link)
52-
Octokit.update_issue(r, issue.number, :body => new_body)
53-
puts "Updated Issue/PR: #{issue.html_url}"
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
5468
end
5569
end
5670

@@ -68,8 +82,10 @@
6882
# Rewrite link with "media" subdomain to "/storage" on the new domain
6983
new_link = file[0].gsub("media.#{old_domain}", "#{new_domain}/storage")
7084
new_comment = issue_comment.body.gsub(file[0], new_link)
71-
Octokit.update_comment(r, issue_comment.id, new_comment)
72-
puts "Updated Issue/PR Comment: #{issue_comment.html_url}"
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
7389
end
7490
end
7591
end

0 commit comments

Comments
 (0)