forked from web-platform-tests/wpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tag master with the PR id that merged at that point. (web-platform-te…
…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
Showing
3 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |