You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repofiles
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. )
  10. // CreateCommitStatus creates a new CommitStatus given a bunch of parameters
  11. // NOTE: All text-values will be trimmed from whitespaces.
  12. // Requires: Repo, Creator, SHA
  13. func CreateCommitStatus(repo *models.Repository, creator *models.User, sha string, status *models.CommitStatus) error {
  14. repoPath := repo.RepoPath()
  15. // confirm that commit is exist
  16. gitRepo, err := git.OpenRepository(repoPath)
  17. if err != nil {
  18. return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err)
  19. }
  20. if _, err := gitRepo.GetCommit(sha); err != nil {
  21. gitRepo.Close()
  22. return fmt.Errorf("GetCommit[%s]: %v", sha, err)
  23. }
  24. gitRepo.Close()
  25. if err := models.NewCommitStatus(models.NewCommitStatusOptions{
  26. Repo: repo,
  27. Creator: creator,
  28. SHA: sha,
  29. CommitStatus: status,
  30. }); err != nil {
  31. return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v", repo.ID, creator.ID, sha, err)
  32. }
  33. return nil
  34. }