From 98e188a32f0124836c976bb812b1990f3f6a26a3 Mon Sep 17 00:00:00 2001 From: Louis Orleans Date: Fri, 1 Sep 2023 12:02:56 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20generalize=20image=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/scripts/get-image-tags.rb | 8 ++- .github/workflows/scripts/lib.rb | 18 ++++-- .../scripts/test/get-image-tags.unit.rb | 57 +++++++++++-------- 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/.github/workflows/scripts/get-image-tags.rb b/.github/workflows/scripts/get-image-tags.rb index 0bf30cc..50746e3 100755 --- a/.github/workflows/scripts/get-image-tags.rb +++ b/.github/workflows/scripts/get-image-tags.rb @@ -12,14 +12,18 @@ def main # log to stderr so that stdout only contains the full tags $stderr.puts "'#{git_repo}', '#{git_ref_name}', '#{git_ref_type}', '#{git_default_branch}'" + image_name = get_image_name(git_repo: git_repo) + tags = get_image_tags( - git_repo: git_repo, git_ref_name: git_ref_name, git_ref_type: git_ref_type, git_default_branch: git_default_branch, semver: '0.0.0', - ).to_a.join(',') + ) + .to_a + .map {|tag| "#{image_name}:#{tag}" } + .join(',') # log to stderr so that stdout only contains the full tags $stderr.puts tags diff --git a/.github/workflows/scripts/lib.rb b/.github/workflows/scripts/lib.rb index 462ea24..56dc625 100644 --- a/.github/workflows/scripts/lib.rb +++ b/.github/workflows/scripts/lib.rb @@ -1,18 +1,15 @@ require 'set' -# @param git_repo [String] # @param git_ref_name [String] # @param git_ref_type [String] # @param git_default_branch [String] # @return [Set[String]] def get_image_tags( - git_repo: nil, git_ref_name: nil, git_ref_type: nil, git_default_branch: nil, semver: nil ) - container_repo = "ghcr.io/#{git_repo.downcase}" versions = Set[] if git_ref_type == 'branch' @@ -24,7 +21,7 @@ def get_image_tags( # TODO: check that this is actually latest parsed = parse_semver(semver) if parsed.pre == nil - versions.add(parsed.major) + versions.add(parsed.major.to_s) versions.add("#{parsed.major}.#{parsed.minor}") versions.add("#{parsed.major}.#{parsed.minor}.#{parsed.patch}") end @@ -39,7 +36,18 @@ def get_image_tags( versions.add('latest') end - return versions.map! { |v| "#{container_repo}/bot:#{v}" } + return versions +end + +# @param registry [String] +# @param git_repo [String] +# @param sub_image [String?] +# @return String +def get_image_name(registry: 'ghcr.io', git_repo: nil, sub_image: nil) + git_repo = git_repo.downcase + + default_sub_image = File.basename git_repo + container_repo = "#{registry}/#{git_repo}/#{sub_image ? sub_image : default_sub_image}" end Semver = Struct.new('Semver', :major, :minor, :patch, :pre, :build) diff --git a/.github/workflows/scripts/test/get-image-tags.unit.rb b/.github/workflows/scripts/test/get-image-tags.unit.rb index 6490d84..15536b4 100755 --- a/.github/workflows/scripts/test/get-image-tags.unit.rb +++ b/.github/workflows/scripts/test/get-image-tags.unit.rb @@ -1,3 +1,5 @@ +#!/usr/bin/env ruby + require 'test/unit' require 'json' require 'set' @@ -7,9 +9,8 @@ require_relative '../lib' class TestGetImageTags < Test::Unit::TestCase def test_simple_branch assert_equal( - Set['ghcr.io/virginity-bot/virginity-bot/bot:feat-foo-bar'], + Set['feat-foo-bar'], get_image_tags( - git_repo: 'Virginity-Bot/virginity-bot', git_ref_name: 'feat/foo-bar', git_ref_type: 'branch', git_default_branch: 'master', @@ -18,12 +19,8 @@ class TestGetImageTags < Test::Unit::TestCase ) assert_equal( - Set[ - 'ghcr.io/virginity-bot/virginity.bot/bot:latest', - 'ghcr.io/virginity-bot/virginity.bot/bot:master' - ], + Set['latest', 'master'], get_image_tags( - git_repo: 'Virginity-Bot/virginity.bot', git_ref_name: 'master', git_ref_type: 'branch', git_default_branch: 'master', @@ -34,15 +31,8 @@ class TestGetImageTags < Test::Unit::TestCase def test_simple_tag assert_equal( - Set[ - 'ghcr.io/virginity-bot/virginity.bot/bot:latest', - 'ghcr.io/virginity-bot/virginity.bot/bot:master', - 'ghcr.io/virginity-bot/virginity.bot/bot:1.0.0', - 'ghcr.io/virginity-bot/virginity.bot/bot:1.0', - 'ghcr.io/virginity-bot/virginity.bot/bot:1' - ], + Set['latest', 'master', '1.0.0', '1.0', '1'], get_image_tags( - git_repo: 'Virginity-Bot/virginity.bot', git_ref_name: '1.0.0', git_ref_type: 'tag', git_default_branch: 'master', @@ -53,13 +43,8 @@ class TestGetImageTags < Test::Unit::TestCase def test_pre_tag assert_equal( - Set[ - 'ghcr.io/virginity-bot/virginity.bot/bot:latest', - 'ghcr.io/virginity-bot/virginity.bot/bot:master', - 'ghcr.io/virginity-bot/virginity.bot/bot:1.0.0-pre' - ], + Set['latest', 'master', '1.0.0-pre'], get_image_tags( - git_repo: 'Virginity-Bot/virginity.bot', git_ref_name: '1.0.0', git_ref_type: 'tag', git_default_branch: 'master', @@ -70,9 +55,8 @@ class TestGetImageTags < Test::Unit::TestCase def test_unsafe_branch_name assert_equal( - Set['ghcr.io/virginity-bot/virginity.bot/bot:feat-foo-bar'], + Set['feat-foo-bar'], get_image_tags( - git_repo: 'Virginity-Bot/virginity.bot', git_ref_name: 'feat/Foo---bar', git_ref_type: 'branch', git_default_branch: 'master', @@ -82,6 +66,33 @@ class TestGetImageTags < Test::Unit::TestCase end end +class TestGetImageName < Test::Unit::TestCase + def test_basic + assert_equal( + 'ghcr.io/octocat/hello-world/hello-world', + get_image_name( + git_repo: 'Octocat/hello-world', + ), + ) + + assert_equal( + 'ghcr.io/octocat/hello-world/foobar', + get_image_name( + git_repo: 'Octocat/hello-world', + sub_image: 'foobar', + ), + ) + + assert_equal( + 'docker.io/octocat/hello-world/hello-world', + get_image_name( + registry: 'docker.io', + git_repo: 'Octocat/hello-world', + ), + ) + end +end + class TestParseSemver < Test::Unit::TestCase def test_parse_basic parsed = parse_semver('1.2.3')