Skip to content

Commit

Permalink
Merge branch 'main' into mattress
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored Feb 6, 2024
2 parents c12cdc8 + ddb0145 commit e204043
Show file tree
Hide file tree
Showing 28 changed files with 256 additions and 117 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ jobs:
lint:
name: Lint
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
checks: write
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.21"
- name: Enforce linting
run: |
cd ./backend/ && lint_output=$(go vet ./...)
if [[ -n "$lint_output" ]]; then
echo "$lint_output"
echo "::error::Linting issues found"
exit 1
fi
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
working-directory: ./backend/
test:
name: Test
runs-on: ubuntu-latest
Expand Down
24 changes: 12 additions & 12 deletions .github/workflows/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ jobs:
lint:
name: Lint
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
checks: write
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.21"
- name: Enforce linting
run: |
cd ./cli/ && lint_output=$(go vet ./...)
if [[ -n "$lint_output" ]]; then
echo "$lint_output"
echo "::error::Linting issues found"
exit 1
fi
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
working-directory: ./cli/
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
<br />

<div align="center">
<!-- Github Actions -->
<a href="https://goreportcard.com/report/github.com/GenerateNU/sac/backend">
<img src="https://goreportcard.com/badge/github.com/GenerateNU/sac/backend"
alt="Backend Go Report" />
</a>
<a href="https://github.com/GenerateNU/sac/actions/workflows/backend.yml">
<img src="https://github.com/GenerateNU/sac/actions/workflows/backend.yml/badge.svg"
alt="Backend Workflow Status" />
Expand All @@ -16,6 +19,10 @@

<br />

<a href="https://goreportcard.com/report/github.com/GenerateNU/sac/cli">
<img src="https://goreportcard.com/badge/github.com/GenerateNU/sac/cli"
alt="CLI Go Report" />
</a>
<a href="https://github.com/GenerateNU/sac/actions/workflows/cli.yml">
<img src="https://github.com/GenerateNU/sac/actions/workflows/cli.yml/badge.svg"
alt="CLI Workflow Status" />
Expand Down
23 changes: 23 additions & 0 deletions backend/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
linters:
enable:
- cyclop
- exportloopref
- gocritic
- gosec
- ineffassign
- misspell
- prealloc
- unconvert
- unparam
- goimports
- whitespace

linters-settings:
whitespace:
multi-func: true

issues:
exclude-rules:
- path: tests/api/helpers/
linters:
- cyclop
1 change: 0 additions & 1 deletion backend/src/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func createSuperUser(settings config.Settings, db *gorm.DB) error {
}

