octokitted 0.0.4 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/octokitted/git_plugin.rb +12 -0
- data/lib/octokitted.rb +39 -5
- data/lib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f50897f06c435796e586cc57693cd25e312344890f209d463df1a967879b36e3
|
4
|
+
data.tar.gz: 7895d7418b7baaf86e33ac9cd457e3d80c9df0b16776cc4a23e9eec2183e02f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32fc230ed082aebecc9c20a046a19dee3f98d1863385506d8cd12b1be5ac86a40bed13a55cabcd561fd0b865db7d7509e34fd4ec34f31993e306812df415aa8c
|
7
|
+
data.tar.gz: 233c1748bfcba805594a9158ec56ef8197d4df432b8024fbf72756d38187b6e9d503e0bb68248b824760333f85e04dd9bf79ae40fead940bdba13d78055c951e
|
@@ -1,11 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "git"
|
4
|
+
require "contracts"
|
4
5
|
|
5
6
|
class GitPlugin
|
6
7
|
attr_reader :login
|
7
8
|
|
9
|
+
include Contracts::Core
|
10
|
+
include Contracts::Builtin
|
11
|
+
|
8
12
|
# Initialize the class
|
13
|
+
Contract KeywordArgs[logger: Any, login: Maybe[String], token: Maybe[String]] => Any
|
9
14
|
def initialize(logger:, login:, token:)
|
10
15
|
@log = logger
|
11
16
|
@login = login
|
@@ -14,25 +19,32 @@ class GitPlugin
|
|
14
19
|
|
15
20
|
# Removes / cleans up all repos that this class has cloned
|
16
21
|
# :param cloned_repos: An array of paths to cloned repos to remove
|
22
|
+
# :return: true to indicate success
|
23
|
+
Contract ArrayOf[String] => true
|
17
24
|
def remove_all_clones!(cloned_repos)
|
18
25
|
@log.debug("removing all cloned repos")
|
19
26
|
cloned_repos.each do |path|
|
20
27
|
@log.debug("removing cloned repo: #{path}")
|
21
28
|
FileUtils.rm_r(path)
|
22
29
|
end
|
30
|
+
true
|
23
31
|
end
|
24
32
|
|
25
33
|
# Removes a single cloned repo
|
26
34
|
# :param path: The path to the cloned repo to remove (String)
|
35
|
+
# :return: true to indicate success
|
36
|
+
Contract String => true
|
27
37
|
def remove_clone!(path)
|
28
38
|
@log.debug("removing cloned repo: #{path}")
|
29
39
|
FileUtils.rm_r(path)
|
40
|
+
return true
|
30
41
|
end
|
31
42
|
|
32
43
|
# Clone a repository
|
33
44
|
# :param path: The relative path to clone the repo to - (default: ".")
|
34
45
|
# :param options: The options to pass to the Git.clone method (default: {} - https://rubydoc.info/gems/git/Git#clone-class_method)
|
35
46
|
# :return: Hash of the Git Object, and the path to the cloned repo
|
47
|
+
Contract KeywordArgs[org: String, repo: String, path: Maybe[String], options: Maybe[Hash]] => Hash
|
36
48
|
def clone(org:, repo:, path: ".", options: {})
|
37
49
|
@log.debug("cloning #{org}/#{repo}")
|
38
50
|
git_object = Git.clone("https://#{@token}@github.com/#{org}/#{repo}.git", repo, path:, log: @log, **options)
|
data/lib/octokitted.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "octokit"
|
4
4
|
require "logger"
|
5
|
+
require "contracts"
|
5
6
|
|
6
7
|
require_relative "octokitted/git_plugin"
|
7
8
|
|
@@ -9,6 +10,9 @@ class Octokitted
|
|
9
10
|
# A Octokitted class to interact with the GitHub API
|
10
11
|
attr_reader :login, :org, :repo, :org_and_repo, :octokit, :cloned_repos
|
11
12
|
|
13
|
+
include Contracts::Core
|
14
|
+
include Contracts::Builtin
|
15
|
+
|
12
16
|
# Initialize the class
|
13
17
|
# :param login: The login to use for GitHubAPI interactions (defaults to the owner of the token)
|
14
18
|
# :param org: The org to use with the Octokitted class
|
@@ -16,6 +20,7 @@ class Octokitted
|
|
16
20
|
# :param token: The token to use to authenticate with the GitHub API
|
17
21
|
# :param logger: The logger to use for logging
|
18
22
|
def initialize(login: nil, org: nil, repo: nil, token: nil, logger: nil)
|
23
|
+
@log = logger || setup_logger
|
19
24
|
@cloned_repos = []
|
20
25
|
org_and_repo_hash = fetch_org_and_repo
|
21
26
|
@login = login
|
@@ -23,7 +28,6 @@ class Octokitted
|
|
23
28
|
@repo = repo || org_and_repo_hash[:repo]
|
24
29
|
@token = token || fetch_token
|
25
30
|
@octokit = setup_octokit_client
|
26
|
-
@log = logger || setup_logger
|
27
31
|
@org_and_repo = org_and_repo_hash[:org_and_repo]
|
28
32
|
@login = @octokit.login if @login.nil? # reset the login to the owner of the token if not provided
|
29
33
|
|
@@ -38,8 +42,9 @@ class Octokitted
|
|
38
42
|
|
39
43
|
# Setter method for the repo instance variable
|
40
44
|
# :param repo: The repo to set
|
41
|
-
# :return:
|
45
|
+
# :return: it does not return as it is a setter method
|
42
46
|
# Example: gh.repo = "test"
|
47
|
+
Contract String => Any
|
43
48
|
def repo=(repo)
|
44
49
|
@repo = repo
|
45
50
|
@org_and_repo = "#{@org}/#{@repo}"
|
@@ -48,8 +53,9 @@ class Octokitted
|
|
48
53
|
|
49
54
|
# Setter method for the org instance variable
|
50
55
|
# :param org: The org to set
|
51
|
-
# :return:
|
56
|
+
# :return: it does not return as it is a setter method
|
52
57
|
# Example: gh.org = "test"
|
58
|
+
Contract String => Any
|
53
59
|
def org=(org)
|
54
60
|
@org = org
|
55
61
|
@org_and_repo = "#{@org}/#{@repo}"
|
@@ -60,20 +66,43 @@ class Octokitted
|
|
60
66
|
# :param path: The relative path to clone the repo to - (default: ".")
|
61
67
|
# :param options: The options to pass (default: {} - https://rubydoc.info/gems/git/Git#clone-class_method)
|
62
68
|
# :return: The Git object to operate with
|
69
|
+
Contract Maybe[String], Maybe[Hash] => Git::Base
|
63
70
|
def clone(path: ".", options: {})
|
64
71
|
result = @git.clone(org: @org, repo: @repo, path:, options:)
|
65
72
|
@cloned_repos << result[:path]
|
66
73
|
return result[:git_object]
|
67
74
|
end
|
68
75
|
|
76
|
+
# Remove a cloned repository
|
77
|
+
# :param path: The relative path to the cloned repo to remove (String)
|
78
|
+
# :return: true to indicate success
|
79
|
+
Contract String => true
|
69
80
|
def remove_clone!(path)
|
81
|
+
valid = false
|
82
|
+
|
83
|
+
# check if the repo exists in the cloned_repos array
|
84
|
+
valid = true if @cloned_repos.include?(path)
|
85
|
+
|
86
|
+
# check if the repo exists in the cloned_repos array with a leading './'
|
87
|
+
if @cloned_repos.include?("./#{path}")
|
88
|
+
valid = true
|
89
|
+
path = "./#{path}" # update the path to include the relative path so the .delete method works
|
90
|
+
end
|
91
|
+
|
92
|
+
raise StandardError, "Not a cloned repository - path: #{path}" unless valid
|
93
|
+
|
70
94
|
@git.remove_clone!(path)
|
71
95
|
@cloned_repos.delete(path)
|
96
|
+
true
|
72
97
|
end
|
73
98
|
|
99
|
+
# Remove all cloned repositories that have been cloned with this instance of Octokitted
|
100
|
+
# :return: true to indicate success
|
101
|
+
Contract None => true
|
74
102
|
def remove_all_clones!
|
75
103
|
@git.remove_all_clones!(@cloned_repos)
|
76
104
|
@cloned_repos = []
|
105
|
+
true
|
77
106
|
end
|
78
107
|
|
79
108
|
private
|
@@ -86,6 +115,7 @@ class Octokitted
|
|
86
115
|
|
87
116
|
# Fetch the org and repo from the environment
|
88
117
|
# :return: A hash containing the org and repo, and the org and repo separately
|
118
|
+
Contract None => HashOf[Symbol, String]
|
89
119
|
def fetch_org_and_repo
|
90
120
|
org_and_repo = ENV.fetch("GITHUB_REPOSITORY", nil)
|
91
121
|
org = nil
|
@@ -98,7 +128,8 @@ class Octokitted
|
|
98
128
|
end
|
99
129
|
|
100
130
|
# fetch the GitHub token from the environment
|
101
|
-
# :return: The GitHub token
|
131
|
+
# :return: The GitHub token or nil if not found
|
132
|
+
Contract None => Maybe[String]
|
102
133
|
def fetch_token
|
103
134
|
# first try to use the OCTOKIT_ACCESS_TOKEN env var if it exists
|
104
135
|
token = ENV.fetch("OCTOKIT_ACCESS_TOKEN", nil) # if running in actions
|
@@ -108,7 +139,10 @@ class Octokitted
|
|
108
139
|
token = ENV.fetch("GITHUB_TOKEN", nil) # if running in actions
|
109
140
|
return token unless token.nil?
|
110
141
|
|
111
|
-
|
142
|
+
# if we get here, we don't have a token - this is okay because we can still do some things...
|
143
|
+
# ... without a token, rate limiting can be an issue
|
144
|
+
@log.warn("No GitHub token found")
|
145
|
+
return nil
|
112
146
|
end
|
113
147
|
|
114
148
|
# Setup an Octokit client
|
data/lib/version.rb
CHANGED