forgejo/routers/web/repo/actions/view_test.go
Earl Warren c258003be9 feat: artifacts can be downloaded using their id instead of their name (#8957)
The web endpoint

`/{owner}/{repo}/actions/runs/{run_id}/artifacts/{artifact_name_or_id}`

can be used with either the artifact name used when it is uploaded or the instance wide unique number of the artifact, if it is not found. For instance:

`/root/myrepo/actions/run/3/artifacts/my_artifact_name`

or

`/root/myrepo/actions/run/3/artifacts/42`

The `upload-artifact@v4` output value `artifact-url` is built in this way and is now a valid URL to access the artifact.

Refs https://codeberg.org/forgejo/forgejo/issues/6147
Refs https://code.forgejo.org/forgejo/runner/issues/187
Refs https://code.forgejo.org/forgejo/upload-artifact/src/tag/v4#outputs

## 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...
  - [x] 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-->
- Bug fixes
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/8957): <!--number 8957 --><!--line 0 --><!--description YXJ0aWZhY3RzIGNhbiBiZSBkb3dubG9hZGVkIHVzaW5nIHRoZWlyIGlkIGluc3RlYWQgb2YgdGhlaXIgbmFtZQ==-->artifacts can be downloaded using their id instead of their name<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8957
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
2025-08-25 15:51:26 +02:00

95 lines
2.2 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package actions
import (
"fmt"
"testing"
actions_model "forgejo.org/models/actions"
unittest "forgejo.org/models/unittest"
"forgejo.org/services/contexttest"
"github.com/stretchr/testify/assert"
)
func Test_artifactsFind(t *testing.T) {
unittest.PrepareTestEnv(t)
for _, testCase := range []struct {
name string
artifactName string
count int
}{
{
name: "Found",
artifactName: "artifact-v4-download",
count: 1,
},
{
name: "NotFound",
artifactName: "notexist",
count: 0,
},
} {
t.Run(testCase.name, func(t *testing.T) {
runID := int64(792)
ctx, _ := contexttest.MockContext(t, fmt.Sprintf("user5/repo4/actions/runs/%v/artifacts/%v", runID, testCase.artifactName))
artifacts := artifactsFind(ctx, actions_model.FindArtifactsOptions{
RunID: runID,
ArtifactName: testCase.artifactName,
})
assert.False(t, ctx.Written())
assert.Len(t, artifacts, testCase.count)
})
}
}
func Test_artifactsFindByNameOrID(t *testing.T) {
unittest.PrepareTestEnv(t)
for _, testCase := range []struct {
name string
nameOrID string
err string
}{
{
name: "NameFound",
nameOrID: "artifact-v4-download",
},
{
name: "NameNotFound",
nameOrID: "notexist",
err: "artifact name not found",
},
{
name: "IDFound",
nameOrID: "22",
},
{
name: "IDNotFound",
nameOrID: "666",
err: "artifact ID not found",
},
{
name: "IDZeroNotFound",
nameOrID: "0",
err: "artifact name not found",
},
} {
t.Run(testCase.name, func(t *testing.T) {
runID := int64(792)
ctx, resp := contexttest.MockContext(t, fmt.Sprintf("user5/repo4/actions/runs/%v/artifacts/%v", runID, testCase.nameOrID))
artifacts := artifactsFindByNameOrID(ctx, runID, testCase.nameOrID)
if testCase.err == "" {
assert.NotEmpty(t, artifacts)
assert.False(t, ctx.Written(), resp.Body.String())
} else {
assert.Empty(t, artifacts)
assert.True(t, ctx.Written())
assert.Contains(t, resp.Body.String(), testCase.err)
}
})
}
}