使用 github/octonode API 上传和提交 zip 的正确步骤

correct steps of using the github/octonode API to upload and commit a zip

我正在尝试使用 nodejs

从 bitbucket 管道将 zip 文件推送到 github

目前正在使用 octonode 一个很好的 github api 包装器。

我想弄清楚的是 process/steps 如何做到这一点。

我成功地以编程方式创建了初始提交

 var github = require('octonode');
 
 const client = github.client(access_token);
 
 const repo = client.repo('myuser/myrepo');
 
 repo.createContents('my.zip', 'Initial Commit', zipFile, (err, data, headers) => {
       
  }); 

但接下来要做的是我最终陷入杂草的地方。

我在想,从 master 创建一个分支,然后更新该分支中的 zip 文件,然后合并。

我好像无法创建分支。 我想我应该使用 createReference,并假设使用 master 的 sha??

我找不到master的sha

repo.createReference('v1.0.0', 'sha', (err, data, headers) => {
      console.log('in createReference callback---err---->', err);
      console.log('in createReference callback---data---->', data);
      console.log('in createReference callback---headers---->', headers);
});

问题是,这是正确的方法吗?

我的最终目标是以编程方式将新的 zip 文件推送到 github。

感谢任何 help/suggestions

我不确定这是否是最好的方法,但这就是我的工作方式

   const octokit = new Octokit.Octokit({
        auth: access_token
    });


    const base64Content = utilService.base64Encode(fs.readFileSync(`${zipFilePath}/${zipFileName}`));

    let newBranch = pluginVersionFromFile;

    /**
     * In order to create a new branch off of master, we first have to get the sha of master
     */
    const branches = await octokit.request(`GET /repos/myusername/${config.gitHubRepoName}/branches`, {
        owner: 'myusername',
        repo: config.gitHubRepoName
    });

    let masterSha;
    _.forEach(branches.data, function(branch) {
        if (branch.name === 'master') {
            masterSha = _.get(branch, 'commit.sha');
        }
        if (branch.name === newBranch) {
            newBranch = newBranch + '.' + moment().format('X');
        }
    });

    /**
     * Create a new branch off of master with the sha of master
     */
    await octokit.request(`POST /repos/myusername/${config.gitHubRepoName}/git/refs`, {
        owner: 'myusername',
        repo: config.gitHubRepoName,
        ref: `refs/heads/${newBranch}`,
        sha: masterSha
    });

    /**
     * In order to update the new version of the file, we now how to find the sha of the file we are going to update
     * that is not really straight forward.
     *
     * first we get all the commits for that file and iterate through them and get the Tree url and the tree
     * url will return the info needed to get the sha of the file.
     */
    const commits = await octokit.request(`GET /repos/myusername/${config.gitHubRepoName}/commits`, {
        owner: 'myusername',
        repo: config.gitHubRepoName,
        path: zipFileName
    });

    const treeURL = _.get(commits, 'data[0].commit.tree.url');

    /*
     * if there was a prior commit, we get the tree URL and from that we get the info about the file
     * that contains the sha
     */
    let sha;
    if (treeURL) {
        const treeResponse = await octokit.request(`GET ${treeURL}`);
        _.forEach(treeResponse.data.tree, function(tree) {
            if (tree.path === zipFileName) {
                sha = tree.sha;
            }
        });
    }

    /*
     * update the file using the sha and the new branch name if file was already uploaded
     * only use the sha if this is not a new file that would have no commits
     */
    const updateFileData = {
        owner: 'myusername',
        repo: config.gitHubRepoName,
        path: zipFileName,
        message: `Plugin version ${pluginVersionFromFile}`,
        content: base64Content,
        branch: newBranch,
        sha:sha
    };

    if (!sha) {
        _.unset(updateFileData, 'sha');
    }

    await octokit.request(`PUT /repos/myusername/${config.gitHubRepoName}/contents/${zipFileName}`, updateFileData);

    /*
     * Create a PR
     */
    const prResponse = await octokit.request(`POST /repos/myusername/${config.gitHubRepoName}/pulls`, {
        owner: 'myusername',
        repo: config.gitHubRepoName,
        head: `myusername:${newBranch}`,
        base: 'master',
        title: `New Version of plugin ${newBranch}`
    });
    const prNumber = _.get(prResponse, 'data.number');

    /*
     * Merge the PR with the PR number from above
     */
    await octokit.request(`PUT /repos/myusername/${config.gitHubRepoName}/pulls/${prNumber}/merge`, {
        owner: 'myusername',
        repo: config.gitHubRepoName,
        pull_number: prNumber,
        commit_title:  `New Version of plugin ${newBranch}`
    });

    /*
    * Delete the branch after the merge is complete
    */
    //eslint-disable-next-line no-console
    console.log(' Deleting the branch ');
    await octokit.request(`DELETE /repos/myusername/${config.gitHubRepoName}/git/refs/heads/${newBranch}`, {
        owner: 'myusername',
        repo: config.gitHubRepoName
    });

    /**
     * Creating a release
     */
    await octokit.request(`POST /repos/myusername/${config.gitHubRepoName}/releases`, {
        owner: 'myusername',
        repo: config.gitHubRepoName,
        tag_name: pluginVersionFromFile,
        name: `Plugin Version  ${pluginVersionFromFile}`
    });