4773: Disable prompting when stdin is not a TTY

charles-dyfis-net

We recently had a question on Stack Overflow from a person trying to pipe output from yes to rbt.

That question would be mooted if a batch mode existed, whether directly/explicitly enabled (--batch) or enabled via detecting stdin being attached to a non-TTY source (such as /dev/null).

See https://stackoverflow.com/questions/53822640/python-subprocess-shell-not-passing-yes-with-to-the-bash-script-im-executing#comment94494416_53822640

grantzukel
#1 grantzukel

Here is the output when the process hangs while running the update command. This doesn't happen when -u is not specified. This script basically runs when jenkins runs to automatically create PR review requests. not sure why its hanging if it completes.

cd neb-api && git checkout bc-NEB-4651-default-listing-type-action && yes | rbt post --repository=neb-api --tracking-branch="origin/bc-NEB-4651-default-listing-type-action" --parent="origin/feature-charting" --branch="bc-NEB-4651-default-listing-type-action" --guess-fields=auto --target-groups=PR_Reviewers --publish --server="http://hidden" --username="admin" --password="hidden" --submit-as="hidden" --bugs-closed="NEB-4040" --description="Adds API for default listing type get call NEB-4040 add getListingTypeByName and unit tests NEB-4040 remove static model methods and update unit tests NEB-4040 add API for default listing types NEB-4040 refactor unit tests and update swagger jira link https://hidden.atlassian.net/browse/NEB-4040" -u -d
Your branch is up to date with 'origin/bc-NEB-4651-default-listing-type-action'.
Already on 'bc-NEB-4651-default-listing-type-action'

