Unverified Commit 21eb114d authored by Dave Machado's avatar Dave Machado
Browse files

Check links on PRs and Update main build script

parent 2ceb3025
#!/bin/bash #!/bin/bash
FORMAT_FILE=../README.md FORMAT_FILE=../README.md
if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then echo "running format validation..."
echo "running on $TRAVIS_BRANCH branch" ./validate_format.py $FORMAT_FILE
LINK_FILE=../README.md if [[ $? != 0 ]]; then
echo "format validation failed!"
exit 1
fi
echo "format validation passed!"
./build.sh
if [[ $? != 0 ]]; then
echo "JSON build failed!"
else else
echo "running on Pull Request #$TRAVIS_PULL_REQUEST" echo "JSON build success!"
DIFF_URL="https://patch-diff.githubusercontent.com/raw/toddmotto/public-apis/pull/$TRAVIS_PULL_REQUEST.diff" fi
curl $DIFF_URL > diff.txt if [ "$TRAVIS_BRANCH" == "master" ]
echo "------- BEGIN DIFF -------" then
cat diff.txt echo "Master build - deploying JSON"
echo "-------- END DIFF --------" ./deploy.sh
cat diff.txt | egrep "\+" > additions.txt fi
echo "------ BEGIN ADDITIONS -----" if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
cat additions.txt echo "running on $TRAVIS_BRANCH branch - skipping Pull Request logic"
echo "------- END ADDITIONS ------" exit 0
LINK_FILE=additions.txt fi
echo "running on Pull Request #$TRAVIS_PULL_REQUEST"
DIFF_URL="https://patch-diff.githubusercontent.com/raw/toddmotto/public-apis/pull/$TRAVIS_PULL_REQUEST.diff"
curl $DIFF_URL > diff.txt
echo "------- BEGIN DIFF -------"
cat diff.txt
echo "-------- END DIFF --------"
cat diff.txt | egrep "\+" > additions.txt
echo "------ BEGIN ADDITIONS -----"
cat additions.txt
echo "------- END ADDITIONS ------"
LINK_FILE=additions.txt
echo "checking if /json was changed..." echo "checking if /json was changed..."
if egrep "\+{3}\s.\/json\/" diff.txt > json.txt; then if egrep "\+{3}\s.\/json\/" diff.txt > json.txt; then
echo "JSON files are auto-generated! Please do not update these files:" echo "JSON files are auto-generated! Please do not update these files:"
cat json.txt cat json.txt
exit 1 exit 1
else else
echo "/json check passed!" echo "/json check passed!"
rm json.txt rm json.txt
fi
fi fi
echo "running format validation..." echo "running link validation..."
./validate_format.py $FORMAT_FILE ./validate_links.py $LINK_FILE
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
echo "format validation failed!" echo "link validation failed!"
exit 1 exit 1
else else
echo "format validation passed!" echo "link validation passed!"
./build.sh && ./deploy.sh
if [[ $? != 0 ]]; then
echo "JSON build and deploy failed!"
else
echo "JSON build and deploy success!"
fi
fi fi
#!/usr/bin/env python3 #!/usr/bin/env python3
import httplib2 import httplib2
import json import re
import socket import socket
import sys import sys
def parse_links(filename): def parse_links(filename):
"""Returns a list of links from JSON object""" """Returns a list of URLs from text file"""
data = json.load(open(filename)) with open(filename) as fp:
links = [] data = fp.read()
for entry in data['entries']: raw_links = re.findall( \
link = entry['Link'] 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', \
https = True if link.startswith('https') else False data)
x = { links = [raw_link.replace(')', '') for raw_link in raw_links]
'link': link,
'https': https,
}
links.append(x)
return links return links
def validate_links(links): def validate_links(links):
"""Checks each entry in JSON file for live link""" """Checks each entry in JSON file for live link"""
print('Validating {} links...'.format(len(links))) print('Validating {} links...'.format(len(links)))
errors = [] errors = []
for each in links: for link in links:
link = each['link']
h = httplib2.Http(disable_ssl_certificate_validation=True, timeout=5) h = httplib2.Http(disable_ssl_certificate_validation=True, timeout=5)
try: try:
resp = h.request(link, 'HEAD') resp = h.request(link, 'HEAD')
...@@ -43,7 +37,7 @@ def validate_links(links): ...@@ -43,7 +37,7 @@ def validate_links(links):
if __name__ == "__main__": if __name__ == "__main__":
num_args = len(sys.argv) num_args = len(sys.argv)
if num_args < 2: if num_args < 2:
print("No .json file passed") print("No .md file passed")
sys.exit(1) sys.exit(1)
errors = validate_links(parse_links(sys.argv[1])) errors = validate_links(parse_links(sys.argv[1]))
if len(errors) > 0: if len(errors) > 0:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment