Skip to content

Commit

Permalink
Merge pull request #1 from sjledoux/add-alphabetic-check
Browse files Browse the repository at this point in the history
Add alphabetic check
  • Loading branch information
sjledoux authored Mar 28, 2024
2 parents b7a0606 + 0e8e469 commit ba2901d
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/test-alphabetical.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Check Alphabeitical Order

on:
pull_request_target:
paths:
- "public_suffix_list.dat"
permissions:
pull-requests: write

jobs:
Alphabetic-Check:
runs-on: ubuntu-latest
outputs:
output1: ${{ steps.read_results.outputs.contents }}
steps:
- name: Get PR files
uses: actions/checkout@v2
with:
ref: "${{ github.ref }}"
path: "pull-request"
- name: Get base ref
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.base.ref }}
path: base
- name: Content check
run: python3 base/alphabet_script.py pull-request/public_suffix_list.dat > results.txt
- name: Read the result
id: read_results
uses: andstor/file-reader-action@v1
with:
path: "results.txt"
- name: Create Comment
uses: mshick/add-pr-comment@v2
with:
message-path: "results.txt"
refresh-message-position: true
- name: Apply Label
if: steps.read_results.outputs.contents == ''
uses: actions/github-script@v7
with:
script: |
github.rest.issues.addLabels({
issue_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['Alphabet Check Success']
})


83 changes: 83 additions & 0 deletions tools/alphabet_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import re
import sys

def check_private_domains(file):
with open(file) as f:
before_private = True
wrong_order_submissions = []
content = f.readlines()
incorrect_domains = {}
for index, line in enumerate(content,0):
if before_private:
if line.startswith("// ===BEGIN PRIVATE DOMAINS==="):
before_private = False
else:
continue
if re.search("^\/\/ .* *: *https:\/\/.*", line):
wrong_domains, next_index = title_is_less(content, index)
if wrong_domains:
wrong_order_submissions.append(wrong_domains)
incorrect_domains[line.removeprefix("//").split(":")[0].strip()
] = check_submission_domains(content, index,
next_index)
return wrong_order_submissions, incorrect_domains


def title_is_less(content, starting_index):
domain = content[starting_index].removeprefix("// ").lower().strip()
wrong_order = []
end_index = -1
for index, line in enumerate(content[starting_index+1:], starting_index+1):
if re.search("^\/\/ .* *: *https:\/\/.*", line):
end_index = index
next_domain = line.removeprefix("//").lower().strip()
if domain > next_domain:
wrong_order = (domain.strip(), next_domain.strip())
return wrong_order, end_index
return None, -1



def check_submission_domains(content, start_index, end_index):
wrong_order_domains = []
for index, line in enumerate(content[start_index:end_index], start_index):
if line.startswith("//"):
continue
else:
if not suffix_is_less(content[index].strip().split("."),
content[index+1].strip().split(".")):
wrong_order_domains.append((content[index].strip(),
content[index+1].strip()))
return wrong_order_domains

def suffix_is_less(suffix_1, suffix_2):
if not suffix_2[-1] or not [suffix_1][-1]:
return True
if suffix_1[-1] > suffix_2[-1]:
return False
elif suffix_1[-1] == suffix_2[-1]:
if len(suffix_1) > 1:
if len(suffix_2) <= 1:
return False
else:
return suffix_is_less(suffix_1[:-1], suffix_2[:-1])
return True

def main(effective_tld_filename):
wrong_order_submissions, incorrect_domains = check_private_domains(
effective_tld_filename)
if wrong_order_submissions:
print(f"The following submissions were entered in the wrong order:")
for submission in wrong_order_submissions:
print(f"\t{submission}")
if incorrect_domains:
for submission in incorrect_domains:
if incorrect_domains[submission]:
print(f"The submission for {submission} contains domains in an improper order, which are:")
for domain in incorrect_domains[submission]:
print(f"\t{domain}")



if __name__ == '__main__':
main(sys.argv[1])

0 comments on commit ba2901d

Please sign in to comment.