Module: TravisDedup

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

Constant Summary collapse

PENDING =
%w[created started queued]
VERSION =
"0.2.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.proObject

Returns the value of attribute pro.



9
10
11
# File 'lib/travis_dedup.rb', line 9

def pro
  @pro
end

Class Method Details

.access_token(github_token) ⇒ Object



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

def access_token(github_token)
  request(:post, "auth/github", github_token: github_token).fetch("access_token")
end

.cli(argv) ⇒ Object



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
# File 'lib/travis_dedup.rb', line 11

def cli(argv)
  parser = OptionParser.new do |opts|
    opts.banner = "      Stop all builds on the same PR when a new job starts.\n\n      Usage:\n          travis-dedup your_org/your_repo $TRAVIS_ACCESS_TOKEN --pro\n\n      Options:\n    BANNER\n    opts.on(\"--pro\", \"travis pro\") { self.pro = true }\n    opts.on(\"-h\", \"--help\",\"Show this\") { puts opts; exit }\n    opts.on('-v', '--version','Show Version'){ require 'travis_dedup/version'; puts TravisDedup::VERSION; exit}\n  end\n  parser.parse!(argv)\n\n  unless argv.size == 2\n    puts parser\n    return 1\n  end\n\n  canceled = dedup(*argv)\n  canceled = (canceled.any? ? canceled.map { |b| b.fetch(\"id\") }.join(\", \") : \"None\")\n  puts \"Builds canceled: \#{canceled}\"\n  0\nend\n".gsub(/^          /, "")

.dedup(repo, access_token) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/travis_dedup.rb', line 38

def dedup(repo, access_token)
  headers = {
    "Authorization" => %{token "#{access_token}"},
    'Accept' => 'application/vnd.travis-ci.2+json' # otherwise we only get half the build data
  }
  builds = request(:get, "repos/#{repo}/builds", {}, headers).fetch("builds")

  seen = []
  builds.select! { |b| PENDING.include?(b.fetch("state")) }
  builds.select do |build|
    pr = build.fetch("pull_request_number")
    id = build.fetch("id")
    if seen.include?(pr)
      request :post, "builds/#{id}/cancel", {}, headers
      true
    else
      seen << pr
      false
    end
  end
end