summaryrefslogtreecommitdiff
path: root/pkg/queue/job.go
blob: e2dd94e8b6ce7a940daf161f19b792223a214bee (about) (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
68
package queue

import (
	"time"

	"github.com/isacikgoz/gitbatch/pkg/git"
)

// Job relates the type of the operation and the entity
type Job struct {
	JobType JobType
	Entity  *git.RepoEntity
}

// JobType is the a git operation supported
type JobType string

const (
	// Fetch is wrapper of git fetch command
	Fetch JobType = "fetch"
	// Pull is wrapper of git pull command
	Pull JobType = "pull"
	// Merge is wrapper of git merge command
	Merge JobType = "merge"
)

// starts the job
func (job *Job) start() error {
	job.Entity.State = git.Working
	// added for testing, TODO: remove
	time.Sleep(time.Second)
	// TODO: Handle errors?
	switch mode := job.JobType; mode {
	case Fetch:
		if err := git.Fetch(job.Entity, git.FetchOptions{
			RemoteName: job.Entity.Remote.Name,
		}); err != nil {
			job.Entity.State = git.Fail
			return nil
		}
	case Pull:
		if err := git.Fetch(job.Entity, git.FetchOptions{
			RemoteName: job.Entity.Remote.Name,
		}); err != nil {
			job.Entity.State = git.Fail
			return nil
		}
		if err := git.Merge(job.Entity, git.MergeOptions{
			BranchName: job.Entity.Remote.Branch.Name,
		}); err != nil {
			job.Entity.State = git.Fail
			return nil
		}
	case Merge:
		if err := git.Merge(job.Entity, git.MergeOptions{
			BranchName: job.Entity.Remote.Branch.Name,
		}); err != nil {
			job.Entity.State = git.Fail
			return nil
		}
	default:
		job.Entity.State = git.Available
		return nil
	}
	job.Entity.State = git.Success
	job.Entity.Refresh()
	return nil
}