11#! /bin/bash
22
3- # Log the start of the script
4- echo " Starting script to get latest failed workflow logs..."
3+ # Parse command-line options
4+ debug=false
5+ while [[ $# -gt 0 ]]; do
6+ case " $1 " in
7+ -d|--debug)
8+ debug=true
9+ shift
10+ ;;
11+ * )
12+ break
13+ ;;
14+ esac
15+ done
516
617# Check if a repository is provided as an argument
718if [[ $# -eq 2 ]]; then
819 repo_owner=$1
920 repo_name=$2
10- echo " Repository owner: $repo_owner "
11- echo " Repository name: $repo_name "
12- elif [[ $# -eq 0 ] ]; then
13- # Get the repository owner and name from the origin remote
14- remote_url= $ ( git remote get-url origin)
15- repo_owner=$( echo " $remote_url " | sed -E ' s#. *github.com[:/]( [^/]+)/.*#\1# ' )
16- repo_name= $ ( echo " $remote_url " | sed -E ' s#. *github.com[:/].*/( [^/]+).*#\1 #' | sed ' s/\.git$ //' ) # Remove .git
21+ if $debug ; then echo " Repository owner: $repo_owner " ; fi
22+ if $debug ; then echo " Repository name: $repo_name " ; fi
23+ elif [[ < span class = " math-inline " > \# \ - eq 0 \]\ ]; then
24+ \ # Get the repository owner and name from the origin remote
25+ remote \_ url \= < /span > (git remote get-url origin)
26+ repo_owner= $( echo " <span class= " math-inline " >remote\_url " \ | sed \ - E ' s\#\.\ *github\ .com\[\:/\]\(\ [^/\]\+\)/\.\*\#\\1\# ' \ )
27+ repo \_ name \= < /span > ( echo " <span class= " math-inline " >remote\_url " \ | sed \ - E ' s\#\.\ *github\ .com\[\:/\]\.\*/\(\ [^/\]\+\)\.\*\#\\1\ #' \ | sed ' s/\\\ .git</span> //' ) # Remove .git
1728 # Log the repository information
18- echo " Repository owner: $repo_owner "
19- echo " Repository name: $repo_name "
29+ if $debug ; then echo " Repository owner: $repo_owner " ; fi
30+ if $debug ; then echo " Repository name: $repo_name " ; fi
2031else
21- echo " Usage: $0 [ owner] [repo]"
22- echo " Or run in a git repository to use the origin remote."
23- exit 1
32+ echo " Usage: <span class= " math-inline " >0 \[\-d\|\-\-debug\] \[ owner\] \ [repo\ ]"
33+ echo " Or run in a git repository to use the origin remote\ ."
34+ exit 1
2435fi
25-
26- # Check if gh and jq are installed
27- if ! command -v gh & > /dev/null; then
28- echo " Error: gh command not found. Please install the GitHub CLI."
29- exit 1
36+ \# Check if gh and jq are installed
37+ if \! command \- v gh & \> /dev/null; then
38+ echo " Error\: gh command not found\. Please install the GitHub CLI\."
39+ exit 1
3040fi
31-
32- if ! command -v jq & > /dev/null; then
33- echo " Error: jq command not found. Please install jq."
34- exit 1
41+ if \! command \- v jq & \> /dev/null; then
42+ echo " Error\: jq command not found\. Please install jq\."
43+ exit 1
3544fi
36-
37- # Get the latest workflow run ID (using jq) and redirect stderr to stdout
38- run_id=$( gh run list -L 1 --json databaseId -R " $repo_owner /$repo_name " 2> /dev/null | jq -r ' .[0].databaseId' )
45+ \# Get the latest workflow run ID \( using jq\) and redirect stderr to stdout
46+ run\_ id\= < /span>( gh run list -L 1 --json databaseId -R " $repo_owner /$repo_name " 2> /dev/null | jq -r ' .[0].databaseId' )
3947
4048# Check if the gh command failed or if no runs were found
4149if [[ -z " $run_id " ]]; then
@@ -44,10 +52,9 @@ if [[ -z "$run_id" ]]; then
4452fi
4553
4654# Log the run ID
47- echo " Latest workflow run ID: $run_id "
48-
49- # Get the run conclusion (e.g., "success", "failure") and redirect stderr to stdout
50- conclusion=$( gh run view $run_id --json conclusion -R " $repo_owner /$repo_name " 2> /dev/null | jq -r ' .conclusion' )
55+ if $debug ; then echo " Latest workflow run ID: <span class=" math-inline" >run\_id" ; fi
56+ \# Get the run conclusion \( e\. g\. , " success" , " failure" \) and redirect stderr to stdout
57+ conclusion\= < /span>( gh run view $run_id --json conclusion -R " $repo_owner /$repo_name " 2> /dev/null | jq -r ' .conclusion' )
5158
5259# Check if the gh command failed
5360if [[ -z " $conclusion " ]]; then
@@ -56,16 +63,23 @@ if [[ -z "$conclusion" ]]; then
5663fi
5764
5865# Log the run conclusion
59- echo " Workflow run conclusion: $conclusion "
66+ if $debug ; then echo " Workflow run conclusion: $conclusion " ; fi
6067
6168# Check if the run failed
6269if [[ " $conclusion " == " failure" ]]; then
63- # Log the failure and view the failed logs
64- echo " Workflow run failed. Viewing failed logs..."
65- gh run view $run_id --log-failed -R " $repo_owner /$repo_name "
66- else
67- echo " The latest workflow run was successful."
68- fi
70+ # Construct and print the command
71+ command_to_run=" gh run view \" $run_id \" --log-failed -R \" $repo_owner /$repo_name \" "
72+ if $debug ; then echo " Executing: <span class=" math-inline" >command\_to\_run" ; fi
73+ \# Capture the output of gh run view
74+ failed\_ logs\= < /span>( eval " $command_to_run " ) # Use eval to execute the command
6975
70- # Log the end of the script
71- echo " Script finished."
76+ # Print the captured logs
77+ echo " $failed_logs "
78+
79+ if $debug ; then echo " Failure status: $conclusion " ; fi
80+ if $debug ; then echo " Run ID: $run_id " ; fi
81+ else
82+ success_message=" The latest workflow run was successful."
83+ if $debug ; then echo " $success_message " ; fi
84+ if $debug ; then echo " Success status: $conclusion " ; fi
85+ if $debug ; then echo "
0 commit comments