Fetch Commit History #55
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Fetch Commit History | |
on: | |
schedule: | |
- cron: '0 0 * * 1' # “At 00:00 on Monday.” | |
workflow_dispatch: | |
jobs: | |
fetch-commit-history: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Get members of "gdsc" organization | |
id: get-members | |
run: | | |
members=$(curl -sSL -H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ secrets.GDSC_WEBSITE }}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
https://api.github.com/orgs/Google-DSC-Kookmin/members | jq -r '.[] | .login' | sed 's/ /_/g') | |
# Set members as an environment variable | |
echo "GDSC_MEMBERS<<EOF" >> $GITHUB_ENV | |
echo "$members">>$GITHUB_ENV | |
echo "EOF">>$GITHUB_ENV | |
shell: bash | |
- name: Fetch commit history for each member | |
id: fetch-commit-history | |
run: | | |
set -x | |
# Access the environment variable and split it into an array | |
members=($GDSC_MEMBERS) | |
commitPeriod=$(date -d "4 weeks ago" --utc +"%Y-%m-%dT%H:%M:%SZ") | |
all_commits=() | |
for member in "${members[@]}"; do | |
echo "Fetching commit history for $member..." | |
member_commits=$(curl -sSL -H "Authorization: Bearer ${{ secrets.GDSC_WEBSITE }}" "https://api.github.com/users/${member}/events") | |
if [[ $(echo "$member_commits" | jq length) -gt 0 ]]; then | |
# Extracting commit-related data (commit ID, name, time, message, link) | |
commits_data=$(echo "$member_commits" | jq -r 'map(select(.type == "PushEvent" and .created_at >= "'"$commitPeriod"'")) | .[] | {commitId: .payload.commits[0].sha, userName: .payload.commits[0].author.name, commitTime: .created_at, commitMessage: .payload.commits[0].message, commitLink: .payload.commits[0].url }') | |
# Append the commit data to the member's entry in all_commits | |
all_commits+=("$commits_data") | |
else | |
echo "No new commit data for $member." | |
fi | |
done | |
all_commits=($(for commit in "${all_commits[@]}"; do echo "$commit"; done | jq -s 'sort_by(.commitTime) | reverse')) | |
if [[ ${#all_commits[@]} -gt 0 ]]; then | |
# Save the commits data to data.json | |
sorted_commits=$(echo "${all_commits[@]}" | jq -s .) | |
mkdir -p data | |
echo "$sorted_commits" > data/data.json | |
else | |
echo "No new commit data for any member." > data/empty_commit_data.txt | |
fi | |
- name: Commit and push fetched data to repository | |
run: | | |
cd $GITHUB_WORKSPACE | |
git config user.email "[email protected]" | |
git config user.name "GitHub Actions" | |
if [[ -n $(git status --porcelain) ]]; then | |
git add data | |
git commit -m "Update commit data" | |
git push origin master | |
fi |