# frozen_string_literal: true require "thor" require_relative "../base" module Neetob class CLI module Github module Labels class Upsert < Base WHITE_COLOR_HEX_CODE = "ffffff" attr_accessor :apps, :required_labels_json_file_path, :sandbox, :all_neeto_repos, :name, :color, :description def initialize(apps, required_labels_json_file_path = "", sandbox = false, all_neeto_repos = false, name = "", color = "", description = "") super() @apps = apps @required_labels_json_file_path = required_labels_json_file_path @sandbox = sandbox @all_neeto_repos = all_neeto_repos @name = name @color = color @description = description end def run check_for_apps_and_all_neeto_repos_option(apps, all_neeto_repos) matching_apps = find_all_matching_apps(apps, :github, sandbox, false, all_neeto_repos) inform_about_default_labels_file matching_apps.each do |app| ui.info("\nWorking on #{app} repo\n") begin required_labels = get_required_labels required_labels.each do |label| create_or_update_label(app, label) sleep(1) end rescue StandardError => e ExceptionHandler.new(e).process end end end private def get_required_labels if name.blank? && (!color.blank? || !description.blank?) ui.error("Please provide \"name\" for the label you want to upsert.") exit end if !name.blank? return create_label_payload end read_json_file(required_labels_json_file_path || default_labels_file_path) end def create_label_payload [ { "name" => name, "color" => color, "description" => description }.compact ] end def create_or_update_label(app, label_details) begin client.update_label(app, label_details["name"], label_details) ui.success("Label \"#{label_details["name"]}\" updated successfully") rescue Octokit::NotFound client.add_label( app, label_details["name"], label_details["color"] || WHITE_COLOR_HEX_CODE, label_details) ui.success("Label \"#{label_details["name"]}\" created successfully") rescue StandardError => e ExceptionHandler.new(e).process end end def default_labels_file_path File.expand_path("../../../../../data/github-labels.json", __dir__) end def inform_about_default_labels_file if required_labels_json_file_path.nil? && name.nil? ui.info("Upserting labels from the \"neetob/data/github-labels.json\" file") end end end end end end end