1+ #! /bin/bash
2+ # CI Interface Script - Direct interface with GitHub Actions
3+
4+ set -e
5+
6+ # Configuration
7+ REPO=" rowingdude/analyzeMFT"
8+ BRANCH=$( git branch --show-current 2> /dev/null || echo " main" )
9+
10+ # Colors
11+ RED=' \033[0;31m'
12+ GREEN=' \033[0;32m'
13+ YELLOW=' \033[1;33m'
14+ BLUE=' \033[0;34m'
15+ NC=' \033[0m'
16+
17+ echo -e " ${BLUE} GitHub Actions CI Interface${NC} "
18+ echo " Repository: $REPO "
19+ echo " Branch: $BRANCH "
20+ echo " "
21+
22+ # Function to setup GitHub CLI
23+ setup_gh () {
24+ if ! command -v gh & > /dev/null; then
25+ echo -e " ${YELLOW} GitHub CLI not found. Installing...${NC} "
26+
27+ # Detect OS and install
28+ if [[ " $OSTYPE " == " linux-gnu" * ]]; then
29+ curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
30+ echo " deb [arch=$( dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
31+ sudo apt update
32+ sudo apt install gh
33+ elif [[ " $OSTYPE " == " darwin" * ]]; then
34+ brew install gh
35+ else
36+ echo -e " ${RED} Please install GitHub CLI manually: https://cli.github.com${NC} "
37+ exit 1
38+ fi
39+ fi
40+
41+ # Check authentication
42+ if ! gh auth status & > /dev/null; then
43+ echo -e " ${YELLOW} Please authenticate with GitHub:${NC} "
44+ gh auth login
45+ fi
46+ }
47+
48+ # Function to push and monitor
49+ push_and_monitor () {
50+ echo -e " ${YELLOW} Pushing current branch to GitHub...${NC} "
51+ git push origin $BRANCH
52+
53+ echo -e " ${YELLOW} Waiting for workflow to start...${NC} "
54+ sleep 5
55+
56+ # Get the latest run for this branch
57+ run_id=$( gh run list --branch $BRANCH --limit 1 --json databaseId --jq ' .[0].databaseId' -R $REPO )
58+
59+ if [ -z " $run_id " ]; then
60+ echo -e " ${RED} No workflow run found${NC} "
61+ exit 1
62+ fi
63+
64+ echo -e " ${GREEN} Monitoring run $run_id ...${NC} "
65+ gh run watch $run_id -R $REPO
66+
67+ # Get conclusion
68+ conclusion=$( gh run view $run_id --json conclusion --jq ' .conclusion' -R $REPO )
69+
70+ if [ " $conclusion " = " failure" ]; then
71+ echo -e " ${RED} Run failed! Getting error details...${NC} "
72+ gh run view $run_id --log-failed -R $REPO | head -100
73+
74+ # Parse and show test failures
75+ echo -e " \n${YELLOW} Test Failures Summary:${NC} "
76+ gh run view $run_id --log-failed -R $REPO | grep -E " FAILED|ERROR" | head -20
77+
78+ return 1
79+ else
80+ echo -e " ${GREEN} All tests passed!${NC} "
81+ return 0
82+ fi
83+ }
84+
85+ # Function to run tests locally first
86+ local_test () {
87+ echo -e " ${BLUE} Running local tests first...${NC} "
88+
89+ if [ -f " run_tests.sh" ]; then
90+ bash run_tests.sh
91+ else
92+ python -m pytest tests/ -q --tb=no --maxfail=5
93+ fi
94+
95+ if [ $? -ne 0 ]; then
96+ echo -e " ${RED} Local tests failed. Fix them before pushing.${NC} "
97+ return 1
98+ fi
99+
100+ echo -e " ${GREEN} Local tests passed!${NC} "
101+ return 0
102+ }
103+
104+ # Function to get real-time logs
105+ stream_logs () {
106+ local run_id=$1
107+ if [ -z " $run_id " ]; then
108+ run_id=$( gh run list --limit 1 --json databaseId --jq ' .[0].databaseId' -R $REPO )
109+ fi
110+
111+ echo -e " ${BLUE} Streaming logs for run $run_id ...${NC} "
112+
113+ # Stream logs in real-time
114+ while true ; do
115+ status=$( gh run view $run_id --json status --jq ' .status' -R $REPO )
116+
117+ if [ " $status " = " completed" ]; then
118+ break
119+ fi
120+
121+ # Get current logs
122+ gh run view $run_id --log -R $REPO | tail -50
123+
124+ sleep 5
125+ done
126+
127+ conclusion=$( gh run view $run_id --json conclusion --jq ' .conclusion' -R $REPO )
128+ echo -e " \nRun completed: $conclusion "
129+ }
130+
131+ # Function to fetch errors as JSON
132+ get_errors_json () {
133+ local run_id=$1
134+ if [ -z " $run_id " ]; then
135+ run_id=$( gh run list --limit 1 --json databaseId --jq ' .[0].databaseId' -R $REPO )
136+ fi
137+
138+ # Create JSON output
139+ echo " {"
140+ echo " \" run_id\" : $run_id ,"
141+ echo " \" errors\" : ["
142+
143+ gh run view $run_id --log-failed -R $REPO | grep -E " FAILED|ERROR" | while read -r line; do
144+ escaped=$( echo " $line " | sed ' s/"/\\"/g' )
145+ echo " \" $escaped \" ,"
146+ done | sed ' $ s/,$//'
147+
148+ echo " ]"
149+ echo " }"
150+ }
151+
152+ # Main menu
153+ case " ${1:- } " in
154+ setup)
155+ setup_gh
156+ ;;
157+ test)
158+ local_test
159+ ;;
160+ push)
161+ local_test && push_and_monitor
162+ ;;
163+ monitor)
164+ push_and_monitor
165+ ;;
166+ stream)
167+ stream_logs $2
168+ ;;
169+ errors)
170+ get_errors_json $2
171+ ;;
172+ status)
173+ gh run list --limit 5 -R $REPO
174+ ;;
175+ * )
176+ echo " Usage: $0 [command] [options]"
177+ echo " "
178+ echo " Commands:"
179+ echo " setup - Setup GitHub CLI"
180+ echo " test - Run local tests"
181+ echo " push - Run tests locally, then push and monitor"
182+ echo " monitor - Push and monitor GitHub Actions"
183+ echo " stream - Stream logs from a run"
184+ echo " errors - Get errors as JSON"
185+ echo " status - Show recent runs"
186+ echo " "
187+ echo " Examples:"
188+ echo " $0 setup"
189+ echo " $0 test"
190+ echo " $0 push"
191+ echo " $0 errors"
192+ ;;
193+ esac
0 commit comments