RBTools 1.0.1
Python 2.7.14 |Anaconda, Inc.| (default, Dec 7 2017, 11:07:58)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
Running on Darwin-17.5.0-x86_64-i386-64bit
Home = /Users/grantzukel
Current directory = /Users/grantzukel/Projects/neb-scratch/neb-api
Command line: rbt post --repository=neb-api --tracking-branch=origin/bc-NEB-4651-default-listing-type-action --parent=origin/feature-charting --branch=bc-NEB-4651-default-listing-type-action --guess-fields=auto --target-groups=PR_Reviewers --publish --server=http://hidden --username=hidden --password=hidden --submit-as=hidden --bugs-closed=NEB-4040 "--description=Adds API for default listing type get call NEB-4040 add getListingTypeByName and unit tests NEB-4040 remove static model methods and update unit tests NEB-4040 add API for default listing types NEB-4040 refactor unit tests and update swagger jira link https://hidden.atlassian.net/browse/NEB-4040" -u -d
Running: tf vc help
Checking for a Subversion repository...
Running: svn --non-interactive info
Command exited with rc 1: [u'svn', u'--non-interactive', u'info']
Checking for a Git repository...
Running: git rev-parse --git-dir
Running: git config core.bare
Running: git rev-parse --show-toplevel
Running: git symbolic-ref -q HEAD
Running: git config --get remote.origin.url
Repository info: Path: https://@bitbucket.org/hidden/hidden.git, Base path: , Supports changesets: False
Checking for a Mercurial repository...
Unable to execute "hg --help": skipping Mercurial
Checking for a CVS repository...
Unable to execute "cvs": skipping CVS
Checking for a Perforce repository...
Unable to execute "p4 help": skipping Perforce
Checking for a Plastic repository...
Unable to execute "cm version": skipping Plastic
Checking for a ClearCase repository...
Unable to execute "cleartool help": skipping ClearCase
Checking for a Bazaar repository...
Unable to execute "bzr help": skipping Bazaar
Checking for a Team Foundation Server repository...
Unable to execute "tf help": skipping TFS
Making HTTP GET request to http://hidden/api/
Running: git rev-parse refs/heads/bc-NEB-4651-default-listing-type-action
Running: git rev-parse origin/feature-charting
Running: git rev-list 385d74957e61c071daf5b5ab0a44da493f702f8d --not --remotes=origin
Running: git status --porcelain --untracked-files=no --ignore-submodules=dirty
Running: git version
Running: git -c core.quotepath=false -c diff.noprefix=false diff --no-color --full-index --ignore-submodules -M --no-ext-diff 385d74957e61c071daf5b5ab0a44da493f702f8d..fe7c71f0d6f5316028d62c64bcda03bd48806bb8
Generated diff size: 8023 bytes
Making HTTP GET request to http://hidden/api/validation/diffs/
Cached response for HTTP GET http://hidden/api/validation/diffs/ expired and was modified
Making HTTP POST request to http://hidden/api/validation/diffs/
Making HTTP GET request to http://hidden/api/repositories/?only-links=&only-fields=id%2Cname%2Cmirror_path%2Cpath
Cached response for HTTP GET http://hidden/api/repositories/?only-links=&only-fields=id%2Cname%2Cmirror_path%2Cpath expired and was modified
Making HTTP GET request to http://hidden/api/review-requests/?status=pending&from-user=bcalisch&repository=1&only-fields=id%2Csummary%2Cdescription%2Cdraft%2Curl%2Cabsolute_url&only-links=draft&expand=draft&show-all-unpublished=True
Cached response for HTTP GET http://hidden/api/review-requests/?status=pending&from-user=hidden&repository=1&only-fields=id%2Csummary%2Cdescription%2Cdraft%2Curl%2Cabsolute_url&only-links=draft&expand=draft&show-all-unpublished=True expired and was modified
Running: git log --reverse --pretty=format:%s%n%n%b ^385d74957e61c071daf5b5ab0a44da493f702f8d fe7c71f0d6f5316028d62c64bcda03bd48806bb8
Update Review Request #67: "NEB-4651 add getListingTypeByName and unit tests"? [Yes/No]: >>> Making HTTP GET request to http://reviews.nebula.care/api/review-requests/67/?only-links=diffs%2Cdraft&only-fields=absolute_url%2Cbugs_closed%2Cid%2Cstatus%2Cpublic
Cached response for HTTP GET http://hidden/api/review-requests/67/?only-links=diffs%2Cdraft&only-fields=absolute_url%2Cbugs_closed%2Cid%2Cstatus%2Cpublic expired and was not modified
Making HTTP GET request to http://hidden/api/review-requests/67/diffs/?only-fields=
Cached response for HTTP GET http://hidden/api/review-requests/67/diffs/?only-fields= expired and was modified
Making HTTP POST request to http://hidden/api/review-requests/67/diffs/
Making HTTP GET request to http://hidden/api/review-requests/67/draft/?only-fields=commit_id
Cached response for HTTP GET http://hidden/api/review-requests/67/draft/?only-fields=commit_id expired and was modified
Making HTTP PUT request to http://hidden/api/review-requests/67/draft/
Review request #67 posted.

http://hidden/r/67/
http://hidden/r/67/diff/

!/usr/bin/env python

encoding=utf8

import sys
reload(sys)
sys.setdefaultencoding('utf8')
import subprocess
from subprocess import PIPE
import json
import re
import simplejson as json
from restkit import Resource, BasicAuth, request
import os
import commands

def run_command(command):

p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
if err:
    if "bash" in command:
        return err
    else:
        if "already been used" in str(err):
            return err
        else:
            print err
return out

def update_jira_ticket_with_comment(ticket_id, reviewboard_link):
server_url = "https://hidden.atlassian.net/rest/api/2/issue"
sub_url = ""
#server_url = os.environ["TICKET_URL"]
username = os.environ["TICKET_USER"]
password = os.environ["TICKET_PASSWORD"]
project = os.environ["TICKET_PROJECT_ID"]
desc = os.environ["TICKET_DESC"]
task_summary = os.environ["TICKET_SUMMARY"]

complete_url = server_url + "/" + ticket_id + "/comment"
headers = { "Content-Type": "application/json"}
comment_payload = {
    "body": "There is a review board review for this ticket and it can be found at the following URL: " + reviewboard_link
}
auth = BasicAuth(os.environ["BIT_BUCKET_USER"], os.environ["BIT_BUCKET_PASSWORD"])
resource = Resource(complete_url, filters=[auth])
response = resource.post(headers={'Content-Type': 'application/json'}, payload=json.dumps(comment_payload))
return response

