Class: Bard::Github

Inherits:
Struct
  • Object
show all
Includes:
CI::Retryable
Defined in:
lib/bard/github.rb

Constant Summary

Constants included from CI::Retryable

CI::Retryable::INITIAL_DELAY, CI::Retryable::MAX_RETRIES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CI::Retryable

#retry_with_backoff

Constructor Details

#initialize(project_name, api_key: nil) ⇒ Github

Returns a new instance of Github.



11
12
13
14
# File 'lib/bard/github.rb', line 11

def initialize(project_name, api_key: nil)
  super(project_name)
  @api_key = api_key
end

Instance Attribute Details

#project_nameObject

Returns the value of attribute project_name

Returns:

  • (Object)

    the current value of project_name



8
9
10
# File 'lib/bard/github.rb', line 8

def project_name
  @project_name
end

Instance Method Details

#add_deploy_key(title:, key:) ⇒ Object



60
61
62
# File 'lib/bard/github.rb', line 60

def add_deploy_key title:, key:
  post("keys", title:, key:)
end

#add_master_branch_protectionObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bard/github.rb', line 84

def add_master_branch_protection
  put("branches/master/protection", {
    required_status_checks: {
      strict: false,
      contexts: [],
    },
    enforce_admins: nil,
    required_pull_request_reviews: nil,
    restrictions: nil,
  })
end

#add_master_key(master_key) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bard/github.rb', line 64

def add_master_key master_key
  response = get("actions/secrets/public-key")
  public_key, public_key_id = response.values_at("key", "key_id")

  def encrypt_secret(encoded_public_key, secret)
    decoded_key = Base64.decode64(encoded_public_key)
    public_key = RbNaCl::PublicKey.new(decoded_key)
    box = RbNaCl::Boxes::Sealed.from_public_key(public_key)
    encrypted_secret = box.encrypt(secret)
    Base64.strict_encode64(encrypted_secret)
  end

  encrypted_master_key = encrypt_secret(public_key, master_key)

  put("actions/secrets/RAILS_MASTER_KEY", {
    encrypted_value: encrypted_master_key,
    key_id: public_key_id,
  })
end

#create_repoObject



96
97
98
99
100
101
# File 'lib/bard/github.rb', line 96

def create_repo
  post("https://api.github.com/orgs/botandrosedesign/repos", {
    name: project_name,
    private: true,
  })
end

#delete(path, params = {}) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/bard/github.rb', line 47

def delete path, params={}
  request(path) do |uri|
    Net::HTTP::Delete.new(uri).tap do |r|
      r.body = JSON.dump(params)
    end
  end
end

#delete_repoObject



103
104
105
# File 'lib/bard/github.rb', line 103

def delete_repo
  delete(nil)
end

#encrypt_secret(encoded_public_key, secret) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/bard/github.rb', line 68

def encrypt_secret(encoded_public_key, secret)
  decoded_key = Base64.decode64(encoded_public_key)
  public_key = RbNaCl::PublicKey.new(decoded_key)
  box = RbNaCl::Boxes::Sealed.from_public_key(public_key)
  encrypted_secret = box.encrypt(secret)
  Base64.strict_encode64(encrypted_secret)
end

#get(path, params = {}) ⇒ Object



16
17
18
19
20
21
# File 'lib/bard/github.rb', line 16

def get path, params={}
  request(path) do |uri|
    uri.query = URI.encode_www_form(params)
    Net::HTTP::Get.new(uri)
  end
end

#patch(path, params = {}) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/bard/github.rb', line 39

def patch path, params={}
  request(path) do |uri|
    Net::HTTP::Patch.new(uri).tap do |r|
      r.body = JSON.dump(params)
    end
  end
end

#post(path, params = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/bard/github.rb', line 23

def post path, params={}
  request(path) do |uri|
    Net::HTTP::Post.new(uri).tap do |r|
      r.body = JSON.dump(params)
    end
  end
end

#put(path, params = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/bard/github.rb', line 31

def put path, params={}
  request(path) do |uri|
    Net::HTTP::Put.new(uri).tap do |r|
      r.body = JSON.dump(params)
    end
  end
end

#read_file(path, branch: "master") ⇒ Object



55
56
57
58
# File 'lib/bard/github.rb', line 55

def read_file path, branch: "master"
   = get("contents/#{path}", ref: branch)
  Base64.decode64(["content"])
end