Class: KDirector::Dsls::Children::Github

Inherits:
KDirector::Directors::ChildDirector show all
Defined in:
lib/k_director/dsls/children/github.rb

Overview

Github DSL provides useful GitHub actions such as (create, delete, list, open repository).

Instance Attribute Summary

Attributes inherited from KDirector::Directors::ChildDirector

#parent

Attributes inherited from KDirector::Directors::BaseDirector

#builder, #k_builder, #options

Instance Method Summary collapse

Methods inherited from KDirector::Directors::ChildDirector

#debug

Methods inherited from KDirector::Directors::BaseDirector

#active?, #add, #add_clipboard, #add_file, #blueprint, builder_type, #configuration, #data, #debug, #debug_dom, #debug_options, default_builder_type, default_on_action, default_on_exist, defaults, #director_name, #director_name=, #dom, #fadd, #github, #inherited_opts, init, #json_dom, #oadd, #on_action, on_action, #on_exist, on_exist, #package_json, #play_actions, #run_command, #run_script, #set_current_folder_action, #settings, #tadd, #template_base_folder, #typed_dom

Constructor Details

#initialize(parent, **opts) ⇒ Github

Returns a new instance of Github.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/k_director/dsls/children/github.rb', line 12

def initialize(parent, **opts)
  super(parent, **opts)

  defaults = {
    repo_name: opts[:repo_name], # || parent.builder.dom&[:github]&[:repo_name]
    username: opts[:username] || default_github_username, # || parent.builder.dom&[:github]&[:username]
    organization: opts[:organization] # || parent.builder.dom&[:github]&[:organization]
  }

  parent.builder.group_set(:github, **repo_info_hash(**defaults))
end

Instance Method Details

#create_repository(**opts) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/k_director/dsls/children/github.rb', line 53

def create_repository(**opts)
  info = repo_info(**opts)

  if info.repo_name.blank?
    log.error 'Repository name is required'
    return
  end

  repo = create_api.all_repositories.find { |r| r.full_name == info.full_name }

  if repo.nil?
    log.heading 'Repository create'
    log.kv 'Repository Name', info.repo_name
    log.kv 'Repository Full Name', info.full_name
    log.kv 'Organization Name', info.organization if info.organization
    success = create_api.create_repository(info.repo_name, organization: info.organization)
    log.info "Repository: #{info.full_name} created" if success
    log.error "Repository: #{info.full_name} was not created" unless success

    system("open -a 'Google Chrome' #{info.link}") if opts[:open]
  else
    log.warn 'Repository already exists, nothing to create'
  end

  log_repo_info(info)
rescue StandardError => e
  log.exception(e)
end

#delete_repository(**opts) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/k_director/dsls/children/github.rb', line 84

def delete_repository(**opts)
  info = repo_info(**opts)

  if info.repo_name.blank?
    log.error 'Repository name is required'
    return
  end

  repo = create_api.all_repositories.find { |r| r.full_name == info.full_name }

  if repo.present?
    log.heading 'Repository delete'
    log.kv 'Repository Name', info.repo_name
    log.kv 'Repository Full Name', info.full_name
    log.kv 'Organization Name', info.organization if info.organization
    success = delete_api.delete_repository(info.full_name, organization: info.organization)
    log.info "Repository: #{info.full_name} deleted" if success
    log.error "Repository: #{info.full_name} was not deleted" unless success
    # system("open -a 'Google Chrome' #{info.link}") if opts[:open]
  else
    log.warn 'Repository does not exist, nothing to delete'
  end

  log_repo_info(info)
rescue StandardError => e
  log.exception(e)
end

#list_repositoriesObject



36
37
38
39
40
41
42
43
44
# File 'lib/k_director/dsls/children/github.rb', line 36

def list_repositories
  repos = create_api.all_repositories

  KExt::Github::Printer.repositories_as_table repos

  log.kv 'Repository count', repos.length
rescue StandardError => e
  log.exception(e)
end

#open_repository(**opts) ⇒ Object



46
47
48
49
50
# File 'lib/k_director/dsls/children/github.rb', line 46

def open_repository(**opts)
  info = repo_info(**opts)

  system("open -a 'Google Chrome' #{info.link}")
end

#organizationObject



32
33
34
# File 'lib/k_director/dsls/children/github.rb', line 32

def organization
  parent.builder.dom.dig(:github, :organization)
end

#repo_info(**opts) ⇒ Object



132
133
134
# File 'lib/k_director/dsls/children/github.rb', line 132

def repo_info(**opts)
  OpenStruct.new(**repo_info_hash(**opts))
end

#repo_info_hash(**opts) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/k_director/dsls/children/github.rb', line 113

def repo_info_hash(**opts)
  repo_name     = (opts[:repo_name] || self.repo_name).to_s
  username      = opts[:username] || self.username
  organization  = opts[:organization] || self.organization
         = organization || username
  full_name     = [, repo_name].compact.join('/')
  link          = "https://github.com/#{full_name}"
  ssh_link      = "[email protected]:#{full_name}.git"

  {
    repo_name: repo_name,
    full_name: full_name,
    link: link,
    ssh_link: ssh_link,
    username: username,
    organization: organization
  }
end

#repo_nameObject



24
25
26
# File 'lib/k_director/dsls/children/github.rb', line 24

def repo_name
  parent.builder.dom.dig(:github, :repo_name)
end

#usernameObject



28
29
30
# File 'lib/k_director/dsls/children/github.rb', line 28

def username
  parent.builder.dom.dig(:github, :username)
end