package artifact import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" "time" "code.geekeey.de/actions/sdk" ) type CreateArtifactRequest struct { RunID string `json:"workflowRunBackendId"` JobRunID string `json:"workflowJobRunBackendId"` Name string `json:"name"` Version int32 `json:"version"` ExpiresAt *time.Time `json:"expiresAt,omitempty"` } type CreateArtifactResponse struct { Ok bool `json:"ok"` SignedUploadUrl string `json:"signedUploadUrl"` } type FinalizeArtifactRequest struct { RunID string `json:"workflowRunBackendId"` JobRunID string `json:"workflowJobRunBackendId"` Name string `json:"name"` Size int64 `json:"size"` Hash string `json:"hash,omitempty"` } type FinalizeArtifactResponse struct { Ok bool `json:"ok"` ArtifactId string `json:"artifactId"` } type ListArtifactsRequest struct { RunID string `json:"workflowRunBackendId"` JobRunID string `json:"workflowJobRunBackendId"` NameFilter string `json:"nameFilter"` IdFilter string `json:"idFilter"` } type Artifact struct { RunID string `json:"workflowRunBackendId"` JobRunID string `json:"workflowJobRunBackendId"` Id string `json:"databaseId"` Name string `json:"name"` Size string `json:"size"` CreatedAt *time.Time `json:"createdAt"` Digest string `json:"digest"` } type ListArtifactsResponse struct { Artifacts []Artifact `json:"artifacts"` } type GetSignedArtifactURLRequest struct { RunID string `json:"workflowRunBackendId"` JobRunID string `json:"workflowJobRunBackendId"` Name string `json:"name"` } type GetSignedArtifactURLResponse struct { SignedUrl string `json:"signedUrl"` } type DeleteArtifactRequest struct { RunID string `json:"workflowRunBackendId"` JobRunID string `json:"workflowJobRunBackendId"` Name string `json:"name"` } type DeleteArtifactResponse struct { Ok bool `json:"ok"` ArtifactId string `json:"artifactId"` } func (c *Client) CreateArtifact(ctx context.Context, req CreateArtifactRequest) (*CreateArtifactResponse, error) { endpoint := "/twirp/github.actions.results.api.v1.ArtifactService/CreateArtifact" var result CreateArtifactResponse if err := c.twirp(ctx, endpoint, req, &result); err != nil { return nil, err } return &result, nil } func (c *Client) FinalizeArtifact(ctx context.Context, req FinalizeArtifactRequest) (*FinalizeArtifactResponse, error) { endpoint := "/twirp/github.actions.results.api.v1.ArtifactService/FinalizeArtifact" var result FinalizeArtifactResponse if err := c.twirp(ctx, endpoint, req, &result); err != nil { return nil, err } return &result, nil } func (c *Client) ListArtifacts(ctx context.Context, req ListArtifactsRequest) (*ListArtifactsResponse, error) { endpoint := "/twirp/github.actions.results.api.v1.ArtifactService/ListArtifacts" var result ListArtifactsResponse if err := c.twirp(ctx, endpoint, req, &result); err != nil { return nil, err } return &result, nil } func (c *Client) GetSignedArtifactURL(ctx context.Context, req GetSignedArtifactURLRequest) (*GetSignedArtifactURLResponse, error) { endpoint := "/twirp/github.actions.results.api.v1.ArtifactService/GetSignedArtifactURL" var result GetSignedArtifactURLResponse if err := c.twirp(ctx, endpoint, req, &result); err != nil { return nil, err } return &result, nil } func (c *Client) DeleteArtifact(ctx context.Context, req DeleteArtifactRequest) (*DeleteArtifactResponse, error) { endpoint := "/twirp/github.actions.results.api.v1.ArtifactService/DeleteArtifact" var result DeleteArtifactResponse if err := c.twirp(ctx, endpoint, req, &result); err != nil { return nil, err } return &result, nil } func (c *Client) twirp(ctx context.Context, url string, in any, out any) error { var b bytes.Buffer if err := json.NewEncoder(&b).Encode(in); err != nil { return err } req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &b) if err != nil { return err } req.Header.Set("content-type", "application/json") req.Header.Set("accept", "application/json") res, err := c.Do(req) if err != nil { return err } defer res.Body.Close() if !sdk.HasContentType(res, "application/json") { return fmt.Errorf("content-type: %q should be %q", res.Header.Get("content-type"), "application/json") } dec := json.NewDecoder(io.LimitReader(res.Body, 32*1024)) if res.StatusCode >= 200 && res.StatusCode < 300 { return dec.Decode(out) } else { body, _ := io.ReadAll(res.Body) return fmt.Errorf("twirp call %s failed: %d: %s", url, res.StatusCode, string(body)) } }