I am trying to download a Github Artifact from Java code. For this I am using the GitHub API for Java. With the API I can authenticate and list all the GHWorkflowRuns and their GHArtifacts.
If I try to download from GHArtifact.getArchiveDownloadUrtl() I just get a 403 response.
When trying the same URL from a browser (unauthenticated in this case) I get
{
message: "You must have the actions scope to download artifacts."
documentation_url: "https://docs.github.com/rest/reference/actions#download-an-artifact"
}
and I checked the personal access token so it should have sufficient access privileges. There is no extra 'Actions' scope in the configuration dialog but I checked Workflow which includes everything on Repository.
There is another function called GHArtifact.download() but I have no clue how to use it. Does anyone know how to download the artifacts or how to use that download function?
Edit: tgdavies mentioned repository.readTar() which has a similar signature. Going from that I tried to create code like this:
GHWorkflowRun run = ...
List<GHArtifact> artifacts = run.listArtifacts().toList();
for (GHArtifact artifact: artifacts) {
if ("desiredname".equals(run.getName())) {
artifact.download(is -> {
return null;
}, null);
}
}
but my compiler complains with
error: method download in class GHArtifact cannot be applied to given types;
artifact.download(is -> {
^
required: InputStreamFunction<T>
found: (is)->{ re[...]ll; },<null>
reason: cannot infer type-variable(s) T
(actual and formal argument lists differ in length)
where T is a type-variable:
T extends Object declared in method <T>download(InputStreamFunction<T>)
I hope this better explains where I am lost.
CodePudding user response:
I don't have a project with artifacts, but this is how you use the download API:
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import java.io.File;
import java.io.IOException;
import static org.apache.commons.io.FileUtils.copyInputStreamToFile;
public class Test {
public static void main(String[] args) throws IOException {
GitHub github = GitHub.connect("your user id", "your access token");
GHRepository repository = github.getRepository("tgdavies/cardcreator");
repository.readTar(is -> {
copyInputStreamToFile(is, new File("foo.tar"));
return null;
}, null);
}
}
CodePudding user response:
Change permissions of the PAT (personal access token) and grant the scope it demands.
As guest, you might run into this issue: https://github.com/actions/upload-artifact/issues/51.