Skip to content

Commit

Permalink
Merge branch 'main' into deb
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug authored Jul 24, 2024
2 parents cdfb256 + 63bdc74 commit 0575068
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ linters-settings:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- prefix(github.com/ustclug/Yuki)
govet:
enable:
- nilness
exhaustive:
# Only run exhaustive check on switches with "//exhaustive:enforce" comment.
explicit-exhaustive-switch: true
Expand Down
10 changes: 7 additions & 3 deletions pkg/server/repo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *Server) handlerGetRepo(c echo.Context) error {
Where(model.Repo{Name: name}).
Limit(1).
Find(&repo)
if err != nil {
if res.Error != nil {
const msg = "Fail to get Repo"
l.Error(msg, slogErrAttr(err))
return newHTTPError(http.StatusInternalServerError, msg)
Expand All @@ -84,8 +84,8 @@ func (s *Server) handlerRemoveRepo(c echo.Context) error {
}

db := s.getDB(c)
err = db.Where(model.Repo{Name: name}).Delete(&model.Repo{}).Error
if err != nil {
res := db.Where(model.Repo{Name: name}).Delete(&model.Repo{})
if res.Error != nil {
const msg = "Fail to delete Repo"
l.Error(msg, slogErrAttr(err), slog.String("repo", name))
return newHTTPError(http.StatusInternalServerError, msg)
Expand All @@ -95,6 +95,10 @@ func (s *Server) handlerRemoveRepo(c echo.Context) error {
l.Error("Fail to delete RepoMeta", slogErrAttr(err), slog.String("repo", name))
}
s.repoSchedules.Remove(name)
// Check repo existence after RepoMeta and schedule removal, to prevent inconsistency
if res.RowsAffected == 0 {
return newHTTPError(http.StatusNotFound, "Repo not found")
}
return c.NoContent(http.StatusNoContent)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/server/repo_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ func TestHandlerRemoveRepo(t *testing.T) {
require.False(t, te.server.repoSchedules.Has(name))
require.ErrorContains(t, te.server.db.First(&model.Repo{Name: name}).Error, "record not found")
require.ErrorContains(t, te.server.db.First(&model.RepoMeta{Name: name}).Error, "record not found")

resp, err = cli.R().Delete("/repos/nonexist")
require.NoError(t, err)
require.Equal(t, 404, resp.StatusCode(), "Removing non-exist repo does not return 404")
}

0 comments on commit 0575068

Please sign in to comment.