-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzenhub_test.go
118 lines (101 loc) · 2.37 KB
/
zenhub_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package zenhub
import (
"context"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"log"
"os"
"testing"
)
var (
zenHubTestClient *Client
repoID, issueNumber int
workspaceID string
)
func TestMain(m *testing.M) {
setupGitHub()
setupZenHub()
os.Exit(m.Run())
}
func setupGitHub() {
// get github secret
githubSecret, ok := os.LookupEnv("GITHUB_SECRET")
if !ok {
log.Panicln("could not get github secret")
}
// get owner
owner, ok := os.LookupEnv("TEST_REPO_OWNER")
if !ok {
log.Panicln("could not get repo owner")
}
// get repo name
name, ok := os.LookupEnv("TEST_REPO_NAME")
if !ok {
log.Println("could not get repo name")
os.Exit(0)
}
// new github client
gitHubTestClient := github.NewClient(
oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubSecret},
)),
)
// get repo
githubTestRepo, _, err := gitHubTestClient.Repositories.Get(context.Background(), owner, name)
if err != nil {
log.Panicln(err)
}
repoID = int(*githubTestRepo.ID)
// get issues with zenhub-test label
issues, _, _ := gitHubTestClient.Issues.ListByRepo(context.Background(), owner, name, &github.IssueListByRepoOptions{
Labels: []string{"zenhub-test"},
})
title := "zenhub test issue"
body := "this issue is used for zenhub tests"
// create issue if none found
if len(issues) == 0 {
issue, _, err := gitHubTestClient.Issues.Create(context.Background(), owner, name, &github.IssueRequest{
Title: &title,
Body: &body,
Labels: &[]string{"test-issue"},
})
if err != nil {
log.Panicln(err)
}
issueNumber = *issue.Number
} else {
// get first issue with correct label and name
for _, issue := range issues {
if *issue.Title == title {
issueNumber = *issue.Number
break
}
}
}
// failsafe
if issueNumber == 0 {
log.Panicln("no issue number found")
}
}
func setupZenHub() {
// get zenhub secret
zenhubSecret, ok := os.LookupEnv("ZENHUB_SECRET")
if !ok {
log.Println("could not get zenhub secret")
os.Exit(0)
}
// new zenhub client
var err error
zenHubTestClient, err = NewClient(Options.Secret(zenhubSecret))
if err != nil {
log.Panicln(err)
}
workspaces, _, err := zenHubTestClient.GetWorkspaces(repoID)
if err != nil {
log.Panicln(err)
}
if len(*workspaces) == 0 {
log.Panicln("no workspaces found")
}
workspaceID = *(*workspaces)[0].ID
}