From 92d011f1a597d6e9020d489da7b5a0b276f20930 Mon Sep 17 00:00:00 2001 From: Clemens Date: Thu, 4 Apr 2024 15:08:02 +0200 Subject: [PATCH] fix some linting issues --- models/forgefed/activity.go | 2 +- models/repo/following_repo.go | 6 +++--- models/repo/following_repo_test.go | 4 ++-- models/repo/repo_repository.go | 8 ++++---- modules/context/repo.go | 2 +- routers/web/repo/repo.go | 5 ++--- services/federation/federation_service.go | 22 ++++++++++++---------- services/repository/repository.go | 4 +++- 8 files changed, 28 insertions(+), 25 deletions(-) diff --git a/models/forgefed/activity.go b/models/forgefed/activity.go index 44214973e6..32b375d8b3 100644 --- a/models/forgefed/activity.go +++ b/models/forgefed/activity.go @@ -18,7 +18,7 @@ type ForgeLike struct { ap.Activity } -func NewForgeLike(actorIRI string, objectIRI string, startTime time.Time) (ForgeLike, error) { +func NewForgeLike(actorIRI, objectIRI string, startTime time.Time) (ForgeLike, error) { result := ForgeLike{} result.Type = ap.LikeType // ToDo: Would validating the source by Actor.Type field make sense? diff --git a/models/repo/following_repo.go b/models/repo/following_repo.go index a935433e40..f3b3a57406 100644 --- a/models/repo/following_repo.go +++ b/models/repo/following_repo.go @@ -14,7 +14,7 @@ type FollowingRepo struct { RepoID int64 `xorm:"NOT NULL"` ExternalID string `xorm:"TEXT UNIQUE(federation_repo_mapping) NOT NULL"` FederationHostID int64 `xorm:"UNIQUE(federation_repo_mapping) NOT NULL"` - Uri string + URI string } func NewFollowingRepo(repoID int64, externalID string, federationHostID int64, uri string) (FollowingRepo, error) { @@ -22,7 +22,7 @@ func NewFollowingRepo(repoID int64, externalID string, federationHostID int64, u RepoID: repoID, ExternalID: externalID, FederationHostID: federationHostID, - Uri: uri, + URI: uri, } if valid, err := validation.IsValid(result); !valid { return FollowingRepo{}, err @@ -35,6 +35,6 @@ func (user FollowingRepo) Validate() []string { result = append(result, validation.ValidateNotEmpty(user.RepoID, "UserID")...) result = append(result, validation.ValidateNotEmpty(user.ExternalID, "ExternalID")...) result = append(result, validation.ValidateNotEmpty(user.FederationHostID, "FederationHostID")...) - result = append(result, validation.ValidateNotEmpty(user.Uri, "Uri")...) + result = append(result, validation.ValidateNotEmpty(user.URI, "Uri")...) return result } diff --git a/models/repo/following_repo_test.go b/models/repo/following_repo_test.go index d83db6aefe..d0dd0a31a7 100644 --- a/models/repo/following_repo_test.go +++ b/models/repo/following_repo_test.go @@ -14,7 +14,7 @@ func Test_FollowingRepoValidation(t *testing.T) { RepoID: 12, ExternalID: "12", FederationHostID: 1, - Uri: "http://localhost:3000/api/v1/activitypub/repo-id/1", + URI: "http://localhost:3000/api/v1/activitypub/repo-id/1", } if res, err := validation.IsValid(sut); !res { t.Errorf("sut should be valid but was %q", err) @@ -23,7 +23,7 @@ func Test_FollowingRepoValidation(t *testing.T) { sut = FollowingRepo{ ExternalID: "12", FederationHostID: 1, - Uri: "http://localhost:3000/api/v1/activitypub/repo-id/1", + URI: "http://localhost:3000/api/v1/activitypub/repo-id/1", } if res, _ := validation.IsValid(sut); res { t.Errorf("sut should be invalid") diff --git a/models/repo/repo_repository.go b/models/repo/repo_repository.go index 9707e76e36..a9d91d3318 100644 --- a/models/repo/repo_repository.go +++ b/models/repo/repo_repository.go @@ -15,9 +15,9 @@ func init() { db.RegisterModel(new(FollowingRepo)) } -func FindFollowingReposByRepoID(ctx context.Context, repoId int64) ([]*FollowingRepo, error) { +func FindFollowingReposByRepoID(ctx context.Context, repoID int64) ([]*FollowingRepo, error) { maxFollowingRepos := 10 - sess := db.GetEngine(ctx).Where("repo_id=?", repoId) + sess := db.GetEngine(ctx).Where("repo_id=?", repoID) sess = sess.Limit(maxFollowingRepos, 0) followingRepoList := make([]*FollowingRepo, 0, maxFollowingRepos) err := sess.Find(&followingRepoList) @@ -32,7 +32,7 @@ func FindFollowingReposByRepoID(ctx context.Context, repoId int64) ([]*Following return followingRepoList, nil } -func StoreFollowingRepos(ctx context.Context, localRepoId int64, followingRepoList []*FollowingRepo) error { +func StoreFollowingRepos(ctx context.Context, localRepoID int64, followingRepoList []*FollowingRepo) error { for _, followingRepo := range followingRepoList { if res, err := validation.IsValid(*followingRepo); !res { return fmt.Errorf("FederationInfo is not valid: %v", err) @@ -46,7 +46,7 @@ func StoreFollowingRepos(ctx context.Context, localRepoId int64, followingRepoLi } defer committer.Close() - _, err = db.GetEngine(ctx).Where("repo_id=?", localRepoId).Delete(FollowingRepo{}) + _, err = db.GetEngine(ctx).Where("repo_id=?", localRepoID).Delete(FollowingRepo{}) if err != nil { return err } diff --git a/modules/context/repo.go b/modules/context/repo.go index d836eda756..850edf666d 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -387,7 +387,7 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) { if idx > 0 { followingRepoString += ";" } - followingRepoString += (*followingRepo).Uri + followingRepoString += followingRepo.URI } ctx.Data["FollowingRepos"] = followingRepoString } else if err != repo_model.ErrMirrorNotExist { diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 7f72599bca..c6fa843f45 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -33,7 +33,6 @@ import ( "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/convert" "code.gitea.io/gitea/services/forms" - "code.gitea.io/gitea/services/repository" repo_service "code.gitea.io/gitea/services/repository" archiver_service "code.gitea.io/gitea/services/repository/archiver" ) @@ -318,9 +317,9 @@ func Action(ctx *context.Context) { case "unwatch": err = repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false) case "star": - err = repository.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true) + err = repo_service.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true) case "unstar": - err = repository.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false) + err = repo_service.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false) case "accept_transfer": err = acceptOrRejectRepoTransfer(ctx, true) case "reject_transfer": diff --git a/services/federation/federation_service.go b/services/federation/federation_service.go index f0d6996bef..61345029fd 100644 --- a/services/federation/federation_service.go +++ b/services/federation/federation_service.go @@ -40,7 +40,7 @@ func ProcessLikeActivity(ctx context.Context, form any, repositoryID int64) (int // parse actorID (person) actorURI := activity.Actor.GetID().String() log.Info("actorURI was: %v", actorURI) - federationHost, err := GetFederationHostForUri(ctx, actorURI) + federationHost, err := GetFederationHostForURI(ctx, actorURI) if err != nil { return http.StatusInternalServerError, "Wrong FederationHost", err } @@ -129,7 +129,7 @@ func CreateFederationHostFromAP(ctx context.Context, actorID forgefed.ActorID) ( return &result, nil } -func GetFederationHostForUri(ctx context.Context, actorURI string) (*forgefed.FederationHost, error) { +func GetFederationHostForURI(ctx context.Context, actorURI string) (*forgefed.FederationHost, error) { // parse actorID (person) log.Info("Input was: %v", actorURI) rawActorID, err := forgefed.NewActorID(actorURI) @@ -214,10 +214,10 @@ func CreateUserFromAP(ctx context.Context, personID forgefed.PersonID, federatio } // Create or update a list of FollowingRepo structs -func StoreFollowingRepoList(ctx context.Context, localRepoId int64, followingRepoList []string) (int, string, error) { +func StoreFollowingRepoList(ctx context.Context, localRepoID int64, followingRepoList []string) (int, string, error) { followingRepos := make([]*repo.FollowingRepo, 0, len(followingRepoList)) for _, uri := range followingRepoList { - federationHost, err := GetFederationHostForUri(ctx, uri) + federationHost, err := GetFederationHostForURI(ctx, uri) if err != nil { return http.StatusInternalServerError, "Wrong FederationHost", err } @@ -225,20 +225,22 @@ func StoreFollowingRepoList(ctx context.Context, localRepoId int64, followingRep if err != nil { return http.StatusNotAcceptable, "Invalid federated repo", err } - followingRepo, err := repo.NewFollowingRepo(localRepoId, followingRepoID.ID, federationHost.ID, uri) + followingRepo, err := repo.NewFollowingRepo(localRepoID, followingRepoID.ID, federationHost.ID, uri) if err != nil { return http.StatusNotAcceptable, "Invalid federated repo", err } followingRepos = append(followingRepos, &followingRepo) } - repo.StoreFollowingRepos(ctx, localRepoId, followingRepos) + if err := repo.StoreFollowingRepos(ctx, localRepoID, followingRepos); err != nil { + return 0, "", err + } return 0, "", nil } -func DeleteFollowingRepos(ctx context.Context, localRepoId int64) error { - return repo.StoreFollowingRepos(ctx, localRepoId, []*repo.FollowingRepo{}) +func DeleteFollowingRepos(ctx context.Context, localRepoID int64) error { + return repo.StoreFollowingRepos(ctx, localRepoID, []*repo.FollowingRepo{}) } func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error { @@ -250,7 +252,7 @@ func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error likeActivityList := make([]forgefed.ForgeLike, 0) for _, followingRepo := range followingRepos { - target := followingRepo.Uri + target := followingRepo.URI likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now()) if err != nil { return err @@ -268,7 +270,7 @@ func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error return err } - _, err = apclient.Post([]byte(json), fmt.Sprintf("%v/inbox/", activity.Object)) + _, err = apclient.Post(json, fmt.Sprintf("%v/inbox/", activity.Object)) if err != nil { log.Error("error %v while sending activity: %q", err, activity) } diff --git a/services/repository/repository.go b/services/repository/repository.go index 30fb1767da..088cb654d4 100644 --- a/services/repository/repository.go +++ b/services/repository/repository.go @@ -67,7 +67,9 @@ func DeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_mod return err } - federation_service.DeleteFollowingRepos(ctx, repo.ID) + if err := federation_service.DeleteFollowingRepos(ctx, repo.ID); err != nil { + return err + } return packages_model.UnlinkRepositoryFromAllPackages(ctx, repo.ID) }