summaryrefslogtreecommitdiffstats
path: root/services/mirror/sync.go
blob: ba9e896dd50e3c50f6ea93f9ae5af77f810d9e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package mirror

import (
	"encoding/json"
	"fmt"

	"code.gitea.io/gitea/models"
	"code.gitea.io/gitea/modules/notification"
	"code.gitea.io/gitea/modules/setting"
)

func syncAction(opType models.ActionType, repo *models.Repository, refName string, data []byte) error {
	if err := models.NotifyWatchers(&models.Action{
		ActUserID: repo.OwnerID,
		ActUser:   repo.MustOwner(),
		OpType:    opType,
		RepoID:    repo.ID,
		Repo:      repo,
		IsPrivate: repo.IsPrivate,
		RefName:   refName,
		Content:   string(data),
	}); err != nil {
		return fmt.Errorf("notifyWatchers: %v", err)
	}

	return nil
}

// SyncPushActionOptions mirror synchronization action options.
type SyncPushActionOptions struct {
	RefName     string
	OldCommitID string
	NewCommitID string
	Commits     *models.PushCommits
}

// SyncPushAction adds new action for mirror synchronization of pushed commits.
func SyncPushAction(repo *models.Repository, opts SyncPushActionOptions) error {
	if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
		opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
	}

	opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)

	notification.NotifyPushCommits(repo.MustOwner(), repo, opts.RefName, opts.OldCommitID, opts.NewCommitID, opts.Commits)

	data, err := json.Marshal(opts.Commits)
	if err != nil {
		return err
	}

	return syncAction(models.ActionMirrorSyncPush, repo, opts.RefName, data)
}

// SyncCreateAction adds new action for mirror synchronization of new reference.
func SyncCreateAction(repo *models.Repository, refName string) error {
	return syncAction(models.ActionMirrorSyncCreate, repo, refName, nil)
}

// SyncDeleteAction adds new action for mirror synchronization of delete reference.
func SyncDeleteAction(repo *models.Repository, refName string) error {
	return syncAction(models.ActionMirrorSyncDelete, repo, refName, nil)
}