feat(cli): rework developer CLI with non-blocking tasks and GitHub CI checks (#6651)
This commit is contained in:
1 parent
f0f9539466
commit
81aa704537
56 files changed
+7950
-2548
No files matched your search
@@ -7,11 +7,9 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/homarr-labs/homarr/tools/homarr-dev/internal/docker"
|
||||
"github.com/homarr-labs/homarr/tools/homarr-dev/internal/gh"
|
||||
)
|
||||
|
||||
func BuildCurrentImage(tag, directory string) error {
|
||||
@@ -19,11 +17,11 @@ func BuildCurrentImage(tag, directory string) error {
|
||||
}
|
||||
|
||||
func buildCurrentImage(ctx context.Context, tag, directory string, stdout, stderr io.Writer) error {
|
||||
root, revision, err := checkoutDetails(ctx, directory)
|
||||
options, err := CurrentBuildOptions(ctx, tag, directory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return docker.BuildContext(ctx, docker.BuildOptions{Context: root, Tag: tag, Source: root, Revision: revision}, stdout, stderr)
|
||||
return docker.BuildContext(ctx, options, stdout, stderr)
|
||||
}
|
||||
|
||||
func BuildPRImage(number int, tag string) error {
|
||||
@@ -31,44 +29,18 @@ func BuildPRImage(number int, tag string) error {
|
||||
}
|
||||
|
||||
func buildPRImage(ctx context.Context, number int, tag string, stdout, stderr io.Writer) error {
|
||||
if number <= 0 {
|
||||
return fmt.Errorf("PR number must be positive")
|
||||
}
|
||||
temporaryRoot, err := os.MkdirTemp("", "homarr-pr-*")
|
||||
if err != nil {
|
||||
return fmt.Errorf("create temporary checkout: %w", err)
|
||||
}
|
||||
defer os.RemoveAll(temporaryRoot)
|
||||
|
||||
checkout := filepath.Join(temporaryRoot, "homarr")
|
||||
clone := exec.CommandContext(ctx, "gh", prCloneArgs(checkout)...)
|
||||
clone.Stdout = stdout
|
||||
clone.Stderr = stderr
|
||||
if err := clone.Run(); err != nil {
|
||||
return fmt.Errorf("clone %s: %w", gh.Repo, err)
|
||||
}
|
||||
command := exec.CommandContext(ctx, "gh", "pr", "checkout", strconv.Itoa(number), "--detach")
|
||||
command.Dir = checkout
|
||||
command.Stdout = stdout
|
||||
command.Stderr = stderr
|
||||
if err := command.Run(); err != nil {
|
||||
return fmt.Errorf("checkout PR #%d: %w", number, err)
|
||||
}
|
||||
_, revision, err := checkoutDetails(ctx, checkout)
|
||||
options, cleanup, err := PRBuildOptions(ctx, number, tag, func(line string) {
|
||||
_, _ = fmt.Fprintln(stdout, line)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return docker.BuildContext(ctx, docker.BuildOptions{
|
||||
Context: checkout,
|
||||
Tag: tag,
|
||||
Source: fmt.Sprintf("https://github.com/%s/pull/%d", gh.Repo, number),
|
||||
Revision: revision,
|
||||
PRNumber: number,
|
||||
}, stdout, stderr)
|
||||
defer cleanup()
|
||||
return docker.BuildContext(ctx, options, stdout, stderr)
|
||||
}
|
||||
|
||||
func prCloneArgs(checkout string) []string {
|
||||
return []string{"repo", "clone", gh.Repo, checkout, "--", "--depth=1", "--filter=blob:none"}
|
||||
func prFetchArgs(repoURL, refSpec string) []string {
|
||||
return []string{"fetch", "--depth=1", repoURL, refSpec}
|
||||
}
|
||||
|
||||
func RebuildImage(image docker.Image) error {
|
||||
@@ -89,7 +61,16 @@ func RebuildImageContext(ctx context.Context, image docker.Image, stdout, stderr
|
||||
}
|
||||
|
||||
func checkoutDetails(ctx context.Context, directory string) (root string, revision string, err error) {
|
||||
rootOutput, err := exec.CommandContext(ctx, "git", "-C", directory, "rev-parse", "--show-toplevel").CombinedOutput()
|
||||
cmdRoot := exec.CommandContext(ctx, "git", "-C", directory, "rev-parse", "--show-toplevel")
|
||||
cmdRoot.Env = append(
|
||||
os.Environ(),
|
||||
"MISE_TRUSTED_CONFIG_PATHS=*",
|
||||
"MISE_QUIET=1",
|
||||
"MISE_SILENT=1",
|
||||
"MISE_YES=1",
|
||||
"MISE_OVERRIDE_CONFIG_FILENAMES=",
|
||||
)
|
||||
rootOutput, err := cmdRoot.CombinedOutput()
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("find Homarr checkout: %w: %s", err, strings.TrimSpace(string(rootOutput)))
|
||||
}
|
||||
@@ -97,7 +78,16 @@ func checkoutDetails(ctx context.Context, directory string) (root string, revisi
|
||||
if _, err := os.Stat(filepath.Join(root, "Dockerfile")); err != nil {
|
||||
return "", "", fmt.Errorf("%s is not a Homarr checkout", root)
|
||||
}
|
||||
revisionOutput, err := exec.CommandContext(ctx, "git", "-C", root, "rev-parse", "HEAD").CombinedOutput()
|
||||
cmdRev := exec.CommandContext(ctx, "git", "-C", root, "rev-parse", "HEAD")
|
||||
cmdRev.Env = append(
|
||||
os.Environ(),
|
||||
"MISE_TRUSTED_CONFIG_PATHS=*",
|
||||
"MISE_QUIET=1",
|
||||
"MISE_SILENT=1",
|
||||
"MISE_YES=1",
|
||||
"MISE_OVERRIDE_CONFIG_FILENAMES=",
|
||||
)
|
||||
revisionOutput, err := cmdRev.CombinedOutput()
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("read checkout revision: %w: %s", err, strings.TrimSpace(string(revisionOutput)))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
package run
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/homarr-labs/homarr/tools/homarr-dev/internal/docker"
|
||||
"github.com/homarr-labs/homarr/tools/homarr-dev/internal/gh"
|
||||
)
|
||||
|
||||
// Cleanup releases a temporary checkout. It is always safe to call.
|
||||
type Cleanup func()
|
||||
|
||||
func noCleanup() {}
|
||||
|
||||
// CurrentBuildOptions describes a build of the checkout that contains
|
||||
// directory, recording the source and revision on the image so it can be
|
||||
// rebuilt later without the user remembering where it came from.
|
||||
func CurrentBuildOptions(ctx context.Context, tag, directory string) (docker.BuildOptions, error) {
|
||||
root, revision, err := checkoutDetails(ctx, directory)
|
||||
if err != nil {
|
||||
return docker.BuildOptions{}, err
|
||||
}
|
||||
return docker.BuildOptions{Context: root, Tag: tag, Source: root, Revision: revision}, nil
|
||||
}
|
||||
|
||||
// PRBuildOptions clones a pull request into a temporary checkout and describes
|
||||
// a build of it. The returned Cleanup must run once the build has finished.
|
||||
//
|
||||
// Progress is reported through log rather than written to stdout, because the
|
||||
// caller is a background job rendering inside a TUI, not a shell.
|
||||
func PRBuildOptions(ctx context.Context, number int, tag string, log func(string)) (docker.BuildOptions, Cleanup, error) {
|
||||
if number <= 0 {
|
||||
return docker.BuildOptions{}, noCleanup, fmt.Errorf("PR number must be positive")
|
||||
}
|
||||
if log == nil {
|
||||
log = func(string) {}
|
||||
}
|
||||
temporaryRoot, err := os.MkdirTemp("", "homarr-pr-*")
|
||||
if err != nil {
|
||||
return docker.BuildOptions{}, noCleanup, fmt.Errorf("create temporary checkout: %w", err)
|
||||
}
|
||||
cleanup := func() { _ = os.RemoveAll(temporaryRoot) }
|
||||
|
||||
checkout := filepath.Join(temporaryRoot, "homarr")
|
||||
if err := os.MkdirAll(checkout, 0o755); err != nil {
|
||||
cleanup()
|
||||
return docker.BuildOptions{}, noCleanup, fmt.Errorf("create checkout directory: %w", err)
|
||||
}
|
||||
|
||||
repoURL := fmt.Sprintf("https://github.com/%s.git", gh.Repo)
|
||||
branchName := fmt.Sprintf("pr-%d", number)
|
||||
refSpec := fmt.Sprintf("refs/pull/%d/head:%s", number, branchName)
|
||||
|
||||
log(fmt.Sprintf("fetching PR #%d (shallow) from %s", number, gh.Repo))
|
||||
initCmd := exec.CommandContext(ctx, "git", "init", checkout)
|
||||
if err := runLogged(initCmd, log); err != nil {
|
||||
cleanup()
|
||||
return docker.BuildOptions{}, noCleanup, fmt.Errorf("init git repository: %w", err)
|
||||
}
|
||||
|
||||
fetchCmd := exec.CommandContext(ctx, "git", "-C", checkout, "fetch", "--depth", "1", repoURL, refSpec)
|
||||
if err := runLogged(fetchCmd, log); err != nil {
|
||||
cleanup()
|
||||
return docker.BuildOptions{}, noCleanup, fmt.Errorf("fetch PR #%d: %w", number, err)
|
||||
}
|
||||
|
||||
checkoutCmd := exec.CommandContext(ctx, "git", "-C", checkout, "checkout", branchName)
|
||||
if err := runLogged(checkoutCmd, log); err != nil {
|
||||
cleanup()
|
||||
return docker.BuildOptions{}, noCleanup, fmt.Errorf("checkout PR #%d: %w", number, err)
|
||||
}
|
||||
|
||||
_, revision, err := checkoutDetails(ctx, checkout)
|
||||
if err != nil {
|
||||
cleanup()
|
||||
return docker.BuildOptions{}, noCleanup, err
|
||||
}
|
||||
log("building " + revision[:min(len(revision), 12)])
|
||||
return docker.BuildOptions{
|
||||
Context: checkout,
|
||||
Tag: tag,
|
||||
Source: fmt.Sprintf("https://github.com/%s/pull/%d", gh.Repo, number),
|
||||
Revision: revision,
|
||||
PRNumber: number,
|
||||
}, cleanup, nil
|
||||
}
|
||||
|
||||
// RebuildOptions describes a rebuild of an existing local image from the
|
||||
// provenance recorded on it, whether that is a pull request or a checkout.
|
||||
func RebuildOptions(ctx context.Context, image docker.Image, log func(string)) (docker.BuildOptions, Cleanup, error) {
|
||||
if image.PRNumber > 0 {
|
||||
return PRBuildOptions(ctx, image.PRNumber, image.Tag, log)
|
||||
}
|
||||
if image.Source == "" {
|
||||
return docker.BuildOptions{}, noCleanup, fmt.Errorf("homarr:%s has no build source metadata", image.Tag)
|
||||
}
|
||||
if info, err := os.Stat(image.Source); err != nil || !info.IsDir() {
|
||||
return docker.BuildOptions{}, noCleanup, fmt.Errorf("build source %s is no longer available", image.Source)
|
||||
}
|
||||
options, err := CurrentBuildOptions(ctx, image.Tag, image.Source)
|
||||
return options, noCleanup, err
|
||||
}
|
||||
|
||||
func runLogged(command *exec.Cmd, log func(string)) error {
|
||||
command.Env = append(
|
||||
os.Environ(),
|
||||
"MISE_TRUSTED_CONFIG_PATHS=*",
|
||||
"MISE_QUIET=1",
|
||||
"MISE_SILENT=1",
|
||||
"MISE_YES=1",
|
||||
"MISE_OVERRIDE_CONFIG_FILENAMES=",
|
||||
)
|
||||
lines, err := docker.StreamCommand(command)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for line := range lines {
|
||||
if line.Done {
|
||||
return line.Err
|
||||
}
|
||||
log(line.Text)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -19,10 +19,10 @@ func TestPRPlanUsesAMD64(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPRCloneIsShallow(t *testing.T) {
|
||||
args := prCloneArgs("/tmp/checkout")
|
||||
if !slices.Contains(args, "--depth=1") || !slices.Contains(args, "--filter=blob:none") {
|
||||
t.Fatalf("clone args = %v", args)
|
||||
func TestPRFetchIsShallow(t *testing.T) {
|
||||
args := prFetchArgs("https://github.com/homarr-labs/homarr.git", "refs/pull/1/head:pr-1")
|
||||
if !slices.Contains(args, "--depth=1") {
|
||||
t.Fatalf("fetch args = %v", args)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user