-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze-code.yml
111 lines (90 loc) · 4.63 KB
/
analyze-code.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
name: Update Lines of Code in Readme
on:
schedule:
- cron: "0 0 * * 0" # Runs weekly on Sunday at midnight (UTC)
workflow_dispatch: # Allows manual trigger
jobs:
count-lines:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
# Install required dependencies: jq (JSON processor), cloc (count lines of code), and locale settings
- name: Install Dependencies
run: |
sudo apt-get update
sudo apt-get install -y jq cloc locales
sudo locale-gen en_US.UTF-8
# Fetch public repositories (excluding forks) and clone only the default branch
- name: Fetch and Clone Repositories
env:
GH_PAT: ${{ secrets.GH_PAT }}
run: |
# Set your GitHub username
USERNAME="<your-github-username>"
# Get a list of public repositories that are not forks
REPOS=$(curl -H "Authorization: token $GH_PAT" -s "https://api.github.com/user/repos?per_page=100" | jq -r '.[] | select(.fork == false) | .full_name') || echo "Error fetching repositories"
mkdir -p public-repos
cd public-repos
for REPO in $REPOS; do
REPO_URL="https://github.com/$REPO.git"
AUTHENTICATED_REPO=$(echo "$REPO_URL" | sed "s/https:\/\//https:\/\/$GH_PAT@/g")
# Determine the default branch dynamically and clone only that branch
DEFAULT_BRANCH=$(curl -H "Authorization: token $GH_PAT" -s "https://api.github.com/repos/$REPO" | jq -r '.default_branch')
echo "Cloning $REPO (default branch: $DEFAULT_BRANCH)..."
git clone --branch "$DEFAULT_BRANCH" --single-branch "$AUTHENTICATED_REPO" "$(basename $REPO)-$DEFAULT_BRANCH" || echo "Failed to clone $REPO."
done
# Run cloc to analyze lines of code, excluding non-source code files
echo "Calculating lines of code..."
mkdir -p ../output
cloc . --exclude-ext=json,html,css,svg,md,py,ps1,scss --json > ../output/cloc-output.json
# Commit and push the updated cloc-output.json and README.md to the current branch
- name: Commit and Push Output
env:
GH_PAT: ${{ secrets.GH_PAT }}
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
# Format and update README
TOTAL_LINES=$(jq '.SUM.code // 0' output/cloc-output.json)
JS_LINES=$(jq '.JavaScript.code // 0' output/cloc-output.json)
TS_LINES=$(jq '.TypeScript.code // 0' output/cloc-output.json)
JSX_LINES=$(jq '.JSX.code // 0' output/cloc-output.json)
CSHARP_LINES=$(jq '."C#".code // 0' output/cloc-output.json)
VUE_LINES=$(jq '."Vuejs Component".code // 0' output/cloc-output.json)
PHP_LINES=$(jq '.PHP.code // 0' output/cloc-output.json)
OTHER_LINES=$((TOTAL_LINES - JS_LINES - TS_LINES - JSX_LINES - PHP_LINES - CSHARP_LINES - VUE_LINES))
# Function to format numbers with commas (ensuring proper locale settings)
format_number() {
export LC_ALL="en_US.UTF-8"
printf "%'d\n" $1
}
FORMATTED_TOTAL=$(format_number $TOTAL_LINES)
FORMATTED_JS=$(format_number $JS_LINES)
FORMATTED_TS=$(format_number $TS_LINES)
FORMATTED_JSX=$(format_number $JSX_LINES)
FORMATTED_CSHARP=$(format_number $CSHARP_LINES)
FORMATTED_VUE=$(format_number $VUE_LINES)
FORMATTED_PHP=$(format_number $PHP_LINES)
FORMATTED_OTHER=$(format_number $OTHER_LINES)
CODE_BLOCK="\`\`\`
[ LANGUAGES BREAKDOWN ]
JavaScript --> $FORMATTED_JS lines
TypeScript --> $FORMATTED_TS lines
JSX --> $FORMATTED_JSX lines
Vue.js --> $FORMATTED_VUE lines
PHP --> $FORMATTED_PHP lines
C# --> $FORMATTED_CSHARP lines
Other --> $FORMATTED_OTHER lines
[ TOTAL LINES OF CODE: $FORMATTED_TOTAL ]
\`\`\`"
# Update README.md by replacing the section between predefined comment markers
echo "$CODE_BLOCK" > temp_block.txt
sed -i '/<!-- LANGUAGES BREAKDOWN START -->/,/<!-- LANGUAGES BREAKDOWN END -->/{
//!d
/<!-- LANGUAGES BREAKDOWN START -->/r temp_block.txt
}' README.md
rm temp_block.txt
git add output/cloc-output.json README.md
git commit -m "chore: update README and cloc-output.json with latest code stats" || echo "No changes to commit"
git push origin HEAD