|
1 | 1 | # Script to update the domain name for links in issue & pr comments. |
2 | 2 | require 'octokit' |
| 3 | +require 'optparse' |
| 4 | +require 'ostruct' |
3 | 5 |
|
4 | 6 | ## Check for environment variables |
5 | 7 | begin |
|
20 | 22 | kit.auto_paginate = true |
21 | 23 | end |
22 | 24 |
|
23 | | -unless ARGV.length == 2 |
| 25 | +unless ARGV.length >= 2 |
24 | 26 | puts "Specify domain names to change using the following format:" |
25 | 27 | puts "- change-domains.rb old_domain new_domain" |
26 | 28 | exit 1 |
27 | 29 | end |
28 | 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. \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 | + |
29 | 41 | # Extract links to attached files using regexp |
30 | 42 | # Looks for the raw markdown formatted image link formatted like this: |
31 | 43 | # [image description](https://media.octodemo.com/user/267/files/e014c3e4-889c-11e6-8637-1f16c810cfe3) |
|
49 | 61 | # Rewrite link with "media" subdomain to "/storage" on the new domain |
50 | 62 | new_link = file[0].gsub("media.#{old_domain}", "#{new_domain}/storage") |
51 | 63 | 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 |
54 | 68 | end |
55 | 69 | end |
56 | 70 |
|
|
68 | 82 | # Rewrite link with "media" subdomain to "/storage" on the new domain |
69 | 83 | new_link = file[0].gsub("media.#{old_domain}", "#{new_domain}/storage") |
70 | 84 | 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 |
73 | 89 | end |
74 | 90 | end |
75 | 91 | end |
|
0 commit comments