mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 19:11:25 +01:00
fix: address verified review findings (actions-runs-1)
Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
9 files changed
+267
-32
No files matched your search
@@ -35,11 +35,11 @@ type Run struct {
|
||||
Conclusion string `json:"conclusion"`
|
||||
URL string `json:"url"`
|
||||
Branch string `json:"branch"`
|
||||
Event string `json:"event,omitempty"`
|
||||
Actor string `json:"actor,omitempty"`
|
||||
Commit string `json:"commit,omitempty"`
|
||||
Attempt int64 `json:"attempt,omitempty"`
|
||||
Jobs []Job `json:"jobs,omitempty"`
|
||||
Event string `json:"event"`
|
||||
Actor string `json:"actor"`
|
||||
Commit string `json:"commit"`
|
||||
Attempt int64 `json:"attempt"`
|
||||
Jobs []Job `json:"jobs"`
|
||||
}
|
||||
|
||||
// Completed reports whether the run finished, whatever its conclusion.
|
||||
@@ -193,7 +193,7 @@ func (s Actions) runsPage(ctx context.Context, opts RunListOptions) (Page[Run],
|
||||
if opts.Workflow != "" {
|
||||
endpoint = s.endpoint() + "workflows/" + url.PathEscape(opts.Workflow) + "/runs"
|
||||
}
|
||||
return listPage(opts.Limit, opts.Page, func(limit, page int) ([]Run, http.Header, error) {
|
||||
runs, err := listPage(opts.Limit, opts.Page, func(limit, page int) ([]Run, http.Header, error) {
|
||||
query.Set("limit", strconv.Itoa(limit))
|
||||
query.Set("page", strconv.Itoa(page))
|
||||
var response struct {
|
||||
@@ -207,6 +207,10 @@ func (s Actions) runsPage(ctx context.Context, opts RunListOptions) (Page[Run],
|
||||
}
|
||||
return runs, headers, err
|
||||
})
|
||||
if opts.Workflow != "" && IsNotFound(err) {
|
||||
return Page[Run]{}, NotFound("workflow", strconv.Quote(opts.Workflow), s.Repository)
|
||||
}
|
||||
return runs, err
|
||||
}
|
||||
|
||||
// nameRuns names runs after their workflows.
|
||||
@@ -240,6 +244,14 @@ func (s Actions) Run(ctx context.Context, id int64) (Run, error) {
|
||||
return response.result(), err
|
||||
}
|
||||
|
||||
// NameRun names a run that a mutation returned after its workflow; it stays as is when the
|
||||
// workflows cannot be listed.
|
||||
func (s Actions) NameRun(ctx context.Context, run Run) Run {
|
||||
runs := []Run{run}
|
||||
_ = s.nameRuns(ctx, runs)
|
||||
return runs[0]
|
||||
}
|
||||
|
||||
// RunDetails retrieves one run with its jobs, named after its workflow.
|
||||
func (s Actions) RunDetails(ctx context.Context, id int64) (Run, error) {
|
||||
names, err := s.workflowNames(ctx)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
gitea "gitea.dev/sdk"
|
||||
@@ -33,14 +34,29 @@ func artifactFrom(artifact *gitea.ActionsArtifact) Artifact {
|
||||
|
||||
// Artifacts lists one page of artifacts belonging to a run, with their total number.
|
||||
func (s Actions) Artifacts(ctx context.Context, run int64, limit, page int) (Page[Artifact], error) {
|
||||
response, err := repoCall(ctx, s.Client, s.Repository, func(ctx context.Context, c *gitea.Client, owner, repo string) (*gitea.ActionsArtifactsResponse, *gitea.Response, error) {
|
||||
return c.Actions.ListRepoRunArtifacts(ctx, owner, repo, run, gitea.ListActionsArtifactsOptions{ListOptions: listOptions(limit, page)})
|
||||
owner, repo, err := splitRepository(s.Repository)
|
||||
if err != nil {
|
||||
return Page[Artifact]{}, err
|
||||
}
|
||||
result, err := listPage(limit, page, func(limit, page int) ([]Artifact, http.Header, error) {
|
||||
response, resp, err := call(ctx, s.Client, func(ctx context.Context, c *gitea.Client) (*gitea.ActionsArtifactsResponse, *gitea.Response, error) {
|
||||
return c.Actions.ListRepoRunArtifacts(ctx, owner, repo, run, gitea.ListActionsArtifactsOptions{ListOptions: listOptions(limit, page)})
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
// The server sends its count in the body only, and caps the page size below what AllArtifacts asks for;
|
||||
// a count smaller than the page means the response lacks it.
|
||||
headers := headerOf(resp).Clone()
|
||||
if total := int(response.TotalCount); total >= len(response.Artifacts) {
|
||||
headers.Set("X-Total-Count", strconv.Itoa(total))
|
||||
}
|
||||
return convertAll(response.Artifacts, artifactFrom), headers, nil
|
||||
})
|
||||
if err != nil {
|
||||
return Page[Artifact]{}, Classify(err, "run", strconv.FormatInt(run, 10), s.Repository)
|
||||
}
|
||||
total := int(response.TotalCount)
|
||||
return Page[Artifact]{Items: convertAll(response.Artifacts, artifactFrom), More: total > page*limit, Total: total, Counted: true}, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// AllArtifacts lists every artifact belonging to a run.
|
||||
|
||||
@@ -6,7 +6,10 @@ package services
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
gitea "gitea.dev/sdk"
|
||||
@@ -46,3 +49,21 @@ func TestArtifactFromMapsServerFields(t *testing.T) {
|
||||
CreatedAt: "2028-01-02T03:04:05Z", UpdatedAt: "2028-01-02T03:04:05Z", ExpiresAt: &expires,
|
||||
}, artifactFrom(artifact))
|
||||
}
|
||||
|
||||
func TestAllArtifactsReadsPastTheServersPageCap(t *testing.T) {
|
||||
const total, serverCap = 130, 50
|
||||
client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
|
||||
size := min(limit, serverCap)
|
||||
items := []string{}
|
||||
for id := (page-1)*size + 1; id <= min(page*size, total); id++ {
|
||||
items = append(items, fmt.Sprintf(`{"id":%d,"name":"a%d"}`, id, id))
|
||||
}
|
||||
_, _ = fmt.Fprintf(w, `{"total_count":%d,"artifacts":[%s]}`, total, strings.Join(items, ","))
|
||||
}))
|
||||
artifacts, err := Actions{Client: client, Repository: "owner/repo"}.AllArtifacts(context.Background(), 7)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, artifacts, total)
|
||||
require.EqualValues(t, total, artifacts[total-1].ID)
|
||||
}
|
||||
Reference in new issue
Block a user