Skip to content

Add docker_image mirroring #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pre_commit_mirror_maker/docker_image/.pre-commit-hooks.yaml
Copy link
Author

Choose a reason for hiding this comment

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

This files overwrites the all/.pre-commit-hooks.yaml. It think that's nice and intuitive, but depends on this line to stay in that order.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- id: {id}
name: {name}
description: {description!r}
entry: '{entry}:{version}'
language: {language}
'{match_key}': {match_val}
args: {args}
require_serial: {require_serial}
additional_dependencies: {additional_dependencies}
minimum_pre_commit_version: {minimum_pre_commit_version!r}
25 changes: 25 additions & 0 deletions pre_commit_mirror_maker/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ def rust_get_package_versions(package_name: str) -> list[str]:
return list(reversed([version['num'] for version in resp['versions']]))


def docker_image_get_package_versions(package_name: str) -> list[str]:
# If package_name contains a slash, it's a user/org repository,
# otherwise it's an official repository
if '/' not in package_name:
package_name = f'library/{package_name}'
base_url = (
'https://hub.docker.com/v2/repositories/'
f'{package_name}/tags?page_size=100'
)

all_tags: list[str] = []
url = base_url

# Fetch all pages of results
while url:
resp = json.load(urllib.request.urlopen(url))
# Add tags from the current page
all_tags.extend(tag['name'] for tag in resp['results'])
# Get URL for next page, if any
url = resp.get('next')

return list(reversed(all_tags))


def node_get_additional_dependencies(
package_name: str, package_version: str,
) -> list[str]:
Expand All @@ -46,6 +70,7 @@ def rust_get_additional_dependencies(


LIST_VERSIONS = {
'docker_image': docker_image_get_package_versions,
'node': node_get_package_versions,
'python': python_get_package_versions,
'ruby': ruby_get_package_versions,
Expand Down
2 changes: 1 addition & 1 deletion pre_commit_mirror_maker/make_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def git(*cmd: str) -> None:
# Commit and tag
git('add', '.')
git('commit', '-m', f'Mirror: {version}')
git('tag', f'v{version}')
git('tag', f'v{version}' if language != 'docker_image' else version)
Copy link
Author

Choose a reason for hiding this comment

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

This may be a controversial decision, but I want to keep the git tags in sync with the docker tags.



def make_repo(repo: str, *, language: str, name: str, **fmt_vars: str) -> None:
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ console_scripts =
pre_commit_mirror_maker =
all/.pre-commit-hooks.yaml
all/.version
docker_image/.pre-commit-hooks.yaml
node/.npmignore
node/*
python/*
Expand Down
7 changes: 7 additions & 0 deletions tests/languages_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from pre_commit_mirror_maker.languages import docker_image_get_package_versions
from pre_commit_mirror_maker.languages import node_get_package_versions
from pre_commit_mirror_maker.languages import python_get_package_versions
from pre_commit_mirror_maker.languages import ruby_get_package_versions
Expand Down Expand Up @@ -39,3 +40,9 @@ def test_rust_get_package_version_output():
ret = rust_get_package_versions('clap')
assert ret
assert_all_text(ret)


def test_docker_image_get_package_version_output():
ret = docker_image_get_package_versions('alpine/flake8')
assert ret
assert_all_text(ret)
20 changes: 20 additions & 0 deletions tests/make_repo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,23 @@ def test_rust_integration(in_git_dir):
assert _cmd('git', 'log', '--oneline')

# TODO: test that the package is installable


def test_docker_image_integration(in_git_dir):
make_repo(
'.',
language='docker_image', name='alpine/flake8', description='',
entry='alpine/flake8', id='flake8-docker', match_key='files',
match_val=r'\.py$', args='[]', require_serial='false',
minimum_pre_commit_version='0',
)
# Our files should exist
assert in_git_dir.join('.version').exists()
assert in_git_dir.join('.pre-commit-hooks.yaml').exists()

# Should have made _some_ tags
assert _cmd('git', 'tag', '-l')
# Should have made _some_ commits
assert _cmd('git', 'log', '--oneline')

# TODO: test that the package is installable
Loading