octokitted 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f1a1eec4292305ffb36ee14107f171a44eb4c4ec36b681ac1cbb43239fee2ca
4
- data.tar.gz: f11e7f4e512223b975247f83720d6d9a465f5126e70e8928007c8be2e01fb350
3
+ metadata.gz: f50897f06c435796e586cc57693cd25e312344890f209d463df1a967879b36e3
4
+ data.tar.gz: 7895d7418b7baaf86e33ac9cd457e3d80c9df0b16776cc4a23e9eec2183e02f2
5
5
  SHA512:
6
- metadata.gz: 520b1c0f16cb7ff369e287eeebeb8c27efbca078ca31fc16825682dd4bc4cd30594532c63eaf76787af5245092fa5a092a7c71f1e0faa07ea1efc48503ec973a
7
- data.tar.gz: f2b6bf07d1a3463b960daaa02aded2db981bb0640affb5b87afc78a3c7726040f907e915bb3e3c2796c404b35ec9c3d08c666e6347c8ba54dde612eaa7ec3014
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: the new org/repo
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: the new org/repo
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,12 +66,17 @@ 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)
70
81
  valid = false
71
82
 
@@ -82,11 +93,16 @@ class Octokitted
82
93
 
83
94
  @git.remove_clone!(path)
84
95
  @cloned_repos.delete(path)
96
+ true
85
97
  end
86
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
87
102
  def remove_all_clones!
88
103
  @git.remove_all_clones!(@cloned_repos)
89
104
  @cloned_repos = []
105
+ true
90
106
  end
91
107
 
92
108
  private
@@ -99,6 +115,7 @@ class Octokitted
99
115
 
100
116
  # Fetch the org and repo from the environment
101
117
  # :return: A hash containing the org and repo, and the org and repo separately
118
+ Contract None => HashOf[Symbol, String]
102
119
  def fetch_org_and_repo
103
120
  org_and_repo = ENV.fetch("GITHUB_REPOSITORY", nil)
104
121
  org = nil
@@ -111,7 +128,8 @@ class Octokitted
111
128
  end
112
129
 
113
130
  # fetch the GitHub token from the environment
114
- # :return: The GitHub token
131
+ # :return: The GitHub token or nil if not found
132
+ Contract None => Maybe[String]
115
133
  def fetch_token
116
134
  # first try to use the OCTOKIT_ACCESS_TOKEN env var if it exists
117
135
  token = ENV.fetch("OCTOKIT_ACCESS_TOKEN", nil) # if running in actions
@@ -121,7 +139,10 @@ class Octokitted
121
139
  token = ENV.fetch("GITHUB_TOKEN", nil) # if running in actions
122
140
  return token unless token.nil?
123
141
 
124
- raise "No GitHub token found"
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
125
146
  end
126
147
 
127
148
  # Setup an Octokit client
data/lib/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Octokitted
4
4
  module Version
5
- VERSION = "0.0.5"
5
+ VERSION = "0.0.6"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octokitted
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grant Birkinbine