lib/multi_repo/service/github.rb in multi_repo-0.4.0 vs lib/multi_repo/service/github.rb in multi_repo-0.5.0
- old
+ new
@@ -81,10 +81,28 @@
def self.disabled_workflows(repo_name)
client.workflows(repo_name)[:workflows].select { |w| w.state == "disabled_inactivity" }
end
+ PR_REGEX = %r{^([^/#]+/[^/#]+)#(\d+)$}
+
+ # Parse a list of PRs that are in URL or org/repo#pr format into a Array of
+ # [repo_name, pr_number] entries.
+ def self.parse_prs(*prs)
+ prs.flatten.map do |pr|
+ # Normalize to org/repo#pr
+ normalized_pr = pr.sub("https://github.com/", "").sub("/pull/", "#")
+
+ if (match = PR_REGEX.match(normalized_pr))
+ repo_name, pr_number = match.captures
+ [repo_name, pr_number.to_i]
+ else
+ raise ArgumentError, "Invalid PR '#{pr}'. PR must be a GitHub URL or in org/repo#pr format."
+ end
+ end
+ end
+
attr_reader :dry_run
def initialize(dry_run: false)
require "octokit"
@@ -136,10 +154,48 @@
else
client.delete_label!(repo_name, label)
end
end
+ def add_labels_to_an_issue(repo_name, issue_number, labels)
+ labels = Array(labels)
+ if dry_run
+ puts "** dry-run: github.add_labels_to_an_issue(#{repo_name.inspect}, #{issue_number.inspect}, #{labels.inspect})".light_black
+ else
+ client.add_labels_to_an_issue(repo_name, issue_number, labels)
+ end
+ end
+
+ def remove_labels_from_an_issue(repo_name, issue_number, labels)
+ Array(labels).each do |label|
+ if dry_run
+ puts "** dry-run: github.remove_label(#{repo_name.inspect}, #{issue_number.inspect}, #{label.inspect})".light_black
+ else
+ client.remove_label(repo_name, issue_number, label)
+ end
+ rescue Octokit::NotFound
+ # Ignore labels that are not found, because we want them removed anyway
+ end
+ end
+
+ def add_comment(repo_name, issue_number, body)
+ if dry_run
+ puts "** dry-run: github.add_comment(#{repo_name.inspect}, #{issue_number.inspect}, #{body.pretty_inspect.chomp})".light_black
+ else
+ client.add_comment(repo_name, issue_number, body)
+ end
+ end
+
+ def assign_user(repo_name, issue_number, assignee)
+ assignee = assignee[1..] if assignee.start_with?("@")
+ if dry_run
+ puts "** dry-run: github.update_issue(#{repo_name.inspect}, #{issue_number.inspect}, \"assignee\" => #{assignee.inspect})".light_black
+ else
+ client.update_issue(repo_name, issue_number, "assignee" => assignee)
+ end
+ end
+
def create_milestone(repo_name, title, due_on)
if dry_run
puts "** dry-run: github.create_milestone(#{repo_name.inspect}, #{title.inspect}, :due_on => #{due_on.strftime("%Y-%m-%d").inspect})".light_black
else
client.create_milestone(repo_name, title, :due_on => due_on)
@@ -203,9 +259,17 @@
if dry_run
puts "** dry-run: github.put(#{command.inspect})".light_black
else
client.put(command)
+ end
+ end
+
+ def merge_pull_request(repo_name, pr_number)
+ if dry_run
+ puts "** dry-run: github.merge_pull_request(#{repo_name.inspect}, #{pr_number.inspect})".light_black
+ else
+ client.merge_pull_request(repo_name, pr_number)
end
end
def create_or_update_repository_secret(repo_name, key, value)
payload = encode_secret(repo_name, value)