You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ni...@apache.org on 2015/07/07 02:57:11 UTC

cordova-coho git commit: Add support for launching URL to create a PR. This closes #88

Repository: cordova-coho
Updated Branches:
  refs/heads/master c26f62080 -> b9528350d


Add support for launching URL to create a PR. This closes #88


Project: http://git-wip-us.apache.org/repos/asf/cordova-coho/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-coho/commit/b9528350
Tree: http://git-wip-us.apache.org/repos/asf/cordova-coho/tree/b9528350
Diff: http://git-wip-us.apache.org/repos/asf/cordova-coho/diff/b9528350

Branch: refs/heads/master
Commit: b9528350db041e50b961aba7f411c7fb13241fae
Parents: c26f620
Author: Nikhil Khandelwal <ni...@microsoft.com>
Authored: Fri Jun 26 10:25:16 2015 -0700
Committer: Nikhil Khandelwal <ni...@microsoft.com>
Committed: Mon Jul 6 17:56:06 2015 -0700

----------------------------------------------------------------------
 package.json     |   1 +
 src/create-pr.js | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/main.js      |   6 +++
 3 files changed, 119 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-coho/blob/b9528350/package.json
----------------------------------------------------------------------
diff --git a/package.json b/package.json
index b797f84..ef9988a 100644
--- a/package.json
+++ b/package.json
@@ -11,6 +11,7 @@
     "co": "~3.0",
     "gnode": "^0.1.0",
     "nlf": "1.1.0",
+    "opener": "^1.4.1",
     "optimist": "0.4",
     "q": "~0.9",
     "request": "2.55.0",

http://git-wip-us.apache.org/repos/asf/cordova-coho/blob/b9528350/src/create-pr.js
----------------------------------------------------------------------
diff --git a/src/create-pr.js b/src/create-pr.js
new file mode 100644
index 0000000..4812172
--- /dev/null
+++ b/src/create-pr.js
@@ -0,0 +1,112 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+var flagutil = require('./flagutil');
+var optimist = require('optimist');
+var executil = require('./executil');
+var gitutil = require('./gitutil');
+var repoutil = require('./repoutil');
+var REMOTE = 'https://github.com/apache/';
+var apputil = require('./apputil');
+var url = require('url');
+var opener = require('opener');
+
+module.exports = function *(argv) {
+    var opt = flagutil.registerHelpFlag(optimist);
+    opt.options('branch', {
+            desc: 'Topic branch for which to create pull request (Default: current branch) ',
+            demand: false
+        });
+    argv = opt
+        .usage('Launch github URL to create PR\n' +
+        '\n' +
+        'Usage: $0 create-pr --branch <topic_branch>')
+        .argv;
+   if (argv.h) {
+        optimist.showHelp();
+        process.exit(1);
+   }
+   var currentRepo = repoutil.getRepoById(repoutil.resolveCwdRepo());
+   var currentBranch = opt.branch;
+   if (!currentBranch) {
+       currentBranch = yield gitutil.retrieveCurrentBranchName();
+   }
+   if (currentBranch == 'master') {
+       console.log('You can crate a PR only for a topic branch that is not master. Use --branch to specify the topic branch or checkout to the topic branch.');
+   }
+   var remoteInfo = yield getRemoteName(currentBranch);
+   var remoteFork = yield getRemoteForkName(remoteInfo.remoteName);
+   var url = REMOTE + currentRepo.repoName + '/compare/master...'  + remoteFork + ':' + remoteInfo.remoteBranch + '?expand=1';
+   console.log("Navigating to: " + url);
+   opener(url);
+}
+
+function* getRemoteForkName(remoteName) {
+    var remotes = (yield executil.execHelper(executil.ARGS('git remote -v'), /*silent*/ true)).split('\n');
+    var remoteUrl;
+    for (var i = 0; i < remotes.length; i++)  {
+       //fork    https://github.com/forkName/cordova-coho.git (push)
+       var tokens = remotes[i].split(/\s+/);
+       if (tokens[2] === '(push)' && tokens[0] === remoteName) {
+           remoteUrl = tokens[1];
+           break;
+       }
+    }
+    if (!remoteUrl) {
+         apputil.fatal('Cannot find remote Url: ' + remotes);
+    }
+    var parsed = url.parse(remoteUrl);
+    // parsed => /forkName/cordova-coho.git
+    var forkName = (parsed.pathname.split('/'))[1];
+    return forkName;
+}
+
+function* getRemoteName(currentBranch) {
+    var branches = (yield executil.execHelper(executil.ARGS('git branch -vv'), /*silent*/ true)).split('\n');
+    //* create-pr           3bed9b5 [remotes/fork/create-pr] Add support for launching URL to create a PR
+    for (var i = 0; i < branches.length; i++)  {
+        //* create-pr           3bed9b5 [remotes/fork/create-pr] Add support for launching URL to create a PR
+        //0   1                    2       3
+        var tokens = branches[i].split(/\s+/);
+        if (tokens[0] == '*') {
+            // found the current branch
+            if(currentBranch !== tokens[1]) {
+                apputil.fatal('Unexpected format. Cannot find remote branch: ' + tokens[1] + '!== ' + currentBranch);
+            }
+            // if there is no upstream remote specified - we have no choice but to bail
+            var remote = tokens[3];
+            if(remote.indexOf('[') !== 0) {
+                apputil.fatal('Cannot determine upstream remote branch. Have you already pushed it? \n' + 
+                    'To push and set upstream: git push -u <remoteFork> ' + currentBranch + '\n' +
+                    'To set upstream branch:   git branch --set-upstream <remoteFork>');
+            }
+            // Strip off the []
+            remote = remote.substring(1, remote.length -1);
+            tokens = remote.split('/');
+            var remoteName = tokens[0];
+            var remoteBranch = tokens[1];
+            if (remoteName === 'remotes') {
+                remoteName = tokens[1];
+                remoteBranch = tokens[2];
+            }
+            return { remoteName : remoteName, remoteBranch: remoteBranch};
+        }
+    }
+    apputil.fatal('Unexpected error. Cannot determine remote: ' + branches);
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cordova-coho/blob/b9528350/src/main.js
----------------------------------------------------------------------
diff --git a/src/main.js b/src/main.js
index c90009a..3e4b4ba 100644
--- a/src/main.js
+++ b/src/main.js
@@ -133,6 +133,12 @@ module.exports = function() {
             entryPoint: lazyRequire('./merge-pr'),
             noChdir : true
         }, {
+            name: 'create-pr',
+            desc: 'Launches github PR UI for the specified topic branch',
+            entryPoint: lazyRequire('./create-pr'),
+            noChdir : true
+        }, 
+        {
             name: 'last-week',
             desc: 'Prints out git logs of things that happened last week.',
             entryPoint: lazyRequire('./last-week')


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org