Skip to content

Commit b1b0ef3

Browse files
committed
add DeleteArtifact api
1 parent 5e3c79d commit b1b0ef3

File tree

4 files changed

+145
-2
lines changed

4 files changed

+145
-2
lines changed

routers/api/v1/api.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,10 @@ func Routes() *web.Router {
12371237
m.Get("/tasks", repo.ListActionTasks)
12381238
m.Get("/runs/{run}/artifacts", repo.GetArtifactsOfRun)
12391239
m.Get("/artifacts", repo.GetArtifacts)
1240-
m.Get("/artifacts/{artifact_id}", repo.GetArtifact)
1240+
m.Group("/artifacts/{artifact_id}", func() {
1241+
m.Get("", repo.GetArtifact)
1242+
m.Delete("", reqRepoWriter(unit.TypeActions), repo.DeleteArtifact)
1243+
})
12411244
m.Get("/artifacts/{artifact_id}/zip", repo.DownloadArtifact)
12421245
m.Get("/artifacts/{artifact_id}/zip/raw", repo.DownloadArtifactRaw)
12431246
}, reqRepoReader(unit.TypeActions), context.ReferencesGitRepo(true))

routers/api/v1/repo/action.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,54 @@ func GetArtifact(ctx *context.APIContext) {
756756
ctx.Error(http.StatusNotFound, "artifact not found", fmt.Errorf("artifact not found"))
757757
}
758758

759+
// DeleteArtifact Deletes a specific artifact for a workflow run.
760+
func DeleteArtifact(ctx *context.APIContext) {
761+
// swagger:operation DELETE /repos/{owner}/{repo}/actions/artifacts/{artifact_id} repository deleteArtifact
762+
// ---
763+
// summary: Deletes a specific artifact for a workflow run
764+
// produces:
765+
// - application/json
766+
// parameters:
767+
// - name: owner
768+
// in: path
769+
// description: name of the owner
770+
// type: string
771+
// required: true
772+
// - name: repo
773+
// in: path
774+
// description: name of the repository
775+
// type: string
776+
// required: true
777+
// - name: artifact_id
778+
// in: path
779+
// description: id of the artifact
780+
// type: string
781+
// required: true
782+
// responses:
783+
// "204":
784+
// description: "No Content"
785+
// "400":
786+
// "$ref": "#/responses/error"
787+
// "404":
788+
// "$ref": "#/responses/notFound"
789+
790+
art, ok := getArtifactByID(ctx)
791+
if !ok {
792+
return
793+
}
794+
795+
if actions.IsArtifactV4(art) {
796+
if err := actions_model.SetArtifactNeedDelete(ctx, art.RunID, art.ArtifactName); err != nil {
797+
ctx.Error(http.StatusInternalServerError, err.Error(), err)
798+
return
799+
}
800+
ctx.Status(http.StatusNoContent)
801+
return
802+
}
803+
// v3 not supported due to not having one unique id
804+
ctx.Error(http.StatusNotFound, "artifact not found", fmt.Errorf("artifact not found"))
805+
}
806+
759807
// DownloadArtifact Downloads a specific artifact for a workflow run redirects to blob url.
760808
func DownloadArtifact(ctx *context.APIContext) {
761809
// swagger:operation GET /repos/{owner}/{repo}/actions/artifacts/{artifact_id}/zip repository downloadArtifact

templates/swagger/v1_json.tmpl

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

tests/integration/api_actions_artifact_v4_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ func TestActionsArtifactV4DownloadArtifactCorrectRepoOwnerFound(t *testing.T) {
477477
session := loginUser(t, user.Name)
478478
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
479479

480-
// confirm artifacts of wrong owner or repo is not visible
480+
// confirm artifacts of correct owner and repo is visible
481481
req := NewRequestWithBody(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/artifacts/%d/zip", repo.FullName(), 22), nil).
482482
AddTokenAuth(token)
483483
MakeRequest(t, req, http.StatusFound)
@@ -514,3 +514,51 @@ func TestActionsArtifactV4Delete(t *testing.T) {
514514
protojson.Unmarshal(resp.Body.Bytes(), &deleteResp)
515515
assert.True(t, deleteResp.Ok)
516516
}
517+
518+
func TestActionsArtifactV4DeletePublicApi(t *testing.T) {
519+
defer prepareTestEnvActionsArtifacts(t)()
520+
521+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
522+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
523+
session := loginUser(t, user.Name)
524+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
525+
526+
// confirm artifacts exists
527+
req := NewRequestWithBody(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/artifacts/%d", repo.FullName(), 22), nil).
528+
AddTokenAuth(token)
529+
MakeRequest(t, req, http.StatusOK)
530+
531+
// delete artifact by id
532+
req = NewRequestWithBody(t, "DELETE", fmt.Sprintf("/api/v1/repos/%s/actions/artifacts/%d", repo.FullName(), 22), nil).
533+
AddTokenAuth(token)
534+
MakeRequest(t, req, http.StatusNoContent)
535+
536+
// confirm artifacts has been deleted
537+
req = NewRequestWithBody(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/artifacts/%d", repo.FullName(), 22), nil).
538+
AddTokenAuth(token)
539+
MakeRequest(t, req, http.StatusNotFound)
540+
}
541+
542+
func TestActionsArtifactV4DeletePublicApiNotAllowedReadScope(t *testing.T) {
543+
defer prepareTestEnvActionsArtifacts(t)()
544+
545+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
546+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
547+
session := loginUser(t, user.Name)
548+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
549+
550+
// confirm artifacts exists
551+
req := NewRequestWithBody(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/artifacts/%d", repo.FullName(), 22), nil).
552+
AddTokenAuth(token)
553+
MakeRequest(t, req, http.StatusOK)
554+
555+
// try delete artifact by id
556+
req = NewRequestWithBody(t, "DELETE", fmt.Sprintf("/api/v1/repos/%s/actions/artifacts/%d", repo.FullName(), 22), nil).
557+
AddTokenAuth(token)
558+
MakeRequest(t, req, http.StatusForbidden)
559+
560+
// confirm artifacts has not been deleted
561+
req = NewRequestWithBody(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/artifacts/%d", repo.FullName(), 22), nil).
562+
AddTokenAuth(token)
563+
MakeRequest(t, req, http.StatusOK)
564+
}

0 commit comments

Comments
 (0)