Skip to content

Commit

Permalink
fix: do not exit early if processing one file fails (#7)
Browse files Browse the repository at this point in the history
Signed-off-by: Michele Palazzi <sysdadmin@m1k.cloud>
  • Loading branch information
ironashram committed Mar 16, 2024
1 parent 5224b3d commit 1dd8d27
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 40 deletions.
46 changes: 46 additions & 0 deletions src/argoaction/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"time"

"github.com/Masterminds/semver"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/ironashram/argocd-apps-action/internal"
"github.com/ironashram/argocd-apps-action/models"
Expand Down Expand Up @@ -154,3 +155,48 @@ var addLabelsToPullRequest = func(githubClient internal.GitHubClient, pr *github

return nil
}

var handleNewVersion = func(chart string, newest *semver.Version, path string, gitOps internal.GitOperations, cfg *models.Config, action internal.ActionInterface, osw internal.OSInterface, githubClient internal.GitHubClient) error {
branchName := "update-" + chart
err := createNewBranch(gitOps, cfg.TargetBranch, branchName)
if err != nil {
action.Fatalf("Error creating new branch: %v\n", err)
return err
}

err = updateTargetRevision(newest, path, action, osw)
if err != nil {
action.Fatalf("Error updating target revision: %v\n", err)
return err
}

commitMessage := "Update " + chart + " to version " + newest.String()
err = commitChanges(gitOps, path, commitMessage)
if err != nil {
action.Fatalf("Error committing changes: %v\n", err)
return err
}

err = pushChanges(gitOps, branchName, cfg)
if err != nil {
action.Fatalf("Error pushing changes: %v\n", err)
return err
}

prTitle := "Update " + chart + " to version " + newest.String()
prBody := "This PR updates " + chart + " to version " + newest.String()
pr, err := createPullRequest(githubClient, cfg.TargetBranch, branchName, prTitle, prBody, action, cfg)
if err != nil {
action.Fatalf("Error creating pull request: %v\n", err)
return err
}

labels := cfg.Labels
err = addLabelsToPullRequest(githubClient, pr, labels, cfg)
if err != nil {
action.Fatalf("Error adding labels to pull request: %v\n", err)
}

action.Infof("Pull request created for %s\n", chart)
return nil
}
44 changes: 4 additions & 40 deletions src/argoaction/process_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ var checkForUpdates = func(gitOps internal.GitOperations, githubClient internal.
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
action.Debugf("Error walking path: %v\n", err)
return err
walkErr = err
return nil
}

if filepath.Ext(path) == ".yaml" {
osw := &internal.OSWrapper{}
err := processFile(path, gitOps, githubClient, cfg, action, osw)
if err != nil {
action.Debugf("Error processing file: %v\n", err)
return err
walkErr = err
}
}

Expand Down Expand Up @@ -119,47 +120,10 @@ var processFile = func(path string, gitOps internal.GitOperations, githubClient
action.Infof("There is a newer %s version: %s\n", chart, newest)

if cfg.CreatePr {
branchName := "update-" + chart
err = createNewBranch(gitOps, cfg.TargetBranch, branchName)
if err != nil {
action.Fatalf("Error creating new branch: %v\n", err)
return err
}

err = updateTargetRevision(newest, path, action, osw)
err = handleNewVersion(chart, newest, path, gitOps, cfg, action, osw, githubClient)
if err != nil {
action.Fatalf("Error updating target revision: %v\n", err)
return err
}

commitMessage := "Update " + chart + " to version " + newest.String()
err = commitChanges(gitOps, path, commitMessage)
if err != nil {
action.Fatalf("Error committing changes: %v\n", err)
return err
}

err = pushChanges(gitOps, branchName, cfg)
if err != nil {
action.Fatalf("Error pushing changes: %v\n", err)
return err
}

prTitle := "Update " + chart + " to version " + newest.String()
prBody := "This PR updates " + chart + " to version " + newest.String()
pr, err := createPullRequest(githubClient, cfg.TargetBranch, branchName, prTitle, prBody, action, cfg)
if err != nil {
action.Fatalf("Error creating pull request: %v\n", err)
return err
}

labels := cfg.Labels
err = addLabelsToPullRequest(githubClient, pr, labels, cfg)
if err != nil {
action.Fatalf("Error adding labels to pull request: %v\n", err)
}

action.Infof("Pull request created for %s\n", chart)
} else {
action.Infof("Create PR is disabled, skipping PR creation for %s\n", chart)
}
Expand Down

0 comments on commit 1dd8d27

Please sign in to comment.