1# Copyright 2020 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15################################################################################
16"""Cloud function that requests coverage builds."""
17import base64
18
19import google.auth
20from google.cloud import ndb
21
22import build_and_run_coverage
23import request_build
24
25BASE_PROJECT = 'oss-fuzz-base'
26
27
28def get_build_steps(project_name, image_project, base_images_project):
29  """Retrieve build steps."""
30  project_yaml_contents, dockerfile_lines = request_build.get_project_data(
31      project_name)
32  return build_and_run_coverage.get_build_steps(project_name,
33                                                project_yaml_contents,
34                                                dockerfile_lines, image_project,
35                                                base_images_project)
36
37
38def request_coverage_build(event, context):
39  """Entry point for coverage build cloud function."""
40  del context  #unused
41  if 'data' in event:
42    project_name = base64.b64decode(event['data']).decode('utf-8')
43  else:
44    raise RuntimeError('Project name missing from payload')
45
46  with ndb.Client().context():
47    credentials, image_project = google.auth.default()
48    build_steps = get_build_steps(project_name, image_project, BASE_PROJECT)
49    if not build_steps:
50      return
51    request_build.run_build(project_name, image_project, build_steps,
52                            credentials,
53                            build_and_run_coverage.COVERAGE_BUILD_TAG)
54