Skip to content

Commit

Permalink
fix: [#84] Update Dockerfile recursively (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbp-bvanb authored Nov 24, 2024
1 parent 8009c22 commit 96b1830
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
10 changes: 6 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ runs:
if [ -n "$(git status --porcelain)" ]; then echo "There are uncommitted changes."; else echo "No changes to commit." && exit 0; fi
for f in Dockerfile go.mod; do
echo "git add file: '${f}'..."
[ -f ${f} ] && git add ${f}
done
go_mod_file="go.mod"
echo "adding ${go_mod_file} file..."
[ -f "${go_mod_file}" ] && git add "${go_mod_file}"
echo "adding nested Dockerfiles..."
find . -name 'Dockerfile' -exec git add {} +
echo "git config user..."
git config user.name github-actions[bot]
Expand Down
31 changes: 23 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,25 @@ def configure_logging(level=logging.INFO):
)


def update_dockerfile_version(new_major: str, new_minor: str, new_patch: str):
if not os.path.exists(DOCKERFILE):
logging.error(f"The file '{DOCKERFILE}' does not exist.")
return

with open(DOCKERFILE, "r") as file:
def update_dockerfile_version_in_directory(
new_major: str, new_minor: str, new_patch: str
):
for root, _, files in os.walk(os.getcwd()):
for file in files:
if file == DOCKERFILE:
dockerfile_path = os.path.join(root, file)
update_dockerfile_version(
dockerfile_path,
new_major,
new_minor,
new_patch,
)


def update_dockerfile_version(
dockerfile_path: str, new_major: str, new_minor: str, new_patch: str
):
with open(dockerfile_path, "r") as file:
lines = file.readlines()

updated_lines = []
Expand All @@ -90,7 +103,7 @@ def update_dockerfile_version(new_major: str, new_minor: str, new_patch: str):
else:
updated_lines.append(line)

with open(DOCKERFILE, "w") as file:
with open(dockerfile_path, "w") as file:
file.writelines(updated_lines)
logging.info(
f"Updated Dockerfile to version {new_major}.{new_minor}.{new_patch}"
Expand All @@ -101,7 +114,9 @@ def main():
configure_logging(LOGGING_LEVEL)
latest_major, latest_minor, latest_patch = get_latest_go_version()

update_dockerfile_version(latest_major, latest_minor, latest_patch)
update_dockerfile_version_in_directory(
latest_major, latest_minor, latest_patch
)

current_version, has_patch = get_go_version_from_mod_file()
latest_major_minor = f"{latest_major}.{latest_minor}"
Expand Down
30 changes: 21 additions & 9 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
get_latest_go_version,
main,
)
from unittest.mock import mock_open, patch, MagicMock
from unittest.mock import patch, MagicMock
import logging
import requests
import pytest

GO_VERSIONS_URL = "https://mocked-url.com"
TEST_NESTED_DOCKERFILE = "test/testdata/" + DOCKERFILE
logging.basicConfig(level=logging.INFO)


Expand All @@ -30,11 +31,25 @@ def read_version_from_file(filepath: str, pattern: str) -> str:


def setup_file_with_version(filepath: str, content: str):
directory = os.path.dirname(filepath)

if not os.path.exists(directory) and directory != "":
os.makedirs(directory)

with open(filepath, "w") as file:
file.write(content)
logging.info(f"Created {filepath} with content:\n{content}")


def setup_file_with_version_and_test(self: any, filepath: str):
setup_file_with_version(filepath, "FROM golang:4.2.0\nsome line\n")
main()
self.assertEqual(
read_version_from_file(filepath, r"FROM\sgolang:(\d+\.\d+\.?\d+?)"),
f"{self.latest_major}.{self.latest_minor}.{self.latest_patch}",
)


def cleanup_files(*filepaths):
for filepath in filepaths:
try:
Expand Down Expand Up @@ -111,16 +126,13 @@ class TestUpdateGolangVersionInDockerfile(unittest.TestCase):

def tearDown(self):
cleanup_files(DOCKERFILE)
cleanup_files(TEST_NESTED_DOCKERFILE)

def test_update_version_in_dockerfile_major_minor_patch(self):
setup_file_with_version(DOCKERFILE, "FROM golang:4.2.0\nsome line\n")
main()
self.assertEqual(
read_version_from_file(
DOCKERFILE, r"FROM\sgolang:(\d+\.\d+\.?\d+?)"
),
f"{self.latest_major}.{self.latest_minor}.{self.latest_patch}",
)
setup_file_with_version_and_test(self, DOCKERFILE)

def test_update_version_in_nested_dockerfile_major_minor_patch(self):
setup_file_with_version_and_test(self, TEST_NESTED_DOCKERFILE)


if __name__ == "__main__":
Expand Down

0 comments on commit 96b1830

Please sign in to comment.