-
Notifications
You must be signed in to change notification settings - Fork 8
/
resource_test.go
95 lines (84 loc) · 2.28 KB
/
resource_test.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
package resource_test
import (
"fmt"
"io/ioutil"
"strconv"
"testing"
"time"
"github.com/shurcooL/githubv4"
resource "github.com/telia-oss/github-pr-resource"
_ "github.com/telia-oss/github-pr-resource/log"
"github.com/telia-oss/github-pr-resource/pullrequest"
)
func createTestPR(count int, baseName string, skipCI, isCrossRepo, created, nocommit bool, approvedReviews int, labels []string) pullrequest.PullRequest {
n := strconv.Itoa(count)
u := time.Now().AddDate(0, 0, count)
c := u
if !created {
c = time.Now().AddDate(0, 0, count-1)
}
m := fmt.Sprintf("commit message%s", n)
if skipCI {
m = "[skip ci]" + m
}
commit := resource.CommitObject{
ID: fmt.Sprintf("commit%s", n),
OID: fmt.Sprintf("oid%s", n),
AbbreviatedOID: fmt.Sprintf("oid%s", n),
CommittedDate: githubv4.DateTime{Time: u},
Message: m,
Author: struct{ User struct{ Login string } }{
User: struct{ Login string }{
Login: fmt.Sprintf("login%s", n),
},
},
}
if nocommit {
commit.ID = ""
}
pr := resource.PullRequestFactory(resource.PullRequestObject{
ID: fmt.Sprintf("pr%s", n),
Number: count,
Title: fmt.Sprintf("pr%s title", n),
URL: fmt.Sprintf("pr%s url", n),
BaseRefName: baseName,
BaseRefOID: "sha",
HeadRefName: fmt.Sprintf("pr%s", n),
IsCrossRepository: isCrossRepo,
CreatedAt: githubv4.DateTime{Time: c},
UpdatedAt: githubv4.DateTime{Time: u},
HeadRef: struct {
ID string
Name string
Target struct {
resource.CommitObject `graphql:"... on Commit"`
}
}{
ID: fmt.Sprintf("commit%s", n),
Name: fmt.Sprintf("pr%s", n),
Target: struct {
resource.CommitObject `graphql:"... on Commit"`
}{commit},
},
Repository: struct{ URL string }{
URL: fmt.Sprintf("repo%s url", n),
},
})
pr.ApprovedReviewCount = approvedReviews
pr.Labels = labels
return pr
}
func createTestDirectory(t *testing.T) string {
dir, err := ioutil.TempDir("", "github-pr-resource")
if err != nil {
t.Fatalf("failed to create temporary directory")
}
return dir
}
func readTestFile(t *testing.T, path string) string {
b, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("failed to read: %s: %s", path, err)
}
return string(b)
}