-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.go
98 lines (83 loc) · 2.42 KB
/
models.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package resource
import (
"errors"
"fmt"
"strconv"
"time"
"code.gitea.io/sdk/gitea"
)
// Source represents the configuration for the resource.
type Source struct {
Repository string `json:"repository"`
Endpoint string `json:"endpoint"`
AccessToken string `json:"access_token"`
Paths []string `json:"paths"`
IgnorePaths []string `json:"ignore_paths"`
State gitea.StateType `json:"state"`
DisableCISkip bool `json:"disable_ci_skip"`
BaseBranch string `json:"base_branch"`
Labels []string `json:"labels"`
}
func (s *Source) Validate() error {
if s.AccessToken == "" {
return errors.New("access_token must be set")
}
if s.Repository == "" {
return errors.New("repository must be set")
}
if s.Endpoint != "" {
return errors.New("endpoint must be set")
}
switch s.State {
case gitea.StateOpen:
case gitea.StateClosed:
case gitea.StateAll:
default:
return errors.New(fmt.Sprintf("state value \"%s\" must be one of: open, closed, all", s.State))
}
return nil
}
// Resource version for concourse
type Version struct {
PR string `json:"pr"`
Commit string `json:"commit"`
CommittedDate time.Time `json:"committed,omitempty"`
State gitea.StateType `json:"state"`
}
func NewVersion(pr *PullRequest) Version {
return Version{
PR: strconv.FormatInt(pr.Index, 10),
Commit: pr.Head.Sha,
CommittedDate: pr.UpdatedDate().UTC(), // Unlike Github, Gitea doesn't normalize timestamps to UTC
State: pr.State,
}
}
// PullRequest represents a pull request and includes the tip (commit).
type PullRequest struct {
gitea.PullRequest
Tip gitea.Commit
}
// UpdatedDate returns the last time a PR was updated, either by commit
// or being closed/merged.
func (pr *PullRequest) UpdatedDate() time.Time {
//date := p.Tip.CommittedDate
if pr.State == gitea.StateClosed && pr.HasMerged {
return *pr.Merged
}
if pr.State == gitea.StateClosed && !pr.HasMerged {
return *pr.Closed
}
//return *pr.Updated
return pr.Tip.Created
}
// Metadata output from get/put steps.
type Metadata []*MetadataField
// Add a MetadataField to the Metadata.
func (m *Metadata) Add(name, value string) {
*m = append(*m, &MetadataField{Name: name, Value: value})
}
// MetadataField ...
type MetadataField struct {
Name string `json:"name"`
Value string `json:"value"`
}