简体   繁体   English

如何通过代码而不是 CLI 下载 github 版本

[英]How to download a github release by code, not by a CLI

I am making a bot that can grab a source.zip / source.targz from Github, then implement it into my app and new changes are applied, but I have not found a way to download a release because the @Octokit/Rest module I'm using only gets the release.我正在制作一个可以从 Github 获取 source.zip / source.targz 的机器人,然后将它实施到我的应用程序中并应用新的更改,但我还没有找到下载版本的方法,因为我的 @Octokit/Rest 模块'''''''''''''''''''''''''''只得到释放。 Not download.不下载。 Other npm packages don't work for me and if so, the node_modules folder can get really big, around 19mb.其他 npm 包对我不起作用,如果是这样,node_modules 文件夹会变得非常大,大约 19mb。 When I do a https GET request, Github requires me to use a USERAGENT, but I don't know how to tie in Octokit with plain HTTPS requests.当我执行 https GET 请求时,Github 要求我使用 USERAGENT,但我不知道如何将 Octokit 与纯 HTTPS 请求联系起来。

I want to be able to download releases by code, but what I get is the following in the zipfile I tie it to:我希望能够通过代码下载版本,但我得到的是我将其绑定到的 zipfile 中的以下内容:

Request forbidden by administrative rules.行政法规禁止的请求。 Please make sure your request has a User-Agent header ( http://developer.github.com/v3/#user-agent-required ).请确保您的请求具有 User-Agent 标头 ( http://developer.github.com/v3/#user-agent-required )。 Check https://developer.github.com for other possible causes.检查https://developer.github.com以了解其他可能的原因。

    var file = fs.createWriteStream('bot.zip')
    var request = await https.get(releases[num].zipball_url, function(response) {
        response.pipe(file)
    })

//https://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js-without-using-third-party-libraries
var octokit = new Octokit.Octokit({
        userAgent: 'Github Bot Updater',
        previews: ['jean-grey', 'symmetra'],
        baseUrl: 'https://api.github.com',
        log: {
            debug: () => {},
            info: () => {},
            warn: console.warn,
            error: console.error
          },
          request: {
            agent: undefined,
            fetch: undefined,
            timeout: 0
          }
    })
    var releases = await octokit.paginate("GET /repos/:owner/:repo/releases", {  owner: username,  repo: reponame})
//octokit has no method for downloading a release

What I did was instead of getting the zipball from the release, you can also get the same thing from going to the main repo page, then going to the branches button in the corner, going to tags and the desired release tag.我所做的不是从发布中获取 zipball,您还可以通过转到主 repo 页面,然后转到角落中的分支按钮,转到标签和所需的发布标签来获得相同的内容。 Then after you click on that you can download that release.然后在您点击之后,您可以下载该版本。 When I looked into that, instead of doing当我调查时,而不是做

releases[num].zipball_url in releases[num].zipball_url

    var request = await https.get(releases[num].zipball_url, function(response) {
        response.pipe(file)
    })

You can do你可以做

https://codeload.github.com/${username}/${reponame}/zip/${releases[num].tag_name}

And that works.这有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM