zrhoffman commented on a change in pull request #6699:
URL: https://github.com/apache/trafficcontrol/pull/6699#discussion_r837814843



##########
File path: .github/actions/pr-to-update-go/pr_to_update_go/go_pr_maker.py
##########
@@ -396,18 +398,25 @@ def set_go_version(self, go_version: str, commit_message: 
str,
 
                print(f'Updated {go_version_file} on {self.repo.name}')
                env_path = os.path.join(os.path.dirname(getenv(ENV_ENV_FILE)), 
".env")
-               content = f"GO_VERSION={go_version}\n"
                sha = self.file_contents(env_path, source_branch_name).sha
+               with open(env_path, encoding='UTF-8') as env_stream:
+                       previous_env_content = env_stream.read()
+               dotenv.set_key(dotenv_path=env_path, key_to_set=GO_VERSION_KEY, 
value_to_set=go_version,
+                       quote_mode='never')
+               with open(env_path, encoding='UTF-8') as env_stream:
+                       env_content = env_stream.read()
                commit = self.repo.update_file(
                        branch=source_branch_name,
-                       content=content,
+                       content=env_content,
                        path=env_path,
                        message=commit_message,
                        sha=sha
                )["commit"]
                if not isinstance(commit, Commit):
                        raise TypeError("'commit' property of file update 
response was not a Commit")
                print(f"Updated {env_path} on {self.repo.name}")
+               with open(env_path, encoding='UTF-8', mode='w') as env_stream:
+                       env_stream.write(previous_env_content)

Review comment:
       > Why does it write the old content back over the file?
   
   Because if it doesn't, then later, when it checks out the branch before 
updating Go modules at 
https://github.com/apache/trafficcontrol/blob/9621deffe14b2dde2482fbcfbc41a67e838095a6/.github/actions/pr-to-update-go/pr_to_update_go/go_pr_maker.py#L292
 , it will throw an error due to
   
   ```git
   error: Your local changes to the following files would be overwritten by 
checkout:
        .env
   Please commit your changes or stash them before you switch branches.
   Aborting
   ```
   
   > We aren't doing that for the `GO_VERSION` file as far as I can tell
   
   Right, because the update to the `GO_VERSION` file is never written to the 
disk, the branch is updated directly using the GitHub API. `.env` is only 
written to make use of the `dotenv` package (is writing to the disk necessary 
to get the updated file contents?).
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to