如果语句有多个条件使用正则表达式

时间:2016-09-19 22:23:59

标签: python if-statement

我想在GitHub中遍历pull请求,如果pull请求在下面的代码中有注释,请执行某些操作(暂时打印pull请求编号)。我有一个pull请求,其中包含我正在寻找的注释(在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)

1 个答案:

答案 0 :(得分: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)