Skip to content

Commit

Permalink
Merge pull request #8058 from bboozzoo/bboozzoo/skip-travis-job
Browse files Browse the repository at this point in the history
run-checks, travis: allow skipping spread jobs by adding a label
  • Loading branch information
sergiocazzolato authored Feb 11, 2020
2 parents 9cf599a + 042bc2e commit 9441644
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 5 deletions.
101 changes: 101 additions & 0 deletions check-pr-skip-spread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/python3

import argparse
import urllib.request
import logging

from html.parser import HTMLParser

# PR label indicating that spread job should be skipped
LABEL_SKIP_SPREAD_JOB = "Skip spread"


class GithubLabelsParser(HTMLParser):
def __init__(self):
super().__init__()
self.in_labels = False
self.deep = 0
self.labels = []

def handle_starttag(self, tag, attributes):
logging.debug(attributes)

if not self.in_labels:
attr_class = [attr[1] for attr in attributes if attr[0] == "class"]
if len(attr_class) == 0:
return
# labels are in separate div defined like:
# <div class=".. labels .." .. >
elems = attr_class[0].split(" ")
if "labels" in elems:
self.in_labels = True
self.deep = 1
logging.debug("labels start")
else:
# nesting counter
self.deep += 1

# inside labels
# label entry has
# <a class=".." data-name="<label name>" />
attr_data_name = [attr[1] for attr in attributes if attr[0] == "data-name"]
if len(attr_data_name) == 0:
return
data_name = attr_data_name[0]
logging.debug("found label: %s", data_name)
self.labels.append(data_name)

def handle_endtag(self, tag):
if self.in_labels:
self.deep -= 1
if self.deep < 1:
logging.debug("labels end")
self.in_labels = False

def handle_data(self, data):
if self.in_labels:
logging.debug("data: %s", data)


def grab_pr_labels(pr_number: int):
# ideally we would use the github API - however we can't because:
# a) its rate limiting and travis IPs hit the API a lot so we regularly
# get errors
# b) using a API token is tricky because travis will not allow the secure
# vars for forks
# so instead we just scrape the html title which is unlikely to change
# radically
parser = GithubLabelsParser()
with urllib.request.urlopen(
"https://github.com/snapcore/snapd/pull/{}".format(pr_number)
) as f:
parser.feed(f.read().decode("utf-8"))
return parser.labels


def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"pr_number", metavar="PR number", help="the github PR number to check"
)
parser.add_argument(
"-d", "--debug", help="enable debug logging", action="store_true"
)
args = parser.parse_args()

lvl = logging.INFO
if args.debug:
lvl = logging.DEBUG
logging.basicConfig(level=lvl)

labels = grab_pr_labels(args.pr_number)
print("labels:", labels)

if LABEL_SKIP_SPREAD_JOB not in labels:
raise SystemExit(1)

print("requested to skip the spread job")


if __name__ == "__main__":
main()
6 changes: 1 addition & 5 deletions check-pr-title.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#!/usr/bin/python3

import argparse
import base64
import json
import os
import re
import sys
import urllib.request

from html.parser import HTMLParser
Expand Down Expand Up @@ -75,7 +71,7 @@ def main():
print("module: short description")
print("E.g.:")
print("daemon: fix frobinator bug")
sys.exit(1)
raise SystemExit(1)


if __name__ == "__main__":
Expand Down
8 changes: 8 additions & 0 deletions run-checks
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ if [ "$UNIT" = 1 ]; then
fi

if [ -n "$SPREAD" ]; then
if [ -n "${TRAVIS_PULL_REQUEST:-}" ] && [ "${TRAVIS_PULL_REQUEST:-}" != "false" ]; then
echo "Checking whether PR author requested to skip spread"
if ./check-pr-skip-spread.py "$TRAVIS_PULL_REQUEST"; then
echo "Skipping spread job on request"
exit 0
fi
fi

TMP_SPREAD="$(mktemp -d)"
addtrap "rm -rf \"$TMP_SPREAD\""

Expand Down

0 comments on commit 9441644

Please sign in to comment.