Skip to content

Instantly share code, notes, and snippets.

@goliatone
Forked from vital101/nodegit.js
Last active December 14, 2020 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goliatone/7b20a4066b27df7eed7fa0fe1153fd72 to your computer and use it in GitHub Desktop.
Save goliatone/7b20a4066b27df7eed7fa0fe1153fd72 to your computer and use it in GitHub Desktop.
NodeGit Clone Private with Token
var url = "git@github.com:nodegit/test.git";
var opts = {
fetchOpts: {
callbacks: {
certificateCheck: () => 0,
credentials: function(url, userName) {
return NodeGit.Credential.sshKeyNew(
userName,
sshPublicKeyPath,
sshPrivateKeyPath,
"");
}
}
}
};
Clone(url, clonePath, opts).then(function(repo) {
assert.ok(repo instanceof Repository);
test.repository = repo;
});
const git = require('nodegit')
const fs = require('fs-extra')
const { URL } = require('url')
const REPO_URL = 'git@github.com:org/path.git'
const CLONE_DIR = '/tmp/private-repo-clone-test'
;(async () => {
await fs.emptyDir(CLONE_DIR)
let authAttempted = false
await git.Clone.clone(REPO_URL, CLONE_DIR, {
fetchOpts: {
callbacks: {
certificateCheck: () => 1,
credentials: (url, username) => {
if (authAttempted) return git.Cred.defaultNew()
authAttempted = true
if (url.startsWith('https://') && url.includes('@')) {
url = new URL(url)
return git.Cred.userpassPlaintextNew(url.username, url.password)
} else {
return git.Cred.sshKeyFromAgent(username)
}
}
},
},
})
})()
const repo = 'vital101/kernl-example-plugin-gitub';
const token = 'my-token-from-oauth';
const cloneURL = `https://${token}:x-oauth-basic@github.com/${repository}`;
const cloneOptions = {
fetchOpts: {
callbacks: {
certificateCheck: () => { return 1; },
credentials: () => {
return NodeGit.Cred.userpassPlaintextNew(token, 'x-oauth-basic');
}
}
};
const cloneRepository = NodeGit.Clone(cloneURL, '/path/to/clone/in/to', cloneOptions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment