@@ -15,38 +15,167 @@ import (
15
15
"code.gitea.io/gitea/tests"
16
16
17
17
"github.com/stretchr/testify/assert"
18
+ "gopkg.in/yaml.v3"
18
19
)
19
20
20
- func TestAPIReposGetDefaultIssueConfig (t * testing.T ) {
21
- defer tests .PrepareTestEnv (t )()
21
+ func createIssueConfig (t * testing.T , user * user_model.User , repo * repo_model.Repository , issueConfig map [string ]any ) {
22
+ config , err := yaml .Marshal (issueConfig )
23
+ assert .NoError (t , err )
22
24
23
- repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
24
- owner := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : repo .OwnerID })
25
+ err = createOrReplaceFileInBranch (user , repo , ".gitea/ISSUE_TEMPLATE/config.yaml" , repo .DefaultBranch , string (config ))
26
+ assert .NoError (t , err )
27
+ }
25
28
26
- urlStr := fmt .Sprintf ("/api/v1/repos/%s/%s/issue_config" , owner .Name , repo .Name )
29
+ func getIssueConfig (t * testing.T , owner , repo string ) api.IssueConfig {
30
+ urlStr := fmt .Sprintf ("/api/v1/repos/%s/%s/issue_config" , owner , repo )
27
31
req := NewRequest (t , "GET" , urlStr )
28
32
resp := MakeRequest (t , req , http .StatusOK )
29
33
30
34
var issueConfig api.IssueConfig
31
35
DecodeJSON (t , resp , & issueConfig )
32
36
33
- assert .True (t , issueConfig .BlankIssuesEnabled )
34
- assert .Equal (t , issueConfig .ContactLinks , make ([]api.IssueConfigContactLink , 0 ))
37
+ return issueConfig
38
+ }
39
+
40
+ func TestAPIRepoGetIssueConfig (t * testing.T ) {
41
+ defer tests .PrepareTestEnv (t )()
42
+
43
+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 49 })
44
+ owner := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : repo .OwnerID })
45
+
46
+ t .Run ("Default" , func (t * testing.T ) {
47
+ issueConfig := getIssueConfig (t , owner .Name , repo .Name )
48
+
49
+ assert .True (t , issueConfig .BlankIssuesEnabled )
50
+ assert .Len (t , issueConfig .ContactLinks , 0 )
51
+ })
52
+
53
+ t .Run ("DisableBlankIssues" , func (t * testing.T ) {
54
+ config := make (map [string ]any )
55
+ config ["blank_issues_enabled" ] = false
56
+
57
+ createIssueConfig (t , owner , repo , config )
58
+
59
+ issueConfig := getIssueConfig (t , owner .Name , repo .Name )
60
+
61
+ assert .False (t , issueConfig .BlankIssuesEnabled )
62
+ assert .Len (t , issueConfig .ContactLinks , 0 )
63
+ })
64
+
65
+ t .Run ("ContactLinks" , func (t * testing.T ) {
66
+ contactLink := make (map [string ]string )
67
+ contactLink ["name" ] = "TestName"
68
+ contactLink ["url" ] = "https://example.com"
69
+ contactLink ["about" ] = "TestAbout"
70
+
71
+ config := make (map [string ]any )
72
+ config ["contact_links" ] = []map [string ]string {contactLink }
73
+
74
+ createIssueConfig (t , owner , repo , config )
75
+
76
+ issueConfig := getIssueConfig (t , owner .Name , repo .Name )
77
+
78
+ assert .True (t , issueConfig .BlankIssuesEnabled )
79
+ assert .Len (t , issueConfig .ContactLinks , 1 )
80
+
81
+ assert .Equal (t , "TestName" , issueConfig .ContactLinks [0 ].Name )
82
+ assert .Equal (t , "https://example.com" , issueConfig .ContactLinks [0 ].URL )
83
+ assert .Equal (t , "TestAbout" , issueConfig .ContactLinks [0 ].About )
84
+ })
85
+
86
+ t .Run ("Full" , func (t * testing.T ) {
87
+ contactLink := make (map [string ]string )
88
+ contactLink ["name" ] = "TestName"
89
+ contactLink ["url" ] = "https://example.com"
90
+ contactLink ["about" ] = "TestAbout"
91
+
92
+ config := make (map [string ]any )
93
+ config ["blank_issues_enabled" ] = false
94
+ config ["contact_links" ] = []map [string ]string {contactLink }
95
+
96
+ createIssueConfig (t , owner , repo , config )
97
+
98
+ issueConfig := getIssueConfig (t , owner .Name , repo .Name )
99
+
100
+ assert .False (t , issueConfig .BlankIssuesEnabled )
101
+ assert .Len (t , issueConfig .ContactLinks , 1 )
102
+
103
+ assert .Equal (t , "TestName" , issueConfig .ContactLinks [0 ].Name )
104
+ assert .Equal (t , "https://example.com" , issueConfig .ContactLinks [0 ].URL )
105
+ assert .Equal (t , "TestAbout" , issueConfig .ContactLinks [0 ].About )
106
+ })
35
107
}
36
108
37
- func TestAPIReposValidateDefaultIssueConfig (t * testing.T ) {
109
+ func TestAPIRepoIssueConfigPaths (t * testing.T ) {
38
110
defer tests .PrepareTestEnv (t )()
39
111
40
- repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
112
+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 49 })
113
+ owner := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : repo .OwnerID })
114
+
115
+ templateConfigCandidates := []string {
116
+ ".gitea/ISSUE_TEMPLATE/config" ,
117
+ ".gitea/issue_template/config" ,
118
+ ".github/ISSUE_TEMPLATE/config" ,
119
+ ".github/issue_template/config" ,
120
+ }
121
+
122
+ for _ , canidate := range templateConfigCandidates {
123
+ for _ , extension := range []string {".yaml" , ".yml" } {
124
+ fullPath := canidate + extension
125
+ t .Run (fullPath , func (t * testing.T ) {
126
+ configMap := make (map [string ]any )
127
+ configMap ["blank_issues_enabled" ] = false
128
+
129
+ configData , err := yaml .Marshal (configMap )
130
+ assert .NoError (t , err )
131
+
132
+ _ , err = createFileInBranch (owner , repo , fullPath , repo .DefaultBranch , string (configData ))
133
+ assert .NoError (t , err )
134
+
135
+ issueConfig := getIssueConfig (t , owner .Name , repo .Name )
136
+
137
+ assert .False (t , issueConfig .BlankIssuesEnabled )
138
+ assert .Len (t , issueConfig .ContactLinks , 0 )
139
+
140
+ _ , err = deleteFileInBranch (owner , repo , fullPath , repo .DefaultBranch )
141
+ assert .NoError (t , err )
142
+ })
143
+ }
144
+ }
145
+ }
146
+
147
+ func TestAPIRepoValidateIssueConfig (t * testing.T ) {
148
+ defer tests .PrepareTestEnv (t )()
149
+
150
+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 49 })
41
151
owner := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : repo .OwnerID })
42
152
43
153
urlStr := fmt .Sprintf ("/api/v1/repos/%s/%s/issue_config/validate" , owner .Name , repo .Name )
44
- req := NewRequest (t , "GET" , urlStr )
45
- resp := MakeRequest (t , req , http .StatusOK )
46
154
47
- var issueConfigValidation api.IssueConfigValidation
48
- DecodeJSON (t , resp , & issueConfigValidation )
155
+ t .Run ("Valid" , func (t * testing.T ) {
156
+ req := NewRequest (t , "GET" , urlStr )
157
+ resp := MakeRequest (t , req , http .StatusOK )
158
+
159
+ var issueConfigValidation api.IssueConfigValidation
160
+ DecodeJSON (t , resp , & issueConfigValidation )
161
+
162
+ assert .True (t , issueConfigValidation .Valid )
163
+ assert .Empty (t , issueConfigValidation .Message )
164
+ })
165
+
166
+ t .Run ("Invalid" , func (t * testing.T ) {
167
+ config := make (map [string ]any )
168
+ config ["blank_issues_enabled" ] = "Test"
169
+
170
+ createIssueConfig (t , owner , repo , config )
171
+
172
+ req := NewRequest (t , "GET" , urlStr )
173
+ resp := MakeRequest (t , req , http .StatusOK )
174
+
175
+ var issueConfigValidation api.IssueConfigValidation
176
+ DecodeJSON (t , resp , & issueConfigValidation )
49
177
50
- assert .True (t , issueConfigValidation .Valid )
51
- assert .Empty (t , issueConfigValidation .Message )
178
+ assert .False (t , issueConfigValidation .Valid )
179
+ assert .NotEmpty (t , issueConfigValidation .Message )
180
+ })
52
181
}
0 commit comments