Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

Commit

Permalink
ci: 新增貢獻者 readme 換行腳本 (#476)
Browse files Browse the repository at this point in the history
歷史提交:
* ci: 新增貢獻者 readme 換行腳本
* test: 測試
* test: 修改成另一個方式
* test: 更多測試
* test: 給 github token
* test: 修正 group 是空值
* test: 修正 ci
* test: 繼續測試
* test: 獨立出來
* test: 小小修正
* chore: 完成!
  • Loading branch information
xMikux authored Oct 10, 2023
1 parent 25c27c6 commit bde2178
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
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

0 comments on commit bde2178

Please sign in to comment.