ui for adding following repos

This commit is contained in:
Michael Jerger 2024-05-24 13:28:15 +02:00
parent eea841d25d
commit 82cb9e0203
15 changed files with 284 additions and 0 deletions

View file

@ -0,0 +1,39 @@
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"code.gitea.io/gitea/modules/validation"
)
// FollowingRepo represents a federated Repository Actor connected with a local Repo
type FollowingRepo struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"UNIQUE(federation_repo_mapping) NOT NULL"`
ExternalID string `xorm:"UNIQUE(federation_repo_mapping) NOT NULL"`
FederationHostID int64 `xorm:"UNIQUE(federation_repo_mapping) NOT NULL"`
URI string
}
func NewFollowingRepo(repoID int64, externalID string, federationHostID int64, uri string) (FollowingRepo, error) {
result := FollowingRepo{
RepoID: repoID,
ExternalID: externalID,
FederationHostID: federationHostID,
URI: uri,
}
if valid, err := validation.IsValid(result); !valid {
return FollowingRepo{}, err
}
return result, nil
}
func (user FollowingRepo) Validate() []string {
var result []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")...)
return result
}

View file

@ -0,0 +1,31 @@
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"testing"
"code.gitea.io/gitea/modules/validation"
)
func Test_FollowingRepoValidation(t *testing.T) {
sut := FollowingRepo{
RepoID: 12,
ExternalID: "12",
FederationHostID: 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)
}
sut = FollowingRepo{
ExternalID: "12",
FederationHostID: 1,
URI: "http://localhost:3000/api/v1/activitypub/repo-id/1",
}
if res, _ := validation.IsValid(sut); res {
t.Errorf("sut should be invalid")
}
}

View file

@ -1,4 +1,5 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
@ -342,6 +343,11 @@ func (repo *Repository) APIURL() string {
return setting.AppURL + "api/v1/repos/" + url.PathEscape(repo.OwnerName) + "/" + url.PathEscape(repo.Name)
}
// APActorID returns the activitypub repository API URL
func (repo *Repository) APActorID() string {
return fmt.Sprintf("%vapi/v1/activitypub/repository-id/%v", setting.AppURL, url.PathEscape(fmt.Sprint(repo.ID)))
}
// GetCommitsCountCacheKey returns cache key used for commits count caching.
func (repo *Repository) GetCommitsCountCacheKey(contextName string, isRef bool) string {
var prefix string

View file

@ -0,0 +1,60 @@
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"context"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/validation"
)
func init() {
db.RegisterModel(new(FollowingRepo))
}
func FindFollowingReposByRepoID(ctx context.Context, repoID int64) ([]*FollowingRepo, error) {
maxFollowingRepos := 10
sess := db.GetEngine(ctx).Where("repo_id=?", repoID)
sess = sess.Limit(maxFollowingRepos, 0)
followingRepoList := make([]*FollowingRepo, 0, maxFollowingRepos)
err := sess.Find(&followingRepoList)
if err != nil {
return make([]*FollowingRepo, 0, maxFollowingRepos), err
}
for _, followingRepo := range followingRepoList {
if res, err := validation.IsValid(*followingRepo); !res {
return make([]*FollowingRepo, 0, maxFollowingRepos), err
}
}
return followingRepoList, nil
}
func StoreFollowingRepos(ctx context.Context, localRepoID int64, followingRepoList []*FollowingRepo) error {
for _, followingRepo := range followingRepoList {
if res, err := validation.IsValid(*followingRepo); !res {
return err
}
}
// Begin transaction
ctx, committer, err := db.TxContext((ctx))
if err != nil {
return err
}
defer committer.Close()
_, err = db.GetEngine(ctx).Where("repo_id=?", localRepoID).Delete(FollowingRepo{})
if err != nil {
return err
}
for _, followingRepo := range followingRepoList {
_, err = db.GetEngine(ctx).Insert(followingRepo)
if err != nil {
return err
}
}
// Commit transaction
return committer.Commit()
}

View file

@ -1,4 +1,5 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo_test
@ -217,3 +218,12 @@ func TestComposeSSHCloneURL(t *testing.T) {
setting.SSH.Port = 123
assert.Equal(t, "ssh://git@[::1]:123/user/repo.git", repo_model.ComposeSSHCloneURL("user", "repo"))
}
func TestAPActorID(t *testing.T) {
repo := repo_model.Repository{ID: 1}
url := repo.APActorID()
expected := "https://try.gitea.io/api/v1/activitypub/repository-id/1"
if url != expected {
t.Errorf("unexpected APActorID, expected: %q, actual: %q", expected, url)
}
}