From 5b198c6ed033acf971af1f482524da1f169cb9d9 Mon Sep 17 00:00:00 2001 From: za Date: Fri, 26 Jan 2024 10:18:00 +0700 Subject: [PATCH 1/7] Format Python script using Black formatter So the style is consistent Refer to issue: https://github.com/OWASP/wrongsecrets/issues/1200 --- scripts/sort_contibutors/main.py | 242 ++++++++++++++++++------------- 1 file changed, 141 insertions(+), 101 deletions(-) diff --git a/scripts/sort_contibutors/main.py b/scripts/sort_contibutors/main.py index 2a883a14f..7fedbf378 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) @@ -134,24 +176,24 @@ def merge_users(l: list) -> list: ranking = dict() 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 +202,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 +218,31 @@ 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 +250,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') From 692f1c91768c0143d67dbbf8a7b3077bc8d9e256 Mon Sep 17 00:00:00 2001 From: za Date: Fri, 26 Jan 2024 14:17:18 +0700 Subject: [PATCH 2/7] Update github workflows pre-commit to add black in the workflow --- .github/workflows/pre-commit.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index f9e22f3b9..e70884cef 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -23,6 +23,9 @@ jobs: uses: actions/setup-python@v5 with: python-version: "3.9" + - name: Black + description: Format Python code with Black + language: python - uses: actions/setup-node@v4 with: node-version: 20 From f69e45541a5423dc4025f661c0ab389fcbe4c4d8 Mon Sep 17 00:00:00 2001 From: za Date: Fri, 26 Jan 2024 14:24:18 +0700 Subject: [PATCH 3/7] Added black.yml for github actions integrations docs: https://black.readthedocs.io/en/stable/integrations/github_actions.html --- .github/workflows/black.yml | 10 ++++++++++ .github/workflows/pre-commit.yml | 9 ++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/black.yml diff --git a/.github/workflows/black.yml b/.github/workflows/black.yml new file mode 100644 index 000000000..9065b5e02 --- /dev/null +++ b/.github/workflows/black.yml @@ -0,0 +1,10 @@ +name: Lint + +on: [push, pull_request] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: psf/black@stable diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index e70884cef..a90d8197a 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -23,9 +23,12 @@ jobs: uses: actions/setup-python@v5 with: python-version: "3.9" - - name: Black - description: Format Python code with Black - language: python + - name: Format python code with black + uses: psf/black@stable + with: + options: "--check --verbose" + src: "./scripts" + version: "~= 22.0" - uses: actions/setup-node@v4 with: node-version: 20 From 357f04e0274e1885d8b6bfbdc31a43db47592e35 Mon Sep 17 00:00:00 2001 From: za Date: Fri, 26 Jan 2024 14:27:06 +0700 Subject: [PATCH 4/7] Rename Lint to Black to make it more explicit --- .github/workflows/black.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/black.yml b/.github/workflows/black.yml index 9065b5e02..a166e6352 100644 --- a/.github/workflows/black.yml +++ b/.github/workflows/black.yml @@ -1,4 +1,4 @@ -name: Lint +name: Black on: [push, pull_request] From 16212380218a459326b0fc57354c73741a659d8b Mon Sep 17 00:00:00 2001 From: za Date: Fri, 26 Jan 2024 15:42:54 +0700 Subject: [PATCH 5/7] Update .pre-commit-config.yaml to add black --- .github/workflows/pre-commit.yml | 4 ---- .pre-commit-config.yaml | 5 +++++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index a90d8197a..b59f74ad4 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -25,10 +25,6 @@ jobs: python-version: "3.9" - name: Format python code with black uses: psf/black@stable - with: - options: "--check --verbose" - src: "./scripts" - version: "~= 22.0" - uses: actions/setup-node@v4 with: node-version: 20 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ba7c9517b..17f769e4d 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: 22.6b0 + hooks: + - id: black + language_version: python3.8 \ No newline at end of file From fb37618b0cd8bbee1f6352aeb28828da81545181 Mon Sep 17 00:00:00 2001 From: za Date: Mon, 29 Jan 2024 08:58:07 +0700 Subject: [PATCH 6/7] Update black version as the previous version is not working --- .pre-commit-config.yaml | 4 ++-- scripts/sort_contibutors/main.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 17f769e4d..b4e92995b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -53,7 +53,7 @@ repos: args: - "--fix" - repo: https://github.com/psf/black - rev: 22.6b0 + rev: 24.1.1 hooks: - id: black - language_version: python3.8 \ No newline at end of file + language_version: python3.10 diff --git a/scripts/sort_contibutors/main.py b/scripts/sort_contibutors/main.py index 7fedbf378..223308c13 100644 --- a/scripts/sort_contibutors/main.py +++ b/scripts/sort_contibutors/main.py @@ -176,6 +176,7 @@ def merge_users(l: list) -> list: ranking = dict() for a in l: + if a["username"] in ranking: ranking[a["username"]] += a["ranking"] else: @@ -221,6 +222,7 @@ def get_contibutors_list(token: str) -> list: token = os.getenv("USER_TOKEN") if token is not None: + contributors = [ {"username": "f3rn0s", "name": "f3rn0s"}, {"username": "szh", "name": "Shlomo Zalman Heigh"}, From cc0061e2b5c6b05bfb86b51338ef53f018ec0a79 Mon Sep 17 00:00:00 2001 From: za Date: Mon, 29 Jan 2024 15:08:13 +0700 Subject: [PATCH 7/7] Removed black github workflow as we implemented black in git pre-commit hook --- .github/workflows/black.yml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 .github/workflows/black.yml diff --git a/.github/workflows/black.yml b/.github/workflows/black.yml deleted file mode 100644 index a166e6352..000000000 --- a/.github/workflows/black.yml +++ /dev/null @@ -1,10 +0,0 @@ -name: Black - -on: [push, pull_request] - -jobs: - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: psf/black@stable