<commit_before>#!/usr/bin/env python

"""
pr_parser.py: SCRIPTS that analyse pr information and write parsed pr information
(especially the multi-relate-pr) to post-build proprerties for downstream job.
"""


import argparse
import sys
import os
import github

from github import Github

class PrParser(object):

    def __init__(self):
        """
        __repo - RackHD/on-xxxx, The repo name of which repo triggers the build 
        __target_branch - master etc., The target branch associated with pr
        __merge_commit_sha - origin/pr/1/merge, The merge_commit_sha associated with the pr
        __pull_id - number in string, the pull id associated with the pr
        __actual_commit - sha1 code, the actual commit code associated with the pr
        """
        self.__repo = None
        self.__target_branch = None
        self.__merge_commit_sha = None
        self.__pull_id = None
        self.__actual_commit = None

    def parse_args(self, args):
        """
        Take in values from the user.
        Repo, branch, merge_commit_sha and pull_id are required. This exits if they are not given.
        :return: Parsed args for assignment
        """
        parser = argparse.ArgumentParser()
        parser.add_argument("--repo",
                            help="Git repo of the triggered pr",
                            action="store")
        parser.add_argument("--target_branch",
                            help="The target branch of the pr for the named repo",
                            action="store")
        parser.add_argument("--actual_commit",
                            help="The actual commit of the pr for the named repo",
                            action="store")
        parser.add_argument("--merge_commit_sha",
                            help="The merge_commit_sha of target the pr",
                            action="store")
        parser.add_argument("--pull_id",
                            help="The pull id of target the pr",
                            action="store")
        parsed_args = parser.parse_args(args)
        return parsed_args

    def assign_args(self, args):
        """
        Assign args to member variables.
        :param args: Parsed args from the user
        :return:
        """
        if args.repo:
            self.__repo = args.repo.lower()
        else:
            print "\nMust specify repository url\n"
            sys.exit(1)
        
        if args.target_branch:
            self.__target_branch = args.target_branch
        else:
            print "\nMust specify a branch name\n"
            sys.exit(1)
        
        if args.actual_commit:
            self.__actual_commit = args.actual_commit
        else:
            print "\nMust specify an actual_commit\n"
            sys.exit(1)

        if args.merge_commit_sha:
            self.__merge_commit_sha = args.merge_commit_sha
        else:
            print "\nMust specify a commit-id\n"
            sys.exit(1)

        if args.pull_id:
            self.__pull_id = args.pull_id
        else:
            print "\nMust specify a pull-id\n"
            sys.exit(1)
        
        
    def parse_pr(self, base_repo, base_pull_id):
        """
        get related prs according to the base pr
        :param repo: string, repo name associated with the pr to be parsed
        :param pull_id: number in string, pull request id of the pr to be parsed
        :return related_prs: list of tuple of string: [(repo, sha, pull_id, commit),...] 
        pr list which is the associated with base pr
        """

        #init github  and get related pr object
        HOME = os.environ['HOME']
        with open('{0}/.ghtoken'.format(HOME), 'r') as file:
            TOKEN = file.read().strip('\n')
        gh = Github(TOKEN)
        pr = gh.get_repo(base_repo).get_pull(long(base_pull_id))
        
        #get all comments and description in the pr
        pr_texts = []
        pr_texts.append(pr.body)
        for pr_comment in pr.get_issue_comments():
            pr_texts.append(pr_comment.body)
        
        # pre processing
        pr_text_segment = []
        for pr_text in pr_texts:
            pr_text = pr_text.lower()
            partition = pr_text.split('jenkins')
            if len(partition) <= 1:
                continue
            for segment in partition[1:]:
                pr_text_segment.append("jenkins"+segment)

        #parse pr
        related_prs = []
        for pr_text in pr_text_segment:
            pr_words = pr_text.replace(':', '').replace(',', ' ').split()

            #find keyword "jenkins"
            if 'jenkins' not in pr_words:
                continue
            position = pr_words.index('jenkins')

            #analyse dependency relationship, "depend" or "ignore" 
            if ('ignore' not in pr_words[position+1]) and ('depend' not in pr_words[position+1]):
                continue
            
            #find "ignore"
            if 'ignore' in pr_words[position+1]:
                related_prs = None
                print "INFO: \"Jenkins: ignore\" in repo: {0} pull_id: {1}".format(base_repo, base_pull_id)
                break<commit_msg>Added check for incorrect Jenkins syntax. If Jenkins is the last word, continue<commit_after>#!/usr/bin/env python

"""
pr_parser.py: SCRIPTS that analyse pr information and write parsed pr information
(especially the multi-relate-pr) to post-build proprerties for downstream job.
"""


