forked from arhamkhnz/github-code-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze-code.yml
More file actions
134 lines (107 loc) · 5.57 KB
/
analyze-code.yml
File metadata and controls
134 lines (107 loc) · 5.57 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
env:
# Check cloc docs for supported languages and exact names (e.g., "Vuejs Component", "C#")
# HIGHLIGHT_LANGS: show these languages individually; everything else goes to "Others"
HIGHLIGHT_LANGS: "JavaScript,TypeScript,JSX,Vuejs Component,PHP,C#"
# IGNORE_LANGS: drop these languages entirely (not shown and not counted)
IGNORE_LANGS: "JSON,HTML,CSS,SCSS,Sass,Markdown,SVG,XML,YAML,TOML,CSV,Text,Properties"
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: |
# 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
# Count LOC for all cloned repos; exclude languages via cloc's own --exclude-lang so totals match SUM
# NOTE: This does NOT apply .gitignore automatically across multiple repos; for per-repo .gitignore, run cloc per folder
cloc . --json --report-file=../output/cloc-output.json --exclude-lang="${IGNORE_LANGS}"
# 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"
# Number formatting: enable thousands separators for all printf in this step
# LC_ALL/LANG must be set before any printf that uses %'d
# --- format helper ---
format_number() {
printf "%'d\n" "$1"
}
# Ensure grouping is enabled for every printf in this step
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
# Grab total from cloc
TOTAL_LINES=$(jq '.SUM.code // 0' output/cloc-output.json)
OTHER_LINES=0
FORMATTED_BREAKDOWN=""
# Normalize the highlight list for exact, comma-bounded matching (no false partial matches)
HL=",$(echo "$HIGHLIGHT_LANGS" | tr -d ' '),"
while read -r entry; do
LANG=$(echo "$entry" | jq -r '.lang')
LINES=$(echo "$entry" | jq -r '.lines')
if [[ "$HL" == *",$LANG,"* ]]; then
FORMATTED=$(format_number "$LINES")
FORMATTED_BREAKDOWN+=$(printf "%-12s --> %s lines\n" "$LANG" "$FORMATTED")$'\n'
else
OTHER_LINES=$((OTHER_LINES + LINES))
fi
done < <(
jq -c 'to_entries
| map(select(.key != "header" and .key != "SUM"))
| map({lang: .key, lines: .value.code})
| map(select(.lines > 0))
| .[]' output/cloc-output.json
)
# "Others" includes all non-highlighted languages that cloc reported (after --exclude-lang); excluded ones never reach here
if [[ $OTHER_LINES -gt 0 ]]; then
FORMATTED_OTHER=$(format_number "$OTHER_LINES")
FORMATTED_BREAKDOWN+=$(printf "%-12s --> %s lines\n" "Others" "$FORMATTED_OTHER")$'\n'
fi
# Format total ONCE (do not overwrite later)
FORMATTED_TOTAL=$(format_number "$TOTAL_LINES")
CODE_BLOCK="\`\`\`
[ LANGUAGES BREAKDOWN ]
$FORMATTED_BREAKDOWN
[ TOTAL LINES OF CODE: $FORMATTED_TOTAL ]
\`\`\`"
# Replace content between markers. Requires these in README.md:
# <!-- LANGUAGES BREAKDOWN START --> ... <!-- LANGUAGES BREAKDOWN END -->
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