Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: 新增貢獻者 readme 換行腳本 #476

Merged
merged 11 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions .github/scripts/contributors_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Simple Contributors Html table generator"""
import os
import json
import requests

GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
AUTH_HEADER = {'Authorization': f'Bearer {GITHUB_TOKEN}'}

def get_github_user_name(api_url: str, default_name: str):
"""
Using api url to get and retrun user name
"""

response = requests.get(api_url, headers=AUTH_HEADER, timeout=6)

if response.status_code == 200:
json_data = json.loads(response.content)
return json_data["name"] if json_data["name"] is not None else default_name
else:
print("ERROR, not successful to get user data")

def get_contributors_list():
"""
Using GitHub contributors api to get list, and return needs info list
"""
response = requests.get("https://api.github.com/repos/xMikux/ModsTranslationPack/contributors?per_page=100", headers=AUTH_HEADER, timeout=6)
contributor_list = []

if response.status_code == 200:
json_data = json.loads(response.content)

for data in json_data:
if "[bot]" not in data["login"]:
user_name = get_github_user_name(data["url"], data["login"])
html_url = data["html_url"]
avatar_url = data["avatar_url"]

user_info = {
"user_name": user_name,
"html_url": html_url,
"avatar_url": avatar_url
}

contributor_list.append(user_info)

return contributor_list

def generate_html_list(user_list: dict):
"""
Generate contributor html table list
"""

html_table = "<table>\n <tr>\n"

for i, user in enumerate(user_list, 1):
html_table += ' <td align="center">\n'
html_table += f' <a href="{user["html_url"]}" title="{user["user_name"]}">\n'
html_table += f' <img src="{user["avatar_url"]}" width="100;" alt="{user["user_name"]}"/>\n'
html_table += f' <br /><sub><b>{user["user_name"]}</b></sub>\n'
html_table += ' </a>\n'
html_table += ' </td>\n'

if i % 7 == 0 and i != len(user_list):
html_table += " </tr>\n <tr>\n"

html_table += " </tr>\n</table>"

return html_table

if __name__ == "__main__":
api_list = get_contributors_list()
html_list = generate_html_list(api_list)
print(html_list)
28 changes: 21 additions & 7 deletions .github/workflows/CI-Contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,26 @@ jobs:
- name: Checking Repository
uses: actions/checkout@v4

- name: Generate Contributors Images
uses: jaywcjlove/github-action-contributors@main
id: contributors
with:
filter-author: (renovate\[bot\]|renovate-bot|dependabot\[bot\])
avatarSize: 100
- name: Generate Contributors Html Table
id: html_list
run: |
list_html=$(cat <<-EOF
python .github/scripts/contributors_generator.py
EOF
)
{
echo 'html_table<<EOF'
$list_html
echo EOF
} >> "$GITHUB_OUTPUT"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Print Html Table
run: |
cat << EOF
${{ steps.html_list.outputs.html_table }}
EOF

- name: Update Readme
run: |
Expand All @@ -35,7 +49,7 @@ jobs:
MULTILINE_MODE: true
START_TAG: "<!-- CONTRIBUTORS_CI_START -->"
END_TAG: "<!-- CONTRIBUTORS_CI_END -->"
CONTENT: "${{steps.contributors.outputs.htmlTable}}"
CONTENT: "${{ steps.html_list.outputs.html_table }}"

- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
Expand Down