繁体   English   中英

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

[英](nodegit) How to read/write from/to private Github repo using a token

我从网上复制了一些代码,并让它克隆并推送到我自己在 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'
}

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

谢谢!

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

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

暂无
暂无

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

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