Skip to content

Commit b33379d

Browse files
committed
Support local version of Transmogrifier
Why these changes are being introduced: Formerly, only git commit SHAs from the remote Transmogrifier github repository could be used when initializing a job. Sometimes during development on Transmogrifier, it would be helpful to be able to run this application on local commits (potentially ones that will never see the Github light of day). How this addresses that need: * Adds new CLI arguments --location-a and --location-b that define where Transmogrifier should be cloned from, supporting local paths Side effects of this change: * None Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/TIMX-401
1 parent dddc0d1 commit b33379d

File tree

5 files changed

+67
-18
lines changed

5 files changed

+67
-18
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ Usage: -c init-job [OPTIONS]
171171
Options:
172172
-d, --job-directory TEXT Job directory to create. [required]
173173
-m, --message TEXT Message to describe Job.
174+
-la, --location-a TEXT Location to clone Transmogrifier version 'A'
174175
-a, --commit-sha-a TEXT Transmogrifier commit SHA for version 'A'
175176
[required]
177+
-lb, --location-b TEXT Location to clone Transmogrifier version 'B'
176178
-b, --commit-sha-b TEXT Transmogrifier commit SHA for version 'B'
177179
[required]
178180
-h, --help Show this message and exit.

abdiff/cli.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,29 @@ def ping() -> None:
8888
help="Message to describe Job.",
8989
default="Not provided.",
9090
)
91+
@click.option(
92+
"-la",
93+
"--location-a",
94+
type=str,
95+
required=False,
96+
default="https://github.com/MITLibraries/transmogrifier.git",
97+
help="Location to clone Transmogrifier version 'A'",
98+
)
9199
@click.option(
92100
"-a",
93101
"--commit-sha-a",
94102
type=str,
95103
required=True,
96104
help="Transmogrifier commit SHA for version 'A'",
97105
)
106+
@click.option(
107+
"-lb",
108+
"--location-b",
109+
type=str,
110+
required=False,
111+
default="https://github.com/MITLibraries/transmogrifier.git",
112+
help="Location to clone Transmogrifier version 'B'",
113+
)
98114
@click.option(
99115
"-b",
100116
"--commit-sha-b",
@@ -106,7 +122,9 @@ def init_job(
106122
job_directory: str,
107123
message: str,
108124
commit_sha_a: str,
125+
location_a: str,
109126
commit_sha_b: str,
127+
location_b: str,
110128
) -> None:
111129
"""Initialize a new Job."""
112130
try:
@@ -119,7 +137,9 @@ def init_job(
119137

120138
build_ab_images(
121139
job_directory,
140+
location_a,
122141
commit_sha_a,
142+
location_b,
123143
commit_sha_b,
124144
)
125145

abdiff/core/build_ab_images.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,34 @@
1515

1616
def build_ab_images(
1717
job_directory: str,
18+
location_a: str,
1819
commit_sha_a: str,
20+
location_b: str,
1921
commit_sha_b: str,
2022
docker_client: docker.client.DockerClient | None = None,
2123
) -> tuple[str, str]:
2224
"""Build Docker images based on 2 commit SHAs.
2325
2426
Args:
2527
job_directory: The directory containing all files related to a job.
28+
location_a: Location of Transmogrifier version 'A' to clone from.
2629
commit_sha_a: The SHA of the first commit for comparison.
30+
location_b: Location of Transmogrifier version 'B' to clone from.
2731
commit_sha_b: The SHA of the second commit for comparison.
2832
docker_client: A configured Docker client.
2933
"""
3034
if not docker_client:
3135
docker_client = docker.from_env()
3236

3337
image_tags = []
34-
for commit_sha in [commit_sha_a, commit_sha_b]:
38+
for location, commit_sha in [(location_a, commit_sha_a), (location_b, commit_sha_b)]:
3539
logger.debug(f"Processing commit: {commit_sha}")
3640
image_tag = generate_image_name(commit_sha)
3741
if docker_image_exists(docker_client, image_tag):
3842
logger.debug(f"Docker image already exists with tag: {image_tag}")
3943
image_tags.append(image_tag)
4044
else:
41-
image = build_image(commit_sha, docker_client)
45+
image = build_image(location, commit_sha, docker_client)
4246
image_tags.append(image.tags[0])
4347
logger.debug(f"Finished processing commit: {commit_sha}")
4448

@@ -67,43 +71,44 @@ def docker_image_exists(
6771

6872

6973
def build_image(
74+
location: str,
7075
commit_sha: str,
7176
docker_client: docker.client.DockerClient,
7277
) -> docker.models.images.Image:
7378
"""Clone repo and build Docker image.
7479
7580
Args:
76-
job_directory: The directory containing all files related to a job.
81+
location: Location of Transmogrifier to clone from.
7782
commit_sha: The SHA of the commit.
7883
docker_client: A configured Docker client.
7984
"""
8085
with tempfile.TemporaryDirectory() as clone_directory:
8186
image_tag = generate_image_name(commit_sha)
82-
clone_repo_and_reset_to_commit(clone_directory, commit_sha)
87+
clone_repo_and_reset_to_commit(location, clone_directory, commit_sha)
8388
image, _ = docker_client.images.build(path=clone_directory, tag=image_tag)
8489
logger.debug(f"Docker image created with tag: {image}")
8590
return image
8691

8792

88-
def clone_repo_and_reset_to_commit(clone_directory: str, commit_sha: str) -> None:
93+
def clone_repo_and_reset_to_commit(
94+
location: str,
95+
clone_directory: str,
96+
commit_sha: str,
97+
) -> None:
8998
"""Clone GitHub repo and reset to a specified commit.
9099
91100
Args:
101+
location: Location of Transmogrifier to clone from.
92102
clone_directory: The directory for the cloned repo.
93103
commit_sha: The SHA of a repo commit.
94104
"""
95-
logger.debug(f"Cloning repo to: {clone_directory}")
96-
transmogrifier_url = "https://github.com/MITLibraries/transmogrifier.git"
105+
logger.debug(f"Cloning repo from: {location}, to: {clone_directory}")
97106
repository = clone_repository(
98-
transmogrifier_url,
107+
location,
99108
clone_directory,
100109
)
101-
logger.debug(f"Cloned repo to: {clone_directory}")
102-
103110
try:
104111
repository.reset(commit_sha, ResetMode.HARD)
105112
logger.debug(f"Cloned repo reset to commit: {commit_sha}")
106113
except KeyError as exception:
107-
raise InvalidRepositoryCommitSHAError(
108-
transmogrifier_url, commit_sha
109-
) from exception
114+
raise InvalidRepositoryCommitSHAError(location, commit_sha) from exception

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ def job_directory(tmp_path):
152152
return str(tmp_path / "example-job-1")
153153

154154

155+
@pytest.fixture
156+
def default_transmogrifier_location():
157+
return "https://github.com/MITLibraries/transmogrifier.git"
158+
159+
155160
@pytest.fixture
156161
def example_job_directory(tmp_path):
157162
"""Copy example job from fixtures to tmp path where it will be modified during test"""

tests/test_build_ab_images.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515

1616
@patch("abdiff.core.build_ab_images.clone_repository")
17-
def test_build_ab_images_success(mocked_clone, job_directory, mocked_docker_client):
17+
def test_build_ab_images_success(
18+
mocked_clone, job_directory, mocked_docker_client, default_transmogrifier_location
19+
):
1820
side_effect = os.makedirs(job_directory + "/clone")
1921
mocked_clone.side_effect = side_effect
2022

2123
images = build_ab_images(
2224
job_directory,
25+
default_transmogrifier_location,
2326
"abc123",
27+
default_transmogrifier_location,
2428
"def456",
2529
mocked_docker_client,
2630
)
@@ -34,7 +38,11 @@ def test_build_ab_images_success(mocked_clone, job_directory, mocked_docker_clie
3438

3539
@patch("abdiff.core.build_ab_images.clone_repository")
3640
def test_build_ab_images_invalid_commit_sha_raise_error(
37-
mocked_clone, job_directory, mocked_docker_client, caplog
41+
mocked_clone,
42+
job_directory,
43+
mocked_docker_client,
44+
caplog,
45+
default_transmogrifier_location,
3846
):
3947
caplog.set_level("DEBUG")
4048
side_effect = os.makedirs(job_directory + "/clone")
@@ -44,7 +52,9 @@ def test_build_ab_images_invalid_commit_sha_raise_error(
4452
with pytest.raises(InvalidRepositoryCommitSHAError):
4553
build_ab_images(
4654
job_directory,
55+
default_transmogrifier_location,
4756
"invalid",
57+
default_transmogrifier_location,
4858
"def456",
4959
mocked_docker_client,
5060
)
@@ -69,20 +79,27 @@ def test_docker_image_exists_returns_false(mocked_docker_client):
6979

7080

7181
@patch("abdiff.core.build_ab_images.clone_repository")
72-
def test_build_image_success(mocked_clone, mocked_docker_client):
82+
def test_build_image_success(
83+
mocked_clone, mocked_docker_client, default_transmogrifier_location
84+
):
7385
image = build_image(
86+
default_transmogrifier_location,
7487
"abc123",
7588
mocked_docker_client,
7689
)
7790
assert image.tags[0] == "transmogrifier-example-job-1-abc123:latest"
7891

7992

8093
@patch("abdiff.core.build_ab_images.clone_repository")
81-
def test_clone_repo_and_reset_to_commit_success(mocked_clone, job_directory):
94+
def test_clone_repo_and_reset_to_commit_success(
95+
mocked_clone, job_directory, default_transmogrifier_location
96+
):
8297
clone_directory = job_directory + "/clone"
8398
assert not os.path.exists(clone_directory)
8499
side_effect = os.makedirs(clone_directory)
85100
mocked_clone.side_effect = side_effect
86101

87-
clone_repo_and_reset_to_commit(clone_directory, "abc123")
102+
clone_repo_and_reset_to_commit(
103+
default_transmogrifier_location, clone_directory, "abc123"
104+
)
88105
assert os.path.exists(clone_directory)

0 commit comments

Comments
 (0)