(nodegit) 如何使用令牌从/向私有 Github 存储库读取/写入

问题描述

我从网上复制了一些代码,并使其工作以克隆并推送到我自己在 github 上的存储库之一。使用相同的代码,我尝试对现在由我拥有但由我所属的组织并且我拥有写入权限的存储库执行相同操作。这失败了 403。

这是我使用的代码

GithubClient.js

var fs = require('fs');
var NodeGit = require('nodegit');
var config = require('./github-config.js');


var cloneOpts = {
  callbacks: {
    certificateCheck: () => {
      return 1;
    },credentials: function(url,username) {
      console.log('creds for' + username);
      return nodegit.Cred.userpassplaintextNew(config.token,"x-oauth-basic");
    }
  }
};

cloneOpts.fetchOpts = {
  callbacks: cloneOpts.callbacks
};

class GitClient {

  static clone(options) {
    cloneOpts.checkoutBranch = options.branch;
    return NodeGit.Clone(options.remote,options.local,cloneOpts)
      .catch(err => console.log(err));
  }

  static addAndCommit(repo,filesArray,msg) {
    return repo.createCommitOnHead(
      filesArray,NodeGit.Signature.create(config.realname,config.email,new Date().getTime(),0),msg
    );
  }

  static push(repo,repoName,remoteName,refs) {
    return repo.getRemote(remoteName || 'origin')
      .then(remote => {
        return remote.push(
          refs || [`refs/heads/${repoName || 'main'}:refs/heads/${repoName || 'main'}`],cloneOpts
        ).then(function() {
          console.log('success',arguments);
        });
      });
  }
}

module.exports = GitClient;

GithubClientRunner.js

var fs = require('fs');
var GitClient = require('./GithubClient.js');
var config = require('./github-config.js');

let repo;
console.log('starting clone');
return GitClient.clone({
    branch: config.branch,remote: config.remote,local: config.local
  }).then(r => {
    repo = r;
    let contents = new Date().getTime() + ' please ignore - will remove soon';
    console.log('got repo,writing contents:',contents);
    fs.writeFileSync(config.local + '/myTemporaryTestFile',contents);
    // add the file
    return GitClient.addAndCommit(repo,['myTemporaryTestFile'],'Test Commit not breaking anything');
  }).then(() => {
    console.log('commit done,pushing');
    return GitClient.push(repo,config.branch);
  })
  .then(() => {
    console.log('done');
    process.exit(0);
  })
  .catch(err => {
    console.error('uncaught!',err);
    process.exit(1);
  });

这是我自己的仓库的配置(工作):

const config = {
  branch: 'main',remote: 'https://___MY_GITHUB_TOKEN___:x-oauth-basic@github.com/MyGitHubUserName/myTestRepo.git',local: './.gitcache/myTestRepo.io',username: 'MyGitHubUserName',realname: 'My Name',email: 'me@email.com',token: '___MY_GITHUB_TOKEN___'
};
module.exports = config;

结果如下:

starting clone
got repo,writing contents: 1612971904692 please ignore - will remove soon
commit done,pushing
success [Arguments] { '0': undefined }
done

在这是组织存储库的配置(不工作):

const config = {
  branch: 'main',remote: 'https://___MY_GITHUB_TOKEN___:x-oauth-basic@github.com/theOrganisationIAmAMemberOf/someOtherRepo.git',local: './.gitcache/someOtherRepo.io',token: '___MY_GITHUB_TOKEN___'
};
module.exports = config;

现在的结果是:

starting clone
[Error: unexpected HTTP status code: 403] {
  errno: -1,errorFunction: 'Clone.clone'
}

如何使代码适用于两种类型的存储库?

谢谢!

解决方法

我自己发现了错误。代码按预期正常工作。 但是我忘了为组织启用我的令牌。一旦我这样做了,一切都很好。

专业提示:如果您遇到身份验证问题并且错误消息无助于尝试使用命令行 git 客户端实现相同的目标。错误消息会有更多信息。