octokit.net icon indicating copy to clipboard operation
octokit.net copied to clipboard

download to release

Open Vincenzoferrara opened this issue 1 year ago • 5 comments

Describe the need

I'm trying to download a release file but there's nothing written in the documentation, I haven't found anything useful even in the questions. Could you have some examples? A thousand thanks

SDK Version

No response

API Version

No response

Relevant log output

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Vincenzoferrara avatar Jul 07 '23 19:07 Vincenzoferrara

Could you do something like the following?

using Octokit;
using System;
using System.Net.Http;
using System.IO;
using System.Threading.Tasks;

var client = new GitHubClient(new ProductHeaderValue("MyApp"));
var release = await client.Repository.Release.Get("integrations", "terraform-provider-github", "v5.29.0");
Console.WriteLine($"Release {release.Name} has {release.Assets.Count} assets");
using (var httpClient = new HttpClient())
{
  foreach (var asset in release.Assets)
  {
    Console.WriteLine($"Retrieving {asset.Name}");
    var fileData = await httpClient.GetByteArrayAsync(asset.BrowserDownloadUrl);
    File.WriteAllBytes(asset.Name, fileData);
  }
}

kfcampbell avatar Jul 07 '23 20:07 kfcampbell

my repository is private. I honestly thought there was a command (which also included file verification)

chatgpt create this code

` private const string GitHubToken = ""; private const string GitHubOwner = ""; private const string GitHubRepo = ""; private static readonly GitHubClient client = new GitHubClient(new ProductHeaderValue("UpdateManager")) { Credentials = new Credentials(GitHubToken) }; private static readonly string temp_dir = Path.Combine(Path.GetTempPath(), "UpdateTemp"); var releases = await client.Repository.Release.GetAll(GitHubOwner, GitHubRepo); release = releases[0];

var asset = release.Assets.FirstOrDefault(a => a.Name == file_name);

            if (asset != null)
            {
                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Add("Authorization", $"token {GitHubToken}");
                    using (var response = await httpClient.GetAsync(asset.BrowserDownloadUrl))
                    {
                        response.EnsureSuccessStatusCode();
                        using (var stream = await response.Content.ReadAsStreamAsync())
                        {
                            using (var fileStream = File.Create(file_name))
                            {
                                await stream.CopyToAsync(fileStream);
                            }
                        }
                    }
                }
            }

`

but I don't know how to integrate the verification of downloaded files (ps the code i before is part of a bigger code so i may have skipped somewhere)

Vincenzoferrara avatar Jul 07 '23 23:07 Vincenzoferrara

What do you mean by "integrate the verification of downloaded files"?

kfcampbell avatar Jul 10 '23 17:07 kfcampbell

check if the downloaded file was current, or if it is in an inconsistent state.

I know you are using a checksum.

for example if the downalod stops the internet what happens to the file? so here is a file integrity check, to verify that the file is the same as the original

Vincenzoferrara avatar Jul 11 '23 12:07 Vincenzoferrara

however the above code doesn't work, it fails to connect giving a 404 error

i can't figure out how to use the token, with your toolkit it works so it's a problem of my code.

Could I have an example of the code? Thanks in advance

ps: I also tried using a token with full permissions but it gives the same error

Vincenzoferrara avatar Jul 11 '23 12:07 Vincenzoferrara

this code it works for me ` try { var releases = await client.Repository.Release.GetAll(GitHubOwner, GitHubRepo); var release = releases[0]; var latestAsset = await client.Repository.Release.GetAllAssets(GitHubOwner, GitHubRepo, release.Id);

            foreach (var asset in latestAsset)
            {
                if (asset.ContentType == "application/zip" && asset.Name == file_name)
                {
                    var url = asset.Url;

                    using (HttpClient client = new HttpClient())
                    {

                        client.DefaultRequestHeaders.Add("User-Agent", "Anything");
                        client.DefaultRequestHeaders.Add("Accept", "application/octet-stream");
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GitHubToken);

                        // Esegui la richiesta HTTP
                        HttpResponseMessage response = await client.GetAsync(url);

                        response.EnsureSuccessStatusCode();

                        // Leggi il contenuto dell'asset
                        using (Stream stream = await response.Content.ReadAsStreamAsync())
                        {
                            using (FileStream fileStream = File.Create(file_name))
                            {
                                await stream.CopyToAsync(fileStream);
                            }
                        }

                        Console.WriteLine("Update complete! Please restart the application.");
                        Console.WriteLine("Download completato.");
                    }
                    break;
                }
            }
        }
        catch (Exception ex)
        {
            download_not_error = false;
            Console.WriteLine($"Errore durante il download degli aggiornamenti: {ex.Message}");
        }`

Vincenzoferrara avatar Mar 31 '24 19:03 Vincenzoferrara