Kristen Kozak | c39e069 | 2017-01-28 16:54:37 -0800 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import traceback |
| 4 | |
| 5 | def main(argv): |
Kristen Kozak | ed5e46b | 2017-03-24 16:38:12 -0700 | [diff] [blame] | 6 | # Only check the history if the build is running on a pull request. |
rghetia | 9f1a1be | 2018-05-16 09:45:53 -0700 | [diff] [blame] | 7 | # Build could be running on pull request using travis or kokoro. |
| 8 | if is_travis_pull_request() or is_kokoro_presubmit_request(): |
Kristen Kozak | ed5e46b | 2017-03-24 16:38:12 -0700 | [diff] [blame] | 9 | # This function assumes that HEAD^1 is the base branch and HEAD^2 is the |
| 10 | # pull request. |
| 11 | exit_if_pull_request_has_merge_commits() |
Kristen Kozak | 39b5dee | 2017-03-24 17:17:35 -0700 | [diff] [blame] | 12 | print 'Checked pull request history: SUCCEEDED' |
Kristen Kozak | ed5e46b | 2017-03-24 16:38:12 -0700 | [diff] [blame] | 13 | else: |
| 14 | print 'Skipped history check.' |
Kristen Kozak | c39e069 | 2017-01-28 16:54:37 -0800 | [diff] [blame] | 15 | |
rghetia | 9f1a1be | 2018-05-16 09:45:53 -0700 | [diff] [blame] | 16 | def is_kokoro_presubmit_request(): |
| 17 | '''Returns true if KOKORO_GITHUB_PULL_REQUEST_NUMBER is set.''' |
| 18 | if 'KOKORO_GITHUB_PULL_REQUEST_NUMBER' in os.environ: |
| 19 | return True |
| 20 | return False |
| 21 | |
Kristen Kozak | 952ddd8 | 2017-03-22 18:00:36 -0700 | [diff] [blame] | 22 | def is_travis_pull_request(): |
| 23 | '''Returns true if TRAVIS_PULL_REQUEST is set to indicate a pull request.''' |
rghetia | 9f1a1be | 2018-05-16 09:45:53 -0700 | [diff] [blame] | 24 | if 'TRAVIS_PULL_REQUEST' in os.environ: |
| 25 | return os.environ['TRAVIS_PULL_REQUEST'] != 'false' |
| 26 | return False |
Kristen Kozak | c39e069 | 2017-01-28 16:54:37 -0800 | [diff] [blame] | 27 | |
| 28 | def exit_if_pull_request_has_merge_commits(): |
| 29 | '''Exits with an error if any of the commits added by the pull request are |
| 30 | merge commits.''' |
| 31 | # Print the parents of each commit added by the pull request. |
| 32 | git_command = 'git log --format="%P" HEAD^1..HEAD^2' |
| 33 | for line in os.popen(git_command): |
| 34 | parents = line.split() |
| 35 | assert len(parents) >= 1, line |
| 36 | if len(parents) > 1: |
| 37 | print 'Pull request contains a merge commit:' |
| 38 | print_history() |
Kristen Kozak | 39b5dee | 2017-03-24 17:17:35 -0700 | [diff] [blame] | 39 | print 'Checked pull request history: FAILED' |
Kristen Kozak | c39e069 | 2017-01-28 16:54:37 -0800 | [diff] [blame] | 40 | sys.exit(1) |
| 41 | |
Kristen Kozak | c39e069 | 2017-01-28 16:54:37 -0800 | [diff] [blame] | 42 | def print_history(): |
| 43 | os.system('git log HEAD^1 HEAD^2 -30 --graph --oneline --decorate') |
| 44 | |
| 45 | def read_process(command): |
| 46 | '''Runs a command and returns everything printed to stdout.''' |
| 47 | with os.popen(command, 'r') as fd: |
| 48 | return fd.read() |
| 49 | |
| 50 | if __name__ == '__main__': |
| 51 | main(sys.argv) |