Skip to content

Commit

Permalink
fix: overwrites from rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Alec Cunningham authored and Alec Cunningham committed Oct 23, 2024
1 parent e09fd3b commit 51e10fe
Show file tree
Hide file tree
Showing 17 changed files with 688 additions and 71 deletions.
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM golang:1.18-alpine

# Install migrate tool
RUN apk add --no-cache curl bash && \
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.15.2/migrate.linux-amd64.tar.gz | \
tar xvz && mv migrate.linux-amd64 /usr/local/bin/migrate

# Set working directory
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Download dependencies
RUN go mod download

# Copy the source code
COPY . .

# Expose port
EXPOSE 8080

# Run migrations and start the application
CMD ["sh", "-c", "./scripts/migrate.sh up && go run cmd/server/main.go"]
245 changes: 244 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,244 @@
# github-actions-aggregator
# GitHub Actions Aggregator Service

A Go-based service to aggregate and analyze data from GitHub Actions workflows across multiple repositories. This application provides insights into workflow runs, success rates, failure rates, and other statistics over customizable time ranges.

## Table of Contents

- [Features](#features)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [API Endpoints](#api-endpoints)
- [Authentication](#authentication)
- [Testing](#testing)
- [Contributing](#contributing)
- [License](#license)

## Features

- **OAuth 2.0 Authentication**: Securely authenticate users via GitHub OAuth.
- **Data Collection**:
- **Webhooks**: Receive real-time updates on workflow events.
- **Polling**: Periodically poll GitHub API to ensure data completeness.
- **Data Aggregation**: Compute statistics like success rates, failure rates, and more.
- **API Endpoints**: Expose RESTful APIs for accessing aggregated data.
- **Background Processing**: Use worker pools to handle asynchronous tasks.
- **Configurable**: Easily adjust settings like polling intervals and webhook secrets.
- **Secure**: Validate webhook payloads and protect routes with authentication middleware.

## Prerequisites

- **Go**: Version 1.18 or higher.
- **GitHub Account**: For OAuth authentication and API access.
- **PostgreSQL**: For storing data.
- **Redis** (optional): For caching (if implemented).
- **Docker** (optional): For containerization and deployment.

## Installation

1. **Clone the Repository**

```bash
git clone https://github.com/yourusername/github-actions-aggregator.git
cd github-actions-aggregator
```

2. **Install Dependencies**

```bash
go mod download
```

3. **Set Up Environment Variables**

Create a `.env` file or export the required environment variables:

```bash
export GITHUB_CLIENT_ID="your_github_client_id"
export GITHUB_CLIENT_SECRET="your_github_client_secret"
export GITHUB_ACCESS_TOKEN="your_github_access_token"
export GITHUB_WEBHOOK_SECRET="your_webhook_secret"
export DATABASE_URL="postgres://username:password@localhost:5432/yourdbname?sslmode=disable"
export SERVER_PORT="8080"
```

## Configuration

Configuration can be managed via a `config.yaml` file in the `configs/` directory or through environment variables.

**Example `config.yaml`:**

```yaml
server:
port: "8080"
log:
level: "info"
github:
client_id: "your_github_client_id"
client_secret: "your_github_client_secret"
access_token: "your_github_access_token"
webhook_secret: "your_webhook_secret"
```

**Note:** Environment variables override values in the configuration file.

## Usage

### Running the Application

1. **Run Database Migrations**

```bash
./scripts/migrate.sh
```

2. **Start the Application**

```bash
go run cmd/server/main.go
```

### Accessing the Application

- **Login with GitHub**: Navigate to `http://localhost:8080/login` to authenticate via GitHub.
- **API Requests**: Use tools like `curl` or Postman to interact with the API endpoints.

## API Endpoints

### Authentication

- `GET /login`: Redirects the user to GitHub for OAuth authentication.
- `GET /callback`: Handles the OAuth callback from GitHub.

### Workflow Statistics

- `GET /workflows/:id/stats`: Retrieves statistics for a specific workflow.

**Query Parameters:**

- `start_time` (optional): Start of the time range (ISO 8601 format).
- `end_time` (optional): End of the time range (ISO 8601 format).

**Example Request:**

```http
GET /workflows/123/stats?start_time=2023-09-01T00:00:00Z&end_time=2023-09-30T23:59:59Z
```

**Example Response:**

```json
{
"workflow_id": 123,
"workflow_name": "CI Build and Test",
"total_runs": 200,
"success_count": 150,
"failure_count": 30,
"cancelled_count": 10,
"timed_out_count": 5,
"action_required_count": 5,
"success_rate": 75.0,
"failure_rate": 15.0,
"cancelled_rate": 5.0,
"timed_out_rate": 2.5,
"action_required_rate": 2.5,
"start_time": "2023-09-01T00:00:00Z",
"end_time": "2023-09-30T23:59:59Z"
}
```

## Authentication

### Setting Up OAuth with GitHub

1. **Register a New OAuth Application**

- Go to [GitHub Developer Settings](https://github.com/settings/developers).
- Click on **"New OAuth App"**.
- Fill in the application details:
- **Application Name**
- **Homepage URL**: `http://localhost:8080`
- **Authorization Callback URL**: `http://localhost:8080/callback`
- Obtain your **Client ID** and **Client Secret**.

2. **Configure Application Credentials**

Set your `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` in your environment variables or `config.yaml`.

### Permissions and Scopes

Ensure that your GitHub OAuth application has the necessary scopes:

- `read:user`
- `repo`
- `workflow`

## Testing

### Running Unit Tests

```bash
go test ./tests/unit/...
```

### Running Integration Tests

```bash
go test ./tests/integration/...
```

### Test Coverage

You can generate a test coverage report using:

```bash
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
```

## Contributing

Contributions are welcome! Please follow these steps:

1. **Fork the Repository**

Click on the "Fork" button at the top right of the repository page.

2. **Clone Your Fork**

```bash
git clone https://github.com/yourusername/github-actions-aggregator.git
```

3. **Create a Feature Branch**

```bash
git checkout -b feature/your-feature-name
```

4. **Commit Your Changes**

```bash
git commit -am "Add new feature"
```

5. **Push to Your Fork**

```bash
git push origin feature/your-feature-name
```

6. **Create a Pull Request**

Go to the original repository and open a pull request.

## License

This project is licensed under the [MIT License](LICENSE).

---

**Disclaimer:** This project is not affiliated with GitHub. Ensure compliance with GitHub's [Terms of Service](https://docs.github.com/en/github/site-policy/github-terms-of-service) when using their APIs.
45 changes: 23 additions & 22 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,44 @@ package main
import (
"log"
"os"
"os/signal"
"syscall"

"github.com/mooshe3/github-actions-aggregator/pkg/api"
"github.com/mooshe3/github-actions-aggregator/pkg/config"
"github.com/mooshe3/github-actions-aggregator/pkg/db"
"github.com/mooshe3/github-actions-aggregator/pkg/logger"
"github.com/mooshe3/github-actions-aggregator/pkg/worker"
"os/exec"

"github.com/moosh3/github-actions-aggregator/pkg/api"
"github.com/moosh3/github-actions-aggregator/pkg/config"
"github.com/moosh3/github-actions-aggregator/pkg/db"
"github.com/moosh3/github-actions-aggregator/pkg/github"
"github.com/moosh3/github-actions-aggregator/pkg/logger"
)

func main() {
// Initialize configurations
// Load configuration
cfg := config.LoadConfig()

// Initialize logger
logger.Init(cfg.LogLevel)

// Run migrations
err := runMigrations()
if err != nil {
log.Fatalf("Failed to run migrations: %v", err)
}

// Initialize database
database, err := db.InitDB(cfg)
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}

// Start the worker pool
wp := worker.NewWorkerPool(database, 5) // Adjust the number of workers as needed
wp.Start()
// Initialize GitHub client
githubClient := github.NewClient(cfg.GitHub.AccessToken)

// Start the API server
go api.StartServer(cfg, database)

// Wait for interrupt signal to gracefully shut down the worker pool
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
<-quit

log.Println("Shutting down worker pool...")
wp.Stop()
api.StartServer(cfg, database, githubClient)
}

log.Println("Server exiting")
func runMigrations() error {
cmd := exec.Command("./scripts/migrate.sh", "up")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
11 changes: 11 additions & 0 deletions configs/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
server:
port: "8080"

log:
level: "info"

github:
client_id: "your_github_client_id"
client_secret: "your_github_client_secret"
access_token: "your_github_access_token"
webhook_secret: "your_webhook_secret"
1 change: 1 addition & 0 deletions migrations/20231015010101_create_users_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS users;
7 changes: 7 additions & 0 deletions migrations/20231015010101_create_users_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
Loading

0 comments on commit 51e10fe

Please sign in to comment.