2016-09-19 53 views
0

我想循环遍历GitHub中的pull请求,并且如果pull请求在下面的代码中有注释,请执行一些操作(现在打印请求数)。我有一个包含我正在查找的评论(分散在拉请求中的多个注释)的拉请求,但它不打印拉请求数。我怀疑它与我正在使用的正则表达式有关,因为如果我打破if语句来查找正则表达式或字符串值,它可以正常工作,但是当我尝试将它们合并到一个if语句中时,它不起作用。如果语句使用正则表达式有多个条件

我不认为这是一个重复的问题,因为我查看了可能已经有答案的问题下的所有建议。

for pr in repo.pull_requests(): 
     #check to ensure pull request meets criteria before processing 
     conflicts_base_branch = "This branch has no conflicts with the base branch" 
     checks = "All checks have passed" 
     sign_off_comment_present = re.compile(r"\B#sign-off\b", re.IGNORECASE) 
     for comment in list(repo.issue(pr.number).comments()): 
      if (conflicts_base_branch and checks in comment.body) and (sign_off_comment_present.search(comment.body)): 
       print(pr.number) 
+0

所有这些都出现在_one_评论或不同评论中吗?你的版本要求他们都在同一评论中。同样'conflict_base_branch和在comment.body'中的检查与'(conflicts_base_branch)和(在in_comment.body中检查)'或'(True)和(在checks.body中检查)'相同,所以它只检查是否存在后者。 – mata

+0

这些出现在拉取请求中的多个注释中。这是否意味着它应该是'如果comments_body中有冲突_base_branch并且在comment.body'中检查?如果是这样,我该如何将它与正则表达式结合起来? – DBS

+0

您需要在迭代时分别检查它们。检查我的答案 – mata

回答

1

您的解决方案要求的所有条件,要在同一评论满足,如果他们在不同的意见将无法正常工作。为此,您需要跟踪在反复评论时满足哪些条件,例如:

for pr in repo.pull_requests(): 
    #check to ensure pull request meets criteria before processing 
    conflicts_base_branch = "This branch has no conflicts with the base branch" 
    checks = "All checks have passed" 
    sign_off_comment_present = re.compile(r"\B#sign-off\b", re.IGNORECASE) 
    passes_checks = False 
    has_signoff = False 
    has_no_conflicts = False 
    for comment in list(repo.issue(pr.number).comments()): 
     if checks in comment.body: 
      passes_checks = True 
     if conflicts_base_branch in comment.body: 
      has_no_conflicts = True 
     if sign_off_comment_present.search(comment.body): 
      has_signoff = True 
    if passes_checks and has_no_conflicts and has_signoff: 
     print(pr.number) 
+0

谢谢!这工作!我忘记检查每个条件,同时通过拉请求评论。 – DBS