|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2024 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +import os |
| 16 | +import shutil |
| 17 | +from typing import Dict |
| 18 | + |
| 19 | +import click |
| 20 | +from git import Commit, Repo |
| 21 | +from library_generation.model.generation_config import from_yaml |
| 22 | +from library_generation.utilities import find_versioned_proto_path |
| 23 | +from library_generation.utils.commit_message_formatter import format_commit_message |
| 24 | +from library_generation.utilities import get_file_paths |
| 25 | +from library_generation.utils.commit_message_formatter import wrap_nested_commit |
| 26 | + |
| 27 | + |
| 28 | +@click.group(invoke_without_command=False) |
| 29 | +@click.pass_context |
| 30 | +@click.version_option(message="%(version)s") |
| 31 | +def main(ctx): |
| 32 | + pass |
| 33 | + |
| 34 | + |
| 35 | +@main.command() |
| 36 | +@click.option( |
| 37 | + "--generation-config-yaml", |
| 38 | + required=True, |
| 39 | + type=str, |
| 40 | + help=""" |
| 41 | + Path to generation_config.yaml that contains the metadata about |
| 42 | + library generation. |
| 43 | + The googleapis commit in the configuration is the latest commit, |
| 44 | + inclusively, from which the commit message is considered. |
| 45 | + """, |
| 46 | +) |
| 47 | +@click.option( |
| 48 | + "--baseline-commit", |
| 49 | + required=True, |
| 50 | + type=str, |
| 51 | + help=""" |
| 52 | + The baseline (oldest) commit, exclusively, from which the commit message is |
| 53 | + considered. |
| 54 | + This commit should be an ancestor of googleapis commit in configuration. |
| 55 | + """, |
| 56 | +) |
| 57 | +@click.option( |
| 58 | + "--repo-url", |
| 59 | + type=str, |
| 60 | + default="https://github.com/googleapis/googleapis.git", |
| 61 | + show_default=True, |
| 62 | + help=""" |
| 63 | + GitHub repository URL. |
| 64 | + """, |
| 65 | +) |
| 66 | +def generate( |
| 67 | + generation_config_yaml: str, |
| 68 | + repo_url: str, |
| 69 | + baseline_commit: str, |
| 70 | +) -> str: |
| 71 | + return generate_pr_descriptions( |
| 72 | + generation_config_yaml=generation_config_yaml, |
| 73 | + repo_url=repo_url, |
| 74 | + baseline_commit=baseline_commit, |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +def generate_pr_descriptions( |
| 79 | + generation_config_yaml: str, |
| 80 | + repo_url: str, |
| 81 | + baseline_commit: str, |
| 82 | +) -> str: |
| 83 | + config = from_yaml(generation_config_yaml) |
| 84 | + paths = get_file_paths(config) |
| 85 | + return __get_commit_messages( |
| 86 | + repo_url=repo_url, |
| 87 | + latest_commit=config.googleapis_commitish, |
| 88 | + baseline_commit=baseline_commit, |
| 89 | + paths=paths, |
| 90 | + generator_version=config.gapic_generator_version, |
| 91 | + is_monorepo=config.is_monorepo, |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def __get_commit_messages( |
| 96 | + repo_url: str, |
| 97 | + latest_commit: str, |
| 98 | + baseline_commit: str, |
| 99 | + paths: Dict[str, str], |
| 100 | + generator_version: str, |
| 101 | + is_monorepo: bool, |
| 102 | +) -> str: |
| 103 | + """ |
| 104 | + Combine commit messages of a repository from latest_commit to |
| 105 | + baseline_commit. Only commits which change files in a pre-defined |
| 106 | + file paths will be considered. |
| 107 | + Note that baseline_commit should be an ancestor of latest_commit. |
| 108 | +
|
| 109 | + :param repo_url: the url of the repository. |
| 110 | + :param latest_commit: the newest commit to be considered in |
| 111 | + selecting commit message. |
| 112 | + :param baseline_commit: the oldest commit to be considered in |
| 113 | + selecting commit message. This commit should be an ancestor of |
| 114 | + :param paths: a mapping from file paths to library_name. |
| 115 | + :param generator_version: the version of the generator. |
| 116 | + :param is_monorepo: whether to generate commit messages in a monorepo. |
| 117 | + :return: commit messages. |
| 118 | + """ |
| 119 | + tmp_dir = "/tmp/repo" |
| 120 | + shutil.rmtree(tmp_dir, ignore_errors=True) |
| 121 | + os.mkdir(tmp_dir) |
| 122 | + repo = Repo.clone_from(repo_url, tmp_dir) |
| 123 | + commit = repo.commit(latest_commit) |
| 124 | + qualified_commits = {} |
| 125 | + while str(commit.hexsha) != baseline_commit: |
| 126 | + commit_and_name = __filter_qualified_commit(paths=paths, commit=commit) |
| 127 | + if commit_and_name != (): |
| 128 | + qualified_commits[commit_and_name[0]] = commit_and_name[1] |
| 129 | + commit_parents = commit.parents |
| 130 | + if len(commit_parents) == 0: |
| 131 | + break |
| 132 | + commit = commit_parents[0] |
| 133 | + shutil.rmtree(tmp_dir, ignore_errors=True) |
| 134 | + return __combine_commit_messages( |
| 135 | + latest_commit=latest_commit, |
| 136 | + baseline_commit=baseline_commit, |
| 137 | + commits=qualified_commits, |
| 138 | + generator_version=generator_version, |
| 139 | + is_monorepo=is_monorepo, |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +def __filter_qualified_commit(paths: Dict[str, str], commit: Commit) -> (Commit, str): |
| 144 | + """ |
| 145 | + Returns a tuple of a commit and libray_name. |
| 146 | + A qualified commit means at least one file changes in that commit is |
| 147 | + within the versioned proto_path in paths. |
| 148 | +
|
| 149 | + :param paths: a mapping from versioned proto_path to library_name. |
| 150 | + :param commit: a commit under consideration. |
| 151 | + :return: a tuple of a commit and library_name if the commit is |
| 152 | + qualified; otherwise an empty tuple. |
| 153 | + """ |
| 154 | + for file in commit.stats.files.keys(): |
| 155 | + versioned_proto_path = find_versioned_proto_path(file) |
| 156 | + if versioned_proto_path in paths: |
| 157 | + return commit, paths[versioned_proto_path] |
| 158 | + return () |
| 159 | + |
| 160 | + |
| 161 | +def __combine_commit_messages( |
| 162 | + latest_commit: str, |
| 163 | + baseline_commit: str, |
| 164 | + commits: Dict[Commit, str], |
| 165 | + generator_version: str, |
| 166 | + is_monorepo: bool, |
| 167 | +) -> str: |
| 168 | + messages = [ |
| 169 | + f"This pull request is generated with proto changes between googleapis commit {baseline_commit} (exclusive) and {latest_commit} (inclusive).", |
| 170 | + "Qualified commits are:", |
| 171 | + ] |
| 172 | + for commit in commits: |
| 173 | + short_sha = commit.hexsha[:7] |
| 174 | + messages.append( |
| 175 | + f"[googleapis/googleapis@{short_sha}](https://github.com/googleapis/googleapis/commit/{commit.hexsha})" |
| 176 | + ) |
| 177 | + |
| 178 | + messages.extend(format_commit_message(commits=commits, is_monorepo=is_monorepo)) |
| 179 | + messages.extend( |
| 180 | + wrap_nested_commit( |
| 181 | + [ |
| 182 | + f"feat: Regenerate with the Java code generator (gapic-generator-java) v{generator_version}" |
| 183 | + ] |
| 184 | + ) |
| 185 | + ) |
| 186 | + |
| 187 | + return "\n".join(messages) |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + main() |
0 commit comments