From a6f45e01a76ea14985da97d054d6d63c2186a739 Mon Sep 17 00:00:00 2001 From: taoufik07 Date: Mon, 13 Feb 2023 21:18:11 +0100 Subject: [PATCH] add golang package mirroring --- pre_commit_mirror_maker/golang/go.mod | 3 +++ pre_commit_mirror_maker/golang/go.sum | 0 pre_commit_mirror_maker/golang/main.go | 3 +++ pre_commit_mirror_maker/languages.py | 18 ++++++++++++++++++ pre_commit_mirror_maker/make_repo.py | 2 +- setup.cfg | 1 + tests/languages_test.py | 7 +++++++ tests/make_repo_test.py | 22 ++++++++++++++++++++++ 8 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 pre_commit_mirror_maker/golang/go.mod create mode 100644 pre_commit_mirror_maker/golang/go.sum create mode 100644 pre_commit_mirror_maker/golang/main.go diff --git a/pre_commit_mirror_maker/golang/go.mod b/pre_commit_mirror_maker/golang/go.mod new file mode 100644 index 0000000..3166a70 --- /dev/null +++ b/pre_commit_mirror_maker/golang/go.mod @@ -0,0 +1,3 @@ +module github.com/pre-commit/dummy + +go 1.19 diff --git a/pre_commit_mirror_maker/golang/go.sum b/pre_commit_mirror_maker/golang/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/pre_commit_mirror_maker/golang/main.go b/pre_commit_mirror_maker/golang/main.go new file mode 100644 index 0000000..804b4cb --- /dev/null +++ b/pre_commit_mirror_maker/golang/main.go @@ -0,0 +1,3 @@ +package main + +func main() {{}} diff --git a/pre_commit_mirror_maker/languages.py b/pre_commit_mirror_maker/languages.py index e6aef84..aeee37c 100644 --- a/pre_commit_mirror_maker/languages.py +++ b/pre_commit_mirror_maker/languages.py @@ -4,6 +4,7 @@ import subprocess import urllib.request +import lxml.html from packaging import version @@ -31,6 +32,21 @@ def rust_get_package_versions(package_name: str) -> list[str]: return list(reversed([version['num'] for version in resp['versions']])) +def golang_get_package_versions(package_name: str) -> list[str]: + url = f'https://pkg.go.dev/{package_name}?tab=versions' + resp = urllib.request.urlopen(url) + versions = lxml.html.parse(resp).xpath( + "//a[@class='js-versionLink']//text()", + ) + return [str(version) for version in versions[::-1]] + + +def golang_get_additional_dependencies( + package_name: str, package_version: str, +) -> list[str]: + return [f'{package_name}@{package_version}'] + + def node_get_additional_dependencies( package_name: str, package_version: str, ) -> list[str]: @@ -48,9 +64,11 @@ def rust_get_additional_dependencies( 'python': python_get_package_versions, 'ruby': ruby_get_package_versions, 'rust': rust_get_package_versions, + 'golang': golang_get_package_versions, } ADDITIONAL_DEPENDENCIES = { 'node': node_get_additional_dependencies, 'rust': rust_get_additional_dependencies, + 'golang': golang_get_additional_dependencies, } diff --git a/pre_commit_mirror_maker/make_repo.py b/pre_commit_mirror_maker/make_repo.py index cb18ca4..1f481b9 100644 --- a/pre_commit_mirror_maker/make_repo.py +++ b/pre_commit_mirror_maker/make_repo.py @@ -66,7 +66,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.lstrip("v")}') def make_repo(repo: str, *, language: str, name: str, **fmt_vars: str) -> None: diff --git a/setup.cfg b/setup.cfg index 3300779..34b5535 100644 --- a/setup.cfg +++ b/setup.cfg @@ -20,6 +20,7 @@ classifiers = [options] packages = find: install_requires = + lxml packaging python_requires = >=3.7 diff --git a/tests/languages_test.py b/tests/languages_test.py index 8f5833f..c11115c 100644 --- a/tests/languages_test.py +++ b/tests/languages_test.py @@ -1,5 +1,6 @@ from __future__ import annotations +from pre_commit_mirror_maker.languages import golang_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 @@ -33,3 +34,9 @@ def test_rust_get_package_version_output(): ret = rust_get_package_versions('clap') assert ret assert_all_text(ret) + + +def test_golang_get_package_version_output(): + ret = golang_get_package_versions('golang.org/x/tools/cmd/goimports') + assert ret + assert_all_text(ret) diff --git a/tests/make_repo_test.py b/tests/make_repo_test.py index 7dd3b4c..f4b6b4b 100644 --- a/tests/make_repo_test.py +++ b/tests/make_repo_test.py @@ -249,3 +249,25 @@ def test_rust_integration(in_git_dir): assert _cmd('git', 'log', '--oneline') # TODO: test that the package is installable + + +def test_golang_integration(in_git_dir): + make_repo( + '.', + language='golang', name='mvdan.cc/gofumpt', description='', + entry='gofumpt', id='gofumpt', match_key='types', + match_val='go', args='', require_serial='false', + minimum_pre_commit_version='3.0.0', + ) + # Our files should exist + assert in_git_dir.join('.version').exists() + assert in_git_dir.join('.pre-commit-hooks.yaml').exists() + assert in_git_dir.join('go.mod').exists() + assert in_git_dir.join('main.go').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