# frozen_string_literal: true require "thor" require_relative "../base" module Neetob class CLI module Github module Labels class Upsert < Base attr_accessor :apps, :required_labels_json_file_path, :sandbox, :all_neeto_repos def initialize(apps, required_labels_json_file_path = "", sandbox = false, all_neeto_repos = false) super() @apps = apps @required_labels_json_file_path = required_labels_json_file_path @sandbox = sandbox @all_neeto_repos = all_neeto_repos 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("\n Working on #{app} repo \n") begin required_labels = read_json_file(required_labels_json_file_path || default_labels_file_path) required_labels.each do |label| create_or_update_label(app, label) end rescue StandardError => e ExceptionHandler.new(e).process end end end private 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"], label_details) ui.success("Label \"#{label_details["name"]}\" created successfully") 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? ui.info("Upserting labels from the \"neetob/data/github-labels.json\" file") end end end end end end end