octokitted 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3328c9396d35d1150fb91b1c3b54cff09abed713d2f3e131593e421ecd21855d
4
- data.tar.gz: e58aa59c1c25cd6da6659602240761ae174633d4d56b460ca8c2838dc664a0d3
3
+ metadata.gz: df4ab1cbb4cf7f9f197be6f48bf19eadfc34fabaae33433e376a21c4327df0e4
4
+ data.tar.gz: 04c286e8800c55ac4d55e71b3058e12733a498e91c1d4fb0bf9153bb39685bd5
5
5
  SHA512:
6
- metadata.gz: 4bd0054595b6120ccc757665ffe2462f55d94cb1d8c9c987c38dc1ca10c1f56894b043e938223b6b78c526131e3946de48692807bb99f9aac24781601dcd62de
7
- data.tar.gz: ee47262226720898b8b37a925e71e054bc37fddcd627f9ce16415794a4cbd0afd3d5b5fdb7c5d4fe12b47b8b8a36d483195772920b361792fe7c169a1325ccc5
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.2"
5
+ VERSION = "0.0.4"
6
6
  end
7
7
  end
data/octokitted.gemspec CHANGED
@@ -21,6 +21,8 @@ Gem::Specification.new do |spec|
21
21
  "bug_tracker_uri" => "https://github.com/grantbirki/octokitted/issues"
22
22
  }
23
23
 
24
+ spec.add_dependency "contracts", "~> 0.17"
25
+ spec.add_dependency "faraday-retry", "~> 2.2"
24
26
  spec.add_dependency "git", "~> 1.18"
25
27
  spec.add_dependency "octokit", "~> 7.1"
26
28
 
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octokitted
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
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
+ - !ruby/object:Gem::Dependency
14
+ name: contracts
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.17'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday-retry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: git
15
43
  requirement: !ruby/object:Gem::Requirement