mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-19 15:40:50 +00:00
feat: make action runs available in api (#7699)
## Summary Inspired by https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository and https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#get-a-workflow-run ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. <!--start release-notes-assistant--> ## Release notes <!--URL:https://codeberg.org/forgejo/forgejo--> - Features - [PR](https://codeberg.org/forgejo/forgejo/pulls/7699): <!--number 7699 --><!--line 0 --><!--description bWFrZSBhY3Rpb24gcnVucyBhdmFpbGFibGUgaW4gYXBp-->make action runs available in api<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7699 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: klausfyhn <klausfyhn@gmail.com> Co-committed-by: klausfyhn <klausfyhn@gmail.com>
This commit is contained in:
parent
85c054c412
commit
fc35915a28
36 changed files with 730 additions and 1 deletions
|
@ -101,3 +101,143 @@ jobs:
|
|||
assert.Len(t, run.Jobs, 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAPIGetListActionRun(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
var (
|
||||
runIDs = []int64{892, 893, 894}
|
||||
dbRuns = make(map[int64]*actions_model.ActionRun, 3)
|
||||
)
|
||||
|
||||
for _, id := range runIDs {
|
||||
dbRuns[id] = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: id})
|
||||
}
|
||||
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: dbRuns[runIDs[0]].RepoID})
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
||||
token := getUserToken(t, user.LowerName, auth_model.AccessTokenScopeWriteRepository)
|
||||
|
||||
testqueries := []struct {
|
||||
name string
|
||||
query string
|
||||
expectedIDs []int64
|
||||
}{
|
||||
{
|
||||
name: "No query parameters",
|
||||
query: "",
|
||||
expectedIDs: runIDs,
|
||||
},
|
||||
{
|
||||
name: "Search for workflow_dispatch events",
|
||||
query: "?event=workflow_dispatch",
|
||||
expectedIDs: []int64{894},
|
||||
},
|
||||
{
|
||||
name: "Search for multiple events",
|
||||
query: "?event=workflow_dispatch&event=push",
|
||||
expectedIDs: []int64{892, 894},
|
||||
},
|
||||
{
|
||||
name: "Search for failed status",
|
||||
query: "?status=failure",
|
||||
expectedIDs: []int64{893},
|
||||
},
|
||||
{
|
||||
name: "Search for multiple statuses",
|
||||
query: "?status=failure&status=running",
|
||||
expectedIDs: []int64{893, 894},
|
||||
},
|
||||
{
|
||||
name: "Search for num_nr",
|
||||
query: "?run_number=1",
|
||||
expectedIDs: []int64{892},
|
||||
},
|
||||
{
|
||||
name: "Search for sha",
|
||||
query: "?head_sha=97f29ee599c373c729132a5c46a046978311e0ee",
|
||||
expectedIDs: []int64{892, 894},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range testqueries {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := NewRequest(t, http.MethodGet,
|
||||
fmt.Sprintf("/api/v1/repos/%s/%s/actions/runs%s",
|
||||
repo.OwnerName, repo.Name, tt.query,
|
||||
),
|
||||
)
|
||||
req.AddTokenAuth(token)
|
||||
|
||||
res := MakeRequest(t, req, http.StatusOK)
|
||||
apiRuns := new(api.ListActionRunResponse)
|
||||
DecodeJSON(t, res, apiRuns)
|
||||
|
||||
assert.Equal(t, int64(len(tt.expectedIDs)), apiRuns.TotalCount)
|
||||
assert.Len(t, apiRuns.Entries, len(tt.expectedIDs))
|
||||
|
||||
resultIDs := make([]int64, apiRuns.TotalCount)
|
||||
for i, run := range apiRuns.Entries {
|
||||
resultIDs[i] = run.ID
|
||||
}
|
||||
|
||||
assert.ElementsMatch(t, tt.expectedIDs, resultIDs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIGetActionRun(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 63})
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
||||
token := getUserToken(t, user.LowerName, auth_model.AccessTokenScopeWriteRepository)
|
||||
|
||||
testqueries := []struct {
|
||||
name string
|
||||
runID int64
|
||||
expectedStatus int
|
||||
}{
|
||||
{
|
||||
name: "existing return ok",
|
||||
runID: 892,
|
||||
expectedStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "non existing run",
|
||||
runID: 9876543210, // I hope this run will not exists, else just change it to another.
|
||||
expectedStatus: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "existing run but wrong repo should not be found",
|
||||
runID: 891,
|
||||
expectedStatus: http.StatusNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range testqueries {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := NewRequest(t, http.MethodGet,
|
||||
fmt.Sprintf("/api/v1/repos/%s/%s/actions/runs/%d",
|
||||
repo.OwnerName, repo.Name, tt.runID,
|
||||
),
|
||||
)
|
||||
req.AddTokenAuth(token)
|
||||
|
||||
res := MakeRequest(t, req, tt.expectedStatus)
|
||||
|
||||
// Only interested in the data if 200 OK
|
||||
if tt.expectedStatus != http.StatusOK {
|
||||
return
|
||||
}
|
||||
|
||||
dbRun := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: tt.runID})
|
||||
apiRun := new(api.ActionRun)
|
||||
DecodeJSON(t, res, apiRun)
|
||||
|
||||
assert.Equal(t, dbRun.Index, apiRun.RunNumber)
|
||||
assert.Equal(t, dbRun.Status.String(), apiRun.Status)
|
||||
assert.Equal(t, dbRun.CommitSHA, apiRun.HeadSHA)
|
||||
assert.Equal(t, dbRun.TriggerUserID, apiRun.TriggeringActor.ID)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue