0

I would like to loop through pull requests in GitHub and if the pull request has the comments in the code below, do something (for now, print the pull request number). I have a pull request that has the comments I'm looking for (spread over multiple comments in the pull request), but it doesn't print the pull request number. I suspect it has something to do with the regex I'm using because if I break the if statement down to look for just the regex or the string values, it works fine, but when I try to combine them in one if statement, it doesn't work.

I don't think this is a duplicate question as I've looked at all the suggestions under questions that may already have your answer.

    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)
3
  • Are all of these present in one comment or in different comments? Your version requires them to be all in the same comment. Also conflicts_base_branch and checks in comment.body is the same as (conflicts_base_branch) and (checks in comment.body) or (True) and (checks in comment.body), so it only checks for the presence of the latter. Commented Sep 19, 2016 at 22:36
  • These are present in multiple comments in the pull request. Does this mean it should be if conflicts_base_branch in comment.body and checks in comment.body? If so, how do I combine that with the regex statement? Commented Sep 19, 2016 at 22:43
  • You need to check them separately while iterating. Check my answer Commented Sep 19, 2016 at 22:47

1 Answer 1

1

Your solution requires all of the conditions to be met on the same comment, it won't work if they are on different comments. For that you need to keep track which conditions are met while iterating through the comments, e.g:

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That worked! I forget about checking each condition separately while going through the pull request comments.

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.