Update GitHub Repo Languages Data #33
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: Update GitHub Repo Languages Data | |
on: | |
schedule: | |
- cron: '0 0 * * *' # Runs every day at midnight | |
workflow_dispatch: # Allows manual triggering | |
jobs: | |
fetch-and-save-data: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Checkout the repository | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
# Step 2: Set up Node.js | |
- name: Set up Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '16' | |
# Step 3: Fetch the repository data from GitHub API | |
- name: Fetch Repo Data | |
env: | |
STATS_API_TOKEN: ${{ secrets.STATS_API_TOKEN }} | |
run: | | |
echo "Fetching repository data..." | |
REPOS=$(curl -s -H "Authorization: token $STATS_API_TOKEN" https://api.github.com/user/repos?type=all&per_page=100) | |
# Save the fetched repository data to a temporary file for later use | |
echo "${REPOS}" > fetched_repos.json | |
# Output fetched data for verification | |
echo "Fetched repository data saved to temporary file: fetched_repos.json" | |
cat fetched_repos.json | |
# Step 4: Extract languages for each repository and write to JSON | |
- name: Extract Languages and Write JSON | |
env: | |
STATS_API_TOKEN: ${{ secrets.STATS_API_TOKEN }} | |
run: | | |
echo "Extracting languages for each repository and writing to JSON..." | |
# Initialize an empty JSON array | |
echo "[]" > repo_data.json | |
# Iterate over each repository from the temporary file and fetch languages | |
cat fetched_repos.json | jq -c '.[]' | while read -r row; do | |
REPO_NAME=$(echo "${row}" | jq -r '.name') | |
REPO_OWNER=$(echo "${row}" | jq -r '.owner.login') | |
LANGUAGES=$(curl -s -H "Authorization: token $STATS_API_TOKEN" "https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/languages") | |
# Check if the languages request succeeded and if there are languages in the response | |
if [[ "${LANGUAGES}" == "{}" || -z "${LANGUAGES}" ]]; then | |
echo "No languages found or error fetching languages for ${REPO_NAME}. Skipping..." | |
continue | |
fi | |
# Append only relevant language information to the JSON file | |
jq --arg repo "${REPO_NAME}" --argjson languages "${LANGUAGES}" \ | |
'. += [{"repo": $repo, "languages": $languages}]' repo_data.json > tmp.json && mv tmp.json repo_data.json | |
done | |
# Output the content for verification | |
echo "Languages extracted and saved to JSON file:" | |
cat repo_data.json | |
# Step 5: Remove the untracked JSON file if it exists | |
- name: Remove Untracked JSON File | |
run: | | |
if [ -f repo_data.json ]; then | |
echo "Removing existing repo_data.json to avoid conflicts when switching branches." | |
rm repo_data.json | |
fi | |
# Step EXTRA: Log JSON File Contents Before Commit | |
- name: Log JSON File Contents | |
run: | | |
echo "Contents of repo_data.json before attempting to commit:" | |
cat repo_data.json | |
# Step 6: Checkout gh-pages branch | |
- name: Checkout gh-pages Branch | |
run: | | |
git fetch | |
git checkout gh-pages | |
# Step EXTRA: Log JSON File Contents Before Commit | |
- name: Log JSON File Contents | |
run: | | |
echo "Contents of repo_data.json before attempting to commit:" | |
cat repo_data.json | |
# Step 7: Commit the JSON file to gh-pages branch | |
- name: Commit JSON File | |
run: | | |
git add repo_data.json | |
git config user.name "github-actions[bot]" | |
git config user.email "github-actions[bot]@users.noreply.github.com" | |
git commit -m "Update repository languages data [skip ci]" || echo "No changes to commit" | |
git push origin gh-pages |