Skip to content

Commit

Permalink
Tag master with the PR id that merged at that point. (web-platform-te…
Browse files Browse the repository at this point in the history
…sts#8005)

For the manifest download stuff we have to have tags per PR merge on
master anyway, and this seems like rather useful information.
  • Loading branch information
jgraham authored and gsnedders committed Nov 7, 2017
1 parent 5cff681 commit bc5dcbf
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ matrix:
include:
- os: linux
python: "2.7"
env: JOB=manifest_upload SCRIPT=tools/ci/ci_manifest.sh
env:
- JOB=manifest_upload SCRIPT=tools/ci/ci_manifest.sh
- secure: "FrlMkMZiwggnhJbLiLxZ4imtXxuzFNozty94g1mneMPEVLrnyhb6c/g2SwN37KKU0WSDlGTz26IYnFvo1ftfSOx+sjRz0HqwW7JnrXULKYo7jiPttIcmeJxlSVeW9yS4blbLaBakytHjSnsf+za7bAaf1aS7RRAtAINgifA6Chg="
deploy:
provider: releases
api_key:
Expand Down
1 change: 1 addition & 0 deletions tools/ci/ci_manifest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cd $WPT_ROOT

mkdir -p ~/meta

python tools/ci/tag_master.py
./wpt manifest -p ~/meta/MANIFEST.json
cp ~/meta/MANIFEST.json $WPT_MANIFEST_FILE
# Force overwrite of any existing file
Expand Down
92 changes: 92 additions & 0 deletions tools/ci/tag_master.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import base64
import json
import logging
import os
import sys
import urllib2

here = os.path.abspath(os.path.dirname(__file__))
wpt_root = os.path.abspath(os.path.join(here, os.pardir, os.pardir))

if not(wpt_root in sys.path):
sys.path.append(wpt_root)

from tools.wpt.testfiles import get_git_cmd

logging.basicConfig()
logger = logging.getLogger(__name__)


def get_pr(repo, owner, rev):
url = ("https://api.github.com/search/issues?q=type:pr+is:merged+repo:%s/%s+%s" %
(repo, owner, rev))
try:
resp = urllib2.urlopen(url)
except Exception as e:
logger.error(e)
return None

if resp.code != 200:
logger.error("Got HTTP status %s" % resp.code)
return None

try:
data = json.loads(resp.read())
except ValueError:
logger.error("Failed to read response as JSON")
return None

items = data["items"]
if len(items) == 0:
logger.error("No PR found for master")
return None
if len(items) > 1:
logger.warning("Found multiple PRs for master")

pr = items[0]

return pr["number"]


def tag(repo, owner, sha, tag):
data = json.dumps({"ref": "refs/tags/%s" % tag,
"sha": sha})
try:
url = "https://api.github.com/repos/%s/%s/git/refs" % (repo, owner)
req = urllib2.Request(url, data=data)

base64string = base64.b64encode('%s' % (os.environ["GH_TOKEN"]))
req.add_header("Authorization", "Basic %s" % base64string)

opener = urllib2.build_opener(urllib2.HTTPSHandler())

resp = opener.open(req)
except Exception as e:
logger.error("Tag creation failed:\n%s" % e)
return

if resp.code != 201:
logger.error("Got HTTP status %s" % resp.code)
else:
logger.info("Tagged master as %s" % tag)


def main():
owner, repo = os.environ["TRAVIS_REPO_SLUG"].split("/", 1)
if os.environ["TRAVIS_PULL_REQUEST"] != "false":
logger.info("Not tagging for PR")
return
if os.environ["TRAVIS_BRANCH"] != "master":
logger.info("Not tagging for non-master branch")
return

git = get_git_cmd(wpt_root)
head_rev = git("rev-parse", "HEAD")

pr = get_pr(owner, repo, head_rev)
if pr is not None:
tag(owner, repo, head_rev, "merge_pr_%s" % pr)


if __name__ == "__main__":
main()

0 comments on commit bc5dcbf

Please sign in to comment.