@@ -1219,3 +1219,112 @@ func TestGetCommitInfoPRLookupPopulatesURLs(t *testing.T) {
12191219 assert .Equal (t , "https://gitea.com/fork-owner/repo" , event .HeadURL , "HeadURL should be populated from PR lookup" )
12201220 assert .Equal (t , "https://gitea.com/owner/repo" , event .BaseURL , "BaseURL should be populated from PR lookup" )
12211221}
1222+
1223+ func TestGetCommitStatuses (t * testing.T ) {
1224+ tests := []struct {
1225+ name string
1226+ event * info.Event
1227+ nilClient bool
1228+ mockHandler func (http.ResponseWriter , * http.Request )
1229+ want []provider.CommitStatusInfo
1230+ wantErr string
1231+ }{
1232+ {
1233+ name : "happy path with multiple statuses" ,
1234+ event : & info.Event {
1235+ Organization : "org" ,
1236+ Repository : "repo" ,
1237+ SHA : "abc123" ,
1238+ },
1239+ mockHandler : func (rw http.ResponseWriter , _ * http.Request ) {
1240+ fmt .Fprint (rw , `[
1241+ {"context":"Pipelines as Code CI / pr-one","status":"success"},
1242+ {"context":"Pipelines as Code CI / pr-two","status":"failure"}
1243+ ]` )
1244+ },
1245+ want : []provider.CommitStatusInfo {
1246+ {Name : "Pipelines as Code CI / pr-one" , Status : "success" },
1247+ {Name : "Pipelines as Code CI / pr-two" , Status : "failure" },
1248+ },
1249+ },
1250+ {
1251+ name : "deduplicates identical statuses" ,
1252+ event : & info.Event {
1253+ Organization : "org" ,
1254+ Repository : "repo" ,
1255+ SHA : "abc123" ,
1256+ },
1257+ mockHandler : func (rw http.ResponseWriter , _ * http.Request ) {
1258+ fmt .Fprint (rw , `[
1259+ {"context":"CI / build","status":"success"},
1260+ {"context":"CI / build","status":"success"},
1261+ {"context":"CI / build","status":"failure"}
1262+ ]` )
1263+ },
1264+ want : []provider.CommitStatusInfo {
1265+ {Name : "CI / build" , Status : "success" },
1266+ {Name : "CI / build" , Status : "failure" },
1267+ },
1268+ },
1269+ {
1270+ name : "empty response" ,
1271+ event : & info.Event {
1272+ Organization : "org" ,
1273+ Repository : "repo" ,
1274+ SHA : "abc123" ,
1275+ },
1276+ mockHandler : func (rw http.ResponseWriter , _ * http.Request ) {
1277+ fmt .Fprint (rw , `[]` )
1278+ },
1279+ },
1280+ {
1281+ name : "nil client returns error" ,
1282+ nilClient : true ,
1283+ event : & info.Event {
1284+ Organization : "org" ,
1285+ Repository : "repo" ,
1286+ SHA : "abc123" ,
1287+ },
1288+ wantErr : "no gitea client has been initialized" ,
1289+ },
1290+ {
1291+ name : "API error" ,
1292+ event : & info.Event {
1293+ Organization : "org" ,
1294+ Repository : "repo" ,
1295+ SHA : "abc123" ,
1296+ },
1297+ mockHandler : func (rw http.ResponseWriter , _ * http.Request ) {
1298+ rw .WriteHeader (http .StatusInternalServerError )
1299+ },
1300+ wantErr : "500" ,
1301+ },
1302+ }
1303+
1304+ for _ , tt := range tests {
1305+ t .Run (tt .name , func (t * testing.T ) {
1306+ var p * Provider
1307+ if tt .nilClient {
1308+ p = & Provider {}
1309+ } else {
1310+ fakeclient , mux , teardown := tgitea .Setup (t )
1311+ defer teardown ()
1312+
1313+ mux .HandleFunc (
1314+ fmt .Sprintf ("/repos/%s/%s/commits/%s/statuses" ,
1315+ tt .event .Organization , tt .event .Repository , tt .event .SHA ),
1316+ tt .mockHandler ,
1317+ )
1318+ p = & Provider {giteaClient : fakeclient }
1319+ }
1320+
1321+ got , err := p .GetCommitStatuses (context .Background (), tt .event )
1322+ if tt .wantErr != "" {
1323+ assert .ErrorContains (t , err , tt .wantErr )
1324+ return
1325+ }
1326+ assert .NilError (t , err )
1327+ assert .DeepEqual (t , got , tt .want )
1328+ })
1329+ }
1330+ }
0 commit comments