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

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



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

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

#add_master_branch_protectionObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bard/github.rb', line 78

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bard/github.rb', line 58

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



90
91
92
93
94
95
# File 'lib/bard/github.rb', line 90

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

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



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

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



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

def delete_repo
  delete(nil)
end

#encrypt_secret(encoded_public_key, secret) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/bard/github.rb', line 62

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



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

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



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

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



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

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



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

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



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

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