From 947d94e0a9f7f42977beb42d76a73b6d83a6ef46 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 15 Nov 2022 14:37:08 +0100 Subject: [PATCH] deploy: support GitLab Job Artifacts as a mirror --- fdroidserver/index.py | 20 ++++++++++++++++++++ tests/index.TestCase | 34 +++++++++++++++++++++++++++------- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/fdroidserver/index.py b/fdroidserver/index.py index 500c2e9e..59610d53 100644 --- a/fdroidserver/index.py +++ b/fdroidserver/index.py @@ -1453,6 +1453,26 @@ def get_mirror_service_urls(url): # GitLab Raw "https://gitlab.com/user/repo/-/raw/branch/folder" gitlab_raw = segments + ['-', 'raw', branch, folder] urls.append('/'.join(gitlab_raw)) + # GitLab Artifacts "https://user.gitlab.io/-/repo/-/jobs/job_id/artifacts/public/folder" + job_id = os.getenv('CI_JOB_ID') + try: + int(job_id) + gitlab_artifacts = [ + "https:", + "", + user + ".gitlab.io", + '-', + repo, + '-', + 'jobs', + job_id, + 'artifacts', + 'public', + folder, + ] + urls.append('/'.join(gitlab_artifacts)) + except (TypeError, ValueError): + pass # no Job ID to use, ignore return urls diff --git a/tests/index.TestCase b/tests/index.TestCase index 6e63ffcf..1c8e9854 100755 --- a/tests/index.TestCase +++ b/tests/index.TestCase @@ -414,10 +414,16 @@ class IndexTest(unittest.TestCase): fdroidserver.index.get_mirror_service_urls(url), ) + @patch.dict(os.environ, clear=True) def test_gitlab_get_mirror_service_urls(self): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): git_mirror_path = Path('git-mirror/fdroid') git_mirror_path.mkdir(parents=True) + ci_job_id = '12345678' + artifacts_url = ( + 'https://group.gitlab.io/-/project/-/jobs/%s/artifacts/public/fdroid' + % ci_job_id + ) with (git_mirror_path / 'placeholder').open('w') as fp: fp.write(' ') for url in [ @@ -427,20 +433,34 @@ class IndexTest(unittest.TestCase): 'https://gitlab.com/group/project.git', ]: with patch('fdroidserver.common.GITLAB_COM_PAGES_MAX_SIZE', 1000): + expected = [ + 'https://group.gitlab.io/project/fdroid', + 'https://gitlab.com/group/project/-/raw/master/fdroid', + ] self.assertEqual( - [ - 'https://group.gitlab.io/project/fdroid', - 'https://gitlab.com/group/project/-/raw/master/fdroid', - ], + expected, fdroidserver.index.get_mirror_service_urls(url), ) + with patch.dict(os.environ, clear=True): + os.environ['CI_JOB_ID'] = ci_job_id + self.assertEqual( + expected + [artifacts_url], + fdroidserver.index.get_mirror_service_urls(url), + ) with patch('fdroidserver.common.GITLAB_COM_PAGES_MAX_SIZE', 10): + expected = [ + 'https://gitlab.com/group/project/-/raw/master/fdroid', + ] self.assertEqual( - [ - 'https://gitlab.com/group/project/-/raw/master/fdroid', - ], + expected, fdroidserver.index.get_mirror_service_urls(url), ) + with patch.dict(os.environ, clear=True): + os.environ['CI_JOB_ID'] = ci_job_id + self.assertEqual( + expected + [artifacts_url], + fdroidserver.index.get_mirror_service_urls(url), + ) def test_make_website(self): tmptestsdir = tempfile.mkdtemp(