diff --git a/infra/terraform/test-org/ci-comment-build-trigger-function/.terraform.lock.hcl b/infra/terraform/test-org/ci-comment-build-trigger-function/.terraform.lock.hcl deleted file mode 100644 index 06d98b0891e..00000000000 --- a/infra/terraform/test-org/ci-comment-build-trigger-function/.terraform.lock.hcl +++ /dev/null @@ -1,95 +0,0 @@ -# This file is maintained automatically by "terraform init". -# Manual edits may be lost in future updates. - -provider "registry.terraform.io/hashicorp/archive" { - version = "2.2.0" - hashes = [ - "h1:CIWi5G6ob7p2wWoThRQbOB8AbmFlCzp7Ka81hR3cVp0=", - "zh:06bd875932288f235c16e2237142b493c2c2b6aba0e82e8c85068332a8d2a29e", - "zh:0c681b481372afcaefddacc7ccdf1d3bb3a0c0d4678a526bc8b02d0c331479bc", - "zh:100fc5b3fc01ea463533d7bbfb01cb7113947a969a4ec12e27f5b2be49884d6c", - "zh:55c0d7ddddbd0a46d57c51fcfa9b91f14eed081a45101dbfc7fd9d2278aa1403", - "zh:73a5dd68379119167934c48afa1101b09abad2deb436cd5c446733e705869d6b", - "zh:841fc4ac6dc3479981330974d44ad2341deada8a5ff9e3b1b4510702dfbdbed9", - "zh:91be62c9b41edb137f7f835491183628d484e9d6efa82fcb75cfa538c92791c5", - "zh:acd5f442bd88d67eb948b18dc2ed421c6c3faee62d3a12200e442bfff0aa7d8b", - "zh:ad5720da5524641ad718a565694821be5f61f68f1c3c5d2cfa24426b8e774bef", - "zh:e63f12ea938520b3f83634fc29da28d92eed5cfbc5cc8ca08281a6a9c36cca65", - "zh:f6542918faa115df46474a36aabb4c3899650bea036b5f8a5e296be6f8f25767", - ] -} - -provider "registry.terraform.io/hashicorp/google" { - version = "4.23.0" - constraints = ">= 3.38.0, < 5.0.0" - hashes = [ - "h1:jy/SN60OhkytGOJUqCrV1lcDnuC/wKjtPJEmDUuoKYo=", - "zh:0827aed99587324e0f3f26f374fdbbd807ebff028f5fef02af9f88fedea07743", - "zh:1881533c70e995662241fbc33439202237c9570ae0c5e96bee0660e2f05c8e83", - "zh:195cb52ad2195c56926d6354c1ae8179a0ca63601da5aac3e9ac31537a8cffdd", - "zh:26bfff740b6ce167b6369dd99eece5a4f540b1efa13dfcba13d7b262b790873d", - "zh:5bd28272c02d4f4031d68f34c2d845167760b84b70172055cf9725315f2d0f8c", - "zh:6fda7aa01758175c025800bd29cd5c5904278c18eb225059d2d9655ce763c202", - "zh:7003b6bf8074e37b919ebcdc1dcfacba24b5244cb959dd5ea4ab2e32394da466", - "zh:9040f7bf02c473cbf87497b07bc28b1e6e137c1b5f28f3d959387f4738884b36", - "zh:b6d7752e332f7d48851e48d703254d0c2a0ee44d5ee289db89a04943c5c6c51d", - "zh:f51e41a41c358d953d08977cef118c1b192ce8968ffe4d063748903218d3b880", - "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", - "zh:fc502588ca54660f1701d786e4edb4aadb0e51078d9c1bd33f2a8b7b6373c7f0", - ] -} - -provider "registry.terraform.io/hashicorp/null" { - version = "3.1.1" - hashes = [ - "h1:71sNUDvmiJcijsvfXpiLCz0lXIBSsEJjMxljt7hxMhw=", - "zh:063466f41f1d9fd0dd93722840c1314f046d8760b1812fa67c34de0afcba5597", - "zh:08c058e367de6debdad35fc24d97131c7cf75103baec8279aba3506a08b53faf", - "zh:73ce6dff935150d6ddc6ac4a10071e02647d10175c173cfe5dca81f3d13d8afe", - "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", - "zh:8fdd792a626413502e68c195f2097352bdc6a0df694f7df350ed784741eb587e", - "zh:976bbaf268cb497400fd5b3c774d218f3933271864345f18deebe4dcbfcd6afa", - "zh:b21b78ca581f98f4cdb7a366b03ae9db23a73dfa7df12c533d7c19b68e9e72e5", - "zh:b7fc0c1615dbdb1d6fd4abb9c7dc7da286631f7ca2299fb9cd4664258ccfbff4", - "zh:d1efc942b2c44345e0c29bc976594cb7278c38cfb8897b344669eafbc3cddf46", - "zh:e356c245b3cd9d4789bab010893566acace682d7db877e52d40fc4ca34a50924", - "zh:ea98802ba92fcfa8cf12cbce2e9e7ebe999afbf8ed47fa45fc847a098d89468b", - "zh:eff8872458806499889f6927b5d954560f3d74bf20b6043409edf94d26cd906f", - ] -} - -provider "registry.terraform.io/hashicorp/random" { - version = "3.2.0" - hashes = [ - "h1:eeUh6cJ6wKLLuo4q9uQ0CA1Zvfqya4Wn1LecLCN8KKs=", - "zh:2960977ce9a7d6a7d3e934e75ec5814735626f95c186ad95a9102344a1a38ac1", - "zh:2fd012abfabe7076f3f2f402eeef4970e20574d20ffec57c162b02b6e848c32f", - "zh:4cd3234671cf01c913023418b227eb78b0659f2cd2e0b387be1f0bb607d29889", - "zh:52e695b4fa3fae735ffc901edff8183745f980923510a744db7616e8f10dc499", - "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", - "zh:848b4a294e5ba15192ee4bfd199c07f60a437d7572efcd2d89db036e1ebc0e6e", - "zh:9d49aa432a05748a9527e95448cebee1238c87c97c7e8dec694bfd709683f9c7", - "zh:b4ad4cf289d3f7408649b74b8639918833613f2a1f3cf51b51f4b2fdaa412dd2", - "zh:c1544c4b416096fb8d8dbf84c4488584a2844a30dd533b957e9e9e60a165f24e", - "zh:dc737d6b4591cad8c9a1d0b347e587e846d8d901789b29b4dd401b6cdf82c017", - "zh:f5645fd39f749dbbf847cbdc87ba0dbd141143f12917a6a8904faf8a9b64111e", - "zh:fdedf610e0d020878a8f1fedda8105e0c33a7e23c4792fca54460685552de308", - ] -} - -provider "registry.terraform.io/hashicorp/template" { - version = "2.2.0" - hashes = [ - "h1:94qn780bi1qjrbC3uQtjJh3Wkfwd5+tTtJHOb7KTg9w=", - "zh:01702196f0a0492ec07917db7aaa595843d8f171dc195f4c988d2ffca2a06386", - "zh:09aae3da826ba3d7df69efeb25d146a1de0d03e951d35019a0f80e4f58c89b53", - "zh:09ba83c0625b6fe0a954da6fbd0c355ac0b7f07f86c91a2a97849140fea49603", - "zh:0e3a6c8e16f17f19010accd0844187d524580d9fdb0731f675ffcf4afba03d16", - "zh:45f2c594b6f2f34ea663704cc72048b212fe7d16fb4cfd959365fa997228a776", - "zh:77ea3e5a0446784d77114b5e851c970a3dde1e08fa6de38210b8385d7605d451", - "zh:8a154388f3708e3df5a69122a23bdfaf760a523788a5081976b3d5616f7d30ae", - "zh:992843002f2db5a11e626b3fc23dc0c87ad3729b3b3cff08e32ffb3df97edbde", - "zh:ad906f4cebd3ec5e43d5cd6dc8f4c5c9cc3b33d2243c89c5fc18f97f7277b51d", - "zh:c979425ddb256511137ecd093e23283234da0154b7fa8b21c2687182d9aea8b2", - ] -} diff --git a/infra/terraform/test-org/ci-comment-build-trigger-function/backend.tf b/infra/terraform/test-org/ci-comment-build-trigger-function/backend.tf deleted file mode 100644 index a7775dd4270..00000000000 --- a/infra/terraform/test-org/ci-comment-build-trigger-function/backend.tf +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Copyright 2019 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. - */ - -terraform { - backend "gcs" { - bucket = "cft-infra-test-tfstate" - prefix = "state/ci-pr-comment-function" - } -} diff --git a/infra/terraform/test-org/ci-comment-build-trigger-function/function.tf b/infra/terraform/test-org/ci-comment-build-trigger-function/function.tf deleted file mode 100644 index 8441a7a45d8..00000000000 --- a/infra/terraform/test-org/ci-comment-build-trigger-function/function.tf +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright 2020 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. - */ - -# TODO: no longer in-use, remove per https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/issues/1537 - -# locals { -# project_id = "cloud-foundation-cicd" -# } - -# module "gcf_service_account" { -# source = "terraform-google-modules/service-accounts/google" -# version = "~> 4.1" -# project_id = local.project_id -# names = ["pr-comment-cf-sa"] -# project_roles = [ -# "${local.project_id}=>roles/cloudbuild.builds.editor" -# ] -# } - -# resource "random_id" "suffix" { -# byte_length = 4 -# } - -# module "pr_comment_build_function" { -# source = "terraform-google-modules/event-function/google" -# version = "~> 2.3" -# name = "pr-comment-downstream-builder-${random_id.suffix.hex}" -# project_id = local.project_id -# region = "us-central1" -# description = "Launches a downstream build that comments on a PR." -# entry_point = "main" -# runtime = "python37" -# source_directory = "${path.module}/function_source" -# service_account_email = module.gcf_service_account.email -# bucket_force_destroy = true - -# environment_variables = { -# CLOUDBUILD_PROJECT = local.project_id -# } - -# event_trigger = { -# event_type = "google.pubsub.topic.publish" -# resource = "projects/${local.project_id}/topics/cloud-builds" -# } -# } diff --git a/infra/terraform/test-org/ci-comment-build-trigger-function/function_source/main.py b/infra/terraform/test-org/ci-comment-build-trigger-function/function_source/main.py deleted file mode 100644 index cbd2e274f64..00000000000 --- a/infra/terraform/test-org/ci-comment-build-trigger-function/function_source/main.py +++ /dev/null @@ -1,151 +0,0 @@ -# Copyright 2020 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 -# -# https://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. - -# flake8: noqa - -import base64 -import sys -import os -import json -import logging -import requests - -from google.cloud.devtools.cloudbuild_v1 import CloudBuildClient as cloudbuild -from google.cloud.devtools.cloudbuild_v1.types import ( - BuildStep, - Build, - BuildOptions) -from google.protobuf import duration_pb2 as duration - -CFT_TOOLS_DEFAULT_IMAGE = 'gcr.io/cloud-foundation-cicd/cft/developer-tools' -CFT_TOOLS_DEFAULT_IMAGE_VERSION = '1' -# Disable terraform-google-module-template as it uses a cookiecutter template -DISABLED_MODULES = ["terraform-example-foundation", - "cloud-foundation-training", - "terraform-google-module-template"] - - -def main(event, context): - """ Triggers a new downstream build based on a PubSub message - originating from a parent cloudbuild """ - # if cloud build project is not set, exit - if not os.getenv('CLOUDBUILD_PROJECT'): - logging.warn('Cloud Build project not set') - sys.exit(1) - # if no data in PubSub event, log and exit - if 'data' not in event: - logging.info('Unable to find data in PubSub event') - sys.exit(1) - # decode data in PubSub event - data = json.loads(base64.b64decode(event['data']).decode('utf-8')) - # if the parent build originated from CF, ignore - if data['substitutions'].get('_IS_TRIGGERED_BY_CF', False): - logging.warn('Triggered by CF, Ignoring') - return - logging.info('Parent build not triggered by CF') - # if parent build is not a lint build, ignore - if 'lint' not in data['tags']: - logging.warn('Parent build is not a lint build') - return - # if parent build has not started, or is in any other state, ignore - if data['status'] != 'WORKING': - logging.warn('Parent build is not in WORKING status') - return - logging.info('Parent build is in WORKING status') - # if repo ref for the parent build has disabled PR bot, ignore - if data['substitutions']['REPO_NAME'] in DISABLED_MODULES: - logging.warn('Comment bot is disabled for this repo') - return - if data['substitutions'].get('_DOCKER_TAG_VERSION_DEVELOPER_TOOLS', False): - logging.info( - f'Found _DOCKER_TAG_VERSION_DEVELOPER_TOOLS. Setting tools image version to {data["substitutions"]["_DOCKER_TAG_VERSION_DEVELOPER_TOOLS"]}' - ) - CFT_TOOLS_DEFAULT_IMAGE_VERSION = data['substitutions'][ - '_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' - ] - # Cloud Build seems to have a bug where if a build is re run through - # Github UI, it will not set _PR_NUMBER or _HEAD_REPO_URL - # workaround using the GH API to infer PR number and _HEAD_REPO_URL - PR_NUMBER = data['substitutions'].get('_PR_NUMBER', False) - _HEAD_REPO_URL = data['substitutions'].get('_HEAD_REPO_URL', False) - # default clone repo step - get_repo_args = [ - '-c', - 'git clone $$REPO_URL . && git checkout $$COMMIT_SHA && git status', - ] - if not (PR_NUMBER or _HEAD_REPO_URL): - logging.warn( - 'Unable to infer PR number via Cloud Build. Trying via GH API') - # get list of github PRs that have this SHA - response = requests.get( - f'https://api.github.com/search/issues?q={data["substitutions"]["COMMIT_SHA"]}' - ) - response.raise_for_status() - response_obj = response.json() - # if more than one PR, ignore - if response_obj['total_count'] != 1: - logging.info(f'Multiple associated PRs found. Exiting...') - return - # if only one PR, its safe to assume that is associated with parent build's PR - logging.info( - f'One associated PR found: {response_obj["items"][0]["number"]}') - PR_NUMBER = response_obj['items'][0]['number'] - # get target repo URL - pr_url = response_obj['items'][0]['html_url'] - _HEAD_REPO_URL = pr_url[: pr_url.find('/pull')] - # fetch PR at head using PR number - get_repo_args = [ - '-c', - 'git clone $$REPO_URL . && git fetch origin pull/$$_PR_NUMBER/head:$$_PR_NUMBER && git checkout $$_PR_NUMBER && git show --name-only', - ] - - # prepare env vars - env = [ - f'_PR_NUMBER={PR_NUMBER}', - f'REPO_NAME={data["substitutions"]["REPO_NAME"]}', - f'REPO_URL={_HEAD_REPO_URL}', - f'COMMIT_SHA={data["substitutions"]["COMMIT_SHA"]}', - ] - get_repo_step = BuildStep( - name='gcr.io/cloud-builders/git', - env=env, - args=get_repo_args, - id='get_repo', - entrypoint='bash', - ) - # lint comment step - lint_args = [ - '-c', - 'source /usr/local/bin/task_helper_functions.sh && printenv && post_lint_status_pr_comment', - ] - lint_step = BuildStep( - name=f'{CFT_TOOLS_DEFAULT_IMAGE}:{CFT_TOOLS_DEFAULT_IMAGE_VERSION}', - env=env, - args=lint_args, - id='lint_comment', - entrypoint='/bin/bash', - ) - # substitutions - sub = { - '_IS_TRIGGERED_BY_CF': '1', - } - # create and trigger build - build = Build( - steps=[get_repo_step, lint_step], - options=BuildOptions(substitution_option='ALLOW_LOOSE'), - substitutions=sub, - timeout=duration.Duration(seconds=1200), - ) - response = cloudbuild().create_build(os.getenv('CLOUDBUILD_PROJECT'), build) - logging.info(response) diff --git a/infra/terraform/test-org/ci-comment-build-trigger-function/function_source/requirements.txt b/infra/terraform/test-org/ci-comment-build-trigger-function/function_source/requirements.txt deleted file mode 100644 index de9ebe36784..00000000000 --- a/infra/terraform/test-org/ci-comment-build-trigger-function/function_source/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ - google-cloud-build==3.17.0 - protobuf3==0.2.1 diff --git a/infra/terraform/test-org/ci-comment-build-trigger-function/versions.tf b/infra/terraform/test-org/ci-comment-build-trigger-function/versions.tf deleted file mode 100644 index 3dd96cf5390..00000000000 --- a/infra/terraform/test-org/ci-comment-build-trigger-function/versions.tf +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright 2020 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. - */ - -terraform { - required_version = ">= 1.4.4" - required_providers { - google = { - version = ">= 3.38, < 5.0" - } - } -}