Skip to content
This repository was archived by the owner on May 3, 2025. It is now read-only.

Commit 63b1bad

Browse files
committed
feat: allow specifying repository as command-line arguments
This commit adds the ability to specify the GitHub repository owner and name as command-line arguments to the get-latest-failed-gha-logs.sh script. It also adds checks to ensure that both `gh` and `jq` are installed, and improves error handling for cases where no workflow runs are found. This enhances the script's flexibility and robustness.
1 parent 5212bf6 commit 63b1bad

1 file changed

Lines changed: 36 additions & 15 deletions

File tree

get-latest-failed-gha-logs.sh

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,55 @@
33
# Log the start of the script
44
echo "Starting script to get latest failed workflow logs..."
55

6-
# Get the repository owner and name from the origin remote
7-
remote_url=$(git remote get-url origin)
8-
repo_owner=$(echo "$remote_url" | sed -E 's#.*github.com[:/]([^/]+)/.*#\1#')
9-
repo_name=$(echo "$remote_url" | sed -E 's#.*github.com[:/].*/([^/]+).*#\1#')
6+
# Check if a repository is provided as an argument
7+
if [[ $# -eq 2 ]]; then
8+
repo_owner=$1
9+
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#')
1017

11-
# Log the repository information
12-
echo "Repository owner: $repo_owner"
13-
echo "Repository name: $repo_name"
18+
# Log the repository information
19+
echo "Repository owner: $repo_owner"
20+
echo "Repository name: $repo_name"
21+
else
22+
echo "Usage: $0 [owner] [repo]"
23+
echo "Or run in a git repository to use the origin remote."
24+
exit 1
25+
fi
26+
27+
# Check if gh and jq are installed
28+
if ! command -v gh &> /dev/null; then
29+
echo "Error: gh command not found. Please install the GitHub CLI."
30+
exit 1
31+
fi
32+
33+
if ! command -v jq &> /dev/null; then
34+
echo "Error: jq command not found. Please install jq."
35+
exit 1
36+
fi
1437

1538
# Get the latest workflow run ID (using jq) and redirect stderr to stdout
16-
run_id=$(gh run list -L 1 --json databaseId -R "$repo_owner/$repo_name" 2>&1 | jq -r '..databaseId')
39+
run_id=$(gh run list -L 1 --json databaseId -R "$repo_owner/$repo_name" 2>/dev/null | jq -r '.[0].databaseId')
1740

18-
# Check if the gh command failed
19-
if [[ $? -ne 0 ]]; then
20-
# Log the error and exit
21-
echo "Error: Failed to get workflow run list."
41+
# Check if the gh command failed or if no runs were found
42+
if [[ -z "$run_id" ]]; then
43+
echo "No workflow runs found or failed to get run list."
2244
exit 1
2345
fi
2446

2547
# Log the run ID
2648
echo "Latest workflow run ID: $run_id"
2749

2850
# Get the run conclusion (e.g., "success", "failure") and redirect stderr to stdout
29-
conclusion=$(gh run view $run_id --json conclusion -R "$repo_owner/$repo_name" 2>&1 | jq -r '.conclusion')
51+
conclusion=$(gh run view $run_id --json conclusion -R "$repo_owner/$repo_name" 2>/dev/null | jq -r '.conclusion')
3052

3153
# Check if the gh command failed
32-
if [[ $? -ne 0 ]]; then
33-
# Log the error and exit
54+
if [[ -z "$conclusion" ]]; then
3455
echo "Error: Failed to get workflow run details."
3556
exit 1
3657
fi

0 commit comments

Comments
 (0)