2022-04-04 21:47:21 +00:00
|
|
|
# Copyright 2022 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""Entrypoint for CI into trial_build. This script will get the command from the
|
|
|
|
last PR comment containing "/gcbrun" and pass it to trial_build.py which will
|
|
|
|
build test versions of base-imags, push them and then do test builds using those
|
|
|
|
images."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
2022-05-24 21:58:56 +00:00
|
|
|
import sys
|
2022-04-04 21:47:21 +00:00
|
|
|
|
|
|
|
import github
|
|
|
|
|
|
|
|
import trial_build
|
|
|
|
|
2022-06-09 01:59:06 +00:00
|
|
|
TRIGGER_COMMAND = '/gcbrun'
|
2022-04-04 21:47:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_comments(pull_request_number):
|
|
|
|
"""Returns comments on the GitHub Pull request referenced by
|
|
|
|
|pull_request_number|."""
|
|
|
|
github_obj = github.Github()
|
|
|
|
repo = github_obj.get_repo('google/oss-fuzz')
|
|
|
|
pull = repo.get_pull(pull_request_number)
|
|
|
|
pull_comments = list(pull.get_comments())
|
|
|
|
issue = repo.get_issue(pull_request_number)
|
|
|
|
issue_comments = list(issue.get_comments())
|
|
|
|
# Github only returns comments if from the pull object when a pull request is
|
|
|
|
# open. If it is a draft, it will only return comments from the issue object.
|
|
|
|
return pull_comments + issue_comments
|
|
|
|
|
|
|
|
|
|
|
|
def get_latest_gcbrun_command(comments):
|
|
|
|
"""Gets the last /gcbrun comment from comments."""
|
|
|
|
for comment in reversed(comments):
|
|
|
|
# This seems to get comments on code too.
|
|
|
|
body = comment.body
|
2022-06-09 01:59:06 +00:00
|
|
|
command_str = f'{TRIGGER_COMMAND} trial_build.py '
|
|
|
|
if not body.startswith(command_str):
|
2022-04-04 21:47:21 +00:00
|
|
|
continue
|
2022-06-09 01:59:06 +00:00
|
|
|
if len(body) == len(command_str):
|
2022-04-04 21:47:21 +00:00
|
|
|
return None
|
2022-06-09 01:59:06 +00:00
|
|
|
return body[len(command_str):].strip().split(' ')
|
2022-04-04 21:47:21 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2022-09-18 12:04:33 +00:00
|
|
|
def exec_command_from_github(pull_request_number, repo, branch):
|
2022-04-04 21:47:21 +00:00
|
|
|
"""Executes the gcbrun command for trial_build.py in the most recent command
|
|
|
|
on |pull_request_number|."""
|
|
|
|
comments = get_comments(pull_request_number)
|
|
|
|
command = get_latest_gcbrun_command(comments)
|
2022-09-18 12:04:33 +00:00
|
|
|
command.extend(['--repo', repo])
|
2022-06-09 01:59:06 +00:00
|
|
|
logging.info('Command: %s.', command)
|
2022-04-04 21:47:21 +00:00
|
|
|
if command is None:
|
|
|
|
logging.info('Trial build not requested.')
|
|
|
|
return None
|
2022-06-09 01:59:06 +00:00
|
|
|
|
|
|
|
# Set the branch so that the trial_build builds the projects from the PR
|
|
|
|
# branch.
|
2022-08-01 23:04:31 +00:00
|
|
|
command.extend(['--branch', branch])
|
2022-04-04 21:47:21 +00:00
|
|
|
logging.info('Command: %s.', command)
|
|
|
|
return trial_build.trial_build_main(command, local_base_build=False)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Entrypoint for GitHub CI into trial_build.py"""
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
pull_request_number = int(os.environ['PULL_REQUEST_NUMBER'])
|
2022-06-09 01:59:06 +00:00
|
|
|
branch = os.environ['BRANCH']
|
2022-09-18 12:04:33 +00:00
|
|
|
repo = os.environ['REPO']
|
|
|
|
if exec_command_from_github(pull_request_number, repo, branch):
|
|
|
|
return 0
|
|
|
|
return 1
|
2022-04-04 21:47:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-05-24 21:58:56 +00:00
|
|
|
sys.exit(main())
|