refactor: deploy key and private route handlers (#38999)

clean up legacy code, fix various bugs:

* add missing "return"
This commit is contained in:
wxiaoguang
2026-08-20 23:57:17 +08:00
committed by GitHub
parent 61be9fcdfa
commit 943f026844
23 changed files with 185 additions and 411 deletions
+2 -7
View File
@@ -34,7 +34,7 @@ func deleteDeployKeyFromDB(ctx context.Context, key *asymkey_model.DeployKey) er
}
// Check if this is the last reference to same key content.
has, err := asymkey_model.IsDeployKeyExistByKeyID(ctx, key.KeyID)
has, err := asymkey_model.IsDeployKeyExistByPublicKeyID(ctx, key.KeyID)
if err != nil {
return err
} else if !has {
@@ -50,18 +50,13 @@ func deleteDeployKeyFromDB(ctx context.Context, key *asymkey_model.DeployKey) er
// Permissions check should be done outside.
func DeleteDeployKey(ctx context.Context, repo *repo_model.Repository, id int64) error {
if err := db.WithTx(ctx, func(ctx context.Context) error {
key, err := asymkey_model.GetDeployKeyByID(ctx, id)
key, err := asymkey_model.GetDeployKeyByID(ctx, repo.ID, id)
if err != nil {
if asymkey_model.IsErrDeployKeyNotExist(err) {
return nil
}
return fmt.Errorf("GetDeployKeyByID: %w", err)
}
if key.RepoID != repo.ID {
return fmt.Errorf("deploy key %d does not belong to repository %d", id, repo.ID)
}
return deleteDeployKeyFromDB(ctx, key)
}); err != nil {
return err
+13 -10
View File
@@ -847,17 +847,20 @@ func ToGitHook(h *git.Hook) *api.GitHook {
}
// ToDeployKey convert asymkey_model.DeployKey to api.DeployKey
func ToDeployKey(apiLink string, key *asymkey_model.DeployKey) *api.DeployKey {
return &api.DeployKey{
ID: key.ID,
KeyID: key.KeyID,
Key: key.Content,
Fingerprint: key.Fingerprint,
URL: fmt.Sprintf("%s%d", apiLink, key.ID),
Title: key.Name,
Created: key.CreatedUnix.AsTime(),
ReadOnly: key.Mode == perm.AccessModeRead, // All deploy keys are read-only.
func ToDeployKey(ctx context.Context, repo *repo_model.Repository, deployKey *asymkey_model.DeployKey) *api.DeployKey {
k := &api.DeployKey{
ID: deployKey.ID,
KeyID: deployKey.KeyID,
URL: repo.APIURL(ctx) + fmt.Sprintf("/keys/%d", deployKey.ID),
Title: deployKey.Name,
Created: deployKey.CreatedUnix.AsTime(),
ReadOnly: deployKey.Mode == perm.AccessModeRead, // All deploy keys are read-only.
}
if err := deployKey.LoadPublicKey(ctx); err == nil {
k.Key = deployKey.PublicKey.Content
k.Fingerprint = deployKey.PublicKey.Fingerprint
}
return k
}
// ToOrganization convert user_model.User to api.Organization