Skip to content

Commit

Permalink
#67: Bump apply latest commit version to all previous resources regar…
Browse files Browse the repository at this point in the history
…dless of commit in which they were actualy updated
  • Loading branch information
iignatevich committed Dec 11, 2024
1 parent fb28c85 commit 8a9cfbf
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 63 deletions.
92 changes: 45 additions & 47 deletions actionBump.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,18 @@ func (b *BumpAction) Execute() error {
return nil
}

files, err := bumper.GetModifiedFiles(b.last)
commits, err := bumper.GetCommits(b.last)
if err != nil {
return err
}

resources := b.collectResources(files)
resources := b.collectResources(commits)
if len(resources) == 0 {
launchr.Term().Info().Println("No resource to update")
return nil
}

version, err := bumper.GetLastCommitShortHash()
if err != nil {
launchr.Log().Error("Can't retrieve commit hash")
return err
}

err = b.updateResources(resources, version)
err = b.updateResources(resources)
if err != nil {
launchr.Log().Error("There is an error during resources update")
return err
Expand All @@ -71,61 +65,65 @@ func (b *BumpAction) Execute() error {
return bumper.Commit()
}

func (b *BumpAction) collectResources(files []string) map[string]*sync.Resource {
// @TODO re-use inventory.GetChangedResources()
resources := make(map[string]*sync.Resource)
for _, path := range files {
if !isVersionableFile(path) {
continue
}
func (b *BumpAction) getResource(path string) *sync.Resource {
if !isVersionableFile(path) {
return nil
}

platform, kind, role, err := sync.ProcessResourcePath(path)
if err != nil {
continue
}
return sync.BuildResourceFromPath(path, ".")
}

if platform == "" || kind == "" || role == "" {
continue
}
func (b *BumpAction) collectResources(commits []*repository.Commit) map[string]map[string]*sync.Resource {
uniqueVersion := map[string]string{}

if sync.IsUpdatableKind(kind) {
resource := sync.NewResource(sync.PrepareMachineResourceName(platform, kind, role), "")
if _, ok := resources[resource.GetName()]; !ok {
// Check is meta/plasma.yaml exists for resource
if !resource.IsValidResource() {
continue
}
resources := make(map[string]map[string]*sync.Resource)
for _, c := range commits {
hash := c.Hash[:13]
for _, path := range c.Files {
resource := b.getResource(path)
if resource == nil {
continue
}

launchr.Term().Printfln("Processing resource %s", resource.GetName())
resources[resource.GetName()] = resource
if _, ok := resources[hash]; !ok {
resources[hash] = make(map[string]*sync.Resource)
}

if _, ok := uniqueVersion[resource.GetName()]; ok {
continue
}
}

launchr.Term().Printfln("Processing resource %s", resource.GetName())
resources[hash][resource.GetName()] = resource
uniqueVersion[resource.GetName()] = hash
}
}

return resources
}

func (b *BumpAction) updateResources(resources map[string]*sync.Resource, version string) error {
if len(resources) == 0 {
func (b *BumpAction) updateResources(hashResourcesMap map[string]map[string]*sync.Resource) error {
if len(hashResourcesMap) == 0 {
return nil
}

launchr.Term().Printf("Updating versions:\n")
for _, r := range resources {
currentVersion, err := r.GetVersion()
if err != nil {
return err
}
for version, resources := range hashResourcesMap {
for mrn, r := range resources {
currentVersion, err := r.GetVersion()
if err != nil {
return err
}

launchr.Term().Printfln("- %s from %s to %s", r.GetName(), currentVersion, version)
if b.dryRun {
continue
}
launchr.Term().Printfln("- %s from %s to %s", mrn, currentVersion, version)
if b.dryRun {
continue
}

err = r.UpdateVersion(version)
if err != nil {
return err
err = r.UpdateVersion(version)
if err != nil {
return err
}
}
}

Expand Down
46 changes: 30 additions & 16 deletions pkg/repository/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ type Bumper struct {
commitMessage string
}

// Commit stores commits hash and list of modified files in it.
type Commit struct {
Hash string
Files []string
}

// NewBumper returns new instance of [Bumper].
func NewBumper() (*Bumper, error) {
r, err := git.PlainOpen("./")
Expand Down Expand Up @@ -67,19 +73,9 @@ func (r *Bumper) IsOwnCommit() bool {
return r.name == commit.Author.Name && r.mail == commit.Author.Email
}

// GetLastCommitShortHash gets the short hash of the latest commit in the Git repository.
func (r *Bumper) GetLastCommitShortHash() (string, error) {
ref, err := r.git.Head()
if err != nil {
return "", err
}

return ref.Hash().String()[:13], nil
}

// GetModifiedFiles gets a list of files modified in the Git repository commits after last Bump.
func (r *Bumper) GetModifiedFiles(last bool) ([]string, error) {
var modifiedFiles []string
// GetCommits gets a list of commits before Bump.
func (r *Bumper) GetCommits(last bool) ([]*Commit, error) {
var result []*Commit

headRef, err := r.git.Head()
if err != nil {
Expand Down Expand Up @@ -117,6 +113,8 @@ func (r *Bumper) GetModifiedFiles(last bool) ([]string, error) {
return diffErr
}

var modifiedFiles []string

for _, ch := range diff {
action, _ := ch.Action()
var path string
Expand All @@ -130,11 +128,19 @@ func (r *Bumper) GetModifiedFiles(last bool) ([]string, error) {
path = ch.To.Name
}

if path != "" {
modifiedFiles = append(modifiedFiles, path)
if path == "" {
continue
}

modifiedFiles = append(modifiedFiles, path)
}

c := &Commit{
Hash: prevCommit.Hash.String(),
Files: modifiedFiles,
}

result = append(result, c)
if last {
return storer.ErrStop
}
Expand All @@ -150,17 +156,25 @@ func (r *Bumper) GetModifiedFiles(last bool) ([]string, error) {
}

if commitsNum == 1 {
var modifiedFiles []string
headTree, _ := headCommit.Tree()
err = headTree.Files().ForEach(func(file *object.File) error {
modifiedFiles = append(modifiedFiles, file.Name)
return nil
})

if err != nil {
return nil, err
}

c := &Commit{
Hash: headCommit.Hash.String(),
Files: modifiedFiles,
}
result = append(result, c)
}

return modifiedFiles, nil
return result, nil
}

// Commit stores the current changes to the Git repository with the default commit message and author.
Expand Down

0 comments on commit 8a9cfbf

Please sign in to comment.