Skip to content

Commit d74dfe4

Browse files
committed
add csv and md report format options
1 parent d6e2cd6 commit d74dfe4

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

gh-cli/get-actions-usage-in-organization.sh

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# Returns a list of all actions used in an organization using the SBOM API
44

55
# Example usage:
6-
# - ./get-actions-usage-in-repository.sh joshjohanning-org count-by-version
7-
# - ./get-actions-usage-in-repository.sh joshjohanning-org count-by-action
6+
# - ./get-actions-usage-in-repository.sh joshjohanning-org count-by-version txt > output.txt
7+
# - ./get-actions-usage-in-repository.sh joshjohanning-org count-by-action md > output.md
88

99
# count-by-version (default): returns a count of actions by version; actions/checkout@v2 would be counted separately from actions/checkout@v3
1010
# count-by-action: returns a count of actions by action name; only care about actions/checkout usage, not the version
@@ -13,18 +13,23 @@
1313
# - The count returned is the # of repositories that use the action - if a single repository uses the action 2x times, it will only be counted 1x
1414
# - The script will take about 1 minute per 100 repositories
1515

16-
if [ $# -lt 1 ] || [ $# -gt 2 ] ; then
17-
echo "Usage: $0 <org> <count-by-version (default) | count-by-action>"
16+
if [ $# -lt 1 ] || [ $# -gt 3 ] ; then
17+
echo "Usage: $0 <org> <count-by-version (default) | count-by-action> | <report format: txt (default) | csv | md>"
1818
exit 1
1919
fi
2020

2121
org=$1
2222
count_method=$2
23+
report_format=$3
2324

2425
if [ -z "$count_method" ]; then
2526
count_method="count-by-version"
2627
fi
2728

29+
if [ -z "$report_format" ]; then
30+
report_format="txt"
31+
fi
32+
2833
repos=$(gh api graphql --paginate -F org="$org" -f query='query($org: String!$endCursor: String){
2934
organization(login:$org) {
3035
repositories(first:100,after: $endCursor) {
@@ -42,6 +47,16 @@ organization(login:$org) {
4247
}
4348
}' --template '{{range .data.organization.repositories.nodes}}{{printf "%s/%s\n" .owner.login .name}}{{end}}')
4449

50+
# if report_format = md
51+
if [ "$report_format" == "md" ]; then
52+
echo "## 🚀 Actions Usage in Organization: $org"
53+
echo ""
54+
echo "| Count | Action |"
55+
echo "| --- | --- |"
56+
elif [ "$report_format" == "csv" ]; then
57+
echo "Count,Action"
58+
fi
59+
4560
actions=()
4661

4762
for repo in $repos; do
@@ -57,4 +72,13 @@ if [ "$count_method" == "count-by-action" ]; then
5772
results=$(echo -e "${results[@]}" | sed 's/@.*//g')
5873
fi
5974

60-
echo -e "$results" | sort | uniq -c | sort -nr | awk '{print $1 " " $2}'
75+
results=$(echo -e "$results" | sort | uniq -c | sort -nr | awk '{print $1 " " $2}')
76+
77+
# if report_format = md
78+
if [ "$report_format" == "md" ]; then
79+
echo -e "${results[@]}" | awk '{print "| " $1 " | " $2 " |"}'
80+
elif [ "$report_format" == "csv" ]; then
81+
echo -e "${results[@]}" | awk '{print $1 "," $2}'
82+
else
83+
echo -e "${results[@]}"
84+
fi

0 commit comments

Comments
 (0)