1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "net/http"
12 "net/url"
13 )
14
15
16
17
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
27
28
29
30
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
46
47
48 type ArtifactList struct {
49 TotalCount *int64 `json:"total_count,omitempty"`
50 Artifacts []*Artifact `json:"artifacts,omitempty"`
51 }
52
53
54
55
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
78
79
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
102
103
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
122
123
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
146
147
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