joshminnie/hubtrics

View on GitHub
bin/sync_labels

Summary

Maintainability
Test Coverage
#! /usr/bin/env ruby

# frozen_string_literal: true

require 'octokit'
require 'optparse'
require 'yaml'

require_relative '../lib/hubtrics'

parser = Hubtrics::Parser.new(
  banner: "Usage: #{File.basename(__FILE__)} [options]",
  switches: [
    {
      switch: :source,
      definition: ['--source REPOSITORY', String, 'Repository to copy labels from']
    },
    {
      switch: :destination,
      definition: ['--destination REPOSITORY', String, 'Repository to copy labels to']
    }
  ]
)
options = parser.parse(ARGV)

begin
  client = Hubtrics.client(options[:client])

  source_labels = client.labels(options[:source])
  source_labels.each do |label|
    begin
      client.update_label(options[:destination], label.name, color: label.color, description: label.description)
      puts "#{label.name} updated with color ##{label.color}: #{label.description}"
    rescue Octokit::NotFound
      client.add_label(options[:destination], label.name, label.color, description: label.description)
      puts "#{label.name} created with color ##{label.color}: #{label.description}"
    end
  end

  destination_labels = client.labels(options[:destination])
  destination_labels.each do |label|
    unless source_labels.map(&:name).include?(label.name)
      client.delete_label!(options[:destination], label.name)
      puts "#{label.name} was removed"
    end
  end
rescue Octokit::TooManyRequests => e
  puts e.response_headers.inspect
  puts e.methods.inspect
end