forked from NotHarshhaa/into-the-devops
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_ci.sh
More file actions
186 lines (149 loc) · 5.13 KB
/
run_ci.sh
File metadata and controls
186 lines (149 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env bash
# CI Script for Into The DevOps Repository
# Author: H A R S H A A
# Description: Performs various checks and validations on the repository content
# Strict mode settings
set -euo pipefail
IFS=$'\n\t'
# Color codes for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color
# Script variables
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly PROJECT_DIR="$(dirname "${SCRIPT_DIR}")"
readonly MAX_LINE_LENGTH=100
readonly PYTHON_MIN_VERSION="3.6"
# Log functions
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" >&2; }
log_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
# Check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Verify system requirements
check_requirements() {
log_info "Checking system requirements..."
# Check Python version
if ! command_exists python3; then
log_error "Python 3 is required but not installed."
exit 1
fi
local python_version=$(python3 -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
if ! awk -v ver="$python_version" -v req="$PYTHON_MIN_VERSION" 'BEGIN{exit(!(ver>=req))}'; then
log_error "Python version must be >= $PYTHON_MIN_VERSION (found: $python_version)"
exit 1
fi
# Check for flake8
if ! command_exists flake8; then
log_error "flake8 is required but not installed. Install with: pip install flake8"
exit 1
}
log_success "All requirements satisfied"
}
# Find all markdown files
find_markdown_files() {
log_info "Locating markdown files..."
local md_files=($(find "${PROJECT_DIR}" -name "*.md" -not -path "${PROJECT_DIR}/tests/*"))
if [ ${#md_files[@]} -eq 0 ]; then
log_warning "No markdown files found!"
return 1
fi
echo "${md_files[@]}"
}
# Run syntax lint on markdown files
check_markdown_syntax() {
local file="$1"
local relative_path="${file#$PROJECT_DIR/}"
log_info "Checking syntax: $relative_path"
if ! python3 "${PROJECT_DIR}/tests/syntax_lint.py" "$file" > /dev/null; then
log_error "Syntax check failed for: $relative_path"
return 1
fi
return 0
}
# Run PEP8 checks
check_pep8() {
log_info "Running PEP8 checks..."
if ! flake8 --max-line-length=$MAX_LINE_LENGTH .; then
log_error "PEP8 check failed"
return 1
fi
return 0
}
# Check for broken links in markdown files
check_broken_links() {
local file="$1"
local relative_path="${file#$PROJECT_DIR/}"
log_info "Checking links in: $relative_path"
# Find all markdown links and verify they exist
local links=($(grep -o '\[.*\](\([^)]*\))' "$file" | sed 's/.*(\(.*\))/\1/'))
for link in "${links[@]}"; do
# Skip external URLs
if [[ $link =~ ^https?:// ]]; then
continue
fi
# Check if internal link exists
if [[ $link == /* ]]; then
link="${PROJECT_DIR}${link}"
else
link="$(dirname "$file")/${link}"
fi
if [[ ! -e "$link" ]]; then
log_warning "Broken link found in $relative_path: $link"
fi
done
}
# Main execution function
main() {
local error_count=0
local warning_count=0
echo "================================================"
log_info "Starting CI checks for Into The DevOps"
echo "================================================"
# Check requirements first
check_requirements
# Process markdown files
local md_files=($(find_markdown_files))
local total_files=${#md_files[@]}
local current=0
echo "------------------------------------------------"
log_info "Processing $total_files markdown files..."
echo "------------------------------------------------"
for file in "${md_files[@]}"; do
((current++))
echo -ne "\rProgress: [$current/$total_files]"
if ! check_markdown_syntax "$file"; then
((error_count++))
fi
check_broken_links "$file"
done
echo # New line after progress
echo "------------------------------------------------"
log_info "Running code style checks..."
echo "------------------------------------------------"
if ! check_pep8; then
((error_count++))
fi
echo "================================================"
log_info "CI Check Summary"
echo "------------------------------------------------"
echo "Files processed: $total_files"
echo "Errors found: $error_count"
echo "Warnings found: $warning_count"
echo "================================================"
if [ $error_count -gt 0 ]; then
log_error "CI checks failed with $error_count errors"
exit 1
fi
log_success "All CI checks passed successfully!"
exit 0
}
# Trap errors
trap 'echo -e "\n${RED}Script failed${NC}: line $LINENO with exit code $?" >&2' ERR
# Run main function
main "$@"