Skip to content

Commit 3fe5b2c

Browse files
Alec CunninghamAlec Cunningham
Alec Cunningham
authored and
Alec Cunningham
committed
fix: update imports
1 parent 7c2a98f commit 3fe5b2c

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

pkg/api/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package api
33
import (
44
"github.com/gin-gonic/gin"
55
"github.com/moosh3/github-actions-aggregator/pkg/auth"
6-
"github.com/moosh3/github-actions-aggregator/pkg/config"
76
"github.com/moosh3/github-actions-aggregator/pkg/db"
8-
"github.com/moosh3/github-actions-aggregator/pkg/github"
7+
"github.com/mooshe3/github-actions-aggregator/pkg/config"
8+
"github.com/mooshe3/github-actions-aggregator/pkg/github"
99
)
1010

1111
func StartServer(cfg *config.Config, db *db.Database, githubClient *github.Client) {

pkg/auth/oauth.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import (
99
"os"
1010

1111
"github.com/gin-gonic/gin"
12+
"github.com/moosh3/github-actions-aggregator/pkg/db/models"
1213
"golang.org/x/oauth2"
1314
"golang.org/x/oauth2/github"
15+
16+
"github.com/moosh3/github-actions-aggregator/pkg/db"
1417
)
1518

1619
var (
@@ -52,35 +55,33 @@ func GitHubCallback(c *gin.Context) {
5255
return
5356
}
5457

55-
// Save or update user in database (implement saveOrUpdateUser)
58+
// Save or update user in database
59+
err = db.SaveUser(user)
60+
if err != nil {
61+
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Failed to save user"})
62+
return
63+
}
5664
// Set user session (implement setUserSession)
65+
setUserSession(c, user)
5766

5867
c.Redirect(http.StatusFound, "/dashboard")
5968
}
6069

61-
func getUserInfo(token *oauth2.Token) (*GitHubUser, error) {
70+
func getUserInfo(token *oauth2.Token) (*models.GitHubUser, error) {
6271
client := oauthConfig.Client(context.Background(), token)
6372
resp, err := client.Get("https://api.github.com/user")
6473
if err != nil {
6574
return nil, err
6675
}
6776
defer resp.Body.Close()
6877

69-
var user GitHubUser
78+
var user models.GitHubUser
7079
if err := json.NewDecoder(resp.Body).Decode(&user); err != nil {
7180
return nil, err
7281
}
7382
return &user, nil
7483
}
7584

76-
type GitHubUser struct {
77-
ID int64 `json:"id"`
78-
Login string `json:"login"`
79-
Name string `json:"name"`
80-
Email string `json:"email"`
81-
AvatarURL string `json:"avatar_url"`
82-
}
83-
8485
func generateStateString() string {
8586
b := make([]byte, 16)
8687
_, err := rand.Read(b)
@@ -102,6 +103,10 @@ func getSavedOAuthState(c *gin.Context) string {
102103
return state
103104
}
104105

106+
func setUserSession(c *gin.Context, user *models.GitHubUser) {
107+
c.Set("user", user)
108+
}
109+
105110
func getEnv(key, fallback string) string {
106111
if value, exists := os.LookupEnv(key); exists {
107112
return value

pkg/db/db.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,32 @@ import (
44
"fmt"
55

66
"github.com/google/go-github/v50/github"
7-
config "github.com/moosh3/github-actions-aggregator/pkg/config"
8-
"github.com/moosh3/github-actions-aggregator/pkg/db/models"
7+
"github.com/mooshe3/github-actions-aggregator/pkg/config"
8+
"github.com/mooshe3/github-actions-aggregator/pkg/db/models"
99
"gorm.io/driver/postgres"
1010
"gorm.io/gorm"
1111
"gorm.io/gorm/clause"
1212
)
1313

14+
// Database represents a wrapper around the GORM database connection.
1415
type Database struct {
16+
// Conn is the underlying GORM database connection.
1517
Conn *gorm.DB
1618
}
1719

18-
func InitDB(cfg *config.Config) (*gorm.DB, error) {
20+
// InitDB initializes and returns a new Database instance.
21+
//
22+
// It takes a configuration object and uses it to establish a connection
23+
// to the PostgreSQL database. It also performs auto-migration of the
24+
// database schema for the Repository, WorkflowRun, and Statistics models.
25+
//
26+
// Parameters:
27+
// - cfg: A pointer to a config.Config struct containing database connection details.
28+
//
29+
// Returns:
30+
// - A pointer to a Database struct containing the initialized GORM DB connection.
31+
// - An error if the database connection or auto-migration fails.
32+
func InitDB(cfg *config.Config) (*Database, error) {
1933
// Database initialization logic
2034
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=UTC",
2135
cfg.Database.Host, cfg.Database.User, cfg.Database.Password, cfg.Database.DBName, cfg.Database.Port)
@@ -31,7 +45,7 @@ func InitDB(cfg *config.Config) (*gorm.DB, error) {
3145
return nil, fmt.Errorf("failed to auto-migrate database schema: %w", err)
3246
}
3347

34-
return db, nil
48+
return &Database{Conn: db}, nil
3549
}
3650

3751
func (db *Database) GetMonitoredRepositories() ([]models.Repository, error) {
@@ -42,13 +56,11 @@ func (db *Database) GetMonitoredRepositories() ([]models.Repository, error) {
4256

4357
func (db *Database) SaveWorkflowRun(run *github.WorkflowRun) error {
4458
workflowRun := models.WorkflowRun{
45-
ID: run.GetID(),
4659
WorkflowID: run.GetWorkflowID(),
4760
RepositoryID: run.GetRepository().GetID(),
4861
Status: run.GetStatus(),
4962
Conclusion: run.GetConclusion(),
5063
RunNumber: run.GetRunNumber(),
51-
Event: run.GetEvent(),
5264
CreatedAt: run.GetCreatedAt().Time,
5365
UpdatedAt: run.GetUpdatedAt().Time,
5466
// Add other fields as needed
@@ -65,3 +77,7 @@ func (db *Database) SaveStatistics(stats *models.Statistics) error {
6577
// Upsert operation
6678
return db.Conn.Save(stats).Error
6779
}
80+
81+
func (db *Database) SaveUser(user *models.GitHubUser) error {
82+
return db.Conn.Save(user).Error
83+
}

pkg/github/polling.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github
22

33
import (
4+
"fmt"
45
"log"
56
"time"
67

@@ -48,14 +49,14 @@ func (p *Poller) pollRepositories() {
4849
}
4950

5051
func (p *Poller) pollWorkflows(repo models.Repository) {
51-
workflows, err := p.client.ListWorkflows(repo.Owner, repo.Name)
52+
workflows, err := p.client.ListWorkflows(repo.Owner.Login, repo.Name)
5253
if err != nil {
53-
log.Printf("Error listing workflows for %s/%s: %v", repo.Owner, repo.Name, err)
54+
log.Printf("Error listing workflows for %s/%s: %v", repo.Owner.Login, repo.Name, err)
5455
return
5556
}
5657

5758
for _, workflow := range workflows {
58-
p.pollWorkflowRuns(repo.Owner, repo.Name, workflow)
59+
p.pollWorkflowRuns(repo.Owner.Login, repo.Name, workflow)
5960
}
6061
}
6162

@@ -67,5 +68,5 @@ func (p *Poller) pollWorkflowRuns(owner, repoName string, workflow *gh.Workflow)
6768
return
6869
}
6970

70-
// Process runs...
71+
fmt.Println(runs)
7172
}

0 commit comments

Comments
 (0)