return tx.Commit().Error

}
return nil
}
2 changes: 1 addition & 1 deletion backend/src/database/super.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func SuperClub() models.Club {
Description: "SAC",
NumMembers: 0,
IsRecruiting: true,
RecruitmentCycle: models.RecruitmentCycle(models.Always),
RecruitmentCycle: models.Always,
RecruitmentType: models.Application,
ApplicationLink: "https://generatenu.com/apply",
Logo: "https://aws.amazon.com/s3",
Expand Down
5 changes: 4 additions & 1 deletion backend/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ func main() {

app := server.Init(db, *config)

app.Listen(fmt.Sprintf("%s:%d", config.Application.Host, config.Application.Port))
err = app.Listen(fmt.Sprintf("%s:%d", config.Application.Host, config.Application.Port))
if err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion backend/src/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func SuperSkipper(h fiber.Handler) fiber.Handler {
return skip.New(h, func(c *fiber.Ctx) bool {
claims, err := types.From(c)
if err != nil {
err.FiberError(c)
_ = err.FiberError(c)
return false
}
if claims == nil {
Expand Down
6 changes: 5 additions & 1 deletion backend/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import (
func Init(db *gorm.DB, settings config.Settings) *fiber.App {
app := newFiberApp()

validate := utilities.RegisterCustomValidators()
validate, err := utilities.RegisterCustomValidators()
if err != nil {
panic(err)
}

middlewareService := middleware.NewMiddlewareService(db, validate, settings.Auth)

apiv1 := app.Group("/api/v1")
Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (a *AuthService) GetRole(id string) (*models.UserRole, *errors.Error) {
return nil, &errors.UserNotFound
}

role := models.UserRole(user.Role)
role := user.Role

return &role, nil
}
2 changes: 1 addition & 1 deletion backend/src/transactions/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func GetAdminIDs(db *gorm.DB, clubID uuid.UUID) ([]uuid.UUID, *errors.Error) {
return nil, &errors.FailedtoGetAdminIDs
}

var adminUUIDs []uuid.UUID
adminUUIDs := make([]uuid.UUID, 0)
for _, adminID := range adminIDs {
adminUUIDs = append(adminUUIDs, adminID.ClubID)
}
Expand Down
30 changes: 22 additions & 8 deletions backend/src/utilities/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,32 @@ import (
"github.com/go-playground/validator/v10"
)

func RegisterCustomValidators() *validator.Validate {
func RegisterCustomValidators() (*validator.Validate, error) {
validate := validator.New(validator.WithRequiredStructEnabled())

validate.RegisterValidation("neu_email", validateEmail)
validate.RegisterValidation("password", validatePassword)
validate.RegisterValidation("mongo_url", validateMongoURL)
validate.RegisterValidation("s3_url", validateS3URL)
validate.RegisterValidation("contact_pointer", func(fl validator.FieldLevel) bool {
if err := validate.RegisterValidation("neu_email", validateEmail); err != nil {
return nil, err
}

if err := validate.RegisterValidation("password", validatePassword); err != nil {
return nil, err
}

if err := validate.RegisterValidation("mongo_url", validateMongoURL); err != nil {
return nil, err
}

if err := validate.RegisterValidation("s3_url", validateS3URL); err != nil {
return nil, err
}

if err := validate.RegisterValidation("contact_pointer", func(fl validator.FieldLevel) bool {
return validateContactPointer(validate, fl)
})
}); err != nil {
return nil, err
}

return validate
return validate, nil
}

func validateEmail(fl validator.FieldLevel) bool {
Expand Down
7 changes: 4 additions & 3 deletions backend/tests/api/club_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ func TestGetClubsWorks(t *testing.T) {
assert.Equal("SAC", dbClub.Description)
assert.Equal(1, dbClub.NumMembers)
assert.Equal(true, dbClub.IsRecruiting)
assert.Equal(models.RecruitmentCycle(models.Always), dbClub.RecruitmentCycle)
assert.Equal(models.RecruitmentType(models.Application), dbClub.RecruitmentType)
assert.Equal(models.Always, dbClub.RecruitmentCycle)
assert.Equal(models.Application, dbClub.RecruitmentType)
assert.Equal("https://generatenu.com/apply", dbClub.ApplicationLink)
assert.Equal("https://aws.amazon.com/s3", dbClub.Logo)
},
Expand Down Expand Up @@ -303,7 +303,7 @@ func TestCreateClubFailsOnInvalidLogo(t *testing.T) {
[]interface{}{
"Not an URL",
"@#139081#$Ad_Axf",
//"https://google.com", <-- TODO uncomment once we figure out s3 url validation
// "https://google.com", <-- TODO uncomment once we figure out s3 url validation
},
)
}
Expand Down Expand Up @@ -343,6 +343,7 @@ func TestUpdateClubFailsOnInvalidBody(t *testing.T) {
{"application_link": "Not an URL"},
{"logo": "@12394X_2"},
} {
invalidData := invalidData
appAssert.TestOnErrorAndDB(
h.TestRequest{
Method: fiber.MethodPatch,
Expand Down
1 change: 1 addition & 0 deletions backend/tests/api/helpers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type TestRequest struct {
TestUserIDReplaces *string
}

//gocyclo:ignore
func (app TestApp) Send(request TestRequest) (*http.Response, error) {
address := fmt.Sprintf("%s%s", app.Address, request.Path)

Expand Down
2 changes: 2 additions & 0 deletions backend/tests/api/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func TestCreateTagFailsBadRequest(t *testing.T) {
}

for _, badBody := range badBodys {
badBody := badBody
appAssert.TestOnErrorAndDB(
h.TestRequest{
Method: fiber.MethodPost,
Expand Down Expand Up @@ -146,6 +147,7 @@ func TestCreateTagFailsValidation(t *testing.T) {
}

for _, badBody := range badBodys {
badBody := badBody
appAssert.TestOnErrorAndDB(
h.TestRequest{
Method: fiber.MethodPost,
Expand Down
3 changes: 3 additions & 0 deletions backend/tests/api/user_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func CreateSetOfTags(t *testing.T, appAssert *h.ExistingAppAssert) ([]uuid.UUID,

categoryIDs := []uuid.UUID{}
for _, category := range *categories {
category := category
appAssert.TestOnStatusAndDB(
h.TestRequest{
Method: fiber.MethodPost,
Expand All @@ -92,6 +93,7 @@ func CreateSetOfTags(t *testing.T, appAssert *h.ExistingAppAssert) ([]uuid.UUID,

tagIDs := []uuid.UUID{}
for _, tag := range *tags {
tag := tag
appAssert.TestOnStatusAndDB(
h.TestRequest{
Method: fiber.MethodPost,
Expand Down Expand Up @@ -212,6 +214,7 @@ func TestCreateUserTagsFailsOnInvalidKey(t *testing.T) {
}

for _, body := range invalidBody {
body := body
h.InitTest(t).TestOnError(
h.TestRequest{
Method: fiber.MethodPost,
Expand Down
1 change: 1 addition & 0 deletions backend/tests/api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func TestUpdateUserFailsOnInvalidBody(t *testing.T) {
{"year": 1963},
{"college": "UT-Austin"},
} {
invalidData := invalidData
h.InitTest(t).TestOnErrorAndDB(
h.TestRequest{
Method: fiber.MethodPatch,
Expand Down
17 changes: 17 additions & 0 deletions cli/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
linters:
enable:
- cyclop
- exportloopref
- gocritic
- gosec
- ineffassign
- misspell
- prealloc
- unconvert
- unparam
- goimports
- whitespace

linters-settings:
whitespace:
multi-func: true
7 changes: 4 additions & 3 deletions cli/commands/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func DropData() error {
return fmt.Errorf("error scanning table name: %w", err)
}

deleteStmt := fmt.Sprintf("DELETE FROM \"%s\"", tablename)
_, err := db.Exec(deleteStmt)
_, err := db.Exec("DELETE FROM $1", tablename)
if err != nil {
return fmt.Errorf("error deleting rows from table %s: %w", tablename, err)
}
Expand All @@ -82,7 +81,9 @@ func DropData() error {
return fmt.Errorf("error in rows handling: %w", err)
}

Migrate()
if Migrate() != nil {
return fmt.Errorf("error migrating database: %w", err)
}

fmt.Println("All rows removed successfully.")
return nil
Expand Down
15 changes: 12 additions & 3 deletions cli/commands/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ func FormatCommand() *cli.Command {
runFrontend := folder != ""
runBackend := c.Bool("backend")

Format(folder, runFrontend, runBackend)
err := Format(folder, runFrontend, runBackend)
if err != nil {
return cli.Exit(err.Error(), 1)
}

return nil
},
Expand All @@ -56,7 +59,10 @@ func Format(folder string, runFrontend bool, runBackend bool) error {
wg.Add(1)
go func() {
defer wg.Done()
BackendFormat()
err := BackendFormat()
if err != nil {
fmt.Println(err)
}
}()
}

Expand All @@ -65,7 +71,10 @@ func Format(folder string, runFrontend bool, runBackend bool) error {
wg.Add(1)
go func() {
defer wg.Done()
FrontendFormat(folder)
err := FrontendFormat(folder)
if err != nil {
fmt.Println(err)
}
}()
}

Expand Down
Loading

0 comments on commit e204043

Please sign in to comment.