// Copyright 2020 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	
	
	
	
)

// ArtifactWorkflowRun represents a GitHub artifact's workflow run.
//
// GitHub API docs: https://docs.github.com/rest/actions/artifacts
type ArtifactWorkflowRun struct {
	ID               *int64  `json:"id,omitempty"`
	RepositoryID     *int64  `json:"repository_id,omitempty"`
	HeadRepositoryID *int64  `json:"head_repository_id,omitempty"`
	HeadBranch       *string `json:"head_branch,omitempty"`
	HeadSHA          *string `json:"head_sha,omitempty"`
}

// Artifact represents a GitHub artifact.  Artifacts allow sharing
// data between jobs in a workflow and provide storage for data
// once a workflow is complete.
//
// GitHub API docs: https://docs.github.com/rest/actions/artifacts
type Artifact struct {
	ID                 *int64               `json:"id,omitempty"`
	NodeID             *string              `json:"node_id,omitempty"`
	Name               *string              `json:"name,omitempty"`
	SizeInBytes        *int64               `json:"size_in_bytes,omitempty"`
	URL                *string              `json:"url,omitempty"`
	ArchiveDownloadURL *string              `json:"archive_download_url,omitempty"`
	Expired            *bool                `json:"expired,omitempty"`
	CreatedAt          *Timestamp           `json:"created_at,omitempty"`
	UpdatedAt          *Timestamp           `json:"updated_at,omitempty"`
	ExpiresAt          *Timestamp           `json:"expires_at,omitempty"`
	WorkflowRun        *ArtifactWorkflowRun `json:"workflow_run,omitempty"`
}

// ArtifactList represents a list of GitHub artifacts.
//
// GitHub API docs: https://docs.github.com/rest/actions/artifacts#artifacts
type ArtifactList struct {
	TotalCount *int64      `json:"total_count,omitempty"`
	Artifacts  []*Artifact `json:"artifacts,omitempty"`
}

// ListArtifacts lists all artifacts that belong to a repository.
//
// GitHub API docs: https://docs.github.com/rest/actions/artifacts#list-artifacts-for-a-repository
//
//meta:operation GET /repos/{owner}/{repo}/actions/artifacts
func ( *ActionsService) ( context.Context, ,  string,  *ListOptions) (*ArtifactList, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/actions/artifacts", , )
	,  := addOptions(, )
	if  != nil {
		return nil, nil, 
	}

	,  := .client.NewRequest("GET", , nil)
	if  != nil {
		return nil, nil, 
	}

	 := new(ArtifactList)
	,  := .client.Do(, , )
	if  != nil {
		return nil, , 
	}

	return , , nil
}

// ListWorkflowRunArtifacts lists all artifacts that belong to a workflow run.
//
// GitHub API docs: https://docs.github.com/rest/actions/artifacts#list-workflow-run-artifacts
//
//meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/artifacts
func ( *ActionsService) ( context.Context, ,  string,  int64,  *ListOptions) (*ArtifactList, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/actions/runs/%v/artifacts", , , )
	,  := addOptions(, )
	if  != nil {
		return nil, nil, 
	}

	,  := .client.NewRequest("GET", , nil)
	if  != nil {
		return nil, nil, 
	}

	 := new(ArtifactList)
	,  := .client.Do(, , )
	if  != nil {
		return nil, , 
	}

	return , , nil
}

// GetArtifact gets a specific artifact for a workflow run.
//
// GitHub API docs: https://docs.github.com/rest/actions/artifacts#get-an-artifact
//
//meta:operation GET /repos/{owner}/{repo}/actions/artifacts/{artifact_id}
func ( *ActionsService) ( context.Context, ,  string,  int64) (*Artifact, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v", , , )

	,  := .client.NewRequest("GET", , nil)
	if  != nil {
		return nil, nil, 
	}

	 := new(Artifact)
	,  := .client.Do(, , )
	if  != nil {
		return nil, , 
	}

	return , , nil
}

// DownloadArtifact gets a redirect URL to download an archive for a repository.
//
// GitHub API docs: https://docs.github.com/rest/actions/artifacts#download-an-artifact
//
//meta:operation GET /repos/{owner}/{repo}/actions/artifacts/{artifact_id}/{archive_format}
func ( *ActionsService) ( context.Context, ,  string,  int64,  int) (*url.URL, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v/zip", , , )

	,  := .client.roundTripWithOptionalFollowRedirect(, , )
	if  != nil {
		return nil, nil, 
	}
	defer .Body.Close()

	if .StatusCode != http.StatusFound {
		return nil, newResponse(), fmt.Errorf("unexpected status code: %s", .Status)
	}

	,  := url.Parse(.Header.Get("Location"))
	if  != nil {
		return nil, newResponse(), 
	}

	return , newResponse(), nil
}

// DeleteArtifact deletes a workflow run artifact.
//
// GitHub API docs: https://docs.github.com/rest/actions/artifacts#delete-an-artifact
//
//meta:operation DELETE /repos/{owner}/{repo}/actions/artifacts/{artifact_id}
func ( *ActionsService) ( context.Context, ,  string,  int64) (*Response, error) {
	 := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v", , , )

	,  := .client.NewRequest("DELETE", , nil)
	if  != nil {
		return nil, 
	}

	return .client.Do(, , nil)
}