-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1075 from gsmet/more-workflows
Add support for artifacts and logs of workflow runs
- Loading branch information
Showing
51 changed files
with
9,261 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.kohsuke.github; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.kohsuke.github.function.InputStreamFunction; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.Date; | ||
import java.util.Objects; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* An artifact from a workflow run. | ||
* | ||
* @author Guillaume Smet | ||
*/ | ||
public class GHArtifact extends GHObject { | ||
|
||
// Not provided by the API. | ||
@JsonIgnore | ||
private GHRepository owner; | ||
|
||
private String name; | ||
private long sizeInBytes; | ||
private String archiveDownloadUrl; | ||
private boolean expired; | ||
private String expiresAt; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public long getSizeInBytes() { | ||
return sizeInBytes; | ||
} | ||
|
||
public URL getArchiveDownloadUrl() { | ||
return GitHubClient.parseURL(archiveDownloadUrl); | ||
} | ||
|
||
public boolean isExpired() { | ||
return expired; | ||
} | ||
|
||
public Date getExpiresAt() { | ||
return GitHubClient.parseDate(expiresAt); | ||
} | ||
|
||
/** | ||
* @deprecated This object has no HTML URL. | ||
*/ | ||
@Override | ||
public URL getHtmlUrl() throws IOException { | ||
return null; | ||
} | ||
|
||
/** | ||
* Deletes the artifact. | ||
* | ||
* @throws IOException | ||
* the io exception | ||
*/ | ||
public void delete() throws IOException { | ||
root.createRequest().method("DELETE").withUrlPath(getApiRoute()).fetchHttpStatusCode(); | ||
} | ||
|
||
/** | ||
* Downloads the artifact. | ||
* | ||
* @param <T> | ||
* the type of result | ||
* @param streamFunction | ||
* The {@link InputStreamFunction} that will process the stream | ||
* @throws IOException | ||
* The IO exception. | ||
* @return the result of reading the stream. | ||
*/ | ||
public <T> T download(InputStreamFunction<T> streamFunction) throws IOException { | ||
requireNonNull(streamFunction, "Stream function must not be null"); | ||
|
||
return root.createRequest().method("GET").withUrlPath(getApiRoute(), "zip").fetchStream(streamFunction); | ||
} | ||
|
||
private String getApiRoute() { | ||
if (owner == null) { | ||
// Workflow runs returned from search to do not have an owner. Attempt to use url. | ||
final URL url = Objects.requireNonNull(getUrl(), "Missing instance URL!"); | ||
return StringUtils.prependIfMissing(url.toString().replace(root.getApiUrl(), ""), "/"); | ||
} | ||
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/actions/artifacts/" + getId(); | ||
} | ||
|
||
GHArtifact wrapUp(GHRepository owner) { | ||
this.owner = owner; | ||
return wrapUp(owner.root); | ||
} | ||
|
||
GHArtifact wrapUp(GitHub root) { | ||
this.root = root; | ||
if (owner != null) | ||
owner.wrap(root); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.kohsuke.github; | ||
|
||
import java.net.MalformedURLException; | ||
import java.util.Iterator; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* Iterable for artifacts listing. | ||
*/ | ||
class GHArtifactsIterable extends PagedIterable<GHArtifact> { | ||
private final transient GHRepository owner; | ||
private final GitHubRequest request; | ||
|
||
private GHArtifactsPage result; | ||
|
||
public GHArtifactsIterable(GHRepository owner, GitHubRequest.Builder<?> requestBuilder) { | ||
this.owner = owner; | ||
try { | ||
this.request = requestBuilder.build(); | ||
} catch (MalformedURLException e) { | ||
throw new GHException("Malformed URL", e); | ||
} | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public PagedIterator<GHArtifact> _iterator(int pageSize) { | ||
return new PagedIterator<>( | ||
adapt(GitHubPageIterator.create(owner.getRoot().getClient(), GHArtifactsPage.class, request, pageSize)), | ||
null); | ||
} | ||
|
||
protected Iterator<GHArtifact[]> adapt(final Iterator<GHArtifactsPage> base) { | ||
return new Iterator<GHArtifact[]>() { | ||
public boolean hasNext() { | ||
return base.hasNext(); | ||
} | ||
|
||
public GHArtifact[] next() { | ||
GHArtifactsPage v = base.next(); | ||
if (result == null) { | ||
result = v; | ||
} | ||
return v.getArtifacts(owner); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.kohsuke.github; | ||
|
||
/** | ||
* Represents the one page of artifacts result when listing artifacts. | ||
*/ | ||
class GHArtifactsPage { | ||
private int total_count; | ||
private GHArtifact[] artifacts; | ||
|
||
public int getTotalCount() { | ||
return total_count; | ||
} | ||
|
||
GHArtifact[] getArtifacts(GHRepository owner) { | ||
for (GHArtifact artifact : artifacts) { | ||
artifact.wrapUp(owner); | ||
} | ||
return artifacts; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.