def clone_repo(bit_bucket_user, bit_bucket_password, repo):
if os.path.exists("./" + repo):
print "Repo exists, we will erase it and clone a fresh copy."
command_output = run_command("rm -rf " + repo)
curl_command = ("git clone https://%s:%s@bitbucket.org/hidden/%s.git" % (bit_bucket_user, bit_bucket_password, repo))
json_returned = run_command(curl_command)
print "Repo Cloned"
else:
print "Repo doesn't exist locally to build the diff for the review requests, cloning repo: " + repo
curl_command = ("git clone https://%s:%s@bitbucket.org/hidden/%s.git" % (bit_bucket_user, bit_bucket_password, repo))
json_returned = run_command(curl_command)
print "Repo Cloned"

def get_bit_bucket_prs(repo, bit_bucket_user, bit_bucket_password):
curl_command = (
"curl https://api.bitbucket.org/2.0/repositories/hidden/%s/pullrequests -u %s:%s --request GET --header 'Content-Type: application/json'" % (
repo, bit_bucket_user, bit_bucket_password))
json_returned = run_command(curl_command)
json_returned = json_returned.encode('ascii', 'ignore').decode('ascii')
json_object = json.loads(str(json_returned))
return json_object

bit_bucket_user = os.environ["BIT_BUCKET_USER"]
bit_bucket_password = os.environ["BIT_BUCKET_PASSWORD"]
review_board_user = os.environ["REVIEW_BOARD_USER"]
review_board_password = os.environ["REVIEW_BOARD_PASSWORD"]
repo = os.environ["SELECT_REPO"]

print "Clone repo."
clone_repo(bit_bucket_user, bit_bucket_password, repo)
json_object = get_bit_bucket_prs(repo, bit_bucket_user, bit_bucket_password)

for obj in json_object["values"]:

origin_branch = obj["destination"]["branch"]["name"]
branch_being_merged = obj["source"]["branch"]["name"]
jira_code = obj["title"]
PR_ID = obj["id"]
desc = obj["description"].replace("\n", "").strip().replace("\r", "")
author_username = obj["author"]["username"]
p = re.compile("(NEB-\d{4})")
jira_code = p.findall(jira_code)[0]
jira_link = "https://hidden.atlassian.net/browse/" + jira_code
summary = obj["summary"]["raw"].replace("\n", "").strip().replace("\r", "") + " jira url " + jira_link
desc = desc + " jira link " + jira_link
server_reviews = "http://hidden"

try:
    rb_command = ("""cd %s && git checkout %s && rbt post --repository=%s --tracking-branch="origin/%s" --parent="origin/%s" --branch="%s" --guess-fields=auto --target-groups=%s --publish --server="%s" --username="%s" --password="%s" --submit-as="%s" --bugs-closed="%s" --description="%s" -d """ % (repo,branch_being_merged, repo, branch_being_merged, origin_branch, branch_being_merged, "PR_Reviewers", server_reviews, review_board_user, review_board_password, author_username, jira_code, desc))
    rb_command_output = run_command(rb_command).encode('utf-8', 'ignore').decode('utf-8')
    print rb_command_output

    if "commit ID specified has already been used" in rb_command_output:
        rb_command = ("""cd %s && git checkout %s && yes | rbt post --repository=%s --tracking-branch="origin/%s" --parent="origin/%s" --branch="%s" --guess-fields=auto --target-groups=%s --publish --server="%s" --username="%s" --password="%s" --submit-as="%s" --bugs-closed="%s" --description="%s" -u -d """ % (repo, branch_being_merged, repo, branch_being_merged, origin_branch, branch_being_merged,"PR_Reviewers", server_reviews, review_board_user, review_board_password, author_username, jira_code,desc))
        print rb_command
        #this is the command it hangs on
        rb_command_output = run_command(rb_command).encode('utf-8', 'ignore').decode('utf-8')
        if rb_command_output:
            print rb_command_output
        else:
            print "your review request has been updated."
except Exception as e:
    print e