Skip to content

Conversation

@AfuroIsCrazy
Copy link

update code

@gemini-code-assist
Copy link

Summary of Changes

Hello @AfuroIsCrazy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors and enhances the check-go-version-yaml.sh script. The changes aim to improve the script's reliability and flexibility in identifying and validating Go versions specified within YAML configuration files. By consolidating logic, using more robust parsing techniques, and refining error handling, the script now provides a more comprehensive and user-friendly mechanism for ensuring consistent Go version declarations across a project's YAML assets.

Highlights

  • Refactored Version Checking Logic: The two previous functions, check_go_version_yaml and check_go_version_env_variable, have been consolidated into a single, more versatile helper function named check_yaml_version_pattern. This new function is designed to handle various YAML key patterns for Go version extraction.
  • Improved YAML Parsing with AWK: The script now utilizes awk for extracting Go versions from YAML files, replacing the less robust grep and sed combination. This change significantly improves the script's ability to correctly parse versions regardless of quoting styles or surrounding whitespace.
  • Enhanced Error Handling and Reporting: A global EXIT_STATUS variable has been introduced to track the overall success or failure of the checks. Instead of exiting on the first error, the script now processes all relevant YAML files and reports all discrepancies before exiting with an appropriate status. The output also includes clearer success and failure messages with emojis.
  • Safer File Iteration: The method for iterating through YAML files has been updated to use find ... -print0 | while IFS= read -r -d $'\0' file; do. This approach robustly handles filenames that may contain spaces or special characters, preventing potential issues.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly improves the check-go-version-yaml.sh script by refactoring it for better robustness, maintainability, and user experience. The changes consolidate duplicated functions, adopt a more reliable parsing method with awk, and ensure all files are checked before reporting a final status. I have one high-severity comment regarding the awk implementation which could lead to incorrect matches, along with a suggested fix to make it more precise.

Comment on lines +27 to +39
local extracted_go_version=$(awk -v key="$key_pattern" \
'BEGIN {IGNORECASE=1}
$0 ~ key {
# Replace the key (case-insensitive) and any following colon/spaces
sub("[ \t]*" key "[ \t]*:?[ \t]*", "", $0);
# Remove quotes (single or double) from the start/end
gsub(/^["\x27]|["\x27]$/, "", $0);
# Trim leading/trailing whitespace again
gsub(/^[ \t]|[ \t]$/, "", $0);
print $0;
exit
}' "$yamlfile"
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current awk script for extracting the version is not anchored to the beginning of the line. This can cause it to incorrectly match the key pattern if it appears in a comment or a value string, leading to false-positive errors.

Additionally, the logic for stripping the key is more complex than necessary, and the version_regex variable on line 20 is unused and should be removed along with its comments.

To make the script more robust, I suggest anchoring the awk pattern to the start of the line (allowing for leading whitespace) and simplifying the value extraction. This will ensure that only actual YAML keys are matched.

Suggested change
local extracted_go_version=$(awk -v key="$key_pattern" \
'BEGIN {IGNORECASE=1}
$0 ~ key {
# Replace the key (case-insensitive) and any following colon/spaces
sub("[ \t]*" key "[ \t]*:?[ \t]*", "", $0);
# Remove quotes (single or double) from the start/end
gsub(/^["\x27]|["\x27]$/, "", $0);
# Trim leading/trailing whitespace again
gsub(/^[ \t]|[ \t]$/, "", $0);
print $0;
exit
}' "$yamlfile"
)
local extracted_go_version=$(awk -v key="$key_pattern" \
'BEGIN { IGNORECASE = 1 }
$0 ~ ("^[[:space:]]*" key "[[:space:]]*:") {
# Remove everything up to the first colon and any following spaces.
sub(/.*:[[:space:]]*/, "");
# Remove quotes (single or double) from the start/end.
gsub(/^["\x27]|["\x27]$/, "");
# Trim leading/trailing whitespace.
gsub(/^[[:space:]]+|[[:space:]]+$/, "");
print $0;
exit;
}' "$yamlfile"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant