diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index f9e22f3b9..b59f74ad4 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -23,6 +23,8 @@ jobs: uses: actions/setup-python@v5 with: python-version: "3.9" + - name: Format python code with black + uses: psf/black@stable - uses: actions/setup-node@v4 with: node-version: 20 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ba7c9517b..b4e92995b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -52,3 +52,8 @@ repos: - id: eslint args: - "--fix" + - repo: https://github.com/psf/black + rev: 24.1.1 + hooks: + - id: black + language_version: python3.10 diff --git a/scripts/sort_contibutors/main.py b/scripts/sort_contibutors/main.py index 2a883a14f..223308c13 100644 --- a/scripts/sort_contibutors/main.py +++ b/scripts/sort_contibutors/main.py @@ -10,60 +10,72 @@ def print_file(s: str, flag: bool) -> None: # True for MD , false for HTML file if flag: - f = open('contributors_file.md', 'w') + f = open("contributors_file.md", "w") f.write(s) return - f = open('contributors_file.html', 'w') + f = open("contributors_file.html", "w") f.write(s) def print_md(user_list: dict, label="") -> str: - string = '{}:\n\n'.format(label) + string = "{}:\n\n".format(label) for value in user_list: - string += '- [{} @{}](https://www.github.com/{})\n'.format(value['name'], - value['username'], value['username']) - return string + '\n' - - -def print_html(leaders: dict, top_contributors: dict, contributors: dict, testers: dict, special_thanks: dict) -> str: - string = '\n' - - string += 'OWASP Project Leaders:\n' - string += '\n" - string += 'Top Contributors:\n' - string += '\n" - string += 'Contributors:\n' - string += '\n" - string += 'Testers:\n' - string += '\n" - string += 'Special mentions for helping out:\n' - string += '\n" - string += '\n' + string += "\n" return string @@ -71,22 +83,44 @@ def parse_contributor_list(user_list: list, user_token: str) -> list: contributors = [] for element in user_list: - ranking = element['contributions'] - username = element['login'] + ranking = element["contributions"] + username = element["login"] name = get_fullname(username, user_token) if name == None: name = username - leaders_and_multijuicer = ['DerGut', 'bkimminich', 'MichaelEischer', 'rseedorff', 'jonasbg', 'scornelissen85', - 'zadjadr', 'stuebingerb', 'sydseter', 'troygerber', 'skandix', 'saymolet', - 'adrianeriksen', 'pseudobeard', 'coffemakingtoaster', 'wurstbrot', 'blucas-accela', - 'fwijnholds', 'stefan-schaermeli', 'nickmalcolm', 'orangecola', 'commjoen', - 'bendehaan', 'benno001'] + leaders_and_multijuicer = [ + "DerGut", + "bkimminich", + "MichaelEischer", + "rseedorff", + "jonasbg", + "scornelissen85", + "zadjadr", + "stuebingerb", + "sydseter", + "troygerber", + "skandix", + "saymolet", + "adrianeriksen", + "pseudobeard", + "coffemakingtoaster", + "wurstbrot", + "blucas-accela", + "fwijnholds", + "stefan-schaermeli", + "nickmalcolm", + "orangecola", + "commjoen", + "bendehaan", + "benno001", + ] # Filter the github bots - if '[bot]' not in username and username not in leaders_and_multijuicer: + if "[bot]" not in username and username not in leaders_and_multijuicer: contributors.append( - {'username': username, 'name': name, 'ranking': ranking}) + {"username": username, "name": name, "ranking": ranking} + ) return contributors @@ -103,26 +137,34 @@ def get_fullname(username: str, user_token: str) -> str: } if username in name_dict: return name_dict[username] - headers = {'X-GitHub-Api-Version': '2022-11-28', - 'Accept': 'application/vnd.github+json', - 'Authorization': 'Bearer ' + user_token} - r = requests.get('https://api.github.com/users/' + - username, headers=headers, timeout=20) + headers = { + "X-GitHub-Api-Version": "2022-11-28", + "Accept": "application/vnd.github+json", + "Authorization": "Bearer " + user_token, + } + r = requests.get( + "https://api.github.com/users/" + username, headers=headers, timeout=20 + ) if r.status_code == 401: print("Invalid token") os._exit(-1) - return r.json()['name'] + return r.json()["name"] # Retrieves the list of contributors of a repository in JSON format def fetch_repository(project: str, user_token: str) -> list: - headers = {'X-GitHub-Api-Version': '2022-11-28', - 'Accept': 'application/vnd.github+json', - 'Authorization': 'Bearer ' + user_token} - r = requests.get('https://api.github.com/repos/OWASP/' + - project + '/contributors?per_page=100', headers=headers, timeout=20) + headers = { + "X-GitHub-Api-Version": "2022-11-28", + "Accept": "application/vnd.github+json", + "Authorization": "Bearer " + user_token, + } + r = requests.get( + "https://api.github.com/repos/OWASP/" + project + "/contributors?per_page=100", + headers=headers, + timeout=20, + ) if r.status_code == 401: print("Invalid token") os._exit(-1) @@ -135,23 +177,24 @@ def merge_users(l: list) -> list: for a in l: - if a['username'] in ranking: - ranking[a['username']] += a['ranking'] + if a["username"] in ranking: + ranking[a["username"]] += a["ranking"] else: - ranking[a['username']] = a['ranking'] - username[a['username']] = {'username': a['username'], - 'name': a['name'], 'ranking': ranking[a['username']]} + ranking[a["username"]] = a["ranking"] + username[a["username"]] = { + "username": a["username"], + "name": a["name"], + "ranking": ranking[a["username"]], + } - l = dict(sorted(username.items(), - key=lambda x: x[1]['ranking'], reverse=True)) + l = dict(sorted(username.items(), key=lambda x: x[1]["ranking"], reverse=True)) special_contributors = [] contributors = [] for key, value in l.items(): - element = {'name': value['name'], - 'username': key, 'ranking': value['ranking']} - if element['ranking'] >= 100: + element = {"name": value["name"], "username": key, "ranking": value["ranking"]} + if element["ranking"] >= 100: special_contributors.append(element) else: contributors.append(element) @@ -160,14 +203,12 @@ def merge_users(l: list) -> list: def get_contibutors_list(token: str) -> list: print("[+] Fetching the Wrong Secrets CTF party contributors list ... ") - wrongsecrets_ctf_list = fetch_repository('wrongsecrets-ctf-party', token) + wrongsecrets_ctf_list = fetch_repository("wrongsecrets-ctf-party", token) print("[+] Fetching the Wrong Secrets Binaries contributors list ... ") - wrongsecrets_binaries_list = fetch_repository( - 'wrongsecrets-binaries', token) + wrongsecrets_binaries_list = fetch_repository("wrongsecrets-binaries", token) print("[+] Fetching the Wrong Secrets contributors list ... ") - wrongsecrets_list = fetch_repository('wrongsecrets', token) - merged_list = wrongsecrets_binaries_list + \ - wrongsecrets_ctf_list + wrongsecrets_list + wrongsecrets_list = fetch_repository("wrongsecrets", token) + merged_list = wrongsecrets_binaries_list + wrongsecrets_ctf_list + wrongsecrets_list print("[+] Sorting the list .. ") return merge_users(merged_list) @@ -178,32 +219,32 @@ def get_contibutors_list(token: str) -> list: # Loads the .env file load_dotenv() -token = os.getenv('USER_TOKEN') +token = os.getenv("USER_TOKEN") if token is not None: contributors = [ - {'username': 'f3rn0s', 'name': 'f3rn0s'}, - {'username': 'szh', 'name': 'Shlomo Zalman Heigh'}, + {"username": "f3rn0s", "name": "f3rn0s"}, + {"username": "szh", "name": "Shlomo Zalman Heigh"}, ] testers = [ - {'username': 'davevs', 'name': 'Dave van Stein'}, - {'username': 'drnow4u', 'name': 'Marcin Nowak'}, - {'username': 'mchangsp', 'name': 'Marc Chang Sing Pang'}, - {'username': 'djvinnie', 'name': 'Vineeth Jagadeesh'} + {"username": "davevs", "name": "Dave van Stein"}, + {"username": "drnow4u", "name": "Marcin Nowak"}, + {"username": "mchangsp", "name": "Marc Chang Sing Pang"}, + {"username": "djvinnie", "name": "Vineeth Jagadeesh"}, ] leaders = [ - {'username': 'bendehaan', 'name': 'Ben de Haan'}, - {'username': 'commjoen', 'name': 'Jeroen Willemsen'} + {"username": "bendehaan", "name": "Ben de Haan"}, + {"username": "commjoen", "name": "Jeroen Willemsen"}, ] special_thanks = [ - {'username': 'madhuakula', 'name': 'Madhu Akula @madhuakula'}, - {'username': 'nbaars', 'name': 'Nanne Baars @nbaars'}, - {'username': 'bkimminich', 'name': 'Björn Kimminich'}, - {'username': 'devsecops', 'name': 'Dan Gora'}, - {'username': 'saragluna', 'name': 'Xiaolu Dai'}, - {'username': 'jonathanGiles', 'name': 'Jonathan Giles'}, + {"username": "madhuakula", "name": "Madhu Akula @madhuakula"}, + {"username": "nbaars", "name": "Nanne Baars @nbaars"}, + {"username": "bkimminich", "name": "Björn Kimminich"}, + {"username": "devsecops", "name": "Dan Gora"}, + {"username": "saragluna", "name": "Xiaolu Dai"}, + {"username": "jonathanGiles", "name": "Jonathan Giles"}, ] l = get_contibutors_list(token) @@ -211,22 +252,23 @@ def get_contibutors_list(token: str) -> list: normal_list = l[1] l = merge_users(special_list + normal_list) - print('[+] Print to HTML file') + print("[+] Print to HTML file") html_payload = print_html(leaders, l[0], l[1], testers, special_thanks) print_file(html_payload, False) - print('[+] Print to MD file') - md_payload = print_md(leaders, 'Leaders') - md_payload += print_md(l[0], 'Top contributors') - md_payload += print_md(l[1], 'Contributors') - md_payload += print_md(testers, 'Testers') - md_payload += print_md(special_thanks, 'Special thanks') + print("[+] Print to MD file") + md_payload = print_md(leaders, "Leaders") + md_payload += print_md(l[0], "Top contributors") + md_payload += print_md(l[1], "Contributors") + md_payload += print_md(testers, "Testers") + md_payload += print_md(special_thanks, "Special thanks") print_file(md_payload, True) - print('[+] Done') + print("[+] Done") else: print( - 'The variable USER_TOKEN does not exists. You must setup the variable' - 'in the following way:') - print('echo USER_TOKEN=github_pat_dfs1N_f>.env') + "The variable USER_TOKEN does not exists. You must setup the variable" + "in the following way:" + ) + print("echo USER_TOKEN=github_pat_dfs1N_f>.env") print('note that the USER_TOKEN requires "repository access" only')