Package: git-change-request
git-change-requesta tool for working with pull/merge requests in a CI context.BackgroundThis tool came about because our Jenkins CI was behind a firewall so we couldn't
use the Jenkins Github plugin out of the box nor did my team want the overhead
of managing asmee.ioclient. Couple that with the fact that in this context the
closest solution was to setup our dynamic Jenkins agent with the github cli to
pull down the PR for testing but the github client does not handle updating a
PR's commit status to report the outcome of the downstream statusSo with those factors in mind this tool was born with the following goals:Be able to checkout a PR locally and update it's status from one tool rather than multiple toolsBe able to support the above PR workflow in any CI system context without needing CI system plugins/dependenciesBe able to support the above PR workflow with any remote repo hosting app like GitLab, Bitbucket, etc.Installationgit-change-requestcan be easily installed with a one line command. It is recommended (as best practice) to create a virtual
environment and install ospclientsdk. Please see the commands below to install. Note this is only supported
in python 3.6 and higher.# install python virtualenv
$ pip install python-virtualenv

# create virtualenv
$ virtualenv cr

# activate virtualenv
$ source cr/bin/activate

# install git-change-request
$ (cr) pip install git-change-requestUsageLet's dive into how to use this the command line tool.SetupYou will need to setup two things to use this tool, a repo url and a personal auth token.For authorization, git-change-request supports thegh clienvironment variablesGH_TOKEN || GITHUB_TOKENFor the repo url git-change-request supports the following options# you can set the following environment variable

$ export GIT_CR_REPO=<repo url from borwser>

# or you can ovverride the variable and pass it on the command line with

git-cr --repo-url "<repo url from the browser>"Command ListOnce you've setup the authentication token and repo url. You can begin running commandslistYou can list PRs on your repo$ git-cr list [--state <state>]example$ git-cr list
[
    {
        "number": 41,
        "title": "Create umb.txt",
        "state": "open"
    }
]state- can be the supported GitHub states ofopen,closed,all. By default if state is not specified
it will list open PRs.statusYou can select to see the status of a PR or update the status of a PR.status getIt will get the full status of a PR, if check-suites are configured for your project the status will
be included along with the combined commit status$ git-cr status get [--number <PR Number>]example$ git-cr status get --number 41
  {
      "checks": {
          "conclusion": "success",
          "status": "completed"
      },
      "status": {
          "state": "pending",
          "statuses": [
              {
                  "state": "success",
                  "context": "\"jenkins downstream tests\"",
                  "description": "\"pytest completed successfully\""
              },
              {
                  "state": "pending",
                  "context": "jenkins downstream tests",
                  "description": "e2e-test running"
              }
          ]
      }
  }number- The PR number to query.status setIt will set the status of a PR with state and relevant information while optionally assign
reviewers to the PR$ git-cr status set [--number <PR Number>] 
                    [--state <state>] 
                    [--context <context>]
                    [--description <description>]
                    [--target-url <url>]
                    [--reviewer <reviewer>, --team <team>]example$ git-cr status set --number 41 --state success --context "jenkins downstream tests" --description "pytest completed successfully" --team "test-team"number- The PR number to update status on.state- The status of the commit, can be any supported GitHub optionssuccess,error,failure,pendingcontext- A label to differentiate the statusdescription- A short description of the statustarget-url- The target URL to associate with this status. This URL will be linked from the GitHub UI to allow users
to easily see the source of the status.reviewer- A reviewer to assign to the the PR once the status has been updatedteam- A project team to assign to the PR once the status has been updatedcheckoutClone the repo and checkout the PR locally.It will first attempt to clone the repo to the current working directory. It is the
equivalent to runninggit clone <repo_url>. If it is determined that repo already exists it
will skip the cloning process, proceed to fetch the PR, create a local branch with a schema
ofpull-<number>, and checkout the branch.$ git-cr checkout [--number <number>]example$ ls -lrta
drwxrwxr-x. 29 user user  4096 May  5 12:12 ..
drwxrwxr-x. 29 user user  4096 May  5 12:12 .

$ git-cr checkout --number 41

$ ls -lrta && cd piqe-ocp-lib && git branch --list
drwxrwxr-x. 29 user user  4096 May  5 12:12 ..
drwxrwxr-x. 29 user user  4096 May  5 12:12 .
drwxrwxr-x. 29 user user  4096 May  5 12:12 piqe_ocp_lib
 master
* pull-41number- The PR number to checkoutviewView a summary of information about the PR.$ git-cr view [--number <number>]example$ git-cr view --number 36
{
    "number": 36,
    "title": "[CSSWA-474] - Library requirements",
    "state": "closed",
    "sha": "bcc84d8a0c1c23b2b2a6d0ef6163a968fe3b3150",
    "merged": true,
    "reviews": [
        {
            "user": "Dannyb48",
            "state": "COMMENTED"
        },
        {
            "user": "gmcrocetti",
            "state": "COMMENTED"
        },
        {
            "user": "btjd",
            "state": "COMMENTED"
        },
        {
            "user": "Dannyb48",
            "state": "COMMENTED"
        },
        {
            "user": "Dannyb48",
            "state": "APPROVED",
            "body": "LGTM"
        },
        {
            "user": "btjd",
            "state": "APPROVED"
        }
    ],
    "status_and_checks": {
        "checks": {
            "conclusion": "success",
            "status": "completed"
        },
        "status": {
            "state": "pending",
            "statuses": []
        }
    }
}number- The PR number to view information on.Developing PluginsIf you're interested in extending support for other backend repo managers like GitLab, Bitbucket
or want to write your own GitHub implementation you can do so easily but still leverage
the same interface, you can do so easilyInstall git-change-requestsCreate a python module and/or file where from git-change-request you importBaseRequestTo enable logging you can create a logger using thecreate_loggerfunction or calling python'sgetLoggerCreate a setup.py file so that it can register the plugin where git-change-request can find it.
Register togit_cr_plugins. Please referherefor more information on entry pointsFinally, export the environment variableGIT_CR_PLUGIN_NAMEto the plugin class name. This will
cause theChangeRequestinterface to inject the new plugin rather than the default.Below is an example of the processfromgit_change_requestimportBaseRequestfromlogginimportgetLoggerclassNewPlugin(BaseRequest):def__init__(self,repo_url):# creating logger for this plugin to get added to git-change-request's loggersself.create_logger(name='newplugin',data_folder=<datafoldername>)# ORlogger=getLogger('git_change_request')self.repo_url=repo_urlself.cr=None# init some client using the repo_urlself.client=SomeClient(repo_url)defwork_on(self,number):self.cr=numberdefget_status(self):# Your code to get statusself.client.get_status(self,self.cr)defsuccess(self,**kwargs):# when implementing state for status update, set them as first class functions.# for example this success function will be called when a client wants to set# the state to 'success' on the commit statusself.client.create_status(**kwargs)fromsetuptoolsimportsetup,find_packagessetup(name='new_plugin',version="1.0",description="new plugin for git-change-request",author="John Doe",packages=find_packages(),include_package_data=True,python_requires=">=3",install_requires=['git-change-request',],entry_points={'git_cr_plugins':'new_plugin = <plugin package name>:<NewPluginClass>'})export GIT_CR_PLUGIN_NAME = NewPlugin