Sha256: d1f8926ef69fc8d85a1f85c719db7dd44969d38e29108b99a9c861107f2e121d

Contents?: true

Size: 2 KB

Versions: 3

Compression:

Stored size: 2 KB

Contents

# 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

          def initialize(apps, required_labels_json_file_path = "", sandbox = false)
            super()
            @apps = apps
            @required_labels_json_file_path = required_labels_json_file_path
            @sandbox = sandbox
          end

          def run
            matching_apps = find_all_matching_apps(apps, :github, sandbox)
            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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
neetob-0.1.2 lib/neetob/cli/github/labels/upsert.rb
neetob-0.1.1 lib/neetob/cli/github/labels/upsert.rb
neetob-0.1.0 lib/neetob/cli/github/labels/upsert.rb