|
3 | 3 | # Log the start of the script |
4 | 4 | echo "Starting script to get latest failed workflow logs..." |
5 | 5 |
|
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#') |
10 | 17 |
|
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 |
14 | 37 |
|
15 | 38 | # 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') |
17 | 40 |
|
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." |
22 | 44 | exit 1 |
23 | 45 | fi |
24 | 46 |
|
25 | 47 | # Log the run ID |
26 | 48 | echo "Latest workflow run ID: $run_id" |
27 | 49 |
|
28 | 50 | # 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') |
30 | 52 |
|
31 | 53 | # Check if the gh command failed |
32 | | -if [[ $? -ne 0 ]]; then |
33 | | - # Log the error and exit |
| 54 | +if [[ -z "$conclusion" ]]; then |
34 | 55 | echo "Error: Failed to get workflow run details." |
35 | 56 | exit 1 |
36 | 57 | fi |
|
0 commit comments