Skip to content

Commit 7854494

Browse files
author
jxu3
committed
Add comprehensive tests for logger, main functionality, models, and proxy handling
- Introduced logger tests to validate logging levels and initialization. - Enhanced main function tests to cover command execution and environment variable handling. - Added tests for model fetching and validation, including error scenarios. - Implemented proxy request handling tests to ensure proper request processing and error management. - Established server tests for health checks and graceful shutdowns. - Refactored existing code to improve testability and maintainability.
1 parent 12cedb4 commit 7854494

25 files changed

+2739
-136
lines changed

.dockerignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Build artifacts
2+
github-copilot-svcs
3+
github-copilot-svcs-*
4+
*.exe
5+
6+
# Go build cache
7+
.go-build-cache
8+
9+
# Test artifacts
10+
coverage.out
11+
coverage.html
12+
13+
# IDE files
14+
.vscode/
15+
.idea/
16+
17+
# OS files
18+
.DS_Store
19+
Thumbs.db
20+
21+
# Temporary files
22+
*.tmp
23+
*.log
24+
25+
# Config files (sensitive)
26+
config.json

.github/workflows/ci.yml

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main, dev ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
test:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Go
20+
uses: actions/setup-go@v4
21+
with:
22+
go-version: '1.21'
23+
24+
- name: Cache Go modules
25+
uses: actions/cache@v3
26+
with:
27+
path: ~/go/pkg/mod
28+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
29+
restore-keys: |
30+
${{ runner.os }}-go-
31+
32+
- name: Download dependencies
33+
run: go mod download
34+
35+
- name: Run tests
36+
run: |
37+
go test -v -race -coverprofile=coverage.out ./...
38+
go tool cover -html=coverage.out -o coverage.html
39+
40+
- name: Upload coverage reports
41+
uses: actions/upload-artifact@v3
42+
with:
43+
name: coverage-report
44+
path: |
45+
coverage.out
46+
coverage.html
47+
48+
- name: Upload coverage to Codecov
49+
uses: codecov/codecov-action@v3
50+
with:
51+
file: ./coverage.out
52+
flags: unittests
53+
name: codecov-umbrella
54+
55+
lint:
56+
runs-on: ubuntu-latest
57+
steps:
58+
- name: Checkout code
59+
uses: actions/checkout@v4
60+
61+
- name: Set up Go
62+
uses: actions/setup-go@v4
63+
with:
64+
go-version: '1.21'
65+
66+
- name: golangci-lint
67+
uses: golangci/golangci-lint-action@v3
68+
with:
69+
version: latest
70+
args: --timeout=5m
71+
72+
security:
73+
runs-on: ubuntu-latest
74+
steps:
75+
- name: Checkout code
76+
uses: actions/checkout@v4
77+
78+
- name: Set up Go
79+
uses: actions/setup-go@v4
80+
with:
81+
go-version: '1.21'
82+
83+
- name: Run Gosec Security Scanner
84+
uses: securecodewarrior/github-action-gosec@master
85+
with:
86+
args: './...'
87+
88+
build:
89+
runs-on: ubuntu-latest
90+
needs: [test, lint]
91+
steps:
92+
- name: Checkout code
93+
uses: actions/checkout@v4
94+
95+
- name: Set up Go
96+
uses: actions/setup-go@v4
97+
with:
98+
go-version: '1.21'
99+
100+
- name: Build binary
101+
run: |
102+
go build -ldflags="-s -w -X main.version=ci-${{ github.sha }}" -o github-copilot-svcs .
103+
104+
- name: Upload build artifact
105+
uses: actions/upload-artifact@v3
106+
with:
107+
name: github-copilot-svcs-${{ github.sha }}
108+
path: github-copilot-svcs
109+
110+
docker:
111+
runs-on: ubuntu-latest
112+
needs: [test, lint]
113+
steps:
114+
- name: Checkout code
115+
uses: actions/checkout@v4
116+
117+
- name: Set up Docker Buildx
118+
uses: docker/setup-buildx-action@v3
119+
120+
- name: Login to Docker Hub
121+
if: github.event_name != 'pull_request'
122+
uses: docker/login-action@v3
123+
with:
124+
username: ${{ secrets.DOCKER_USERNAME }}
125+
password: ${{ secrets.DOCKER_PASSWORD }}
126+
127+
- name: Extract metadata
128+
id: meta
129+
uses: docker/metadata-action@v5
130+
with:
131+
images: ${{ secrets.DOCKER_USERNAME }}/github-copilot-svcs
132+
tags: |
133+
type=ref,event=branch
134+
type=ref,event=pr
135+
type=sha,prefix={{branch}}-
136+
type=raw,value=latest,enable={{is_default_branch}}
137+
138+
- name: Build and push Docker image
139+
uses: docker/build-push-action@v5
140+
with:
141+
context: .
142+
platforms: linux/amd64,linux/arm64
143+
push: ${{ github.event_name != 'pull_request' }}
144+
tags: ${{ steps.meta.outputs.tags }}
145+
labels: ${{ steps.meta.outputs.labels }}
146+
cache-from: type=gha
147+
cache-to: type=gha,mode=max

