-
-
Save goliatone/7b20a4066b27df7eed7fa0fe1153fd72 to your computer and use it in GitHub Desktop.
NodeGit Clone Private with Token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = "[email protected]: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; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const git = require('nodegit') | |
const fs = require('fs-extra') | |
const { URL } = require('url') | |
const REPO_URL = '[email protected]: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) | |
} | |
} | |
}, | |
}, | |
}) | |
})() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const repo = 'vital101/kernl-example-plugin-gitub'; | |
const token = 'my-token-from-oauth'; | |
const cloneURL = `https://${token}:[email protected]/${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