Skip to content

Commit

Permalink
Merge pull request #24 from skilld-labs/buildincibydefault
Browse files Browse the repository at this point in the history
Changing default behavior to build in CI
  • Loading branch information
davidferlay authored Nov 22, 2024
2 parents 428fe0b + c70745a commit 8280bcf
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 101 deletions.
118 changes: 118 additions & 0 deletions git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package plasmactlmeta

import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/launchrctl/launchr"
)

// Checks for uncommitted changes and creates a commit if any are found
func commitChangesIfAny() error {

// Open the existing repository
repoPath, err := os.Getwd()
if err != nil {
log.Fatalf("failed to get current directory: %v", err)
}

repo, err := git.PlainOpen(repoPath)
if err != nil {
return fmt.Errorf("failed to open repository: %w", err)
}

// Get the working tree
worktree, err := repo.Worktree()
if err != nil {
return fmt.Errorf("failed to get worktree: %w", err)
}

// Check for uncommitted changes
status, err := worktree.Status()
if err != nil {
return fmt.Errorf("failed to get worktree status: %w", err)
}

if status.IsClean() {
launchr.Log().Debug("No changes to commit.")
return nil
}

launchr.Term().Info().Println("Unversioned changes detected. Creating commit...")

// Add all changes to the index
err = worktree.AddGlob(".")
if err != nil {
return fmt.Errorf("failed to stage changes: %w", err)
}

// Create a commit with the staged changes
commitMessage := "Work in progress"
commit, err := worktree.Commit(commitMessage, &git.CommitOptions{
Author: &object.Signature{
Name: "Plasmactl",
Email: "[email protected]",
When: time.Now(),
},
})
if err != nil {
return fmt.Errorf("failed to commit changes: %w", err)
}

// Print commit details
obj, err := repo.CommitObject(commit)
if err != nil {
return fmt.Errorf("failed to retrieve commit object: %w", err)
}

//fmt.Printf("Created %s\n", obj.String())
launchr.Term().Printf("Created commit %s\n", obj.Hash.String())
launchr.Term().Printf("Author: %s <%s>\n", obj.Author.Name, obj.Author.Email)
launchr.Term().Printf("Date: %s\n", obj.Author.When.Format("Mon Jan 2 15:04:05 2006 -0700"))
launchr.Term().Printf("Message: %s\n", obj.Message)
launchr.Term().Printf("\n")
return nil
}

// Checks for unpushed commits and pushes them if any are found
func pushCommitsIfAny() error {

// Check for un-pushed commits
cmdFetch := exec.Command("git", "fetch", "--quiet")
if err := cmdFetch.Run(); err != nil {
return fmt.Errorf("failed to fetch updates: %w", err)
}
cmdStatus := exec.Command("git", "status", "-sb")
var statusOut bytes.Buffer
cmdStatus.Stdout = &statusOut
if err := cmdStatus.Run(); err != nil {
return fmt.Errorf("failed to get git status: %w", err)
}

// Parse status output
status := statusOut.String()
if strings.Contains(status, "[ahead") {
launchr.Term().Info().Println("There are un-pushed commits: Pushing...")

// Push the commits
cmdPush := exec.Command("git", "push")
cmdPush.Stdout = &statusOut
cmdPush.Stderr = &statusOut
if err := cmdPush.Run(); err != nil {
return fmt.Errorf("failed to push commits: %w", err)
}
launchr.Term().Info().Println("Successfully pushed commits.")
launchr.Term().Printf("\n")
} else {
launchr.Log().Debug("No un-pushed commits found.")
}

return nil
}
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,34 @@ require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
dario.cat/mergo v1.0.0 // indirect
filippo.io/age v1.2.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/console v1.0.4 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v27.3.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.12.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
Expand All @@ -49,12 +60,16 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pterm/pterm v0.12.79 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
Expand All @@ -68,5 +83,6 @@ require (
golang.org/x/term v0.25.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 8280bcf

Please sign in to comment.