.github/workflows/release.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,52 @@ jobs:
144144
asset_path: ./github-copilot-svcs-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.suffix }}.gz
145145
asset_name: github-copilot-svcs-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.suffix }}.gz
146146
asset_content_type: application/gzip
147+
148+
docker:
149+
needs: release
150+
runs-on: ubuntu-latest
151+
steps:
152+
- name: Checkout code
153+
uses: actions/checkout@v4
154+
155+
- name: Set up Docker Buildx
156+
uses: docker/setup-buildx-action@v3
157+
158+
- name: Login to Docker Hub
159+
uses: docker/login-action@v3
160+
with:
161+
username: ${{ secrets.DOCKER_USERNAME }}
162+
password: ${{ secrets.DOCKER_PASSWORD }}
163+
164+
- name: Login to GitHub Container Registry
165+
uses: docker/login-action@v3
166+
with:
167+
registry: ghcr.io
168+
username: ${{ github.actor }}
169+
password: ${{ secrets.GITHUB_TOKEN }}
170+
171+
- name: Extract metadata
172+
id: meta
173+
uses: docker/metadata-action@v5
174+
with:
175+
images: |
176+
${{ secrets.DOCKER_USERNAME }}/github-copilot-svcs
177+
ghcr.io/${{ github.repository }}
178+
tags: |
179+
type=semver,pattern={{version}},value=${{ needs.release.outputs.version }}
180+
type=semver,pattern={{major}}.{{minor}},value=${{ needs.release.outputs.version }}
181+
type=semver,pattern={{major}},value=${{ needs.release.outputs.version }}
182+
type=raw,value=latest
183+
184+
- name: Build and push Docker image
185+
uses: docker/build-push-action@v5
186+
with:
187+
context: .
188+
platforms: linux/amd64,linux/arm64
189+
push: true
190+
tags: ${{ steps.meta.outputs.tags }}
191+
labels: ${{ steps.meta.outputs.labels }}
192+
build-args: |
193+
VERSION=${{ needs.release.outputs.version }}
194+
cache-from: type=gha
195+
cache-to: type=gha,mode=max

.golangci.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
run:
2+
timeout: 5m
3+
modules-download-mode: readonly
4+
5+
linters-settings:
6+
govet:
7+
enable:
8+
- shadow
9+
gocyclo:
10+
min-complexity: 20
11+
dupl:
12+
threshold: 100
13+
goconst:
14+
min-len: 2
15+
min-occurrences: 3
16+
misspell:
17+
locale: US
18+
lll:
19+
line-length: 140
20+
goimports:
21+
local-prefixes: github.com/privapps/github-copilot-svcs
22+
gocritic:
23+
enabled-tags:
24+
- diagnostic
25+
- performance
26+
- style
27+
disabled-checks:
28+
- dupImport
29+
- ifElseChain
30+
- octalLiteral
31+
- whyNoLint
32+
- wrapperFunc
33+
- returnAfterHttpError
34+
gosec:
35+
excludes:
36+
- G101 # Potential hardcoded credentials - these are URLs, not credentials
37+
- G108 # Profiling endpoint - intentionally exposed for monitoring
38+
39+
linters:
40+
disable-all: true
41+
enable:
42+
- bodyclose
43+
- dogsled
44+
- dupl
45+
- errcheck
46+
- gochecknoinits
47+
- goconst
48+
- gocritic
49+
- gocyclo
50+
- gofmt
51+
- goimports
52+
- mnd
53+
- goprintffuncname
54+
- gosec
55+
- gosimple
56+
- govet
57+
- ineffassign
58+
- lll
59+
- misspell
60+
- nakedret
61+
- revive
62+
- staticcheck
63+
- stylecheck
64+
- typecheck
65+
- unconvert
66+
- unparam
67+
- unused
68+
- whitespace
69+
70+
issues:
71+
exclude-rules:
72+
- path: _test\.go
73+
linters:
74+
- mnd
75+
- gosec
76+
- path: main\.go
77+
linters:
78+
- gochecknoinits
79+
- text: "G108.*pprof"
80+
linters:
81+
- gosec
82+
- text: "G101.*URL"
83+
linters:
84+
- gosec

Dockerfile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
FROM golang:1.21-alpine AS builder
2+
3+
WORKDIR /app
4+
5+
# Install git and ca-certificates for building
6+
RUN apk add --no-cache git ca-certificates
7+
8+
# Copy go mod files
9+
COPY go.mod go.sum ./
10+
11+
# Download dependencies
12+
RUN go mod download
13+
14+
# Copy source code
15+
COPY . .
16+
17+
# Build the binary
18+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w -X main.version=docker" -o github-copilot-svcs .
19+
20+
# Final stage
21+
FROM alpine:latest
22+
23+
# Install ca-certificates for HTTPS requests
24+
RUN apk --no-cache add ca-certificates tzdata
25+
26+
WORKDIR /root/
27+
28+
# Copy the binary from builder
29+
COPY --from=builder /app/github-copilot-svcs .
30+
31+
# Create config directory
32+
RUN mkdir -p /root/.local/share/github-copilot-svcs
33+
34+
# Expose the default port
35+
EXPOSE 8081
36+
37+
# Health check
38+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
39+
CMD wget --no-verbose --tries=1 --spider http://localhost:8081/health || exit 1
40+
41+
# Run the binary
42+
CMD ["./github-copilot-svcs", "run"]

0 commit comments

Comments
 (0)