From b4bb41e0e3ebe05337d003a8b034be8312ab4a98 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Thu, 21 Mar 2024 08:25:40 +0100 Subject: [PATCH] introduce federated repo --- .../federation-architecture.md | 18 ++++++++++ models/repo/federated_repo.go | 36 +++++++++++++++++++ models/repo/repo.go | 2 +- models/repo/repo_repository.go | 12 +++++++ models/user/federated_user.go | 4 +-- 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 models/repo/federated_repo.go create mode 100644 models/repo/repo_repository.go diff --git a/docs/unsure-where-to-put/federation-architecture.md b/docs/unsure-where-to-put/federation-architecture.md index 271b87de3b..11e8590f32 100644 --- a/docs/unsure-where-to-put/federation-architecture.md +++ b/docs/unsure-where-to-put/federation-architecture.md @@ -102,8 +102,26 @@ classDiagram } } + namespace repository { + class Repository { + <> + ID int64 + } + + class FederatedRepository { + ID int64 + RepositoryID int64 + ExternalID string + FederationHost int64 + Validate() []string + } + } + User *-- FederatedUser: FederatedUser.UserID PersonID -- FederatedUser : mapped by PersonID.ID == FederatedUser.externalID & FederationHost.ID PersonID -- FederationHost : mapped by PersonID.Host == FederationHost.HostFqdn FederatedUser -- FederationHost + + Repository *-- FederatedRepository + FederatedRepository -- FederationHost ``` diff --git a/models/repo/federated_repo.go b/models/repo/federated_repo.go new file mode 100644 index 0000000000..854c1915b6 --- /dev/null +++ b/models/repo/federated_repo.go @@ -0,0 +1,36 @@ +// Copyright 2024 The forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repo + +import ( + "code.gitea.io/gitea/modules/validation" +) + +// FederatedRepo represents a federated Repository Actor connected with a local Repo +type FederatedRepo struct { + ID int64 `xorm:"pk autoincr"` + RepositoryID int64 `xorm:"NOT NULL"` + ExternalID string `xorm:"TEXT UNIQUE(federation_repo_mapping) NOT NULL"` + FederationHostID int64 `xorm:"UNIQUE(federation_repo_mapping) NOT NULL"` +} + +func NewFederatedRepo(repositoryID int64, externalID string, federationHostID int64) (FederatedRepo, error) { + result := FederatedRepo{ + RepositoryID: repositoryID, + ExternalID: externalID, + FederationHostID: federationHostID, + } + if valid, err := validation.IsValid(result); !valid { + return FederatedRepo{}, err + } + return result, nil +} + +func (user FederatedRepo) Validate() []string { + var result []string + result = append(result, validation.ValidateNotEmpty(user.RepositoryID, "UserID")...) + result = append(result, validation.ValidateNotEmpty(user.ExternalID, "ExternalID")...) + result = append(result, validation.ValidateNotEmpty(user.FederationHostID, "FederationHostID")...) + return result +} diff --git a/models/repo/repo.go b/models/repo/repo.go index da1a159f35..af3488b8f6 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Gitea Authors. All rights reserved. +// Copyright 2021, 2024 The Gitea & forgejo Authors. All rights reserved. // SPDX-License-Identifier: MIT package repo diff --git a/models/repo/repo_repository.go b/models/repo/repo_repository.go new file mode 100644 index 0000000000..01dace0346 --- /dev/null +++ b/models/repo/repo_repository.go @@ -0,0 +1,12 @@ +// Copyright 2024 The forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repo + +import ( + "code.gitea.io/gitea/models/db" +) + +func init() { + db.RegisterModel(new(FederatedRepo)) +} diff --git a/models/user/federated_user.go b/models/user/federated_user.go index 5ac605b0b1..7c8fe7c8ad 100644 --- a/models/user/federated_user.go +++ b/models/user/federated_user.go @@ -10,8 +10,8 @@ import ( type FederatedUser struct { ID int64 `xorm:"pk autoincr"` UserID int64 `xorm:"NOT NULL"` - ExternalID string `xorm:"TEXT UNIQUE(federation_mapping) NOT NULL"` - FederationHostID int64 `xorm:"UNIQUE(federation_mapping) NOT NULL"` + ExternalID string `xorm:"TEXT UNIQUE(federation_user_mapping) NOT NULL"` + FederationHostID int64 `xorm:"UNIQUE(federation_user_mapping) NOT NULL"` } func NewFederatedUser(userID int64, externalID string, federationHostID int64) (FederatedUser, error) {