Hello, been looking around on the google group and general google searches 
to highlight what I'm doing wrong but I can't seem to find anything. I'm 
trying to write a python script that loops open PR's on bitbucket and 
creates review requests based on the open PR's with their JIRA link int he 
summary. I am however getting some issues and even -d option isn't 
returning anything I can use to diagnose the issue. This was working last 
week. I'm running this script on PR generation from Jenkins and it auto 
creates my requests. I started getting the following error:


example bash command that is getting generated to run the script I generate 
with the python script.

bash review 'mjb-neb-4110-dirty-note' 'PR_Reviewers' 'myrepo' 'myuser' 
'mypass' 'post' 'development' 'openingPRUser/reviewBuser' '<ul><li>The 
front end uses null as the basis for dirty note. Move the logic of 
normalizing blank and empty appointment note values to null from the 
appointment converter to the sequelize model itself, so all api calls will 
consistently return null if the note does not have content. Added 
integration tests for this.</li><li>Fixed description; refactor test a 
bit</li></ul><p>QAd by Kyle on local branch.</p> jira url 
https://ourjira.atlassian.net/browse/NEB-4110' 'NEB-4110'

CRITICAL: Too many revisions specified

#!/usr/bin/env python
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import subprocess
import json
import re
import simplejson as json
from restkit import Resource, BasicAuth, request
import os

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:
            print err
    return out

def update_jira_ticket_with_comment(ticket_id, reviewboard_link):
    server_url = "https://ourjira.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:%[email protected]/ourteam/%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:%[email protected]/ourteam/%s.git"; % (bit_bucket_user, 
bit_bucket_password, repo))
        json_returned = run_command(curl_command)
        print "Repo Cloned"

def generate_bash_file():
    review_generation_bash = """#!/usr/bin/env bash
    set +ex

    BRANCH_NAME=$1
    REVIEWERS=$2
    REPO=$3
    rb_user=$4
    rb_password=$5
    action=$6
    base_branch=$7
    
    #added these options
    submit_as=$8
    summary=$9
    jira_id=$10


    cd $REPO
    git checkout $BRANCH_NAME

    if [ "$action" == "post" ]; then

        rbt post --repository=$REPO \
                       --tracking-branch=origin/$BRANCH_NAME \
                       --parent=origin/$base_branch \
                       --branch=$BRANCH_NAME \
                       --guess-fields=auto \
                       --target-groups=$REVIEWERS \
                       --publish \
                       --server=http://reviews.nebula.care \
                       --username=$rb_user \
                       --password=$rb_password \
                       --submit-as=$submit_as \
                       --bugs-closed=$jira_id \
                       --summary=$summary \
                       -d
    else
        rbt post --repository=$REPO \
                       --tracking-branch=origin/$BRANCH_NAME \
                       --parent=origin/$base_branch \
                       --branch=$BRANCH_NAME \
                       --guess-fields=auto \
                       --target-groups=$REVIEWERS \
                       --publish \
                       --server=http://reviews.nebula.care \
                       --username=$rb_user \
                       --password=$rb_password \
                       --submit-as=$submit_as \ 
                       --summary=$summary \
                       --bugs-closed=$jira_id \
                       -u \ 
                       -d
    fi

    cd ../
    exit 0
    """

    write_bash_file = open("review", "w")
    write_bash_file.write(review_generation_bash)
    write_bash_file.close()
    return "successfully created review board commands bash file for execution."

def get_bit_bucket_prs(repo, bit_bucket_user, bit_bucket_password):
    curl_command = (
                "curl 
https://api.bitbucket.org/2.0/repositories/ourteam/%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


print "Load the environment variables required to run the job."
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)
print "Create Reviewboard command bash file."
generate_bash_file()

json_object = get_bit_bucket_prs(repo, bit_bucket_user, bit_bucket_password)

for obj in json_object["values"]:
    reviewers = "PR_Reviewers"
    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()
    author_username = obj["author"]["username"]
    p = re.compile("(NEB-\d{4})")
    jira_code = p.findall(jira_code)[0]
    jira_link = "https://ourjira.atlassian.net/browse/"; + jira_code
    summary = obj["summary"]["html"].replace("\n", "").strip() + " jira url " + 
jira_link

    try:
        print "try and create review request for " + branch_being_merged + " 
and " + origin_branch

        rb_command = ("bash review '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' 
'%s'" % (branch_being_merged, reviewers, repo, review_board_user, 
review_board_password, "post", origin_branch, author_username, summary, 
jira_code))
        rb_command_output = run_command(rb_command).encode('utf-8', 
'ignore').decode('utf-8')
        #print rb_command_output

        if "There don't seem to be any diffs!" in rb_command_output:
            print "There is most likely no pr between branch " + 
branch_being_merged + " and " + origin_branch

        if "commit ID specified has already been used" in rb_command_output:
            print "There seems to be a review for branch " + 
branch_being_merged + " and " + origin_branch + " , lets try to update it."
            rb_command = ("bash review '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' 
'%s' '%s'" % (branch_being_merged, reviewers, repo, review_board_user, 
review_board_password, "update", origin_branch,author_username, summary, 
jira_code))
            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






-- 
Supercharge your Review Board with Power Pack: 
https://www.reviewboard.org/powerpack/
Want us to host Review Board for you? Check out RBCommons: 
https://rbcommons.com/
Happy user? Let us know! https://www.reviewboard.org/users/
--- 
You received this message because you are subscribed to the Google Groups 
"Review Board Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to