| 24 | } |
| 25 | |
| 26 | func ListArtifacts(httpClient *http.Client, repo ghrepo.Interface, runID string) ([]Artifact, error) { |
| 27 | var results []Artifact |
| 28 | |
| 29 | restPrefix := ghinstance.RESTPrefix(repo.RepoHost()) |
| 30 | perPage := 100 |
| 31 | u, err := safeurl.JoinPathWithHostPrefix(restPrefix, "repos", repo.RepoOwner(), repo.RepoName(), "actions", "artifacts") |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | if runID != "" { |
| 36 | u, err = safeurl.JoinPathWithHostPrefix(restPrefix, "repos", repo.RepoOwner(), repo.RepoName(), "actions", "runs", runID, "artifacts") |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | } |
| 41 | u.SetQuery("per_page", strconv.Itoa(perPage)) |
| 42 | var pageURL safeurl.SafeURL = u |
| 43 | |
| 44 | for { |
| 45 | var payload artifactsPayload |
| 46 | nextURL, err := apiGet(httpClient, pageURL, &payload) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | results = append(results, payload.Artifacts...) |
| 51 | |
| 52 | if nextURL == "" { |
| 53 | break |
| 54 | } |
| 55 | pageURL = safeurl.NewImmutableSafeURL(nextURL) |
| 56 | } |
| 57 | |
| 58 | return results, nil |
| 59 | } |
| 60 | |
| 61 | func apiGet(httpClient *http.Client, url safeurl.SafeURL, data interface{}) (string, error) { |
| 62 | req, err := http.NewRequest("GET", url.String(), nil) |