Sha256: 9594423d475c71213c941aa32cb7ebef8d3d50d5930f864a821297cc3dd35658

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

#!/usr/bin/env ruby

require "bundler/inline"
gemfile do
  source "https://rubygems.org"
  gem "multi_repo", require: "multi_repo/cli", path: File.expand_path("..", __dir__)
end

opts = Optimist.options do
  synopsis "Add or remove labels on a set of pull requests."

  opt :prs,    "The list of PRs to merge", :type => :strings, :required => true
  opt :add,    "Labels to add",            :type => :strings, :required => true
  opt :remove, "Labels to remove",         :type => :strings, :required => true

  MultiRepo::CLI.common_options(self, :only => :dry_run)
end

# TODO: Normalize any PR format to `org/repo#pr`
PR_REGEX = %r{^([^/#]+/[^/#]+)#([^/#]+)$}
Optimist.die :prs, "must be in the form `org/repo#pr`" unless opts[:prs].all? { |pr| pr.match?(PR_REGEX) }

def github
  MultiRepo::Service::Github.client
end

def add_labels(repo_name, pr_number, labels:, dry_run:, **_)
  labels = Array(labels)
  if dry_run
    puts "** dry-run: github.add_labels_to_an_issue(#{repo_name.inspect}, #{pr_number.inspect}, #{labels.inspect})".light_black
  else
    github.add_labels_to_an_issue(repo_name, pr_number, labels)
  end
end

def remove_labels(repo_name, pr_number, labels:, dry_run:, **_)
  Array(labels).each do |label|
    remove_label(repo_name, pr_number, label: label, dry_run: dry_run)
  end
end

def remove_label(repo_name, pr_number, label:, dry_run:, **_)
  if dry_run
    puts "** dry-run: github.remove_label(#{repo_name.inspect}, #{pr_number.inspect}, #{label.inspect})".light_black
  else
    github.remove_label(repo_name, pr_number, label)
  end
rescue Octokit::NotFound
  # Ignore labels that are not found, because we want them removed anyway
end

opts[:prs].each do |pr|
  puts MultiRepo::CLI.header(pr)

  repo_name, pr_number = PR_REGEX.match(pr).captures

  add_labels(repo_name, pr_number, labels: opts[:add], **opts)
  remove_labels(repo_name, pr_number, labels: opts[:remove], **opts)

  puts
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
multi_repo-0.4.0 scripts/pull_request_labeler
multi_repo-0.3.1 scripts/pull_request_labeler
multi_repo-0.3.0 scripts/pull_request_labeler
multi_repo-0.2.3 scripts/pull_request_labeler
multi_repo-0.2.2 scripts/pull_request_labeler