...

Source file src/github.com/google/go-github/v55/github/actions_artifacts.go

Documentation: github.com/google/go-github/v55/github

     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/en/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/en/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/en/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/en/rest/actions/artifacts#list-artifacts-for-a-repository
    56  func (s *ActionsService) ListArtifacts(ctx context.Context, owner, repo string, opts *ListOptions) (*ArtifactList, *Response, error) {
    57  	u := fmt.Sprintf("repos/%v/%v/actions/artifacts", owner, repo)
    58  	u, err := addOptions(u, opts)
    59  	if err != nil {
    60  		return nil, nil, err
    61  	}
    62  
    63  	req, err := s.client.NewRequest("GET", u, nil)
    64  	if err != nil {
    65  		return nil, nil, err
    66  	}
    67  
    68  	artifactList := new(ArtifactList)
    69  	resp, err := s.client.Do(ctx, req, artifactList)
    70  	if err != nil {
    71  		return nil, resp, err
    72  	}
    73  
    74  	return artifactList, resp, nil
    75  }
    76  
    77  // ListWorkflowRunArtifacts lists all artifacts that belong to a workflow run.
    78  //
    79  // GitHub API docs: https://docs.github.com/en/rest/actions/artifacts#list-workflow-run-artifacts
    80  func (s *ActionsService) ListWorkflowRunArtifacts(ctx context.Context, owner, repo string, runID int64, opts *ListOptions) (*ArtifactList, *Response, error) {
    81  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/artifacts", owner, repo, runID)
    82  	u, err := addOptions(u, opts)
    83  	if err != nil {
    84  		return nil, nil, err
    85  	}
    86  
    87  	req, err := s.client.NewRequest("GET", u, nil)
    88  	if err != nil {
    89  		return nil, nil, err
    90  	}
    91  
    92  	artifactList := new(ArtifactList)
    93  	resp, err := s.client.Do(ctx, req, artifactList)
    94  	if err != nil {
    95  		return nil, resp, err
    96  	}
    97  
    98  	return artifactList, resp, nil
    99  }
   100  
   101  // GetArtifact gets a specific artifact for a workflow run.
   102  //
   103  // GitHub API docs: https://docs.github.com/en/rest/actions/artifacts#get-an-artifact
   104  func (s *ActionsService) GetArtifact(ctx context.Context, owner, repo string, artifactID int64) (*Artifact, *Response, error) {
   105  	u := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v", owner, repo, artifactID)
   106  
   107  	req, err := s.client.NewRequest("GET", u, nil)
   108  	if err != nil {
   109  		return nil, nil, err
   110  	}
   111  
   112  	artifact := new(Artifact)
   113  	resp, err := s.client.Do(ctx, req, artifact)
   114  	if err != nil {
   115  		return nil, resp, err
   116  	}
   117  
   118  	return artifact, resp, nil
   119  }
   120  
   121  // DownloadArtifact gets a redirect URL to download an archive for a repository.
   122  //
   123  // GitHub API docs: https://docs.github.com/en/rest/actions/artifacts#download-an-artifact
   124  func (s *ActionsService) DownloadArtifact(ctx context.Context, owner, repo string, artifactID int64, followRedirects bool) (*url.URL, *Response, error) {
   125  	u := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v/zip", owner, repo, artifactID)
   126  
   127  	resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
   128  	if err != nil {
   129  		return nil, nil, err
   130  	}
   131  	defer resp.Body.Close()
   132  
   133  	if resp.StatusCode != http.StatusFound {
   134  		return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
   135  	}
   136  
   137  	parsedURL, err := url.Parse(resp.Header.Get("Location"))
   138  	if err != nil {
   139  		return nil, newResponse(resp), err
   140  	}
   141  
   142  	return parsedURL, newResponse(resp), nil
   143  }
   144  
   145  // DeleteArtifact deletes a workflow run artifact.
   146  //
   147  // GitHub API docs: https://docs.github.com/en/rest/actions/artifacts#delete-an-artifact
   148  func (s *ActionsService) DeleteArtifact(ctx context.Context, owner, repo string, artifactID int64) (*Response, error) {
   149  	u := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v", owner, repo, artifactID)
   150  
   151  	req, err := s.client.NewRequest("DELETE", u, nil)
   152  	if err != nil {
   153  		return nil, err
   154  	}
   155  
   156  	return s.client.Do(ctx, req, nil)
   157  }
   158  

View as plain text