Skip to content
Browse files

Closes #42: Use OAuth instead of Basic Auth. Fixes #15.

More Details:
 - jeresig#15
  • Loading branch information...
1 parent b9e2fdd commit 20e0a59609df8ec9dad830d2b38f8c2922dda1b0 @sindresorhus sindresorhus committed with Apr 16, 2012
Showing with 67 additions and 40 deletions.
  1. +1 −7 .gitignore
  2. +1 −4 config.json
  3. +4 −0 package.json
  4. +61 −29 pulley.js
View
8 .gitignore
@@ -1,7 +1 @@
-.project
-.settings
-.buildpath
-*~
-*.diff
-*.patch
-.DS_Store
+node_modules/
View
5 config.json
@@ -4,8 +4,5 @@
"repos": {
"jeresig/pulley": false
},
- "gitconfig": {
- "user": "",
- "password": ""
- }
+ "githubToken": ""
}
View
4 package.json
@@ -15,6 +15,10 @@
"type": "git",
"url": "git://github.com/jeresig/pulley.git"
},
+ "dependencies": {
+ "prompt": "~0.1.12",
+ "request": "~2.9.202"
+ },
"preferGlobal": "true",
"licenses": [{
"type": "MIT",
View
90 pulley.js
@@ -11,6 +11,8 @@
child = require( "child_process" ),
http = require( "https" ),
fs = require( "fs" ),
+ prompt = require("prompt"),
+ request = require("request"),
// Process references
exec = child.exec,
@@ -23,50 +25,79 @@
// Localized application references
user_repo = "",
tracker = "",
+ token = "",
// Initialize config file
- config = JSON.parse(fs.readFileSync( __dirname + "/config.json" ));
+ config = JSON.parse( fs.readFileSync( __dirname + "/config.json" ) );
- process.stdout.write( "Initializing... " );
+ // We don't want the default prompt message
+ prompt.message = "";
- // If the user or password is blank, check git config and fill them in from there
- if ( !config.gitconfig.user || !config.gitconfig.password) {
- exec( "git config --get-regexp github", function( error, stdout, stderr ) {
- config.gitconfig.user = config.gitconfig.user || (/github.user (.*)/.exec( stdout ) || [])[1];
- config.gitconfig.password = config.gitconfig.password || (/github.password (.*)/.exec( stdout ) || [])[1];
+ process.stdout.write( "Initializing... " );
+ exec( "git config --global --get pulley.token", function( error, stdout, stderr ) {
+ token = stdout;
+ if ( token ) {
init();
+ } else {
+ login();
+ }
+ });
+
+ function login() {
+ console.log("Please login with your GitHub credentials.");
+ console.log("Your credentials are only needed this one time to get a token from GitHub.");
+ prompt.start();
+ prompt.get([{
+ name: "username",
+ message: "Username",
+ empty: false
+ }, {
+ name: "password",
+ message: "Password",
+ empty: false,
+ hidden: true
+ }], function( err, result ) {
+ var auth = result.username + ":" + result.password;
+ request.post("https://" + auth + "@api.github.com/authorizations", {
+ json: true,
+ body: {
+ scopes: ["repo"],
+ note: "Pulley",
+ note_url: "https://github.com/jeresig/pulley"
+ }
+ }, function( err, res, body ) {
+ token = body.token;
+ if ( token ) {
+ exec( "git config --global --add pulley.token " + token, function( error, stdout, stderr ) {
+ console.log( "Success!".green );
+ init();
+ });
+ } else {
+ console.log( ( body.message + ". Try again." ).red );
+ login();
+ }
+ });
});
-
- } else {
- init();
}
function init() {
if ( !id ) {
exit( "No pull request ID specified, please provide one." );
}
+ exec( "git remote -v show " + config.remote, function( error, stdout, stderr ) {
+ user_repo = (/URL:.*?([\w\-]+\/[\w\-]+)/.exec( stdout ) || [])[1];
+ tracker = config.repos[ user_repo ];
- // If user and password are good, run init. Otherwise exit with a message
- if ( config.gitconfig.user && config.gitconfig.password ) {
-
- exec( "git remote -v show " + config.remote, function( error, stdout, stderr ) {
- user_repo = (/URL:.*?([\w\-]+\/[\w\-]+)/.exec( stdout ) || [])[1];
- tracker = config.repos[ user_repo ];
+ if ( user_repo ) {
+ tracker = tracker || "https://github.com/" + user_repo + "/issues/";
- if ( user_repo ) {
- tracker = tracker || "https://github.com/" + user_repo + "/issues/";
+ getStatus();
- getStatus();
-
- } else {
- exit( "External repository not found." );
- }
- });
-
- } else {
- exit( "Please specify a Github username and password:\ngit config --global github.user USERNAME\ngit config --global github.password PASSWORD" );
- }
+ } else {
+ exit( "External repository not found." );
+ }
+ });
}
function getStatus() {
@@ -241,14 +272,15 @@
});
}
+ // TODO: Add check to API call if autorization fails. Show login to reauthorize.
function callApi(options, callback, data) {
setTimeout(function(){
var req, datastring;
options.host = options.host || "api.github.com";
options.port = 443;
options.headers = {
- "Authorization": "Basic " + new Buffer(config.gitconfig.user + ":" + config.gitconfig.password).toString('base64'),
+ Authorization: "token " + token,
Host: "api.github.com"
};

0 comments on commit 20e0a59

Please sign in to comment.
Something went wrong with that request. Please try again.