Skip to content

Commit c531b8f

Browse files
klausfyhnklausfyhn
authored andcommitted
feat(api): return run_number in workflow dispatch (go-gitea#7286)
- This is a follow up on go-gitea#7193 and resolves go-gitea#6312. - The ID by itself is not very useful, so also return the index of the workflow run. Co-authored-by: Klaus Fyhn <[email protected]> Co-authored-by: Klaus Fyhn <[email protected]> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7286 Reviewed-by: Gusted <[email protected]> Co-authored-by: klausfyhn <[email protected]> Co-committed-by: klausfyhn <[email protected]>
1 parent 513319c commit c531b8f

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

modules/structs/workflow.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type DispatchWorkflowOption struct {
2222
type DispatchWorkflowRun struct {
2323
// the workflow run id
2424
ID int64 `json:"id"`
25+
// a unique number for each run of a repository
26+
RunNumber int64 `json:"run_number"`
2527
// the jobs name
2628
Jobs []string `json:"jobs"`
2729
}

routers/api/v1/repo/action.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,8 @@ func DispatchWorkflow(ctx *context.APIContext) {
640640
// schema:
641641
// "$ref": "#/definitions/DispatchWorkflowOption"
642642
// responses:
643+
// "201":
644+
// "$ref": "#/responses/DispatchWorkflowRun"
643645
// "204":
644646
// "$ref": "#/responses/empty"
645647
// "404":
@@ -681,8 +683,9 @@ func DispatchWorkflow(ctx *context.APIContext) {
681683
}
682684

683685
workflowRun := &api.DispatchWorkflowRun{
684-
ID: run.ID,
685-
Jobs: jobs,
686+
ID: run.ID,
687+
RunNumber: run.Index,
688+
Jobs: jobs,
686689
}
687690

688691
if opt.ReturnRunInfo {

templates/swagger/v1_json.tmpl

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/api_repo_actions_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
package integration
55

66
import (
7+
"fmt"
78
"net/http"
9+
"net/url"
10+
"strings"
811
"testing"
912

1013
actions_model "code.gitea.io/gitea/models/actions"
1114
auth_model "code.gitea.io/gitea/models/auth"
1215
repo_model "code.gitea.io/gitea/models/repo"
16+
unit_model "code.gitea.io/gitea/models/unit"
1317
"code.gitea.io/gitea/models/unittest"
1418
user_model "code.gitea.io/gitea/models/user"
1519
api "code.gitea.io/gitea/modules/structs"
20+
files_service "code.gitea.io/gitea/services/repository/files"
1621
"code.gitea.io/gitea/tests"
1722

1823
"github.com/stretchr/testify/assert"
@@ -41,3 +46,58 @@ func TestAPISearchActionJobs_RepoRunner(t *testing.T) {
4146
assert.Len(t, jobs, 1)
4247
assert.EqualValues(t, job.ID, jobs[0].ID)
4348
}
49+
50+
func TestAPIWorkflowDispatchReturnInfo(t *testing.T) {
51+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
52+
workflowName := "dispatch.yml"
53+
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
54+
token := getUserToken(t, user2.LowerName, auth_model.AccessTokenScopeWriteRepository)
55+
56+
// create the repo
57+
repo, _, f := tests.CreateDeclarativeRepo(t, user2, "api-repo-workflow-dispatch",
58+
[]unit_model.Type{unit_model.TypeActions}, nil,
59+
[]*files_service.ChangeRepoFile{
60+
{
61+
Operation: "create",
62+
TreePath: fmt.Sprintf(".forgejo/workflows/%s", workflowName),
63+
ContentReader: strings.NewReader(`name: WD
64+
on: [workflow-dispatch]
65+
jobs:
66+
t1:
67+
runs-on: docker
68+
steps:
69+
- run: echo "test 1"
70+
t2:
71+
runs-on: docker
72+
steps:
73+
- run: echo "test 2"
74+
`,
75+
),
76+
},
77+
},
78+
)
79+
defer f()
80+
81+
req := NewRequestWithJSON(
82+
t,
83+
http.MethodPost,
84+
fmt.Sprintf(
85+
"/api/v1/repos/%s/%s/actions/workflows/%s/dispatches",
86+
repo.OwnerName, repo.Name, workflowName,
87+
),
88+
&api.DispatchWorkflowOption{
89+
Ref: repo.DefaultBranch,
90+
ReturnRunInfo: true,
91+
},
92+
)
93+
req.AddTokenAuth(token)
94+
95+
res := MakeRequest(t, req, http.StatusCreated)
96+
run := new(api.DispatchWorkflowRun)
97+
DecodeJSON(t, res, run)
98+
99+
assert.NotZero(t, run.ID)
100+
assert.NotZero(t, run.RunNumber)
101+
assert.Len(t, run.Jobs, 2)
102+
})
103+
}

0 commit comments

Comments
 (0)