2024-01-18 16:40:33 +00:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-12 21:50:38 +00:00
|
|
|
"slices"
|
2024-01-18 16:40:33 +00:00
|
|
|
|
2024-01-12 21:50:38 +00:00
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
2024-01-18 16:40:33 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unit"
|
2024-01-12 21:50:38 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-01-31 14:55:12 +00:00
|
|
|
actions_service "code.gitea.io/gitea/services/actions"
|
2024-01-18 16:40:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateRepositoryUnits updates a repository's units
|
|
|
|
func UpdateRepositoryUnits(ctx context.Context, repo *repo_model.Repository, units []repo_model.RepoUnit, deleteUnitTypes []unit.Type) (err error) {
|
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer committer.Close()
|
|
|
|
|
|
|
|
// Delete existing settings of units before adding again
|
|
|
|
for _, u := range units {
|
|
|
|
deleteUnitTypes = append(deleteUnitTypes, u.Type)
|
|
|
|
}
|
|
|
|
|
2024-01-12 21:50:38 +00:00
|
|
|
if slices.Contains(deleteUnitTypes, unit.TypeActions) {
|
|
|
|
if err := actions_model.CleanRepoScheduleTasks(ctx, repo); err != nil {
|
|
|
|
log.Error("CleanRepoScheduleTasks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 14:55:12 +00:00
|
|
|
for _, u := range units {
|
|
|
|
if u.Type == unit.TypeActions {
|
|
|
|
if err := actions_service.DetectAndHandleSchedules(ctx, repo); err != nil {
|
|
|
|
log.Error("DetectAndHandleSchedules: %v", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 16:40:33 +00:00
|
|
|
if _, err = db.GetEngine(ctx).Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(repo_model.RepoUnit)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(units) > 0 {
|
|
|
|
if err = db.Insert(ctx, units); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return committer.Commit()
|
|
|
|
}
|