Skip to content

Commit b747d8e

Browse files
authored
Fix handling registry image 404 (#2691)
The caller of the modified function: ``` registryImage, imageID, err := fetchRegistryImageAndImageID(p) if err != nil { return release.PluginRelease{}, false, err } if registryImage == "" || imageID == "" { c.logger.InfoContext(ctx, "unable to detect registry image and image ID", slog.String("owner", identity.Owner()), slog.String("plugin", identity.Plugin()), slog.String("version", p.PluginVersion), ) return release.PluginRelease{}, true, nil } ``` So we need `fetchRegistryImageAndImageID` to be able to return `"", "", nil`
1 parent c537abe commit b747d8e

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

‎internal/cmd/release/main.go‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io/fs"
1010
"log/slog"
11+
"net/http"
1112
"os"
1213
"os/exec"
1314
"path/filepath"
@@ -22,6 +23,7 @@ import (
2223
githubkeychain "github.com/google/go-containerregistry/pkg/authn/github"
2324
"github.com/google/go-containerregistry/pkg/name"
2425
"github.com/google/go-containerregistry/pkg/v1/remote"
26+
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
2527
"github.com/google/go-github/v72/github"
2628
"github.com/spf13/pflag"
2729
"golang.org/x/mod/semver"
@@ -590,12 +592,32 @@ func (c *command) pluginReleasesURL(releaseName string) string {
590592

591593
func fetchRegistryImageAndImageID(plugin *plugin.Plugin) (string, string, error) {
592594
identity := plugin.Identity
593-
imageName := fmt.Sprintf("ghcr.io/%s/plugins-%s-%s:%s", release.GithubOwnerBufbuild, identity.Owner(), identity.Plugin(), plugin.PluginVersion)
595+
imageName := fmt.Sprintf(
596+
"ghcr.io/%s/plugins-%s-%s:%s",
597+
release.GithubOwnerBufbuild,
598+
identity.Owner(),
599+
identity.Plugin(),
600+
plugin.PluginVersion,
601+
)
602+
registryImage, imageID, err := fetchImageAndImageIDFromRegistry(
603+
imageName,
604+
remote.WithAuthFromKeychain(githubkeychain.Keychain),
605+
)
606+
if err != nil {
607+
if isImageNotFoundError(err) {
608+
return "", "", nil
609+
}
610+
return "", "", err
611+
}
612+
return registryImage, imageID, nil
613+
}
614+
615+
func fetchImageAndImageIDFromRegistry(imageName string, options ...remote.Option) (string, string, error) {
594616
parsedName, err := name.ParseReference(imageName)
595617
if err != nil {
596618
return "", "", err
597619
}
598-
remoteImage, err := remote.Image(parsedName, remote.WithAuthFromKeychain(githubkeychain.Keychain))
620+
remoteImage, err := remote.Image(parsedName, options...)
599621
if err != nil {
600622
return "", "", err
601623
}
@@ -609,3 +631,11 @@ func fetchRegistryImageAndImageID(plugin *plugin.Plugin) (string, string, error)
609631
}
610632
return fmt.Sprintf("%s@%s", imageName, remoteDigest.String()), manifest.Config.Digest.String(), nil
611633
}
634+
635+
func isImageNotFoundError(err error) bool {
636+
var transportErr *transport.Error
637+
if !errors.As(err, &transportErr) {
638+
return false
639+
}
640+
return transportErr.StatusCode == http.StatusNotFound
641+
}

0 commit comments

Comments
 (0)