From 3ef57385ec32ac63eb8d1e0e80a64de1c2cfb83f Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 9 Feb 2024 16:44:03 +0100 Subject: [PATCH] fix linting --- models/user/user.go | 6 ++--- models/user/user_repository.go | 2 +- modules/forgefed/federation_service.go | 34 ++++++++++++------------ routers/api/actions/runner/runner.go | 4 +-- routers/api/v1/activitypub/repository.go | 2 +- routers/api/v1/activitypub/response.go | 30 --------------------- 6 files changed, 24 insertions(+), 54 deletions(-) diff --git a/models/user/user.go b/models/user/user.go index 5490bd4cf1..a7c19744a4 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -811,12 +811,12 @@ func ValidateUser(u *User, cols ...string) error { return nil } -func (user User) Validate() []string { +func (u User) Validate() []string { var result []string - if err := ValidateUser(&user); err != nil { + if err := ValidateUser(&u); err != nil { result = append(result, err.Error()) } - if err := ValidateEmail(user.Email); err != nil { + if err := ValidateEmail(u.Email); err != nil { result = append(result, err.Error()) } return result diff --git a/models/user/user_repository.go b/models/user/user_repository.go index ee1f6dd6d2..c823aa1c84 100644 --- a/models/user/user_repository.go +++ b/models/user/user_repository.go @@ -65,7 +65,7 @@ func FindFederatedUser(ctx context.Context, externalID string, if err != nil { return nil, nil, err } else if !has { - return nil, nil, fmt.Errorf("User %v for federated user is missing.", federatedUser.UserID) + return nil, nil, fmt.Errorf("User %v for federated user is missing", federatedUser.UserID) } if res, err := validation.IsValid(*user); !res { diff --git a/modules/forgefed/federation_service.go b/modules/forgefed/federation_service.go index a4dec26052..8b5e0714bf 100644 --- a/modules/forgefed/federation_service.go +++ b/modules/forgefed/federation_service.go @@ -22,10 +22,10 @@ import ( "github.com/google/uuid" ) -func LikeActivity(ctx *context.APIContext, form any, repositoryId int64) (error, int, string) { +func LikeActivity(ctx *context.APIContext, form any, repositoryID int64) (int, string, error) { activity := form.(*forgefed.ForgeLike) if res, err := validation.IsValid(activity); !res { - return err, http.StatusNotAcceptable, "Invalid activity" + return http.StatusNotAcceptable, "Invalid activity", err } log.Info("Activity validated:%v", activity) @@ -33,69 +33,69 @@ func LikeActivity(ctx *context.APIContext, form any, repositoryId int64) (error, actorURI := activity.Actor.GetID().String() rawActorID, err := forgefed.NewActorID(actorURI) if err != nil { - return err, http.StatusInternalServerError, "Invalid ActorID" + return http.StatusInternalServerError, "Invalid ActorID", err } federationHost, err := forgefed.FindFederationHostByFqdn(ctx, rawActorID.Host) if err != nil { - return err, http.StatusInternalServerError, "Could not loading FederationHost" + return http.StatusInternalServerError, "Could not loading FederationHost", err } if federationHost == nil { result, err := CreateFederationHostFromAP(ctx, rawActorID) if err != nil { - return err, http.StatusNotAcceptable, "Invalid FederationHost" + return http.StatusNotAcceptable, "Invalid FederationHost", err } federationHost = result } if !activity.IsNewer(federationHost.LatestActivity) { - return fmt.Errorf("Activity already processed"), http.StatusNotAcceptable, "Activity out of order." + return http.StatusNotAcceptable, "Activity out of order.", fmt.Errorf("Activity already processed") } actorID, err := forgefed.NewPersonID(actorURI, string(federationHost.NodeInfo.Source)) if err != nil { - return err, http.StatusNotAcceptable, "Invalid PersonID" + return http.StatusNotAcceptable, "Invalid PersonID", err } log.Info("Actor accepted:%v", actorID) // parse objectID (repository) objectID, err := forgefed.NewRepositoryID(activity.Object.GetID().String(), string(forgefed.ForgejoSourceType)) if err != nil { - return err, http.StatusNotAcceptable, "Invalid objectId" + return http.StatusNotAcceptable, "Invalid objectId", err } - if objectID.ID != fmt.Sprint(repositoryId) { - return err, http.StatusNotAcceptable, "Invalid objectId" + if objectID.ID != fmt.Sprint(repositoryID) { + return http.StatusNotAcceptable, "Invalid objectId", err } log.Info("Object accepted:%v", objectID) // Check if user already exists user, _, err := user.FindFederatedUser(ctx, actorID.ID, federationHost.ID) if err != nil { - return err, http.StatusInternalServerError, "Searching for user failed" + return http.StatusInternalServerError, "Searching for user failed", err } if user != nil { log.Info("Found local federatedUser: %v", user) } else { user, _, err = CreateUserFromAP(ctx, actorID, federationHost.ID) if err != nil { - return err, http.StatusInternalServerError, "Error creating federatedUser" + return http.StatusInternalServerError, "Error creating federatedUser", err } log.Info("Created federatedUser from ap: %v", user) } log.Info("Got user:%v", user.Name) // execute the activity if the repo was not stared already - alreadyStared := repo.IsStaring(ctx, user.ID, repositoryId) + alreadyStared := repo.IsStaring(ctx, user.ID, repositoryID) if !alreadyStared { - err = repo.StarRepo(ctx, user.ID, repositoryId, true) + err = repo.StarRepo(ctx, user.ID, repositoryID, true) if err != nil { - return err, http.StatusNotAcceptable, "Error staring" + return http.StatusNotAcceptable, "Error staring", err } } federationHost.LatestActivity = activity.StartTime err = forgefed.UpdateFederationHost(ctx, federationHost) if err != nil { - return err, http.StatusNotAcceptable, "Error updating federatedHost" + return http.StatusNotAcceptable, "Error updating federatedHost", err } - return nil, 0, "" + return 0, "", nil } func CreateFederationHostFromAP(ctx *context.APIContext, actorID forgefed.ActorID) (*forgefed.FederationHost, error) { diff --git a/routers/api/actions/runner/runner.go b/routers/api/actions/runner/runner.go index 8df6f297ce..7641db6792 100644 --- a/routers/api/actions/runner/runner.go +++ b/routers/api/actions/runner/runner.go @@ -58,8 +58,8 @@ func (s *Service) Register( // TODO: agent_labels should be removed from pb after Gitea 1.20 released. // Old version runner's agent_labels slice is not empty and labels slice is empty. // And due to compatibility with older versions, it is temporarily marked as Deprecated in pb, so use `//nolint` here. - if len(req.Msg.AgentLabels) > 0 && len(req.Msg.Labels) == 0 { //nolint:staticcheck - labels = req.Msg.AgentLabels //nolint:staticcheck + if len(req.Msg.AgentLabels) > 0 && len(req.Msg.Labels) == 0 { + labels = req.Msg.AgentLabels } // create new runner diff --git a/routers/api/v1/activitypub/repository.go b/routers/api/v1/activitypub/repository.go index 7264bb1487..42241ebffe 100644 --- a/routers/api/v1/activitypub/repository.go +++ b/routers/api/v1/activitypub/repository.go @@ -73,7 +73,7 @@ func RepositoryInbox(ctx *context.APIContext) { log.Info("RepositoryInbox: repo: %v", repository) form := web.GetForm(ctx) - err, httpStatus, title := forgefed.LikeActivity(ctx, form, repository.ID) + httpStatus, title, err := forgefed.LikeActivity(ctx, form, repository.ID) if err != nil { ctx.Error(httpStatus, title, err) } diff --git a/routers/api/v1/activitypub/response.go b/routers/api/v1/activitypub/response.go index e3997c60cb..882e2bbe6c 100644 --- a/routers/api/v1/activitypub/response.go +++ b/routers/api/v1/activitypub/response.go @@ -4,10 +4,8 @@ package activitypub import ( - "fmt" "net/http" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/forgefed" "code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/context" @@ -17,34 +15,6 @@ import ( "github.com/go-ap/jsonld" ) -// Respond with a ActivityStreams Collection -func responseCollection(ctx *context.APIContext, iri string, listOptions db.ListOptions, items []string, count int64) { - collection := ap.OrderedCollectionNew(ap.IRI(iri)) - collection.First = ap.IRI(iri + "?page=1") - collection.TotalItems = uint(count) - if listOptions.Page == 0 { - response(ctx, collection) - return - } - - page := ap.OrderedCollectionPageNew(collection) - page.ID = ap.IRI(fmt.Sprintf("%s?page=%d", iri, listOptions.Page)) - if listOptions.Page > 1 { - page.Prev = ap.IRI(fmt.Sprintf("%s?page=%d", iri, listOptions.Page-1)) - } - if listOptions.Page*listOptions.PageSize < int(count) { - page.Next = ap.IRI(fmt.Sprintf("%s?page=%d", iri, listOptions.Page+1)) - } - for _, item := range items { - err := page.OrderedItems.Append(ap.IRI(item)) - if err != nil { - ctx.ServerError("Append", err) - } - } - - response(ctx, page) -} - // Respond with an ActivityStreams object func response(ctx *context.APIContext, v interface{}) { binary, err := jsonld.WithContext(