Update GitHub Repo Languages Data #22
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-languages: | |
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: Install dependencies | |
- name: Install dependencies | |
run: npm install | |
# Step 4: Fetch repository languages data from GitHub API | |
- name: Fetch Repo Languages Data | |
env: | |
STATS_API_TOKEN: ${{ secrets.STATS_API_TOKEN }} | |
run: | | |
echo "Fetching repository languages data..." | |
REPOS=$(curl -s -H "Authorization: token $STATS_API_TOKEN" https://api.github.com/user/repos?type=all&per_page=100 | jq -r '.[].name') | |
echo "[" > repo_data.json | |
for REPO in $REPOS; do | |
LANGUAGES=$(curl -s -H "Authorization: token $STATS_API_TOKEN" "https://api.github.com/repos/${{ github.repository_owner }}/$REPO/languages") | |
if [ "$LANGUAGES" != "{}" ]; then | |
echo "{ \"repo\": \"$REPO\", \"languages\": $LANGUAGES }," >> repo_data.json | |
fi | |
done | |
sed -i '$ s/,$//' repo_data.json # Remove the trailing comma from the last object | |
echo "]" >> repo_data.json | |
# Output the content for verification | |
echo "Fetched languages saved to JSON file:" | |
cat repo_data.json | |
# Step 5: Remove untracked JSON file if it exists to avoid conflicts | |
- name: Remove Untracked JSON File | |
run: | | |
if [ -f repo_data.json ]; then | |
rm repo_data.json | |
fi | |
# Step 6: Checkout gh-pages branch | |
- name: Checkout gh-pages Branch | |
run: | | |
git fetch | |
git checkout gh-pages | |
# Step 7: Commit JSON file to gh-pages branch | |
- name: Commit JSON File | |
run: | | |
mv repo_data.json ./ # Move the file to the root if needed | |
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 |