import argparse
import sys
import os
import github

from github import Github

class PrParser(object):

    def __init__(self):
        """
        __repo - RackHD/on-xxxx, The repo name of which repo triggers the build 
        __target_branch - master etc., The target branch associated with pr
        __merge_commit_sha - origin/pr/1/merge, The merge_commit_sha associated with the pr
        __pull_id - number in string, the pull id associated with the pr
        __actual_commit - sha1 code, the actual commit code associated with the pr
        """
        self.__repo = None
        self.__target_branch = None
        self.__merge_commit_sha = None
        self.__pull_id = None
        self.__actual_commit = None

    def parse_args(self, args):
        """
        Take in values from the user.
        Repo, branch, merge_commit_sha and pull_id are required. This exits if they are not given.
        :return: Parsed args for assignment
        """
        parser = argparse.ArgumentParser()
        parser.add_argument("--repo",
                            help="Git repo of the triggered pr",
                            action="store")
        parser.add_argument("--target_branch",
                            help="The target branch of the pr for the named repo",
                            action="store")
        parser.add_argument("--actual_commit",
                            help="The actual commit of the pr for the named repo",
                            action="store")
        parser.add_argument("--merge_commit_sha",
                            help="The merge_commit_sha of target the pr",
                            action="store")
        parser.add_argument("--pull_id",
                            help="The pull id of target the pr",
                            action="store")
        parsed_args = parser.parse_args(args)
        return parsed_args

    def assign_args(self, args):
        """
        Assign args to member variables.
        :param args: Parsed args from the user
        :return:
        """
        if args.repo:
            self.__repo = args.repo.lower()
        else:
            print "\nMust specify repository url\n"
            sys.exit(1)
        
        if args.target_branch:
            self.__target_branch = args.target_branch
        else:
            print "\nMust specify a branch name\n"
            sys.exit(1)
        
        if args.actual_commit:
            self.__actual_commit = args.actual_commit
        else:
            print "\nMust specify an actual_commit\n"
            sys.exit(1)

        if args.merge_commit_sha:
            self.__merge_commit_sha = args.merge_commit_sha
        else:
            print "\nMust specify a commit-id\n"
            sys.exit(1)

        if args.pull_id:
            self.__pull_id = args.pull_id
        else:
            print "\nMust specify a pull-id\n"
            sys.exit(1)
        
        
    def parse_pr(self, base_repo, base_pull_id):
        """
        get related prs according to the base pr
        :param repo: string, repo name associated with the pr to be parsed
        :param pull_id: number in string, pull request id of the pr to be parsed
        :return related_prs: list of tuple of string: [(repo, sha, pull_id, commit),...] 
        pr list which is the associated with base pr
        """

        #init github  and get related pr object
        HOME = os.environ['HOME']
        with open('{0}/.ghtoken'.format(HOME), 'r') as file:
            TOKEN = file.read().strip('\n')
        gh = Github(TOKEN)
        pr = gh.get_repo(base_repo).get_pull(long(base_pull_id))
        
        #get all comments and description in the pr
        pr_texts = []
        pr_texts.append(pr.body)
        for pr_comment in pr.get_issue_comments():
            pr_texts.append(pr_comment.body)
        
        # pre processing
        pr_text_segment = []
        for pr_text in pr_texts:
            pr_text = pr_text.lower()
            partition = pr_text.split('jenkins')
            if len(partition) <= 1:
                continue
            for segment in partition[1:]:
                pr_text_segment.append("jenkins"+segment)

        #parse pr
        related_prs = []
        for pr_text in pr_text_segment:
            pr_words = pr_text.replace(':', '').replace(',', ' ').split()

            #find keyword "jenkins"
            if 'jenkins' not in pr_words:
                continue
            position = pr_words.index('jenkins')

            #Checks to make sure jenkins is not the last element in pr_words
            if ((position+1) == len(pr_words)) :
                continue

            #analyse dependency relationship, "depend" or "ignore" 
            if ('ignore' not in pr_words[position+1]) and ('depend' not in pr_words[position+1]):
                continue
            
            #find "ignore"
            if 'ignore' in pr_words[position+1]:
                related_prs = None
                print "INFO: \"Jenkins: ignore\" in repo: {0} pull_id: {1}".format(base_repo, base_pull_id)
                break
{'fraction_non_alphanumeric': 0.08728203184230478, 'fraction_numerical': 0.0029378316906747536, 'mean_word_length': 1.7107629077832007, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4757032', 'n_tokens_mistral': 2894, 'n_tokens_neox': 2819, 'n_words': 875}