octokitted 0.0.3 → 0.0.5

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: 79ee296a98c30f97fd70b08cf0c5c4d907425bb03047c315796b48fc171f461f
4
- data.tar.gz: 952ad085cde53de7abcae1be6292e2a4e3b9d72af876b772e67fa0013e37f734
3
+ metadata.gz: 0f1a1eec4292305ffb36ee14107f171a44eb4c4ec36b681ac1cbb43239fee2ca
4
+ data.tar.gz: f11e7f4e512223b975247f83720d6d9a465f5126e70e8928007c8be2e01fb350
5
5
  SHA512:
6
- metadata.gz: 5e3e6310f04d682ffaeb5077e707cb11fd1c4a3ad691614798a775e09798cb52d6c42c99dd7577c6043593eb06e55ded2bfb0f1bb53421288000c63d2bf17609
7
- data.tar.gz: 65780350c6e442466e07e83008b96a8ad49acea371c2de44b41a8a99b5dfce9c973febaecdf9a25fc057cc105c6d2a8d5d022cc31d978f31b371885369dd31f7
6
+ metadata.gz: 520b1c0f16cb7ff369e287eeebeb8c27efbca078ca31fc16825682dd4bc4cd30594532c63eaf76787af5245092fa5a092a7c71f1e0faa07ea1efc48503ec973a
7
+ data.tar.gz: f2b6bf07d1a3463b960daaa02aded2db981bb0640affb5b87afc78a3c7726040f907e915bb3e3c2796c404b35ec9c3d08c666e6347c8ba54dde612eaa7ec3014
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "git"
4
+
5
+ class GitPlugin
6
+ attr_reader :login
7
+
8
+ # Initialize the class
9
+ def initialize(logger:, login:, token:)
10
+ @log = logger
11
+ @login = login
12
+ @token = token
13
+ end
14
+
15
+ # Removes / cleans up all repos that this class has cloned
16
+ # :param cloned_repos: An array of paths to cloned repos to remove
17
+ def remove_all_clones!(cloned_repos)
18
+ @log.debug("removing all cloned repos")
19
+ cloned_repos.each do |path|
20
+ @log.debug("removing cloned repo: #{path}")
21
+ FileUtils.rm_r(path)
22
+ end
23
+ end
24
+
25
+ # Removes a single cloned repo
26
+ # :param path: The path to the cloned repo to remove (String)
27
+ def remove_clone!(path)
28
+ @log.debug("removing cloned repo: #{path}")
29
+ FileUtils.rm_r(path)
30
+ end
31
+
32
+ # Clone a repository
33
+ # :param path: The relative path to clone the repo to - (default: ".")
34
+ # :param options: The options to pass to the Git.clone method (default: {} - https://rubydoc.info/gems/git/Git#clone-class_method)
35
+ # :return: Hash of the Git Object, and the path to the cloned repo
36
+ def clone(org:, repo:, path: ".", options: {})
37
+ @log.debug("cloning #{org}/#{repo}")
38
+ git_object = Git.clone("https://#{@token}@github.com/#{org}/#{repo}.git", repo, path:, log: @log, **options)
39
+
40
+ # configure the git environment
41
+ git_object.config("user.name", @login)
42
+ git_object.config("user.email", "#{@login}@github.com")
43
+
44
+ repo_path = File.join(path, repo)
45
+ return { git_object:, path: repo_path }
46
+ rescue StandardError => e
47
+ # Remove token from error to prevent token leak
48
+ raise e, e.message.gsub(@token, "REDACTED_TOKEN")
49
+ end
50
+ end
data/lib/octokitted.rb CHANGED
@@ -3,9 +3,11 @@
3
3
  require "octokit"
4
4
  require "logger"
5
5
 
6
+ require_relative "octokitted/git_plugin"
7
+
6
8
  class Octokitted
7
9
  # A Octokitted class to interact with the GitHub API
8
- attr_reader :login, :org, :repo, :org_and_repo, :client
10
+ attr_reader :login, :org, :repo, :org_and_repo, :octokit, :cloned_repos
9
11
 
10
12
  # Initialize the class
11
13
  # :param login: The login to use for GitHubAPI interactions (defaults to the owner of the token)
@@ -14,19 +16,22 @@ class Octokitted
14
16
  # :param token: The token to use to authenticate with the GitHub API
15
17
  # :param logger: The logger to use for logging
16
18
  def initialize(login: nil, org: nil, repo: nil, token: nil, logger: nil)
19
+ @cloned_repos = []
17
20
  org_and_repo_hash = fetch_org_and_repo
18
21
  @login = login
19
22
  @org = org || org_and_repo_hash[:org]
20
23
  @repo = repo || org_and_repo_hash[:repo]
21
24
  @token = token || fetch_token
22
- @client = setup_client
25
+ @octokit = setup_octokit_client
23
26
  @log = logger || setup_logger
24
27
  @org_and_repo = org_and_repo_hash[:org_and_repo]
28
+ @login = @octokit.login if @login.nil? # reset the login to the owner of the token if not provided
25
29
 
26
- @login = @client.login if @login.nil? # reset the login to the owner of the token if not provided
30
+ # setup the git plugin
31
+ @git = GitPlugin.new(logger: @log, login: @login, token: @token)
27
32
 
28
33
  @log.debug("Octokitted initialized")
29
- @log.debug("login: #{@client.login}")
34
+ @log.debug("login: #{@octokit.login}")
30
35
  @log.debug("org: #{@org}")
31
36
  @log.debug("repo: #{@repo}")
32
37
  end
@@ -51,6 +56,39 @@ class Octokitted
51
56
  @log.debug("updated org/repo: #{@org_and_repo}")
52
57
  end
53
58
 
59
+ # Clone the currently set owner/repo repository
60
+ # :param path: The relative path to clone the repo to - (default: ".")
61
+ # :param options: The options to pass (default: {} - https://rubydoc.info/gems/git/Git#clone-class_method)
62
+ # :return: The Git object to operate with
63
+ def clone(path: ".", options: {})
64
+ result = @git.clone(org: @org, repo: @repo, path:, options:)
65
+ @cloned_repos << result[:path]
66
+ return result[:git_object]
67
+ end
68
+
69
+ def remove_clone!(path)
70
+ valid = false
71
+
72
+ # check if the repo exists in the cloned_repos array
73
+ valid = true if @cloned_repos.include?(path)
74
+
75
+ # check if the repo exists in the cloned_repos array with a leading './'
76
+ if @cloned_repos.include?("./#{path}")
77
+ valid = true
78
+ path = "./#{path}" # update the path to include the relative path so the .delete method works
79
+ end
80
+
81
+ raise StandardError, "Not a cloned repository - path: #{path}" unless valid
82
+
83
+ @git.remove_clone!(path)
84
+ @cloned_repos.delete(path)
85
+ end
86
+
87
+ def remove_all_clones!
88
+ @git.remove_all_clones!(@cloned_repos)
89
+ @cloned_repos = []
90
+ end
91
+
54
92
  private
55
93
 
56
94
  # construct a logger for the class
@@ -88,7 +126,7 @@ class Octokitted
88
126
 
89
127
  # Setup an Octokit client
90
128
  # :return: An Octokit client
91
- def setup_client
129
+ def setup_octokit_client
92
130
  Octokit::Client.new(
93
131
  access_token: @token,
94
132
  login: @login,
data/lib/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Octokitted
4
4
  module Version
5
- VERSION = "0.0.3"
5
+ VERSION = "0.0.5"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octokitted
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grant Birkinbine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-15 00:00:00.000000000 Z
11
+ date: 2023-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: contracts