|
| 1 | +package org.kohsuke.github; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 4 | +import org.apache.commons.lang3.StringUtils; |
| 5 | +import org.kohsuke.github.function.InputStreamFunction; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.URL; |
| 9 | +import java.util.Date; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +import static java.util.Objects.requireNonNull; |
| 13 | + |
| 14 | +/** |
| 15 | + * An artifact from a workflow run. |
| 16 | + * |
| 17 | + * @author Guillaume Smet |
| 18 | + */ |
| 19 | +public class GHArtifact extends GHObject { |
| 20 | + |
| 21 | + // Not provided by the API. |
| 22 | + @JsonIgnore |
| 23 | + private GHRepository owner; |
| 24 | + |
| 25 | + private String name; |
| 26 | + private long sizeInBytes; |
| 27 | + private String archiveDownloadUrl; |
| 28 | + private boolean expired; |
| 29 | + private String expiresAt; |
| 30 | + |
| 31 | + public String getName() { |
| 32 | + return name; |
| 33 | + } |
| 34 | + |
| 35 | + public long getSizeInBytes() { |
| 36 | + return sizeInBytes; |
| 37 | + } |
| 38 | + |
| 39 | + public URL getArchiveDownloadUrl() { |
| 40 | + return GitHubClient.parseURL(archiveDownloadUrl); |
| 41 | + } |
| 42 | + |
| 43 | + public boolean isExpired() { |
| 44 | + return expired; |
| 45 | + } |
| 46 | + |
| 47 | + public Date getExpiresAt() { |
| 48 | + return GitHubClient.parseDate(expiresAt); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @deprecated This object has no HTML URL. |
| 53 | + */ |
| 54 | + @Override |
| 55 | + public URL getHtmlUrl() throws IOException { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Deletes the artifact. |
| 61 | + * |
| 62 | + * @throws IOException |
| 63 | + * the io exception |
| 64 | + */ |
| 65 | + public void delete() throws IOException { |
| 66 | + root.createRequest().method("DELETE").withUrlPath(getApiRoute()).fetchHttpStatusCode(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Downloads the artifact. |
| 71 | + * |
| 72 | + * @param <T> |
| 73 | + * the type of result |
| 74 | + * @param streamFunction |
| 75 | + * The {@link InputStreamFunction} that will process the stream |
| 76 | + * @throws IOException |
| 77 | + * The IO exception. |
| 78 | + * @return the result of reading the stream. |
| 79 | + */ |
| 80 | + public <T> T download(InputStreamFunction<T> streamFunction) throws IOException { |
| 81 | + requireNonNull(streamFunction, "Stream function must not be null"); |
| 82 | + |
| 83 | + return root.createRequest().method("GET").withUrlPath(getApiRoute(), "zip").fetchStream(streamFunction); |
| 84 | + } |
| 85 | + |
| 86 | + private String getApiRoute() { |
| 87 | + if (owner == null) { |
| 88 | + // Workflow runs returned from search to do not have an owner. Attempt to use url. |
| 89 | + final URL url = Objects.requireNonNull(getUrl(), "Missing instance URL!"); |
| 90 | + return StringUtils.prependIfMissing(url.toString().replace(root.getApiUrl(), ""), "/"); |
| 91 | + } |
| 92 | + return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/actions/artifacts/" + getId(); |
| 93 | + } |
| 94 | + |
| 95 | + GHArtifact wrapUp(GHRepository owner) { |
| 96 | + this.owner = owner; |
| 97 | + return wrapUp(owner.root); |
| 98 | + } |
| 99 | + |
| 100 | + GHArtifact wrapUp(GitHub root) { |
| 101 | + this.root = root; |
| 102 | + if (owner != null) |
| 103 | + owner.wrap(root); |
| 104 | + return this; |
| 105 | + } |
| 106 | +} |
0 commit comments