Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(environment): Modified CI workflow, added staticcheck as a linter #22

Merged
merged 6 commits into from
Dec 28, 2024
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
17 changes: 13 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CI Workflow

on:
pull_request:
branches: ["main", "dev"]
branches: [ "main", "dev" ]
push:
branches-ignore: ["main"]
branches-ignore: [ "main" ]

jobs:
lint-and-test:
Expand Down Expand Up @@ -39,8 +39,17 @@ jobs:
exit 1
fi

- name: Run golint
run: golint ./... || exit 1
- name: Install golangci-lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.53.3

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.62.2

- name: Run go vet
run: go vet ./...

- name: Run tests
run: go test ./... -v
363 changes: 363 additions & 0 deletions .golangci.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion controllers/sampleController_test.go
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package controllers
package controllers_test
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ module rpl-service

go 1.23

require (
gorm.io/driver/postgres v1.5.11
gorm.io/gorm v1.25.12
)
require gorm.io/gorm v1.25.12

require (
//github.com/jackc/pgpassfile v1.0.0 // indirect
Expand All @@ -16,5 +13,5 @@ require (
github.com/jinzhu/now v1.1.5 // indirect
//golang.org/x/crypto v0.17.0 // indirect
//golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/text v0.21.0 // indirect
)
32 changes: 2 additions & 30 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,36 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
11 changes: 2 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@ import (
"fmt"
)

//TIP <p>To run your code, right-click the code and select <b>Run</b>.</p> <p>Alternatively, click
// the <icon src="AllIcons.Actions.Execute"/> icon in the gutter and select the <b>Run</b> menu item from here.</p>

func main() {
//TIP <p>Press <shortcut actionId="ShowIntentionActions"/> when your caret is at the underlined text
// to see how GoLand suggests fixing the warning.</p><p>Alternatively, if available, click the lightbulb to view possible fixes.</p>
s := "gopher"
fmt.Printf("Hello and welcome, %s!\n", s)

maxNumber := 100
for i := 1; i <= 5; i++ {
//TIP <p>To start your debugging session, right-click your code in the editor and select the Debug option.</p> <p>We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.</p>
fmt.Println("i =", 100/i)
fmt.Println("i =", maxNumber/i)
}
}
2 changes: 1 addition & 1 deletion models/courseModels.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Exercise struct {
BaseCode string
Points int
UnitNumber int
TestIds []uint `gorm:"foreignkey:ExerciseId"`
TestIDs []uint `gorm:"foreignkey:ExerciseId"`
}

type Test struct {
Expand Down
4 changes: 2 additions & 2 deletions models/userModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

type IsEnrolled struct {
gorm.Model
UserId uint
CourseId uint
UserID uint
CourseID uint
IsOwner bool
}

Expand Down
54 changes: 32 additions & 22 deletions services/users/userService.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ import (
"rpl-service/models"
)

func EnrollToCourse(db *gorm.DB, userID, courseID uint) error {
// TODO: delete the following line
// if !userExists(db, userID) {
// return errors.New("user does not exist")
//}

if userInCourse(db, userID, courseID) {
return errors.New("user is already in course")
}

db.Model(models.IsEnrolled{}).Create(models.IsEnrolled{
Model: gorm.Model{},
UserID: userID,
CourseID: courseID,
IsOwner: false,
})

return nil
}

func CreateCourse(db *gorm.DB, userID uint, courseName, description string) error {
currentCourse := models.Course{
Model: gorm.Model{},
Expand All @@ -19,8 +39,8 @@ func CreateCourse(db *gorm.DB, userID uint, courseName, description string) erro

db.Model(models.IsEnrolled{}).Create(models.IsEnrolled{
Model: gorm.Model{},
UserId: userID,
CourseId: currentCourse.ID,
UserID: userID,
CourseID: currentCourse.ID,
IsOwner: true,
})

Expand All @@ -43,37 +63,22 @@ func RemoveStudent(db *gorm.DB, userID, courseID, studentID uint) error {
return nil
}

func EnrollToCourse(db *gorm.DB, userID, courseID uint) error {
if userInCourse(db, userID, courseID) {
return errors.New("user is already in course")
}

db.Model(models.IsEnrolled{}).Create(models.IsEnrolled{
Model: gorm.Model{},
UserId: userID,
CourseId: courseID,
IsOwner: false,
})

return nil
}

func CreateExercise(db *gorm.DB, exercise models.ExerciseDTO, userID, courseID uint) error {
if !isOwner(db, userID, courseID) {
return errors.New("this user doesn't have permission to create an exercise")
}

var testIds []uint
var testIDs []uint
for _, test := range exercise.TestData {
testIds = append(testIds, CreateTest(db, test))
testIDs = append(testIDs, CreateTest(db, test))
}

db.Model(models.Exercise{}).Create(models.Exercise{
Model: gorm.Model{},
Name: exercise.Name,
Description: exercise.Description,
BaseCode: exercise.BaseCode,
TestIds: testIds,
TestIDs: testIDs,
Points: exercise.Points,
UnitNumber: exercise.UnitNumber,
})
Expand All @@ -99,10 +104,15 @@ func CreateTest(db *gorm.DB, test models.TestDTO) uint {

func isOwner(db *gorm.DB, userID uint, courseID uint) bool {
currentUser := models.IsEnrolled{}
db.Model(models.IsEnrolled{}).Where("UserId = ? AND CourseId = ?", userID, courseID).First(&currentUser)
db.Model(models.IsEnrolled{}).Where("UserID = ? AND CourseID = ?", userID, courseID).First(&currentUser)
return currentUser.IsOwner
}

// func userExists(db *gorm.DB, id uint) bool {
// //TODO: use Auth0
// return true
//}

func courseExists(db *gorm.DB, courseID uint) bool {
return db.Model(models.Course{}).Where("ID = ?", courseID).Error != nil
}
Expand All @@ -111,5 +121,5 @@ func userInCourse(db *gorm.DB, userID, courseID uint) bool {
if !courseExists(db, courseID) {
return false
}
return db.Model(models.IsEnrolled{}).Where("UserId = ? AND CourseId = ?", userID, courseID).Error != nil
return db.Model(models.IsEnrolled{}).Where("UserID = ? AND CourseID = ?", userID, courseID).Error != nil
}
Loading