Class: CronForGithub::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cron_for_github/client.rb

Instance Method Summary collapse

Instance Method Details

#access_tokenObject



42
43
44
# File 'lib/cron_for_github/client.rb', line 42

def access_token
  ENV['GITHUB_ACCESS_TOKEN']
end

#clientObject



3
4
5
# File 'lib/cron_for_github/client.rb', line 3

def client
  @client ||= Octokit::Client.new(access_token: access_token)
end

#create_ref(slug, expected_ref, sha) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/cron_for_github/client.rb', line 13

def create_ref(slug, expected_ref, sha)
  logger.info('ref creating')
  logger.info(slug: slug, ref: expected_ref)
  return_ref = client.create_ref(slug, expected_ref, sha)
  logger.info('ref created')
  logger.debug(return_ref)
  return_ref
end

#delete_ref(slug, ref) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/cron_for_github/client.rb', line 33

def delete_ref(slug, ref)
  logger.info('ref deleting')
  logger.info(slug: slug, ref: ref)
  return_ref = client.delete_ref(slug, ref)
  logger.info('ref deleted')
  logger.debug(return_ref)
  return_ref
end

#latest_sha(slug, head_ref) ⇒ Object



7
8
9
10
11
# File 'lib/cron_for_github/client.rb', line 7

def latest_sha(slug, head_ref)
  response = client.ref(slug, head_ref)
  logger.debug(response)
  response.object.sha
end

#loggerObject



54
55
56
# File 'lib/cron_for_github/client.rb', line 54

def logger
  ::CronForGithub.logger
end

#refs(slug, refs_prefix) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/cron_for_github/client.rb', line 22

def refs(slug, refs_prefix)
  client
    .refs(slug, refs_prefix)
    .map(&:ref)
    .map { |ref| remove_prefix_refs(ref) }
rescue Octokit::NotFound
  logger.info('no repos exist:')
  logger.info(slug: slug, refs_prefix: refs_prefix)
  []
end

#remove_prefix_refs(ref) ⇒ Object

remove refs/ from “refs/heads/ping/foo-bar”



47
48
49
50
51
52
# File 'lib/cron_for_github/client.rb', line 47

def remove_prefix_refs(ref)
  regex = %r{\Arefs/(?<stripped_ref>.+)\Z}
  match = regex.match(ref)
  fail InvalidRefPrefixError, "ref: #{ref}" unless match
  match[:stripped_ref]
end