Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
362 changes: 362 additions & 0 deletions scripts/lint-automation/ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,362 @@
# Lint Automation System Architecture

## System Flow Diagram

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Deploy Next.js Workflow β”‚
β”‚ (Triggers on push) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”‚ (workflow_run on completed)
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Lint Automation Workflow β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1. Checkout Code β”‚
β”‚ 2. Install Dependencies β”‚
β”‚ 3. Run Lint Analysis ─────────────────────────┐ β”‚
β”‚ β”œβ”€ ESLint Check β”‚ β”‚
β”‚ └─ TypeScript Compiler Check β”‚ β”‚
β”‚ β–Ό β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ lint-analyzer.ts β”‚ β”‚
β”‚ β”‚ ───────────────── β”‚ β”‚
β”‚ β”‚ β€’ Parse ESLint β”‚ β”‚
β”‚ β”‚ β€’ Parse TSC output β”‚ β”‚
β”‚ β”‚ β€’ Analyze issues β”‚ β”‚
β”‚ β”‚ β€’ Generate reports β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ β”‚
β”‚ β–Ό β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ lint-analysis- β”‚ β”‚
β”‚ β”‚ report.json β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ 4. Create/Update GitHub Issues β”‚ β”‚
β”‚ β–Ό β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ github-issue- β”‚ β”‚
β”‚ β”‚ creator.ts β”‚ β”‚
β”‚ β”‚ ───────────────── β”‚ β”‚
β”‚ β”‚ β€’ Group by file β”‚ β”‚
β”‚ β”‚ β€’ Check existing β”‚ β”‚
β”‚ β”‚ β€’ Create/Update β”‚ β”‚
β”‚ β”‚ β€’ Close resolved β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚ β”‚
β–Ό β–Ό β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ New Issues β”‚ β”‚Update Issues β”‚ β”‚Close Issues β”‚
β”‚ Created β”‚ β”‚with changes β”‚ β”‚when resolved β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Component Details

### 1. lint-analyzer.ts

**Purpose**: Analyzes code and generates structured lint reports

**Key Methods**:
- `analyzeLintIssues()` - Main entry point
- `runLint()` - Executes ESLint
- `runTypeScriptCheck()` - Executes TSC
- `parseLintOutput()` - Parses ESLint output
- `parseTypeScriptOutput()` - Parses TSC output
- `analyzeIssue()` - Enriches issue with analysis

**Output**:
```json
{
"summary": {
"totalIssues": 10,
"errorCount": 8,
"warningCount": 2,
"affectedFiles": 5,
"commonPatterns": ["no-explicit-any (5)", "no-unused-vars (3)"]
},
"issues": [
{
"file": "./app/api/verify/missouri/logic.ts",
"line": 44,
"column": 27,
"severity": "error",
"message": "Unexpected any. Specify a different type.",
"ruleId": "@typescript-eslint/no-explicit-any",
"category": "Type Safety",
"likelyCause": "...",
"suggestedSolution": "...",
"preventionTip": "...",
"similarFiles": [],
"patternAnalysis": "..."
}
],
"recommendations": {...}
}
```

### 2. github-issue-creator.ts

**Purpose**: Manages GitHub issues based on lint reports

**Key Methods**:

#### Issue Creation & Management
- `createIssuesFromReport()` - Main orchestrator
- `createIssue()` - Creates new GitHub issue
- `checkExistingFileIssue()` - Searches for existing file issues
- `groupIssuesForGitHub()` - Groups violations by file

#### Issue Updates (NEW in v2.0)
- `updateExistingFileIssue()` - Updates existing issues with changes
- `extractViolationsFromIssueBody()` - Parses current violations
- `markIssueAsUnread()` - Adds "unread-updates" label

#### Resolution & Sign-off (NEW in v2.0)
- `closeResolvedFileIssue()` - Closes fixed issues
- `generateSolutionSignOff()` - Creates @copilot sign-off
- `getSolutionDescription()` - Maps solutions to rule types

#### Helpers
- `generateFileIssueBody()` - Creates issue body content
- `generateCodeExample()` - Generates before/after code examples
- `generateEnhancedTitle()` - Adds regional context to titles

## Data Flow

### Issue Creation Flow

```
Lint Report
β”‚
β”œβ”€ Group by File ────────────────┐
β”‚ β”‚
β–Ό β–Ό
For each file: File: missouri/logic.ts
β”‚ Issues: 3
β”‚ Rules: 2
β”œβ”€ Check existing issue
β”‚ β”‚
β”‚ β”œβ”€ Exists? ──── Yes ──▢ updateExistingFileIssue()
β”‚ β”‚ β”‚
β”‚ β”‚ β”œβ”€ Extract old violations
β”‚ β”‚ β”œβ”€ Extract new violations
β”‚ β”‚ β”œβ”€ Find differences
β”‚ β”‚ β”‚ β”œβ”€ New errors?
β”‚ β”‚ β”‚ β”‚ └─▢ Add comment
β”‚ β”‚ β”‚ β”‚ Mark unread
β”‚ β”‚ β”‚ └─ Resolved errors?
β”‚ β”‚ β”‚ └─▢ Add comment
β”‚ β”‚ └─ Update issue body
β”‚ β”‚
β”‚ └─ No ──────────▢ createIssue()
β”‚ β”‚
β”‚ └─▢ New GitHub Issue
β”‚
└─ Next file
```

