Repository: scikit-learn/scikit-learn
File: build_tools/azure/get_commit_message.py (Python)

Code before:
import argparse
import logging
import os
import subprocess

logger = logging.getLogger(__name__)


def get_commit_message():
    """Retrieve the commit message."""

    if "COMMIT_MESSAGE" in os.environ or os.environ.get("GITHUB_ACTIONS", False):
        raise RuntimeError(
            f"It seems that the `COMMIT_MESSAGE` environment variable is set or you are"
            f"in a GitHub Actions context "
            f"(GITHUB_ACTIONS={os.environ.get('GITHUB_ACTIONS', False)})."
            f"Instead, please use directly `COMMIT_MESSAGE`."
        )

    logger.debug("`commit_message` coming from Azure")

    # If 'COMMIT_MESSAGE' is unset we are in Azure, and we retrieve the commit
    # message using Azure-specific variables.

    build_source_version_message = os.environ["BUILD_SOURCEVERSIONMESSAGE"]

    if os.environ["BUILD_REASON"] == "PullRequest":
        # By default pull requests use refs/pull/PULL_ID/merge as the source branch
        # which has a "Merge ID into ID" as a commit message. The latest commit
        # message is the second to last commit
        commit_id = build_source_version_message.split()[1]
        git_cmd = ["git", "log", commit_id, "-1", "--pretty=%B"]
        commit_message = subprocess.run(
            git_cmd, capture_output=True, text=True
        ).stdout.strip()
    else:
        commit_message = build_source_version_message

    # Sanitize the commit message to avoid introducing a vulnerability: a PR
    # submitter could include the "##vso" special marker in their commit
    # message to attempt to obfuscate the injection of arbitrary commands in
    # the Azure pipeline.
    #
    # This can be a problem if the PR reviewers do not pay close enough
    # attention to the full commit message prior to clicking the merge button

Review comment (suggestion):
I think this logging is not really necessary, if you get an error above you are not in Azure, if you don't get an error, well then you are on Azure

Code after:
import argparse
import os
import subprocess


def get_commit_message():
    """Retrieve the commit message."""

    if "COMMIT_MESSAGE" in os.environ or "BUILD_SOURCEVERSIONMESSAGE" not in os.environ:
        raise RuntimeError(
            "This legacy script should only be used on Azure. "
            "On GitHub actions, use the 'COMMIT_MESSAGE' environment variable"
        )

    build_source_version_message = os.environ["BUILD_SOURCEVERSIONMESSAGE"]

    if os.environ["BUILD_REASON"] == "PullRequest":
        # By default pull requests use refs/pull/PULL_ID/merge as the source branch
        # which has a "Merge ID into ID" as a commit message. The latest commit
        # message is the second to last commit
        commit_id = build_source_version_message.split()[1]
        git_cmd = ["git", "log", commit_id, "-1", "--pretty=%B"]
        commit_message = subprocess.run(
            git_cmd, capture_output=True, text=True
        ).stdout.strip()
    else:
        commit_message = build_source_version_message

    # Sanitize the commit message to avoid introducing a vulnerability: a PR
    # submitter could include the "##vso" special marker in their commit
    # message to attempt to obfuscate the injection of arbitrary commands in
    # the Azure pipeline.
    #
    # This can be a problem if the PR reviewers do not pay close enough
    # attention to the full commit message prior to clicking the merge button
    # and as a result make the inject code run in a protected branch with
    # elevated access to CI secrets. On a protected branch, Azure
    # already sanitizes `BUILD_SOURCEVERSIONMESSAGE`, but the message
    # will still be sanitized here out of precaution.
    commit_message = commit_message.replace("##vso", "..vso")

    return commit_message


def parsed_args():