octokitted 0.0.3 → 0.0.4

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: df4ab1cbb4cf7f9f197be6f48bf19eadfc34fabaae33433e376a21c4327df0e4
4
+ data.tar.gz: 04c286e8800c55ac4d55e71b3058e12733a498e91c1d4fb0bf9153bb39685bd5
5
5
  SHA512:
6
- metadata.gz: 5e3e6310f04d682ffaeb5077e707cb11fd1c4a3ad691614798a775e09798cb52d6c42c99dd7577c6043593eb06e55ded2bfb0f1bb53421288000c63d2bf17609
7
- data.tar.gz: 65780350c6e442466e07e83008b96a8ad49acea371c2de44b41a8a99b5dfce9c973febaecdf9a25fc057cc105c6d2a8d5d022cc31d978f31b371885369dd31f7
6
+ metadata.gz: cd75ef4dbe2385bc1324a9b5762e0a542a5981f0195704a0a5b21a5d602b022ac61721e86d462540c464a7b193b3600b57579b8cfe0dde46e3dcbe1a78a14ad0
7
+ data.tar.gz: 933e1cc9a5c42a650c3849ace20e8bea0cdc3e0a1855d5bb0eb2bc3b2c409da6041297441d70477fd78dab6b814dfe76d5f4fcd11552f50dd161a0784b37e84f
@@ -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,26 @@ 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
+ @git.remove_clone!(path)
71
+ @cloned_repos.delete(path)
72
+ end
73
+
74
+ def remove_all_clones!
75
+ @git.remove_all_clones!(@cloned_repos)
76
+ @cloned_repos = []
77
+ end
78
+
54
79
  private
55
80
 
56
81
  # construct a logger for the class
@@ -88,7 +113,7 @@ class Octokitted
88
113
 
89
114
  # Setup an Octokit client
90
115
  # :return: An Octokit client
91
- def setup_client
116
+ def setup_octokit_client
92
117
  Octokit::Client.new(
93
118
  access_token: @token,
94
119
  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.4"
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.4
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