github.com/google/go-github/v64@v64.0.0/github/actions_artifacts.go (about)

     1  // Copyright 2020 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"net/http"
    12  	"net/url"
    13  )
    14  
    15  // ArtifactWorkflowRun represents a GitHub artifact's workflow run.
    16  //
    17  // GitHub API docs: https://docs.github.com/rest/actions/artifacts
    18  type ArtifactWorkflowRun struct {
    19  	ID               *int64  `json:"id,omitempty"`
    20  	RepositoryID     *int64  `json:"repository_id,omitempty"`
    21  	HeadRepositoryID *int64  `json:"head_repository_id,omitempty"`
    22  	HeadBranch       *string `json:"head_branch,omitempty"`
    23  	HeadSHA          *string `json:"head_sha,omitempty"`
    24  }
    25  
    26  // Artifact represents a GitHub artifact.  Artifacts allow sharing
    27  // data between jobs in a workflow and provide storage for data
    28  // once a workflow is complete.
    29  //
    30  // GitHub API docs: https://docs.github.com/rest/actions/artifacts
    31  type Artifact struct {
    32  	ID                 *int64               `json:"id,omitempty"`
    33  	NodeID             *string              `json:"node_id,omitempty"`
    34  	Name               *string              `json:"name,omitempty"`
    35  	SizeInBytes        *int64               `json:"size_in_bytes,omitempty"`
    36  	URL                *string              `json:"url,omitempty"`
    37  	ArchiveDownloadURL *string              `json:"archive_download_url,omitempty"`
    38  	Expired            *bool                `json:"expired,omitempty"`
    39  	CreatedAt          *Timestamp           `json:"created_at,omitempty"`
    40  	UpdatedAt          *Timestamp           `json:"updated_at,omitempty"`
    41  	ExpiresAt          *Timestamp           `json:"expires_at,omitempty"`
    42  	WorkflowRun        *ArtifactWorkflowRun `json:"workflow_run,omitempty"`
    43  }
    44  
    45  // ArtifactList represents a list of GitHub artifacts.
    46  //
    47  // GitHub API docs: https://docs.github.com/rest/actions/artifacts#artifacts
    48  type ArtifactList struct {
    49  	TotalCount *int64      `json:"total_count,omitempty"`
    50  	Artifacts  []*Artifact `json:"artifacts,omitempty"`
    51  }
    52  
    53  // ListArtifacts lists all artifacts that belong to a repository.
    54  //
    55  // GitHub API docs: https://docs.github.com/rest/actions/artifacts#list-artifacts-for-a-repository
    56  //
    57  //meta:operation GET /repos/{owner}/{repo}/actions/artifacts
    58  func (s *ActionsService) ListArtifacts(ctx context.Context, owner, repo string, opts *ListOptions) (*ArtifactList, *Response, error) {
    59  	u := fmt.Sprintf("repos/%v/%v/actions/artifacts", owner, repo)
    60  	u, err := addOptions(u, opts)
    61  	if err != nil {
    62  		return nil, nil, err
    63  	}
    64  
    65  	req, err := s.client.NewRequest("GET", u, nil)
    66  	if err != nil {
    67  		return nil, nil, err
    68  	}
    69  
    70  	artifactList := new(ArtifactList)
    71  	resp, err := s.client.Do(ctx, req, artifactList)
    72  	if err != nil {
    73  		return nil, resp, err
    74  	}
    75  
    76  	return artifactList, resp, nil
    77  }
    78  
    79  // ListWorkflowRunArtifacts lists all artifacts that belong to a workflow run.
    80  //
    81  // GitHub API docs: https://docs.github.com/rest/actions/artifacts#list-workflow-run-artifacts
    82  //
    83  //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/artifacts
    84  func (s *ActionsService) ListWorkflowRunArtifacts(ctx context.Context, owner, repo string, runID int64, opts *ListOptions) (*ArtifactList, *Response, error) {
    85  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/artifacts", owner, repo, runID)
    86  	u, err := addOptions(u, opts)
    87  	if err != nil {
    88  		return nil, nil, err
    89  	}
    90  
    91  	req, err := s.client.NewRequest("GET", u, nil)
    92  	if err != nil {
    93  		return nil, nil, err
    94  	}
    95  
    96  	artifactList := new(ArtifactList)
    97  	resp, err := s.client.Do(ctx, req, artifactList)
    98  	if err != nil {
    99  		return nil, resp, err
   100  	}
   101  
   102  	return artifactList, resp, nil
   103  }
   104  
   105  // GetArtifact gets a specific artifact for a workflow run.
   106  //
   107  // GitHub API docs: https://docs.github.com/rest/actions/artifacts#get-an-artifact
   108  //
   109  //meta:operation GET /repos/{owner}/{repo}/actions/artifacts/{artifact_id}
   110  func (s *ActionsService) GetArtifact(ctx context.Context, owner, repo string, artifactID int64) (*Artifact, *Response, error) {
   111  	u := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v", owner, repo, artifactID)
   112  
   113  	req, err := s.client.NewRequest("GET", u, nil)
   114  	if err != nil {
   115  		return nil, nil, err
   116  	}
   117  
   118  	artifact := new(Artifact)
   119  	resp, err := s.client.Do(ctx, req, artifact)
   120  	if err != nil {
   121  		return nil, resp, err
   122  	}
   123  
   124  	return artifact, resp, nil
   125  }
   126  
   127  // DownloadArtifact gets a redirect URL to download an archive for a repository.
   128  //
   129  // GitHub API docs: https://docs.github.com/rest/actions/artifacts#download-an-artifact
   130  //
   131  //meta:operation GET /repos/{owner}/{repo}/actions/artifacts/{artifact_id}/{archive_format}
   132  func (s *ActionsService) DownloadArtifact(ctx context.Context, owner, repo string, artifactID int64, maxRedirects int) (*url.URL, *Response, error) {
   133  	u := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v/zip", owner, repo, artifactID)
   134  
   135  	resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
   136  	if err != nil {
   137  		return nil, nil, err
   138  	}
   139  	defer resp.Body.Close()
   140  
   141  	if resp.StatusCode != http.StatusFound {
   142  		return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
   143  	}
   144  
   145  	parsedURL, err := url.Parse(resp.Header.Get("Location"))
   146  	if err != nil {
   147  		return nil, newResponse(resp), err
   148  	}
   149  
   150  	return parsedURL, newResponse(resp), nil
   151  }
   152  
   153  // DeleteArtifact deletes a workflow run artifact.
   154  //
   155  // GitHub API docs: https://docs.github.com/rest/actions/artifacts#delete-an-artifact
   156  //
   157  //meta:operation DELETE /repos/{owner}/{repo}/actions/artifacts/{artifact_id}
   158  func (s *ActionsService) DeleteArtifact(ctx context.Context, owner, repo string, artifactID int64) (*Response, error) {
   159  	u := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v", owner, repo, artifactID)
   160  
   161  	req, err := s.client.NewRequest("DELETE", u, nil)
   162  	if err != nil {
   163  		return nil, err
   164  	}
   165  
   166  	return s.client.Do(ctx, req, nil)
   167  }