Skip to content

Commit ad52987

Browse files
committed
CI: fast finish travis builds for the same PR
closes pandas-dev#12438
1 parent fb7af6e commit ad52987

File tree

2 files changed

+76
-12
lines changed

2 files changed

+76
-12
lines changed

ci/install_travis.sh

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
#!/bin/bash
22

3-
# There are 2 distinct pieces that get zipped and cached
4-
# - The venv site-packages dir including the installed dependencies
5-
# - The pandas build artifacts, using the build cache support via
6-
# scripts/use_build_cache.py
7-
#
8-
# if the user opted in to use the cache and we're on a whitelisted fork
9-
# - if the server doesn't hold a cached version of venv/pandas build,
10-
# do things the slow way, and put the results on the cache server
11-
# for the next time.
12-
# - if the cache files are available, instal some necessaries via apt
13-
# (no compiling needed), then directly goto script and collect 200$.
14-
#
3+
# Fast finish the PR if we have an existing one
4+
echo "[Checking to see if this build is outdated]"
5+
ci/travis_fast_finish.py || { echo "Failing outdated build to end it."; exit 1; }
156

167
function edit_init()
178
{

ci/travis_fast_finish.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
3+
# script to cancel previous travis builds for the same PR
4+
# originally from
5+
# https://github.com/conda-forge/staged-recipes/pull/2257
6+
7+
try:
8+
from future_builtins import (
9+
map,
10+
filter,
11+
)
12+
except ImportError:
13+
pass
14+
15+
import codecs
16+
import contextlib
17+
import json
18+
import os
19+
20+
try:
21+
from urllib.request import (
22+
Request,
23+
urlopen,
24+
)
25+
except ImportError:
26+
from urllib2 import (
27+
Request,
28+
urlopen,
29+
)
30+
31+
32+
def check_latest_pr_build(repo, pr, build_num):
33+
# Not a PR so it is latest.
34+
if pr is None:
35+
return True
36+
37+
headers = {
38+
"Accept": "application/vnd.travis-ci.2+json",
39+
}
40+
url = "https://api.travis-ci.org/repos/{repo}/builds?event_type=pull_request"
41+
42+
request = Request(url.format(repo=repo), headers=headers)
43+
with contextlib.closing(urlopen(request)) as response:
44+
reader = codecs.getreader("utf-8")
45+
data = json.load(reader(response))
46+
47+
# Parse the response to get a list of build numbers for this PR.
48+
builds = data["builds"]
49+
pr_builds = filter(lambda b: b["pull_request_number"] == pr, builds)
50+
pr_build_nums = sorted(map(lambda b: int(b["number"]), pr_builds))
51+
52+
# Check if our build number is the latest (largest)
53+
# out of all of the builds for this PR.
54+
if build_num < max(pr_build_nums):
55+
return False
56+
else:
57+
return True
58+
59+
60+
def main():
61+
repo = os.environ["TRAVIS_REPO_SLUG"]
62+
63+
pr = os.environ["TRAVIS_PULL_REQUEST"]
64+
pr = None if pr == "false" else int(pr)
65+
66+
build_num = int(os.environ["TRAVIS_BUILD_NUMBER"])
67+
68+
return int(check_latest_pr_build(repo, pr, build_num) is False)
69+
70+
71+
if __name__ == "__main__":
72+
import sys
73+
sys.exit(main())

0 commit comments

Comments
 (0)