### Issue Resolution Flow

```
Check all open issues
β”‚
β”œβ”€ Get current file issues from report
β”‚
β–Ό
For each open issue:
β”‚
β”œβ”€ File still has errors? ──── Yes ──▢ Skip
β”‚
β”œβ”€ File has no errors? ────── Yes ──▢ closeResolvedFileIssue()
β”‚
β”œβ”€ Extract resolved violations
β”œβ”€ Group by rule type
β”œβ”€ generateSolutionSignOff()
β”‚ β”œβ”€ List fixed violations
β”‚ β”œβ”€ Describe solutions
β”‚ └─ @copilot sign-off
β”œβ”€ Add comment
└─ Close issue
```

### Update Detection Logic

```
Existing Issue Body:
- Line 10: no-unused-vars
- Line 20: no-explicit-any
- Line 30: no-explicit-any

New Lint Report:
- Line 10: no-unused-vars
- Line 20: no-explicit-any
- Line 40: no-explicit-any (NEW!)

Comparison:
β”œβ”€ Unchanged: Line 10, 20
β”œβ”€ Resolved: Line 30
└─ New: Line 40

Result:
β”œβ”€ Add update comment:
β”‚ "πŸ†• New: Line 40
β”‚ βœ… Fixed: Line 30"
β”œβ”€ Update issue body
└─ Add "unread-updates" label
```

## State Diagram

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ No Issues β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”‚ Lint error detected
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Άβ”‚ Issue Open │◀──────┐
β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ β”‚
β”‚ β”‚ β”‚
New β”‚ β”‚ β”‚ More errors
errors β”‚ β”‚ β”‚ detected
detected β”‚ β–Ό β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Issue with β”‚β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚ "unread- β”‚
β”‚ β”‚ updates" β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β”‚ β”‚ Errors partially fixed
β”‚ β”‚
└──────────────────
β”‚
β”‚ All errors fixed
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Issue Closed β”‚
β”‚ with @copilotβ”‚
β”‚ sign-off β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Integration Points

### GitHub API Endpoints Used

1. **Issue Search**
- `GET /search/issues` - Find existing issues
- Query: `repo:owner/repo is:issue is:open label:lint`

2. **Issue Management**
- `POST /repos/:owner/:repo/issues` - Create issue
- `PATCH /repos/:owner/:repo/issues/:number` - Update issue
- `POST /repos/:owner/:repo/issues/:number/comments` - Add comment

3. **Label Management**
- `POST /repos/:owner/:repo/issues/:number/labels` - Add label
- Labels used: `lint`, `automated`, `unread-updates`, etc.

### Workflow Triggers

```yaml
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
workflow_run:
workflows: ["Deploy Next.js site to Pages"]
types: [completed]
branches: [main]
workflow_dispatch:
```

## Error Handling

### Graceful Degradation

1. **No GitHub Token**
- Logs what would be done
- Doesn't fail the workflow
- Useful for local testing

2. **API Rate Limiting**
- Delays between API calls (1.5s)
- Catches rate limit errors
- Continues with remaining issues

3. **Race Conditions**
- Random delays before creation
- Double-check before creating
- Update if created by another process

4. **Parsing Errors**
- Fallback to text parsing
- Skip malformed issues
- Log warnings, continue processing

## Performance Considerations

### Optimization Strategies

1. **Batching**: Process multiple issues in sequence
2. **Caching**: GitHub API responses cached during run
3. **Parallel Processing**: Could be added for large repos
4. **Rate Limiting**: 1.5s delay between API calls
5. **Conditional Execution**: Only run on main branch pushes

### Scalability

- **Small repos (<10 files)**: < 30 seconds
- **Medium repos (10-50 files)**: 1-2 minutes
- **Large repos (50+ files)**: 2-5 minutes

Each file issue takes ~2-3 seconds (API call + delay).

## Security

### Token Permissions Required

```yaml
permissions:
contents: read # Read repository
issues: write # Create/update/close issues
pull-requests: write # Comment on PRs
```

### Security Measures

1. **Token Security**: Use GitHub secrets
2. **Input Validation**: Sanitize file paths
3. **Rate Limiting**: Prevent API abuse
4. **Error Handling**: No sensitive data in logs

## Future Enhancements

### Planned Features

1. **Auto-fix PRs**: Create PRs with fixes
2. **Priority Scoring**: Rank issues by impact
3. **Trend Analysis**: Track issue patterns over time
4. **Custom Rules**: Support project-specific lints
5. **Integration**: Slack/Discord notifications
6. **Metrics Dashboard**: Visualize code quality trends

### Architectural Improvements

1. **Caching Layer**: Redis for API responses
2. **Queue System**: Background processing
3. **Webhooks**: Real-time updates
4. **Plugin System**: Custom analyzers
5. **Multi-repo Support**: Organization-wide analysis
Loading