0

all,

I have a script I am building to find all open pull requests and compare the sha hash, however I can't seem to find them....

for repo in g.get_user().get_repos():
 print (repo.full_name)
 json_pulls = requests.get('https://api.github.com/repos/' + repo.full_name + '/pulls?state=open+updated=<' + str(cutoff_date.date())+ '&sort=created&order=asc')
 if (json_pulls.ok):
     for item in json_pulls.json():
         for c in item.items():
             #print(c["0"]["title"])
            #print (json.dumps(state))
            print(c)

The code cycles through the existing repos and list the pull requests and I get the output:

output

But, I can't for the life of me figure out how to collect individual fields...

I tried using the references:

  1. print(c['title']) - not defined is the error
  2. print(c['0']['title']) -a tubular error

what I am looking for is a simple list for each request....

title
id
state
base / sha
head / sha 

Can someone please point out what I am doing wrong in referencing the json items in my python script as it is driving me crazy.

The full code as is with your help of course ... :

# py -m pip install <module> to install the imported modules below.
#
#
# Import stuff
from github import Github
from datetime import datetime, timedelta
import requests
import json
import simplejson
#
#
#declare stuff
# set the past days to search in the range
PAST = 5

# get the cut off date for the repos 10 days ago
cutoff_date = datetime.now() - timedelta(days=PAST)
#print (cutoff_date.date())

# Repo oauth key for my repo
OAUTH_KEY = "(get from github personal keys)"

# set base URL for API query
BASE_URL = 'https://api.github.com/repos/'

#
#
# BEGIN STUFF

# First create a Github instance:
g = Github(login_or_token=OAUTH_KEY, per_page=100)

# get all repositories for my account that are open and updated in the last 
no. of days....
 for repo in g.get_user().get_repos():
 print (repo.full_name)
 json_pulls = requests.get('https://api.github.com/repos/' + repo.full_name 
 + '/pulls?state=open+updated=<' + str(cutoff_date.date())+ 
 '&sort=created&order=asc')
 if (json_pulls.ok):
    for item in json_pulls.json():
      print(item['title'], item['id'], item['state'], item['base']['sha'], 
  item['head']['sha'])

The repo site is a simple site, with two repos, and 1 or 2 pull requests to play against.

The idea of the script, when it is done, it to cycle through all the repos, find the pull requests that are older than x days and open, locates the sha for the branch (and sha for the master branches, to skip..... ) remove the branches that are not master branches, thus removing old code and pull requests to keep the repos tidy....

5
  • Could you post the logic before the for loop too (see stackoverflow.com/help/mcve) so that one can just copy paste the snipped and test it? Commented Apr 18, 2017 at 16:17
  • Dump the screen shot. Give us a testable couple of lines of example input. Commented Apr 18, 2017 at 16:17
  • Its just item["title"] , etc... Commented Apr 18, 2017 at 16:22
  • What is cutoff_date.date(). Why not just post a url we can test? Commented Apr 18, 2017 at 16:26
  • The idea of the script is to go through all the repos under the account, find all the pull requests that are open more than x days, grab the SHAs to idenifty the branch for the pull request and the master branch ID. Then remove the branches, skipping the master branch and cleaning up the repo. IE, no stale branches, no stale pull requests.... nice and tidy Commented Apr 19, 2017 at 8:09

2 Answers 2

2

json_pulls.json() returns a list of dictionaries, so you can just do:

 for item in json_pulls.json():
    print (item['title'], item['id'], item['state'], item['base']['sha'], item['head']['sha'])

There's no need to iterate over item.items().

Sign up to request clarification or add additional context in comments.

3 Comments

@tdelaney I checked the github documentation, it returns a JSON object, which should be converted to a Python dict.
Oops, missed a level.
Yes.... That was was it.. this worked nicely for me.. Thanks for the help!
0
for key, value in item.items():
    if (key == 'title'):
        print(value)
        #Do stuff with the title here

To add: Json should return as a dict, and the for command in python will default search for a key->val pair when called on dictionary.items().

1 Comment

That's an expensive way to do if 'title' in item:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.