1
1
// Copyright 2023 The Gitea Authors. All rights reserved.
2
+ // Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
2
3
// SPDX-License-Identifier: MIT
3
4
4
5
package integration
5
6
6
7
import (
8
+ "context"
7
9
"fmt"
8
10
"net/http"
9
11
"net/url"
@@ -15,10 +17,82 @@ import (
15
17
"code.gitea.io/gitea/models/unittest"
16
18
user_model "code.gitea.io/gitea/models/user"
17
19
files_service "code.gitea.io/gitea/services/repository/files"
20
+ "code.gitea.io/gitea/tests"
18
21
19
22
"github.com/stretchr/testify/assert"
20
23
)
21
24
25
+ func TestActionsWebRouteLatestWorkflowRun (t * testing.T ) {
26
+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
27
+ user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
28
+
29
+ // create the repo
30
+ repo , _ , f := CreateDeclarativeRepo (t , user2 , "" ,
31
+ []unit_model.Type {unit_model .TypeActions }, nil ,
32
+ []* files_service.ChangeRepoFile {
33
+ {
34
+ Operation : "create" ,
35
+ TreePath : ".gitea/workflows/workflow-1.yml" ,
36
+ ContentReader : strings .NewReader ("name: workflow-1\n on:\n push:\n jobs:\n job-1:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n " ),
37
+ },
38
+ {
39
+ Operation : "create" ,
40
+ TreePath : ".gitea/workflows/workflow-2.yml" ,
41
+ ContentReader : strings .NewReader ("name: workflow-2\n on:\n push:\n jobs:\n job-2:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n " ),
42
+ },
43
+ },
44
+ )
45
+ defer f ()
46
+
47
+ t .Run ("valid workflows" , func (t * testing.T ) {
48
+ defer tests .PrintCurrentTest (t )()
49
+
50
+ // helpers
51
+ getWorkflowRunRedirectURI := func (workflow string ) string {
52
+ req := NewRequest (t , "GET" , fmt .Sprintf ("%s/actions/workflows/%s/runs/latest" , repo .HTMLURL (), workflow ))
53
+ resp := MakeRequest (t , req , http .StatusTemporaryRedirect )
54
+
55
+ return resp .Header ().Get ("Location" )
56
+ }
57
+
58
+ // two runs have been created
59
+ assert .Equal (t , 2 , unittest .GetCount (t , & actions_model.ActionRun {RepoID : repo .ID }))
60
+
61
+ // Get the redirect URIs for both workflows
62
+ workflowOneURI := getWorkflowRunRedirectURI ("workflow-1.yml" )
63
+ workflowTwoURI := getWorkflowRunRedirectURI ("workflow-2.yml" )
64
+
65
+ // Verify that the two are different.
66
+ assert .NotEqual (t , workflowOneURI , workflowTwoURI )
67
+
68
+ // Verify that each points to the correct workflow.
69
+ workflowOne := unittest .AssertExistsAndLoadBean (t , & actions_model.ActionRun {RepoID : repo .ID , Index : 1 })
70
+ err := workflowOne .LoadAttributes (context .Background ())
71
+ assert .NoError (t , err )
72
+ assert .Equal (t , workflowOneURI , workflowOne .HTMLURL ())
73
+
74
+ workflowTwo := unittest .AssertExistsAndLoadBean (t , & actions_model.ActionRun {RepoID : repo .ID , Index : 2 })
75
+ err = workflowTwo .LoadAttributes (context .Background ())
76
+ assert .NoError (t , err )
77
+ assert .Equal (t , workflowTwoURI , workflowTwo .HTMLURL ())
78
+ })
79
+
80
+ t .Run ("existing workflow, non-existent branch" , func (t * testing.T ) {
81
+ defer tests .PrintCurrentTest (t )()
82
+
83
+ req := NewRequest (t , "GET" , fmt .Sprintf ("%s/actions/workflows/workflow-1.yml/runs/latest?branch=foobar" , repo .HTMLURL ()))
84
+ MakeRequest (t , req , http .StatusNotFound )
85
+ })
86
+
87
+ t .Run ("non-existing workflow" , func (t * testing.T ) {
88
+ defer tests .PrintCurrentTest (t )()
89
+
90
+ req := NewRequest (t , "GET" , fmt .Sprintf ("%s/actions/workflows/workflow-3.yml/runs/latest" , repo .HTMLURL ()))
91
+ MakeRequest (t , req , http .StatusNotFound )
92
+ })
93
+ })
94
+ }
95
+
22
96
func TestActionsWebRouteLatestRun (t * testing.T ) {
23
97
onGiteaRun (t , func (t * testing.T , u * url.URL ) {
24
98
user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
@@ -44,7 +118,10 @@ func TestActionsWebRouteLatestRun(t *testing.T) {
44
118
resp := MakeRequest (t , req , http .StatusTemporaryRedirect )
45
119
46
120
// Verify that it redirects to the run we just created
47
- expectedURI := fmt .Sprintf ("%s/actions/runs/1" , repo .HTMLURL ())
48
- assert .Equal (t , expectedURI , resp .Header ().Get ("Location" ))
121
+ workflow := unittest .AssertExistsAndLoadBean (t , & actions_model.ActionRun {RepoID : repo .ID })
122
+ err := workflow .LoadAttributes (context .Background ())
123
+ assert .NoError (t , err )
124
+
125
+ assert .Equal (t , workflow .HTMLURL (), resp .Header ().Get ("Location" ))
49
126
})
50
127
}
0 commit comments