Skip to content

Commit 3ffb4cf

Browse files
authored
Create change-domains-in-links.rb
1 parent 7090ff0 commit 3ffb4cf

File tree

1 file changed

+95
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)