Skip to content

Commit

Permalink
fix lint errors (#189)
Browse files Browse the repository at this point in the history
* fix lint errors

* fix all lint errors

* minor fix

---------

Co-authored-by: yiling <yiling@yilingdeMacBook-Air.local>
  • Loading branch information
Yiling-J and yiling authored Nov 21, 2024
1 parent 7fe973f commit 0dde27b
Show file tree
Hide file tree
Showing 48 changed files with 259 additions and 183 deletions.
2 changes: 1 addition & 1 deletion accounting/consumer/metering.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (m *Metering) startMetering() {
}

func (m *Metering) preReadMsgs() {
var err error = nil
var err error
var i int = 0
for {
i++
Expand Down
6 changes: 2 additions & 4 deletions api/handler/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1599,10 +1599,8 @@ func getSourceRepoPathFromSourceUrl(sourceUrl string) (string, error) {
// Remove leading and trailing slashes
path := strings.Trim(parsedURL.Path, "/")

// Remove ".git" suffix if present
if strings.HasSuffix(path, ".git") {
path = strings.TrimSuffix(path, ".git")
}
// Remove ".git" suffix
path = strings.TrimSuffix(path, ".git")

return path, nil
}
Expand Down
7 changes: 6 additions & 1 deletion api/handler/runtime_architecture.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ func (r *RuntimeArchitectureHandler) ScanArchitecture(ctx *gin.Context) {
}

var req types.RuntimeFrameworkModels
ctx.ShouldBindJSON(&req)
err = ctx.ShouldBindJSON(&req)
if err != nil {
slog.Error("Failed to bind json", slog.Any("error", err))
httpbase.BadRequest(ctx, err.Error())
return
}

err = r.rac.ScanArchitecture(ctx, id, scanType, req.Models)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion api/middleware/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ func BuildJwtSession(jwtSignKey string) gin.HandlerFunc {
}

sessions.Default(c).Set(httpbase.CurrentUserCtxVar, claims.CurrentUser)
sessions.Default(c).Save()
err = sessions.Default(c).Save()
if err != nil {
slog.Error("fail to save session", slog.Any("error", err))
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
return
}

c.Next()
}
Expand Down
8 changes: 4 additions & 4 deletions api/router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func createModelRoutes(config *config.Config, apiGroup *gin.RouterGroup, needAPI
// The DownloadFile has a different file_path format for lfs files and non-lfs files,
// and an lfs parameter needs to be added.
// 2. DownloadFile returns an object store url for lfs files, while SDKDownload redirects directly.
modelsGroup.GET("/:namespace/:name/download/*file_path", middleware.RepoType(types.ModelRepo), repoCommonHandler.DownloadFile)
modelsGroup.GET("/:namespace/:name/download/*file_path", middleware.RepoType(types.ModelRepo), repoCommonHandler.ResolveDownload)
modelsGroup.GET("/:namespace/:name/resolve/*file_path", middleware.RepoType(types.ModelRepo), repoCommonHandler.ResolveDownload)
modelsGroup.POST("/:namespace/:name/raw/*file_path", middleware.RepoType(types.ModelRepo), repoCommonHandler.CreateFile)
modelsGroup.PUT("/:namespace/:name/raw/*file_path", middleware.RepoType(types.ModelRepo), repoCommonHandler.UpdateFile)
Expand Down Expand Up @@ -502,7 +502,7 @@ func createDatasetRoutes(config *config.Config, apiGroup *gin.RouterGroup, dsHan
datasetsGroup.POST("/:namespace/:name/raw/*file_path", middleware.RepoType(types.DatasetRepo), repoCommonHandler.CreateFile)
datasetsGroup.GET("/:namespace/:name/raw/*file_path", middleware.RepoType(types.DatasetRepo), repoCommonHandler.FileRaw)
datasetsGroup.GET("/:namespace/:name/blob/*file_path", middleware.RepoType(types.DatasetRepo), repoCommonHandler.FileInfo)
datasetsGroup.GET("/:namespace/:name/download/*file_path", middleware.RepoType(types.DatasetRepo), repoCommonHandler.DownloadFile)
datasetsGroup.GET("/:namespace/:name/download/*file_path", middleware.RepoType(types.DatasetRepo), repoCommonHandler.ResolveDownload)
datasetsGroup.GET("/:namespace/:name/resolve/*file_path", middleware.RepoType(types.DatasetRepo), repoCommonHandler.ResolveDownload)
datasetsGroup.PUT("/:namespace/:name/raw/*file_path", middleware.RepoType(types.DatasetRepo), repoCommonHandler.UpdateFile)
datasetsGroup.POST("/:namespace/:name/update_downloads", middleware.RepoType(types.DatasetRepo), repoCommonHandler.UpdateDownloads)
Expand Down Expand Up @@ -541,7 +541,7 @@ func createCodeRoutes(config *config.Config, apiGroup *gin.RouterGroup, codeHand
codesGroup.POST("/:namespace/:name/raw/*file_path", middleware.RepoType(types.CodeRepo), repoCommonHandler.CreateFile)
codesGroup.GET("/:namespace/:name/raw/*file_path", middleware.RepoType(types.CodeRepo), repoCommonHandler.FileRaw)
codesGroup.GET("/:namespace/:name/blob/*file_path", middleware.RepoType(types.CodeRepo), repoCommonHandler.FileInfo)
codesGroup.GET("/:namespace/:name/download/*file_path", middleware.RepoType(types.CodeRepo), repoCommonHandler.DownloadFile)
codesGroup.GET("/:namespace/:name/download/*file_path", middleware.RepoType(types.CodeRepo), repoCommonHandler.ResolveDownload)
codesGroup.GET("/:namespace/:name/resolve/*file_path", middleware.RepoType(types.CodeRepo), repoCommonHandler.ResolveDownload)
codesGroup.PUT("/:namespace/:name/raw/*file_path", middleware.RepoType(types.CodeRepo), repoCommonHandler.UpdateFile)
codesGroup.POST("/:namespace/:name/update_downloads", middleware.RepoType(types.CodeRepo), repoCommonHandler.UpdateDownloads)
Expand Down Expand Up @@ -594,7 +594,7 @@ func createSpaceRoutes(config *config.Config, apiGroup *gin.RouterGroup, spaceHa
spaces.POST("/:namespace/:name/raw/*file_path", middleware.RepoType(types.SpaceRepo), repoCommonHandler.CreateFile)
spaces.GET("/:namespace/:name/raw/*file_path", middleware.RepoType(types.SpaceRepo), repoCommonHandler.FileRaw)
spaces.GET("/:namespace/:name/blob/*file_path", middleware.RepoType(types.SpaceRepo), repoCommonHandler.FileInfo)
spaces.GET("/:namespace/:name/download/*file_path", middleware.RepoType(types.SpaceRepo), repoCommonHandler.DownloadFile)
spaces.GET("/:namespace/:name/download/*file_path", middleware.RepoType(types.SpaceRepo), repoCommonHandler.ResolveDownload)
spaces.GET("/:namespace/:name/resolve/*file_path", middleware.RepoType(types.SpaceRepo), repoCommonHandler.ResolveDownload)
spaces.PUT("/:namespace/:name/raw/*file_path", middleware.RepoType(types.SpaceRepo), repoCommonHandler.UpdateFile)
spaces.POST("/:namespace/:name/update_downloads", middleware.RepoType(types.SpaceRepo), repoCommonHandler.UpdateDownloads)
Expand Down
3 changes: 1 addition & 2 deletions builder/deploy/common/appname.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ func stringToNumber(encoded string) (int64, error) {

var num int64
base := int64(len(alphabet))
encodedRunes := []rune(encoded)

for _, r := range encodedRunes {
for _, r := range encoded {
num = num*base + alphabetMap[r]
}

Expand Down
42 changes: 30 additions & 12 deletions builder/deploy/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ func newDeployer(s scheduler.Scheduler, ib imagebuilder.Builder, ir imagerunner.
}

go d.refreshStatus()
go d.s.Run()
go func() {
err = d.s.Run()
if err != nil {
slog.Error("run scheduler failed", slog.Any("error", err))
}
}()
go d.startAccounting()

return d, nil
Expand All @@ -105,8 +110,8 @@ func (d *deployer) GenerateUniqueSvcName(dr types.DeployRepo) string {

func (d *deployer) serverlessDeploy(ctx context.Context, dr types.DeployRepo) (*database.Deploy, error) {
var (
deploy *database.Deploy = nil
err error = nil
deploy *database.Deploy
err error
)
slog.Info("do deployer.serverlessDeploy check type", slog.Any("dr.Type", dr.Type))
if dr.Type == types.SpaceType {
Expand Down Expand Up @@ -217,14 +222,24 @@ func (d *deployer) Deploy(ctx context.Context, dr types.DeployRepo) (int64, erro
Status: bldTaskStatus,
Message: bldTaskMsg,
}
d.store.CreateDeployTask(ctx, buildTask)
err = d.store.CreateDeployTask(ctx, buildTask)
if err != nil {
return -1, fmt.Errorf("failed to create deploy task: %w", err)
}
runTask := &database.DeployTask{
DeployID: deploy.ID,
TaskType: 1,
}
d.store.CreateDeployTask(ctx, runTask)
err = d.store.CreateDeployTask(ctx, runTask)
if err != nil {
return -1, fmt.Errorf("failed to create deploy task: %w", err)
}

go d.s.Queue(buildTask.ID)
go func() {
if err := d.s.Queue(buildTask.ID); err != nil {
slog.Error("failed to queue task", slog.Any("error", err))
}
}()

return deploy.ID, nil
}
Expand Down Expand Up @@ -509,10 +524,10 @@ func (d *deployer) UpdateCluster(ctx context.Context, data types.ClusterRequest)
// UpdateDeploy implements Deployer.
func (d *deployer) UpdateDeploy(ctx context.Context, dur *types.DeployUpdateReq, deploy *database.Deploy) error {
var (
frame *database.RuntimeFramework = nil
resource *database.SpaceResource = nil
hardware *types.HardWare = nil
err error = nil
frame *database.RuntimeFramework
resource *database.SpaceResource
hardware *types.HardWare
err error
)

if dur.RuntimeFrameworkID != nil {
Expand Down Expand Up @@ -602,9 +617,12 @@ func (d *deployer) StartDeploy(ctx context.Context, deploy *database.Deploy) err
DeployID: deploy.ID,
TaskType: 1,
}
d.store.CreateDeployTask(ctx, runTask)
err = d.store.CreateDeployTask(ctx, runTask)
if err != nil {
return fmt.Errorf("failed to create deploy task: %w", err)
}

go d.s.Queue(runTask.ID)
go func() { _ = d.s.Queue(runTask.ID) }()

return nil
}
Expand Down
11 changes: 7 additions & 4 deletions builder/deploy/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (rs *FIFOScheduler) Run() error {
rs.failDeployFollowingTasks(t.WatchID(), err.Error())
}

rs.next()
_, _ = rs.next()
}(t)
}

Expand All @@ -97,9 +97,9 @@ func (rs *FIFOScheduler) Run() error {

func (rs *FIFOScheduler) Queue(deployTaskID int64) error {
// simply trigger next task
rs.next()
_, err := rs.next()

return nil
return err
}

// run next task
Expand Down Expand Up @@ -179,7 +179,10 @@ func (rs *FIFOScheduler) next() (Runner, error) {
// mark task as cancelled
deployTask.Status = cancelled
deployTask.Message = "repo not found"
rs.store.UpdateDeployTask(ctx, deployTask)
err = rs.store.UpdateDeployTask(ctx, deployTask)
if err != nil {
slog.Error("update deploy task failed", "error", err)
}
}
t = &sleepTask{
du: 5 * time.Second,
Expand Down
40 changes: 32 additions & 8 deletions builder/git/gitserver/gitaly/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,18 @@ func (c *Client) CreateRepoFile(req *types.CreateFileReq) (err error) {
},
},
}
userCommitFilesClient.Send(actions[0])
userCommitFilesClient.Send(actions[1])
userCommitFilesClient.Send(actions[2])
err = userCommitFilesClient.Send(actions[0])
if err != nil {
return err
}
err = userCommitFilesClient.Send(actions[1])
if err != nil {
return err
}
err = userCommitFilesClient.Send(actions[2])
if err != nil {
return err
}
_, err = userCommitFilesClient.CloseAndRecv()
if err != nil {
return err
Expand Down Expand Up @@ -281,9 +290,18 @@ func (c *Client) UpdateRepoFile(req *types.UpdateFileReq) (err error) {
},
},
}
userCommitFilesClient.Send(actions[0])
userCommitFilesClient.Send(actions[1])
userCommitFilesClient.Send(actions[2])
err = userCommitFilesClient.Send(actions[0])
if err != nil {
return err
}
err = userCommitFilesClient.Send(actions[1])
if err != nil {
return err
}
err = userCommitFilesClient.Send(actions[2])
if err != nil {
return err
}
_, err = userCommitFilesClient.CloseAndRecv()
if err != nil {
return err
Expand Down Expand Up @@ -349,8 +367,14 @@ func (c *Client) DeleteRepoFile(req *types.DeleteFileReq) (err error) {
},
},
}
userCommitFilesClient.Send(actions[0])
userCommitFilesClient.Send(actions[1])
err = userCommitFilesClient.Send(actions[0])
if err != nil {
return err
}
err = userCommitFilesClient.Send(actions[1])
if err != nil {
return err
}
_, err = userCommitFilesClient.CloseAndRecv()
if err != nil {
return err
Expand Down
13 changes: 8 additions & 5 deletions builder/git/gitserver/gitaly/info_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import (
"context"
"fmt"
"io"
"log/slog"
"strconv"
"time"

gitalyclient "gitlab.com/gitlab-org/gitaly/v16/client"
gitalypb "gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/v16/streamio"
"opencsg.com/csghub-server/builder/git/gitserver"
)

var (
uploadPackTimeout = 10 * time.Minute
)
// var (
// uploadPackTimeout = 10 * time.Minute
// )

func (c *Client) InfoRefsResponse(ctx context.Context, req gitserver.InfoRefsReq) (io.Reader, error) {
repoType := fmt.Sprintf("%ss", string(req.RepoType))
Expand Down Expand Up @@ -131,7 +131,10 @@ func (c *Client) ReceivePack(ctx context.Context, req gitserver.ReceivePackReq)
return stream.Send(&gitalypb.PostReceivePackRequest{Data: data})
})
_, err := io.Copy(sw, req.Request.Body)
stream.CloseSend()
errClose := stream.CloseSend()
if errClose != nil {
slog.Error("closeSend stream failed", slog.Any("error", errClose))
}
errC <- err
}()

Expand Down
10 changes: 5 additions & 5 deletions builder/git/gitserver/gitea/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ func generateAccessTokenFromGitea(config *config.Config) (string, error) {
}
jsonData, err := json.Marshal(data)
if err != nil {
slog.Error("Error encoding JSON data:", err)
slog.Error("Error encoding JSON data:", "error", err)
return "", err
}

req, err := http.NewRequest("POST", giteaUrl, bytes.NewBuffer(jsonData))
if err != nil {
slog.Error("Error creating request:", err)
slog.Error("Error creating request:", "error", err)
return "", err
}

Expand All @@ -126,21 +126,21 @@ func generateAccessTokenFromGitea(config *config.Config) (string, error) {
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
slog.Error("Error sending request:", err)
slog.Error("Error sending request:", "error", err)
return "", err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
slog.Error("Error reading response body:", err)
slog.Error("Error reading response body:", "error", err)
return "", err
}

var tokenResponse TokenResponse
err = json.Unmarshal(body, &tokenResponse)
if err != nil {
slog.Error("Error decoding JSON response:", err)
slog.Error("Error decoding JSON response:", "error", err)
return "", err
}

Expand Down
10 changes: 5 additions & 5 deletions builder/git/membership/gitea/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ func generateAccessTokenFromGitea(config *config.Config) (string, error) {
}
jsonData, err := json.Marshal(data)
if err != nil {
slog.Error("Error encoding JSON data:", err)
slog.Error("Error encoding JSON data:", "error", err)
return "", err
}

req, err := http.NewRequest("POST", giteaUrl, bytes.NewBuffer(jsonData))
if err != nil {
slog.Error("Error creating request:", err)
slog.Error("Error creating request:", "error", err)
return "", err
}

Expand All @@ -126,21 +126,21 @@ func generateAccessTokenFromGitea(config *config.Config) (string, error) {
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
slog.Error("Error sending request:", err)
slog.Error("Error sending request:", "error", err)
return "", err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
slog.Error("Error reading response body:", err)
slog.Error("Error reading response body:", "error", err)
return "", err
}

var tokenResponse TokenResponse
err = json.Unmarshal(body, &tokenResponse)
if err != nil {
slog.Error("Error decoding JSON response:", err)
slog.Error("Error decoding JSON response:", "error", err)
return "", err
}

Expand Down
Loading

0 comments on commit 0dde27b

Please sign in to comment.