From 72d88f90a6d0b1b2f5195ffb33b417921bd84c9d Mon Sep 17 00:00:00 2001 From: ntheanh201 <ntheanh201@gmail.com> Date: Fri, 27 Dec 2024 10:31:38 +0700 Subject: [PATCH] update GH actions to change go version in go.mod file --- .github/actions/pr-to-update-go/README.rst | 6 ++++-- .../pr-to-update-go/pr_to_update_go/constants.py | 9 +++++++++ .../pr-to-update-go/pr_to_update_go/go_pr_maker.py | 12 ++++++++++-- .github/workflows/pr-to-update-go.yml | 1 + go.mod | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/.github/actions/pr-to-update-go/README.rst b/.github/actions/pr-to-update-go/README.rst index 4c3ff29708..a2b9ae7ede 100644 --- a/.github/actions/pr-to-update-go/README.rst +++ b/.github/actions/pr-to-update-go/README.rst @@ -45,8 +45,9 @@ Environment Variables +----------------------------+----------------------------------------------------------------------------------+ | ``GO_VERSION_FILE`` | Required. The file in the repo containing the version of Go used by the repo. | +----------------------------+----------------------------------------------------------------------------------+ - - +| ``GO_MOD_FILE`` | Optional. The go.mod file in the repo containing the version of Go used by the | +| | repo. If not provided, the action will not update the go.mod file. | ++----------------------------+----------------------------------------------------------------------------------+ Outputs ======= @@ -66,6 +67,7 @@ Example usage GIT_AUTHOR_NAME: asf-ci-trafficcontrol GITHUB_TOKEN: ${{ github.token }} GO_VERSION_FILE: GO_VERSION + GO_MOD_FILE: go.mod Tests ===== diff --git a/.github/actions/pr-to-update-go/pr_to_update_go/constants.py b/.github/actions/pr-to-update-go/pr_to_update_go/constants.py index 9a86262ba6..83d09c68c9 100644 --- a/.github/actions/pr-to-update-go/pr_to_update_go/constants.py +++ b/.github/actions/pr-to-update-go/pr_to_update_go/constants.py @@ -25,6 +25,8 @@ ENV_ENV_FILE - The repository-relative path to an environment file containing a line setting the variable GO_VERSION to the Go version (e.g. GO_VERSION=3.2.1) + ENV_GO_MOD_FILE - The repository-relative path to the go.mod file containing the + version of Go used by the repo (e.g. go 3.2.1). Miscellaneous: @@ -71,6 +73,13 @@ containing the Go version. """ +ENV_GO_MOD_FILE: Final = 'GO_MOD_FILE' +""" +The name of the environment variable set to repository-relative path to the +go.mod file containing the version of Go used by the repo (e.g. go 3.2.1). +""" + + ENV_ENV_FILE: Final = 'ENV_FILE' """ The name of the environment variable set to repository-relative path to an diff --git a/.github/actions/pr-to-update-go/pr_to_update_go/go_pr_maker.py b/.github/actions/pr-to-update-go/pr_to_update_go/go_pr_maker.py index 9d6d635ec5..b861fad56a 100644 --- a/.github/actions/pr-to-update-go/pr_to_update_go/go_pr_maker.py +++ b/.github/actions/pr-to-update-go/pr_to_update_go/go_pr_maker.py @@ -50,6 +50,7 @@ GO_REPO_NAME, RELEASE_PAGE_URL, ENV_GO_VERSION_FILE, + ENV_GO_MOD_FILE, ENV_GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL_TEMPLATE, GO_VERSION_KEY, @@ -375,7 +376,7 @@ def set_go_version(self, go_version: str, commit_message: str, Makes the commits necessary to change the Go version used by the repository. - This includes updating the GO_VERSION and .env files at the repository's + This includes updating the GO_VERSION, GO_MOD, and .env files at the repository's root. """ master_tip = self.repo.get_branch('master').commit @@ -388,11 +389,18 @@ def set_go_version(self, go_version: str, commit_message: str, with open(go_version_file, 'w') as go_version_file_stream: go_version_file_stream.write(f'{go_version}\n') env_file = getenv(ENV_ENV_FILE) + go_mod_file = getenv(ENV_GO_MOD_FILE) + if go_mod_file: + with open(go_mod_file, 'r') as go_mod_file_stream: + content = go_mod_file_stream.read() + updated_content = re.sub(r'go \d+\.\d+(\.\d+)?', f'go {go_version}', content) + with open(go_mod_file, 'w') as go_mod_file_stream: + go_mod_file_stream.write(updated_content) env_path = PurePath(os.path.dirname(env_file), ".env") set_key(dotenv_path=env_path, key_to_set=GO_VERSION_KEY, value_to_set=go_version, quote_mode='never') return self.update_files_on_tree(head=master_tip, files_to_check=[go_version_file, - env_file], commit_message=commit_message, source_branch_name=source_branch_name) + env_file, go_mod_file], commit_message=commit_message, source_branch_name=source_branch_name) def update_files_on_tree(self, head: Union[Commit, GitCommit], files_to_check: list[str], commit_message: str, source_branch_name: diff --git a/.github/workflows/pr-to-update-go.yml b/.github/workflows/pr-to-update-go.yml index 10b735c699..17cff8913f 100644 --- a/.github/workflows/pr-to-update-go.yml +++ b/.github/workflows/pr-to-update-go.yml @@ -33,6 +33,7 @@ jobs: PR_GITHUB_TOKEN: ${{ secrets.ASFCI_TOKEN }} GO_VERSION_FILE: GO_VERSION ENV_FILE: .env + GO_MOD_FILE: go.mod steps: - name: Checkout repo uses: actions/checkout@master diff --git a/go.mod b/go.mod index 57082a6dcc..3fe2ba2ae5 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ module github.com/apache/trafficcontrol/v8 // specific language governing permissions and limitations // under the License. -go 1.23.0 +go 1.23.4 require ( code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5