def process_issue(kokoro_session, gh: github.GitHub, issue: dict, result) -> None: # Reify the "issue" into a full pull request object from github. This # is necessary because github gives us back an issue object, but it # doesn't contain all of the PR info. pull = gh.get_url(issue["pull_request"]["url"]) # Before doing any processing, check to make sure the PR was actually merged. # "closed" PRs can be merged or just closed without merging. if not pull.get("merged_at"): result.skipped = True result.print("Closed, not merged, skipping.") # Remove the label so we don't continue processing it. gh.update_pull_labels(pull, add=["autorelease: closed"], remove=["autorelease: pending"]) return # Determine language. lang = common.guess_language(gh, pull["base"]["repo"]["full_name"]) # Run releasetool tag for the PR. ctx = run_releasetool_tag(lang, gh, pull) # Trigger Kokoro release build result.print(f"Triggering {ctx.kokoro_job_name} using {ctx.release_tag}") kokoro.trigger_build( kokoro_session, job_name=ctx.kokoro_job_name, sha=ctx.release_tag, env_vars={"AUTORELEASE_PR": pull["html_url"]}, )
def guess_language(gh: GitHub, repo_full_name: str) -> str: special_cases = { # 3 special cases inherited from the original determine_language() code. "googleapis/docuploader": "python_tool", "googleapis/synthtool": "python_tool", # 2 more special cases where the most prevalent language is not the same as # what was declared in the old repos.json. "GoogleCloudPlatform/cloud-code-samples": "dotnet", "googleapis/doc-templates": "python", } special_case = special_cases.get(repo_full_name) if special_case: return special_case # Does the repo name have a language name in it? lang_names = { "cpp", "dotnet", "elixir", "go", "java", "nodejs", "php", "python", "python_tool", "ruby", } chunks = set(re.split("/|-", repo_full_name)) x = lang_names.intersection(chunks) if 1 == len(x): return x.pop() # Found the language name in the repo name # Fetch how many lines of each language are found in the repo. languages = gh.get_languages(repo_full_name) ranks = [ (count, lang) for (lang, count) in languages.items() # Ignore languages we don't care about, like Shell. if lang in _SILVER_LANGUAGE_NAMES ] ranks.sort(reverse=True) if ranks: # Return the most prevalent language in the repo. return _SILVER_LANGUAGE_NAMES[ranks[0][1]] else: raise Exception("Unable to determine repository language.")
def trigger_kokoro_build_for_pull_request( kokoro_session, gh: github.GitHub, issue: dict, result, update_labels: bool = True, use_allowlist: bool = True, ) -> None: """Triggers the Kokoro job for a given pull request if possible. If the pull request is not merged, remove the `autorelease: pending` label and mark it as closed. Otherwise, determine the name of the Kokoro job name and trigger a build. """ # Reify the "issue" into a full pull request object from github. This # is necessary because github gives us back an issue object, but it # doesn't contain all of the PR info. pull = gh.get_url(issue["pull_request"]["url"]) # Before doing any processing, check to make sure the PR was actually merged. # "closed" PRs can be merged or just closed without merging. if not pull.get("merged_at"): result.skipped = True result.print("Closed, not merged, skipping.") # Remove the label so we don't continue processing it. gh.update_pull_labels(pull, add=["autorelease: closed"], remove=["autorelease: pending"]) return # If the Kokoro job has already been triggered, don't trigger again. if "labels" in pull and any( "name" in label and label["name"] == "autorelease: triggered" for label in pull["labels"]): return # Determine language. lang = common.guess_language(gh, pull["base"]["repo"]["full_name"]) # As part of the migration to release-please tagging, cross-reference the # language against an allowlist to allow migrating language-by-language. if use_allowlist and lang not in LANGUAGE_ALLOWLIST: result.skipped = True result.print(f"Language {lang} not in allowlist, skipping.") return language_module = importlib.import_module( f"releasetool.commands.tag.{lang}") package_name = language_module.package_name(pull) kokoro_job_name = language_module.kokoro_job_name( pull["base"]["repo"]["full_name"], package_name) pull_request_url = pull["html_url"] if kokoro_job_name is None: result.skipped = True result.print(f"No Kokoro job for {pull_request_url}, skipping.") return sha = pull["merge_commit_sha"] # Trigger Kokoro release build result.print(f"Triggering {kokoro_job_name} using {sha}") kokoro.trigger_build( kokoro_session, job_name=kokoro_job_name, sha=sha, env_vars={"AUTORELEASE_PR": pull_request_url}, ) if update_labels: gh.update_pull_labels(pull, add=["autorelease: triggered"])