travis_dedup 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 007c6dee85cf3e9a12349700b6d50c50619826db
4
- data.tar.gz: eb663f3908fabd9fb7b2cbf08662286dc52abc09
3
+ metadata.gz: 14a0b42455ff794f353c282be857cd4cfe122656
4
+ data.tar.gz: 57bbe0650d1551bcac0304bc2cb3084c167af4b0
5
5
  SHA512:
6
- metadata.gz: c8095f99a463e177c1e41436ca804f249c8e2326d52d58780e46031e42a222e65ab36ccd4221e1290b74b59d74896fa42dc12b27f4d40bf0a84f705bf4d41b31
7
- data.tar.gz: a1021578505d929ce3f63eaef7789e1aa2ca10bbe83a2ed7d7cf13d67e83c50ddcab034f2c2f1210075af630842847f7f1d37eb6489da6336b9dbe883763104a
6
+ metadata.gz: 8de894c16503ccb38b9f7cc5aa3e3301f6734851ac5dbaa392fe4555b598c30608eb8a6b80b2bac2c6f3f10dd486075a09cc0c27af5b7bb808ffd30e7f9dd883
7
+ data.tar.gz: 77dacb5de7e21bd0c52c2dda124d3ba45a30ab903d292d41262bd35b368ff78d7213fea5c1aadf331dfd7be297993c3b4f7b0f82ec3201936d6d78879d560417
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2013 Michael Grosser <michael@grosser.it>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -3,10 +3,10 @@ require 'json'
3
3
  require 'optparse'
4
4
 
5
5
  module TravisDedup
6
- PENDING = %w[created started queued]
6
+ ACTIVE = %w[created started queued]
7
7
 
8
8
  class << self
9
- attr_accessor :pro
9
+ attr_accessor :pro, :verbose
10
10
 
11
11
  def cli(argv)
12
12
  parser = OptionParser.new do |opts|
@@ -29,26 +29,42 @@ module TravisDedup
29
29
  return 1
30
30
  end
31
31
 
32
- canceled = dedup(*argv)
33
- canceled = (canceled.any? ? canceled.map { |b| b.fetch("id") }.join(", ") : "None")
34
- puts "Builds canceled: #{canceled}"
32
+ puts dedup_message(*argv)
35
33
  0
36
34
  end
37
35
 
38
36
  def dedup(repo, access_token)
39
- headers = {
40
- "Authorization" => %{token "#{access_token}"},
41
- 'Accept' => 'application/vnd.travis-ci.2+json' # otherwise we only get half the build data
42
- }
43
- builds = request(:get, "repos/#{repo}/builds", {}, headers).fetch("builds")
37
+ dedup_builds(repo, access_token).last
38
+ end
44
39
 
40
+ def dedup_message(repo, access_token)
41
+ all, canceled = dedup_builds(repo, access_token)
42
+ canceled = (canceled.any? ? canceled.map { |b| b.fetch("id") }.join(", ") : "None")
43
+ "Found #{all.size} builds, canceled: #{canceled}"
44
+ end
45
+
46
+ def access_token(github_token)
47
+ request(:post, "auth/github", github_token: github_token).fetch("access_token")
48
+ end
49
+
50
+ private
51
+
52
+ def dedup_builds(repo, access_token)
53
+ builds = active_builds(repo, access_token)
54
+ cancel = duplicate_builds(builds)
55
+ cancel(cancel, access_token)
56
+ return builds, cancel
57
+ end
58
+
59
+ def cancel(builds, access_token)
60
+ builds.each { |build| request :post, "builds/#{build.fetch("id")}/cancel", {}, headers(access_token) }
61
+ end
62
+
63
+ def duplicate_builds(builds)
45
64
  seen = []
46
- builds.select! { |b| PENDING.include?(b.fetch("state")) }
47
65
  builds.select do |build|
48
- pr = build.fetch("pull_request_number")
49
- id = build.fetch("id")
66
+ next unless pr = build.fetch("pull_request_number")
50
67
  if seen.include?(pr)
51
- request :post, "builds/#{id}/cancel", {}, headers
52
68
  true
53
69
  else
54
70
  seen << pr
@@ -57,11 +73,17 @@ module TravisDedup
57
73
  end
58
74
  end
59
75
 
60
- def access_token(github_token)
61
- request(:post, "auth/github", github_token: github_token).fetch("access_token")
76
+ def active_builds(repo, access_token)
77
+ request(:get, "repos/#{repo}/builds", {}, headers(access_token)).fetch("builds")
78
+ .select { |b| ACTIVE.include?(b.fetch("state")) }
62
79
  end
63
80
 
64
- private
81
+ def headers(access_token)
82
+ {
83
+ "Authorization" => %{token "#{access_token}"},
84
+ 'Accept' => 'application/vnd.travis-ci.2+json' # otherwise we only get half the build data
85
+ }
86
+ end
65
87
 
66
88
  def host
67
89
  pro ? "https://api.travis-ci.com" : "https://api.travis-ci.org"
@@ -1,3 +1,3 @@
1
1
  module TravisDedup
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: travis_dedup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-03 00:00:00.000000000 Z
11
+ date: 2015-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -45,6 +45,7 @@ executables:
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - MIT-LICENSE.txt
48
49
  - bin/travis-dedup
49
50
  - lib/travis_dedup.rb
50
51
  - lib/travis_dedup/version.rb