Module: Credits

Defined in:
lib/credits.rb,
lib/credits/credits.rb,
lib/credits/version.rb

Defined Under Namespace

Classes: CLI, Error

Constant Summary collapse

VERSION =
'1.0.1'

Class Method Summary collapse

Class Method Details

.request_remaining(token, username) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/credits/credits.rb', line 6

def self.request_remaining(token, username)
  @owner = nil
  @url = 'https://api.travis-ci.com/v2_subscriptions'

  headers = {
    'Authorization' => "token #{token}",
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'Travis-API-Version' => '3',
    'path' => '/v2_subscriptions'
  }
  response = HTTParty.get(
    @url,
    query: { format: :json },
    headers: headers
  )

  if response.code == 200
    response['v2_subscriptions'].each do |sub|
      if sub['owner']['@type'] == 'user' && sub['owner']['login'] == username
        @owner = username
      elsif sub['owner']['@type'] == 'organization' && sub['owner']['login'] == username
        @owner = username
      end
    end
  end

  if response.code == 200 && !@owner.nil?
    response['v2_subscriptions'].each do |sub|
      if sub['owner']['@type'] == 'user' && sub['owner']['login'] == username
        owner = sub['owner']['login']
        remaining_credits = sub['addons'][0]['current_usage']['remaining']

        print "User: #{owner}\n"
        print "Remaining Travis CI Credits: #{remaining_credits}\n"
      elsif sub['owner']['@type'] == 'organization' && sub['owner']['login'] == username
        owner = sub['owner']['login']
        remaining_credits = sub['addons'][0]['current_usage']['remaining']

        print "User: #{owner}\n"
        print "Remaining Travis CI Credits: #{remaining_credits}\n"
      end
    end
  else
    print 'Wrong token or username'
  end
end

.request_usage(from, to, token, username) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/credits/credits.rb', line 54

def self.request_usage(from, to, token, username)
  @total = 0
  @url = "https://api.travis-ci.com/v3/owner/github/#{username}/executions_per_repo?from=#{from}&to=#{to}"

  headers = {
    'Authorization' => "token #{token}",
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'Travis-API-Version' => '3',
  }
  response = HTTParty.get(
    @url,
    query: { format: :json },
    headers: headers
  )

  if response.code == 200
    response['executionsperrepo'].each do |rec|
      puts "Credits consumed for '#{rec['os']}'' builds for repository '#{rec['repository']['name']}' are '#{rec['credits_consumed']}'"
      @total += rec['credits_consumed'].to_i
    end
  else
    puts "ERROR"
  end
  puts "Total Credits Consumed: #{@total} credits" if @total > 0
end