Python:字符串索引必须是整数

时间:2015-08-04 00:58:15

标签: python python-2.7

我正在研究一些Python代码来自动化github合并请求。

我在下面找到了以下代码。当我运行它时,我得到TypeError:字符串索引必须是整数。

我在这里找到了几个线程来解决这个错误,但是我不确定如何在代码中实现修复。

#!/usr/bin/env python
import json
import requests
import datetime


OAUTH_KEY = "xxxxxxxxxxxx"
repos = ['my_app'] # Add all repo's you want to automerged here
ignore_branches = ['master', 'release', 'staging', 'development'] # Add 'master' here if you don't want to automerge into master


# Print merge/no-merge message to logfile
def print_message(merging):
  if merging == True:
   message = "Merging: "
 else:
  message = "Not merging: "
 print message + str(pr_id) + " - " + user + " wants to merge " + head_ref + " into " + base_ref


# Merge the actual pull request
def merge_pr():
  r = requests.put("https://api.github.com/repos/:owner/%s/pulls/%d/merge"%(repo,pr_id,),
   data=json.dumps({"commit_message": "Auto_Merge"}),
   auth=('token', OAUTH_KEY))
  if "merged" in r.json() and r.json()["merged"]==True:
   print "Merged: " + r.json()['sha']
  else:
   print "Failed: " + r.json()['message']


# Main

print datetime.datetime.now()

for repo in repos:
  r = requests.get('https://api.github.com/repos/:owner/%s/pulls' % repo, auth=('token', OAUTH_KEY))
  data = r.json()

for i in data:
  head_ref=i["head"]["ref"]
  base_ref=i["base"]["ref"]
  user=i["user"]["login"]
  pr_id = i["number"]
  if base_ref in ignore_branches:
   print_message(False)
  else:
   print_message(True)
   merge_pr()

1 个答案:

答案 0 :(得分:0)

哪一行代码显示有问题? 如果是这一行:



'else:
  message = "Not merging: "
 print message + str(pr_id) + " - " + user + " wants to merge " + head_ref + " into " + base_ref'




然后尝试将此代码放在

下面



if merging == True:
  message = "Merging: "




elif message == False:
  message = "Not merging: "
print message + pr_id + " - " + user + " wants to merge " + head_ref + " into " + base_ref ''