github.com/bshelton229/agent@v3.5.4+incompatible/api/artifacts.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // ArtifactsService handles communication with the artifact related methods of
     8  // the Buildkite Artifact API.
     9  type ArtifactsService struct {
    10  	client *Client
    11  }
    12  
    13  // Artifact represents an artifact on the Buildkite Agent API
    14  type Artifact struct {
    15  	// The ID of the artifact. The ID is assigned to it after a successful
    16  	// batch creation
    17  	ID string `json:"-"`
    18  
    19  	// The path to the artifact relative to the working directory
    20  	Path string `json:"path"`
    21  
    22  	// The absolute path to the artifact
    23  	AbsolutePath string `json:"absolute_path"`
    24  
    25  	// The glob path used to find this artifact
    26  	GlobPath string `json:"glob_path"`
    27  
    28  	// The size of the file in bytes
    29  	FileSize int64 `json:"file_size"`
    30  
    31  	// A Sha1Sum calculation of the file
    32  	Sha1Sum string `json:"sha1sum"`
    33  
    34  	// The HTTP url to this artifact once it's been uploaded
    35  	URL string `json:"url,omitempty"`
    36  
    37  	// The destination specified on the command line when this file was
    38  	// uploaded
    39  	UploadDestination string `json:"upload_destination,omitempty"`
    40  
    41  	// Information on how to upload this artifact.
    42  	UploadInstructions *ArtifactUploadInstructions `json:"-"`
    43  }
    44  
    45  type ArtifactBatch struct {
    46  	ID                string      `json:"id"`
    47  	Artifacts         []*Artifact `json:"artifacts"`
    48  	UploadDestination string      `json:"upload_destination"`
    49  }
    50  
    51  type ArtifactUploadInstructions struct {
    52  	Data   map[string]string `json: "data"`
    53  	Action struct {
    54  		URL       string `json:"url,omitempty"`
    55  		Method    string `json:"method"`
    56  		Path      string `json:"path"`
    57  		FileInput string `json:"file_input"`
    58  	}
    59  }
    60  
    61  type ArtifactBatchCreateResponse struct {
    62  	ID                 string                      `json:"id"`
    63  	ArtifactIDs        []string                    `json:"artifact_ids"`
    64  	UploadInstructions *ArtifactUploadInstructions `json:"upload_instructions"`
    65  }
    66  
    67  // ArtifactSearchOptions specifies the optional parameters to the
    68  // ArtifactsService.Search method.
    69  type ArtifactSearchOptions struct {
    70  	Query string `url:"query,omitempty"`
    71  	Scope string `url:"scope,omitempty"`
    72  }
    73  
    74  type ArtifactBatchUpdateArtifact struct {
    75  	ID    string `json:"id"`
    76  	State string `json:"state"`
    77  }
    78  
    79  type ArtifactBatchUpdateRequest struct {
    80  	Artifacts []*ArtifactBatchUpdateArtifact `json:"artifacts"`
    81  }
    82  
    83  // Accepts a slice of artifacts, and creates them on Buildkite as a batch.
    84  func (as *ArtifactsService) Create(jobId string, batch *ArtifactBatch) (*ArtifactBatchCreateResponse, *Response, error) {
    85  	u := fmt.Sprintf("jobs/%s/artifacts", jobId)
    86  
    87  	req, err := as.client.NewRequest("POST", u, batch)
    88  	if err != nil {
    89  		return nil, nil, err
    90  	}
    91  
    92  	createResponse := new(ArtifactBatchCreateResponse)
    93  	resp, err := as.client.Do(req, createResponse)
    94  	if err != nil {
    95  		return nil, resp, err
    96  	}
    97  
    98  	return createResponse, resp, err
    99  }
   100  
   101  // Updates a paticular artifact
   102  func (as *ArtifactsService) Update(jobId string, artifactStates map[string]string) (*Response, error) {
   103  	u := fmt.Sprintf("jobs/%s/artifacts", jobId)
   104  	payload := ArtifactBatchUpdateRequest{}
   105  
   106  	for id, state := range artifactStates {
   107  		payload.Artifacts = append(payload.Artifacts, &ArtifactBatchUpdateArtifact{id, state})
   108  	}
   109  
   110  	req, err := as.client.NewRequest("PUT", u, payload)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	resp, err := as.client.Do(req, nil)
   116  	if err != nil {
   117  		return resp, err
   118  	}
   119  
   120  	return resp, err
   121  }
   122  
   123  // Searches Buildkite for a set of artifacts
   124  func (as *ArtifactsService) Search(buildId string, opt *ArtifactSearchOptions) ([]*Artifact, *Response, error) {
   125  	u := fmt.Sprintf("builds/%s/artifacts/search", buildId)
   126  	u, err := addOptions(u, opt)
   127  	if err != nil {
   128  		return nil, nil, err
   129  	}
   130  
   131  	req, err := as.client.NewRequest("GET", u, nil)
   132  	if err != nil {
   133  		return nil, nil, err
   134  	}
   135  
   136  	a := []*Artifact{}
   137  	resp, err := as.client.Do(req, &a)
   138  	if err != nil {
   139  		return nil, resp, err
   140  	}
   141  
   142  	return a, resp, err
   143  }