How can I download the latest release of a private repository as a zip-file programmatically using Octokit.net?
I am not looking to download the entire repository, just the source code from a specified release, for example the latest one.
So far, I have created a GitHubClient, authorized with a GitHubToken, and I have gotten a list of all releases in the repo. I'm able to get information on the latest release, like the name, tag, Url, and ZipballUrl, etc.
I've attempted to download the release using the ZipballUrl, but it does not return a valid zip.
In my code below, I'm trying to download the latest release from the Octokit repo.
String workspaceName = "octokit";
String repositoryName = "octokit.net";
String filename = "temp.zip";
var client = new GitHubClient(new ProductHeaderValue(repositoryName));
String GitHubToken = "--- token goes here ---";
var tokenAuth = new Credentials(GitHubToken);
client.Credentials = tokenAuth;
// Retrieve a List of Releases in the Repository, and get latest using [0]-subscript
var releases = await client.Repository.Release.GetAll(workspaceName, repositoryName);
var latest = releases[0];
// Get a HttpResponse from the Zipball URL, which is then converted to a byte array
var response = await client.Connection.Get<object>(new Uri(latest.ZipballUrl), new Dictionary<string, string>(), "application/json");
byte[] releaseBytes = Encoding.ASCII.GetBytes(response.HttpResponse.Body.ToString());
// Create the resulting file using the byte array
await File.WriteAllBytesAsync(filename, releaseBytes);
I'm not very familiar with the GitHubClient, nor Http- or WebClients, so I cannot tell if the mistake lies in the HttpResponse and the content I get from there, or anywhere else in the file writing process.
SOLVED:
I could not solve this entirely using the GitHubClient, so I am still interested to know if that is possible.
However, I managed to solve this by adding a WebClient to take care of the download after finding the ZipballUrl using Octokit. This had to be configured with User-Agent and Authorization headers to work.