k_ext-github 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/setup ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # frozen_string_literal: true
4
+
5
+ set -euo pipefail
6
+ IFS=$'\n\t'
7
+ set -vx
8
+
9
+ bundle install
10
+
11
+ # Do any other automated setup that you need to do here
Binary file
data/hooks/pre-commit ADDED
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'English'
5
+
6
+ # NOTE: you may need change file permissions
7
+ # chmod +x hooks/pre-commit
8
+
9
+ exit 0 if ARGV.include?('--no-verify')
10
+
11
+ warning_keywords = %w[console.log]
12
+ keywords = %w[binding.pry console.dir byebug debugger]
13
+ files_changed = `git diff-index --name-only HEAD --`.split
14
+
15
+ # puts '----------------------------------------------------------------------'
16
+ # puts remove files changed from the pre-commit checking if they are one of the following files
17
+ # puts '----------------------------------------------------------------------'
18
+ # files_changed = files_changed - ['hooks/pre-commit']
19
+ # files_changed = files_changed - ['hooks/update-version']
20
+
21
+ # byebug may need to be in these files
22
+ files_changed -= ['Gemfile']
23
+ files_changed -= ['Gemfile.lock']
24
+ files_changed -= ['.gitignore']
25
+ files_changed -= ['README.md']
26
+
27
+ files_changed = files_changed.reject { |f| f.downcase.end_with?('.json') }
28
+ files_changed = files_changed.reject { |f| f.downcase.end_with?('.yml') }
29
+
30
+ # ignore files from specific folders
31
+
32
+ file_groups = files_changed.select do |item|
33
+ item.start_with?('hooks') # ||
34
+ # item.start_with?('lib/generators')
35
+ end
36
+
37
+ files_changed -= file_groups
38
+
39
+ # remove files that are changed because they are deleted
40
+ files_changed = files_changed.select { |filename| File.file?(filename) }
41
+
42
+ # puts '----------------------------------------------------------------------'
43
+ # puts 'Files Changed'
44
+ # puts '----------------------------------------------------------------------'
45
+ # puts files_changed
46
+ # puts '----------------------------------------------------------------------'
47
+
48
+ unless files_changed.length.zero?
49
+ # puts "#{keywords.join('|')}"
50
+ # puts "#{files_changed.join(' ')}"
51
+
52
+ `git grep -q -E "#{warning_keywords.join('|')}" #{files_changed.join(' ')}`
53
+
54
+ if $CHILD_STATUS.exitstatus.zero?
55
+ puts '' # Check following lines:''
56
+ puts $CHILD_STATUS.exitstatus
57
+ files_changed.each do |file|
58
+ warning_keywords.each do |keyword|
59
+ # puts "#{keyword} ::: #{file}"
60
+ `git grep -q -E #{keyword} #{file}`
61
+ if $CHILD_STATUS.exitstatus.zero?
62
+ line = `git grep -n #{keyword} #{file} | awk -F ":" '{print $2}'`.split.join(', ')
63
+ puts "WARNING:\t\033[31m#{file}\033[0m contains #{keyword} at line \033[33m#{line}\033[0m."
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ `git grep -q -E "#{keywords.join('|')}" #{files_changed.join(' ')}`
70
+
71
+ if $CHILD_STATUS.exitstatus.zero?
72
+ puts '' # Check following lines:''
73
+ puts $CHILD_STATUS.exitstatus
74
+ files_changed.each do |file|
75
+ keywords.each do |keyword|
76
+ # puts "#{keyword} ::: #{file}"
77
+ `git grep -q -E #{keyword} #{file}`
78
+ if $CHILD_STATUS.exitstatus.zero?
79
+ line = `git grep -n #{keyword} #{file} | awk -F ":" '{print $2}'`.split.join(', ')
80
+ puts "ERROR :\t\033[31m#{file}\033[0m contains #{keyword} at line \033[33m#{line}\033[0m."
81
+ end
82
+ end
83
+ end
84
+ puts '# Force commit with --no-verify'
85
+ exit 1
86
+ end
87
+ end
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # NOTE: you may need change file permissions
5
+ # chmod +x hooks/update-version
6
+
7
+ exit 1 if ARGV.empty?
8
+
9
+ version = ARGV[0]
10
+ version = version[1..-1] # revoke 'v' character, e.g. v0.1.1 becomes 0.1.1
11
+
12
+ namespaces = %w[KExt Github]
13
+
14
+ indent = 0
15
+ output = ['# frozen_string_literal: true', '']
16
+
17
+ namespaces.each do |namespace|
18
+ output.push "#{' ' * indent}module #{namespace}"
19
+ indent += 1
20
+ end
21
+
22
+ output.push "#{' ' * indent}VERSION = \'#{version}'"
23
+ indent -= 1
24
+
25
+ namespaces.each do
26
+ output.push "#{' ' * indent}end"
27
+ indent -= 1
28
+ end
29
+
30
+ output.push('')
31
+
32
+ printf "%-25<label>s : %<version>s\n", label: 'GEM VERSION', version: version
33
+ File.open('lib/k_ext/github/version.rb', 'w+') { |f| f.write(output.join("\n")) }
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/k_ext/github/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.required_ruby_version = '>= 2.5'
7
+ spec.name = 'k_ext-github'
8
+ spec.version = KExt::Github::VERSION
9
+ spec.authors = ['David Cruwys']
10
+ spec.email = ['david@ideasmen.com.au']
11
+
12
+ spec.summary = 'KExt/Github provides useful github extensions specifically for the klueless code generator'
13
+ spec.description = <<-TEXT
14
+ KExt/Github provides useful github extensions specifically for the klueless code generator
15
+ TEXT
16
+ spec.homepage = 'http://appydave.com/gems/k-ext-github'
17
+ spec.license = 'MIT'
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.' unless spec.respond_to?(:metadata)
22
+
23
+ # spec.metadata['allowed_push_host'] = "Set to 'http://mygemserver.com'"
24
+
25
+ spec.metadata['homepage_uri'] = spec.homepage
26
+ spec.metadata['source_code_uri'] = 'https://github.com/klueless-io/k_ext-github'
27
+ spec.metadata['changelog_uri'] = 'https://github.com/klueless-io/k_ext-github/commits/master'
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the RubyGem files that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
32
+ `git ls-files -z`.split("\x0").reject do |f|
33
+ f.match(%r{^(test|spec|features)/})
34
+ end
35
+ end
36
+ spec.bindir = 'exe'
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = ['lib']
39
+ # spec.extensions = ['ext/k_ext_github/extconf.rb']
40
+
41
+ spec.add_dependency 'k_log', '~> 0.0.0'
42
+ spec.add_dependency 'k_type', '~> 0.0.0'
43
+ # spec.add_dependency 'k_util' , '~> 0.0.0'
44
+ spec.add_dependency 'octokit', '~> 4.0'
45
+
46
+ spec.add_dependency 'virtus', '~> 1.0.5'
47
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'virtus'
4
+ require 'k_log'
5
+
6
+ require 'k_ext/github/version'
7
+ require 'k_ext/github/configuration'
8
+ require 'k_ext/github/models/hook'
9
+ require 'k_ext/github/models/owner'
10
+ require 'k_ext/github/models/repository'
11
+ require 'k_ext/github/printer'
12
+ require 'k_ext/github/api'
13
+
14
+ module KExt
15
+ module Github
16
+ # raise KExt::Github::Error, 'Sample message'
17
+ class Error < StandardError; end
18
+
19
+ class << self
20
+ def configuration
21
+ @configuration ||= KExt::Github::Configuration.new
22
+ end
23
+
24
+ def reset
25
+ @configuration = KExt::Github::Configuration.new
26
+ end
27
+
28
+ def configure
29
+ yield(configuration)
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ if ENV['KLUE_DEBUG']&.to_s&.downcase == 'true'
36
+ namespace = 'KExt::Github::Version'
37
+ file_path = $LOADED_FEATURES.find { |f| f.include?('k_ext/github/version') }
38
+ version = KExt::Github::VERSION.ljust(9)
39
+ puts "#{namespace.ljust(35)} : #{version.ljust(9)} : #{file_path}"
40
+ end
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KExt
4
+ module Github
5
+ # Print formatted log for GitHub data such as repositories
6
+ require 'octokit'
7
+
8
+ # The API provides a limited set of service methods for working against GitHub
9
+ class Api
10
+ include KLog::Logging
11
+ include Virtus.model
12
+
13
+ # attribute :client_id, String, :writer => :private
14
+ # attribute :secret, String, :writer => :private
15
+
16
+ # attribute :sites, Array[Netlify::Site], :writer => :private
17
+ attribute :client, Octokit::Client, writer: :private
18
+
19
+ def self.instance(access_token)
20
+ GithubApi.new(token: access_token)
21
+ end
22
+
23
+ # Create API for communicating with GitHub
24
+ #
25
+ # Provide token OR login/password
26
+ #
27
+ # Create a token here
28
+ # https://github.com/settings/tokens/new
29
+ #
30
+ # @param [String] token
31
+ # @param [String] login
32
+ # @param [String] password
33
+ def initialize(token: nil, login: nil, password: nil)
34
+ auth(token: token, login: login, password: password) if token || login || password
35
+ end
36
+
37
+ # Authenticate against GitHub with username and password or token
38
+ #
39
+ # Provide token OR login/password
40
+ #
41
+ # Create a token here
42
+ # https://github.com/settings/tokens/new
43
+ # @param [String] token
44
+ # @param [String] login
45
+ # @param [String] password
46
+ def auth(token: nil, login: nil, password: nil)
47
+ raise 'Provide credentials. Token or username/password' if token.nil? && (login.nil? || password.nil?)
48
+
49
+ self.client = if token.nil?
50
+ Octokit::Client.new(login: login, password: password)
51
+ else
52
+ Octokit::Client.new(access_token: token)
53
+ end
54
+
55
+ # Fetch the current user
56
+ log.kv 'GitHub User', client.user.name
57
+ end
58
+
59
+ # --------------------------------------------------------------------------------
60
+ # Service Actions
61
+ # --------------------------------------------------------------------------------
62
+
63
+ # list of repositories for this user
64
+ #
65
+ def repositories
66
+ items = @client.repositories({}, query: { per_page: 100 })
67
+
68
+ items.map { |item| KExt::Github::Models::Repository.new(item) }
69
+ end
70
+
71
+ # create repository
72
+ #
73
+ # @param [String] repository_name e.g. klueless-io/z-test-aerial.com
74
+ def create_repository(repository_name, **options)
75
+ @client.create_repository(repository_name, options)
76
+ end
77
+
78
+ # delete repository
79
+ #
80
+ # @param [String] repository_name e.g. klueless-io/z-test-aerial.com
81
+ def delete_repository(repository_name, **options)
82
+ @client.delete_repository(repository_name, **options)
83
+ end
84
+
85
+ # list of hooks for repository
86
+ #
87
+ # @param [String] repository_name e.g. klueless-io/z-test-aerial.com
88
+ def hooks(repository_name)
89
+ items = @client.hooks(repository_name)
90
+
91
+ items.map { |item| KExt::Github::Models::Hook.new(item) }
92
+ end
93
+
94
+ # list of repositories for organization
95
+ #
96
+ # @param [String] org_name Organization name
97
+ def organization_repositories(org_name)
98
+ items = @client.repositories(org_name, query: { per_page: 100 })
99
+
100
+ items.map { |item| KExt::Github::Models::Repository.new(item) }
101
+ end
102
+ # organization_repositories(org, options = {}) => Array<Sawyer::Resource> (also: #org_repositories, #org_repos)
103
+
104
+ # remove hook from repository by id
105
+ #
106
+ # @param [String] repository_name e.g. klueless-io/z-test-aerial.com
107
+ # @param [Integer] id hook ID
108
+ def remove_hook(repository_name, id)
109
+ @client.remove_hook(repository_name, id)
110
+ end
111
+
112
+ # remove all hooks in a repository
113
+ #
114
+ # @param [String] repository_name e.g. klueless-io/z-test-aerial.com
115
+ def remove_hooks(repository_name)
116
+ hooks = hooks(repository_name)
117
+
118
+ hooks.each do |hook|
119
+ @client.remove_hook(repository_name, hook.id)
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KExt
4
+ module Github
5
+ # Configuration class for KExt::Github
6
+ class Configuration
7
+ attr_accessor :user
8
+ attr_accessor :personal_access_token
9
+ attr_accessor :personal_access_token_delete
10
+
11
+ def initialize
12
+ @user = ENV['GITHUB_USER']
13
+ @personal_access_token = ENV['GITHUB_PERSONAL_ACCESS_TOKEN']
14
+ @personal_access_token_delete = ENV['GITHUB_PERSONAL_ACCESS_TOKEN_DELETE']
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KExt
4
+ module Github
5
+ module Models
6
+ # This model represents a GitHub hook
7
+ class Hook
8
+ include Virtus.model
9
+
10
+ def initialize(attributes = nil)
11
+ # Virtus will take your attributes and match them to the attribute definitions listed below
12
+ super(attributes)
13
+ end
14
+
15
+ attribute :type, String
16
+ attribute :id, String
17
+ attribute :name, String
18
+ attribute :active, String
19
+ attribute :events, String
20
+ attribute :config, Hash
21
+ attribute :updated_at, String
22
+ attribute :created_at, String
23
+ attribute :url, String
24
+ attribute :test_url, String
25
+ attribute :ping_url, String
26
+ attribute :last_response, String
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KExt
4
+ module Github
5
+ module Models
6
+ # This model represents a GitHub owner
7
+ class Owner
8
+ include Virtus.model
9
+
10
+ def initialize(attributes = nil)
11
+ # Virtus will take your attributes and match them to the attribute definitions listed below
12
+ super(attributes)
13
+ end
14
+
15
+ attribute :login, String
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KExt
4
+ module Github
5
+ module Models
6
+ # This model represents a GitHub Repository
7
+ class Repository
8
+ include Virtus.model
9
+
10
+ def initialize(attributes = nil)
11
+ # Virtus will take your attributes and match them to the attribute definitions listed below
12
+ super(attributes)
13
+ end
14
+
15
+ attribute :id, String
16
+ attribute :node_id, String
17
+ attribute :name, String
18
+ attribute :full_name, String
19
+ attribute :private, String
20
+ attribute :description, String
21
+ attribute :url, String
22
+ attribute :created_at, String
23
+ attribute :updated_at, String
24
+ attribute :pushed_at, String
25
+ attribute :git_url, String
26
+ attribute :owner, KExt::Github::Models::Owner
27
+ end
28
+ end
29
+ end
30
+ end