簡體   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