0

我从网上复制了一些代码,让它可以克隆并推送到我自己在 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),
      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',
  username: 'MyGitHubUserName',
  realname: 'My Name',
  email: 'me@email.com',
  token: '___MY_GITHUB_TOKEN___'
};
module.exports = config;

现在的结果是:

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

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

谢谢!

4

1 回答 1

0

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

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

于 2021-02-10T20:44:50.513 回答