The backend implements a sophisticated monitoring system that tracks GitHub repositories in real-time. Here's how it works:
- Each project has its own timer managed by a
Map
calledprojectTimers
- When a project is created or updated,
setupProjectTimer()
is called to:- Clear any existing timer for that project
- Create a new timer with the project's check interval
- Store the timer reference for cleanup
For each check interval:
-
Branch Retrieval:
- Fetches all branches associated with the project from the database
- Each branch is checked independently
-
GitHub API Integration:
- Uses Octokit to fetch the latest commit for each branch
- Compares the latest commit SHA with the stored SHA
- If different, marks as a change
-
Change Detection: When changes are detected:
- Updates branch's
last_commit_sha
in database - Updates project's
updated_at
timestamp - Logs the change in
check_logs
table with:- Commit details (SHA, message, author)
- Branch information
- Timestamp
- Updates branch's
-
Action Execution: For each detected change:
- Fetches all actions associated with the project
- For webhook actions:
- Makes HTTP POST request to webhook URL
- Sends project, branch, and commit details
- For script actions:
- Creates temporary script file
- Injects environment variables from secrets
- Executes script with proper permissions
- Captures output and errors
- Cleans up temporary files
- Each check operation is wrapped in try-catch blocks
- Errors are logged but don't stop the monitoring process
- Failed checks are recorded in the logs
- Individual action failures don't affect other actions
The system maintains several tables that are updated during the monitoring process:
projects
: Stores project configurationsbranches
: Tracks branch states and last commit SHAscheck_logs
: Records all check operations and their resultsactions
: Stores webhook URLs and script contentssecrets
: Manages environment variables for scripts
The backend is built using Express.js and SQLite, providing a RESTful API for managing repository monitoring and automated actions.
The backend uses SQLite with the following tables:
-
projects: Stores repository monitoring configurations
- id: INTEGER PRIMARY KEY - name: TEXT - repo_url: TEXT - check_interval: INTEGER (minutes) - last_check: TEXT (ISO timestamp) - created_at: TEXT
-
branches: Stores branch configurations for each project
- id: INTEGER PRIMARY KEY - project_id: INTEGER (foreign key) - branch_name: TEXT
-
actions: Stores webhook and script actions for projects
- id: INTEGER PRIMARY KEY - project_id: INTEGER (foreign key) - name: TEXT - action_type: TEXT ('webhook' or 'script') - webhook_url: TEXT - script_content: TEXT
-
logs: Stores execution history
- id: INTEGER PRIMARY KEY - project_id: INTEGER - commit_hash: TEXT - commit_message: TEXT - branch: TEXT - status: TEXT - created_at: TEXT
-
GET /api/projects
- Fetches all projects with their branches and actions
- Response includes:
- Project details
- Associated branches (comma-separated)
- Associated actions
{ "id": 1, "name": "My Project", "repo_url": "https://github.com/user/repo", "check_interval": 5, "branches": ["main", "develop"], "actions": [ { "id": 1, "name": "Slack Notification", "action_type": "webhook", "webhook_url": "https://hooks.slack.com/..." } ] }
-
POST /api/projects
- Creates a new project
- Required fields:
- name: Project name
- repoUrl: GitHub repository URL
- branches: Array of branch names to monitor
- checkInterval: Monitoring interval in minutes
-
PUT /api/projects/:id
- Updates an existing project
- Supports updating all project fields
- Automatically updates associated branches
-
DELETE /api/projects/:id
- Deletes a project and its associated data
-
GET /api/projects/:projectId/actions
- Fetches all actions for a specific project
-
POST /api/projects/:projectId/actions
- Creates a new action for a project
- Required fields:
- name: Action name
- actionType: "webhook" or "script"
- webhookUrl: URL for webhook actions
- scriptContent: Script content for script actions
-
PUT /api/projects/:projectId/actions/:actionId
- Updates an existing action
- Supports updating all action fields
-
DELETE /api/projects/:projectId/actions/:actionId
- Deletes an action
- GET /api/projects/:projectId/logs
- Fetches execution history for a project
- Supports pagination and filtering
The backend implements a polling mechanism that:
- Periodically checks each project based on its
check_interval
- Uses GitHub API to fetch latest commits
- Compares with last known state
- Executes associated actions when changes are detected
- All endpoints return appropriate HTTP status codes
- Database operations are wrapped in try-catch blocks
- Actions execution is logged with detailed error information
-
Install dependencies:
npm install
-
Environment variables:
PORT=3001 GITHUB_TOKEN=your_github_token
-
Start the server:
npm start
The database is automatically initialized when the server starts:
- Checks if database file exists
- Creates tables if they don't exist
- Adds any missing columns to existing tables
When adding new features:
- Update database schema if needed
- Add new API endpoints
- Update background processing if required
- Add appropriate error handling
- Update this documentation
- All webhook URLs must be HTTPS
- Script actions are executed in a sandboxed environment
- GitHub token is required for repository access
- Input validation is performed on all endpoints
- 400: Bad Request (invalid input)
- 404: Resource Not Found
- 500: Internal Server Error
- 503: Service Unavailable (GitHub API issues)