code.gitea.io/gitea@v1.21.7/services/repository/setting.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repository
     5  
     6  import (
     7  	"context"
     8  	"slices"
     9  
    10  	actions_model "code.gitea.io/gitea/models/actions"
    11  	"code.gitea.io/gitea/models/db"
    12  	repo_model "code.gitea.io/gitea/models/repo"
    13  	"code.gitea.io/gitea/models/unit"
    14  	"code.gitea.io/gitea/modules/log"
    15  	actions_service "code.gitea.io/gitea/services/actions"
    16  )
    17  
    18  // UpdateRepositoryUnits updates a repository's units
    19  func UpdateRepositoryUnits(ctx context.Context, repo *repo_model.Repository, units []repo_model.RepoUnit, deleteUnitTypes []unit.Type) (err error) {
    20  	ctx, committer, err := db.TxContext(ctx)
    21  	if err != nil {
    22  		return err
    23  	}
    24  	defer committer.Close()
    25  
    26  	// Delete existing settings of units before adding again
    27  	for _, u := range units {
    28  		deleteUnitTypes = append(deleteUnitTypes, u.Type)
    29  	}
    30  
    31  	if slices.Contains(deleteUnitTypes, unit.TypeActions) {
    32  		if err := actions_model.CleanRepoScheduleTasks(ctx, repo); err != nil {
    33  			log.Error("CleanRepoScheduleTasks: %v", err)
    34  		}
    35  	}
    36  
    37  	for _, u := range units {
    38  		if u.Type == unit.TypeActions {
    39  			if err := actions_service.DetectAndHandleSchedules(ctx, repo); err != nil {
    40  				log.Error("DetectAndHandleSchedules: %v", err)
    41  			}
    42  			break
    43  		}
    44  	}
    45  
    46  	if _, err = db.GetEngine(ctx).Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(repo_model.RepoUnit)); err != nil {
    47  		return err
    48  	}
    49  
    50  	if len(units) > 0 {
    51  		if err = db.Insert(ctx, units); err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	return committer.Commit()
    57  }