Skip to content

Commit

Permalink
fix: prs are now getting created
Browse files Browse the repository at this point in the history
Signed-off-by: Michele Palazzi <sysdadmin@m1k.cloud>
  • Loading branch information
ironashram committed Feb 24, 2024
1 parent 2505a66 commit f470635
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 163 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
fetch-depth: '0'

- name: Check updates for ArgoCD Apps
uses: ironashram/argocd-apps-action@v0.0.4
uses: ironashram/argocd-apps-action@v0.0.5
with:
target_branch: main
create_pr: true
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ runs:
steps:
- name: Download ArgoCD Apps Action Binary
env:
ACTION_VERSION: "v0.0.4"
ACTION_VERSION: "v0.0.5"
shell: bash
run: |
wget https://github.com/ironashram/argocd-apps-action/releases/download/${ACTION_VERSION}/argocd-apps-action-${ACTION_VERSION}-linux-amd64.tar.gz
Expand Down
30 changes: 21 additions & 9 deletions src/argoaction/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ import (
"github.com/google/go-github/v59/github"
)

var createNewBranch = func(gitOps internal.GitOperations, branchName string) error {
var createNewBranch = func(gitOps internal.GitOperations, baseBranch, branchName string) error {
worktree, err := gitOps.Worktree()
if err != nil {
return err
}

err = worktree.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(baseBranch),
})
if err != nil {
return err
}

headRef, err := gitOps.Head()
if err != nil {
return err
Expand All @@ -31,7 +43,7 @@ var createNewBranch = func(gitOps internal.GitOperations, branchName string) err
return fmt.Errorf("failed to create new branch: %w", err)
}

worktree, err := gitOps.Worktree()
worktree, err = gitOps.Worktree()
if err != nil {
return err
}
Expand All @@ -52,11 +64,11 @@ var commitChanges = func(gitOps internal.GitOperations, path string, commitMessa
return fmt.Errorf("failed to commit changes: %w", err)
}

realWorktree, ok := worktree.(*git.Worktree)
if !ok {
return fmt.Errorf("failed to assert worktree type")
}
basePath := realWorktree.Filesystem.Root()
basePath, err := worktree.Root()
if err != nil {
return fmt.Errorf("failed to get worktree root: %w", err)
}

relativePath, err := filepath.Rel(basePath, path)
if err != nil {
return fmt.Errorf("failed to get relative path: %w", err)
Expand Down Expand Up @@ -86,7 +98,7 @@ var pushChanges = func(gitOps internal.GitOperations, branchName string, cfg *mo
Username: "github-actions[bot]",
Password: cfg.Token,
},
RefSpecs: []config.RefSpec{config.RefSpec(branchName + ":" + branchName)},
RefSpecs: []config.RefSpec{config.RefSpec("refs/heads/" + branchName + ":refs/heads/" + branchName)},
})
if err != nil {
return fmt.Errorf("failed to push changes: %w", err)
Expand Down Expand Up @@ -117,7 +129,7 @@ var createPullRequest = func(githubClient internal.GitHubClient, baseBranch stri
return errors.New("action is nil")
}

_, _, err := pullRequests.Create(context.Background(), cfg.Owner, cfg.Repo, newPR)
_, _, err := pullRequests.Create(context.Background(), cfg.Owner, cfg.Name, newPR)
if err != nil {
return err
}
Expand Down
117 changes: 43 additions & 74 deletions src/argoaction/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ import (
"errors"
"fmt"
"testing"
"time"

"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/google/go-github/v59/github"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -24,7 +19,7 @@ func TestCreatePullRequest(t *testing.T) {
cfg := &models.Config{
CreatePr: true,
TargetBranch: "main",
Repo: "your-github-repo",
Name: "your-github-repo",
Owner: "your-github-owner",
Token: "your-github-token",
}
Expand All @@ -49,9 +44,9 @@ func TestCreatePullRequest(t *testing.T) {

mockClient = &internal.MockGithubClient{
PullRequestsService: &internal.MockPullRequestsService{
CreateFunc: func(ctx context.Context, owner string, repo string, newPR *github.NewPullRequest) (*github.PullRequest, *github.Response, error) {
CreateFunc: func(ctx context.Context, owner string, name string, newPR *github.NewPullRequest) (*github.PullRequest, *github.Response, error) {
assert.Equal(t, cfg.Owner, owner)
assert.Equal(t, cfg.Repo, repo)
assert.Equal(t, cfg.Name, name)
assert.Equal(t, expectedPR, newPR)
return nil, nil, nil
},
Expand Down Expand Up @@ -80,7 +75,7 @@ func TestCreatePullRequest_Error(t *testing.T) {
cfg := &models.Config{
CreatePr: true,
TargetBranch: "main",
Repo: "your-github-repo",
Name: "your-github-repo",
Owner: "your-github-owner",
Token: "your-github-token",
}
Expand All @@ -106,9 +101,9 @@ func TestCreatePullRequest_Error(t *testing.T) {
expectedError := errors.New("failed to create pull request")
mockClient = &internal.MockGithubClient{
PullRequestsService: &internal.MockPullRequestsService{
CreateFunc: func(ctx context.Context, owner string, repo string, newPR *github.NewPullRequest) (*github.PullRequest, *github.Response, error) {
CreateFunc: func(ctx context.Context, owner string, name string, newPR *github.NewPullRequest) (*github.PullRequest, *github.Response, error) {
assert.Equal(t, cfg.Owner, owner)
assert.Equal(t, cfg.Repo, repo)
assert.Equal(t, cfg.Name, name)
assert.Equal(t, expectedPR, newPR)
return nil, nil, expectedError
},
Expand All @@ -120,73 +115,40 @@ func TestCreatePullRequest_Error(t *testing.T) {
assert.EqualError(t, err, expectedError.Error())
}

func setupRepoAndInitialCommit() (*git.Repository, error) {
repo, err := git.Init(memory.NewStorage(), memfs.New())
if err != nil {
return nil, err
}

wt, err := repo.Worktree()
if err != nil {
return nil, err
}

file, err := wt.Filesystem.Create("test.txt")
if err != nil {
return nil, err
}

_, err = file.Write([]byte("This is a test file"))
if err != nil {
return nil, err
}

file.Close()

_, err = wt.Add("test.txt")
if err != nil {
return nil, err
}

_, err = wt.Commit("Initial commit", &git.CommitOptions{
Author: &object.Signature{
Name: "Test",
Email: "test@test.com",
When: time.Now(),
},
})

if err != nil {
return nil, err
}
func TestCreateNewBranch(t *testing.T) {
gitOps := new(internal.MockGitRepo)
worktree := new(internal.MockWorktree)

return repo, nil
}
headRef := plumbing.NewHashReference(plumbing.HEAD, plumbing.ZeroHash)
gitOps.On("Worktree").Return(worktree, nil)
gitOps.On("Head").Return(headRef, nil)
gitOps.On("SetReference", mock.Anything, mock.Anything).Return(nil)

func TestCreateNewBranch(t *testing.T) {
repo, err := setupRepoAndInitialCommit()
assert.NoError(t, err)
gitOps := &internal.GitRepo{Repo: repo}
worktree.On("Checkout", mock.Anything).Return(nil)

err = createNewBranch(gitOps, "test-branch")
assert.NoError(t, err)
err := createNewBranch(gitOps, "base", "new-branch")

ref, err := repo.Reference(plumbing.NewBranchReferenceName("test-branch"), true)
gitOps.AssertExpectations(t)
worktree.AssertExpectations(t)
assert.NoError(t, err)
assert.NotNil(t, ref)
}

func TestCommitChanges(t *testing.T) {
repo, err := setupRepoAndInitialCommit()
assert.NoError(t, err)
gitOps := &internal.GitRepo{Repo: repo}
mockRepo := new(internal.MockGitRepo)
worktree := new(internal.MockWorktree)

err = commitChanges(gitOps, ".", "Test commit")
assert.NoError(t, err)
mockRepo.On("Worktree").Return(worktree, nil)
hash := plumbing.NewHash("0000000000000000000000000000000000000000")
worktree.On("Add", ".").Return(hash, nil)
commitHash := plumbing.NewHash("0000000000000000000000000000000000000001")
worktree.On("Commit", "Test commit", mock.Anything).Return(commitHash, nil)
worktree.On("Root").Return("/valid/path", nil)

err := commitChanges(mockRepo, "/valid/path", "Test commit")

ref, _ := repo.Head()
commit, _ := repo.CommitObject(ref.Hash())
assert.Equal(t, "Test commit", commit.Message)
assert.NoError(t, err)
mockRepo.AssertExpectations(t)
worktree.AssertExpectations(t)
}

func TestPushChanges(t *testing.T) {
Expand All @@ -199,22 +161,29 @@ func TestPushChanges(t *testing.T) {

func TestCreateNewBranch_Error(t *testing.T) {
expectedError := fmt.Errorf("failed to create new branch: %w", errors.New("some error"))
mockRepo := &internal.MockGitRepo{}
dummyRef := &plumbing.Reference{}
mockRepo.On("Head").Return(dummyRef, expectedError)
err := createNewBranch(mockRepo, "test-branch")
mockRepo := new(internal.MockGitRepo)
worktree := new(internal.MockWorktree)

mockRepo.On("Worktree").Return(worktree, nil)

worktree.On("Checkout", mock.Anything).Return(expectedError)

err := createNewBranch(mockRepo, "main", "test-branch")

assert.EqualError(t, err, expectedError.Error())
mockRepo.AssertExpectations(t)
worktree.AssertExpectations(t)
}

func TestCommitChanges_Error(t *testing.T) {
expectedError := fmt.Errorf("failed to commit changes: %w", errors.New("some error"))
mockRepo := &internal.MockGitRepo{}
mockWorktree := &git.Worktree{}
mockRepo := new(internal.MockGitRepo)
mockWorktree := new(internal.MockWorktree)
mockRepo.On("Worktree").Return(mockWorktree, expectedError)
err := commitChanges(mockRepo, ".", "Test commit")
assert.EqualError(t, err, fmt.Errorf("failed to commit changes: %w", expectedError).Error())
mockRepo.AssertExpectations(t)
mockWorktree.AssertExpectations(t)
}

func TestPushChanges_Error(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion src/argoaction/process_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var processFile = func(path string, gitOps internal.GitOperations, githubClient

if cfg.CreatePr {
branchName := "update-" + chart
err = createNewBranch(gitOps, branchName)
err = createNewBranch(gitOps, cfg.TargetBranch, branchName)
if err != nil {
action.Debugf("Error creating new branch: %v\n", err)
return err
Expand Down
63 changes: 0 additions & 63 deletions src/argoaction/process_files_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package argoaction

import (
"errors"
"net/http"
"testing"

"github.com/go-git/go-git/v5/plumbing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -91,65 +89,4 @@ func TestProcessFile(t *testing.T) {
mockOSInterface.AssertExpectations(t)
mockGitHubClient.AssertExpectations(t)
})

t.Run("Error creating new branch", func(t *testing.T) {
cfg.CreatePr = true
mockRepo := new(internal.MockGitRepo)
mockAction.On("Debugf", "Checking %s from %s, current version is %s\n", mock.AnythingOfType("[]interface {}")).Once()
mockAction.On("Infof", "There is a newer %s version: %s\n", mock.AnythingOfType("[]interface {}")).Once()
mockAction.On("Debugf", "Error creating new branch: %v\n", mock.AnythingOfType("[]interface {}")).Once()

mockRef := plumbing.NewHashReference(plumbing.NewBranchReferenceName("master"), plumbing.NewHash("e5bd3914e2e596debea16f433f57875b5b90bcd6"))
mockRepo.On("Head").Return(mockRef, nil)
mockRepo.On("SetReference", mock.AnythingOfType("string"), mock.AnythingOfType("*plumbing.Reference")).Return(nil)

mockWorktree := new(internal.MockWorktree)
mockWorktree.On("Checkout", mock.AnythingOfType("*git.CheckoutOptions")).Return(errors.New("error creating new branch")).Once()
mockRepo.On("Worktree").Return(mockWorktree, nil)

fileContent := []byte("spec:\n source:\n chart: chart1\n repoURL: https://test.local\n targetRevision: 0.1.2 \n")
mockOSInterface.On("ReadFile", mock.Anything).Return([]byte(fileContent), nil).Once()

err := processFile("valid.yaml", mockRepo, mockGitHubClient, cfg, mockAction, mockOSInterface)

assert.Error(t, err)
mockAction.AssertExpectations(t)
mockOSInterface.AssertExpectations(t)
mockRepo.AssertExpectations(t)
})

t.Run("Error creating pull request", func(t *testing.T) {
cfg.CreatePr = true
mockRepo := new(internal.MockGitRepo)
mockAction.On("Debugf", "Checking %s from %s, current version is %s\n", mock.AnythingOfType("[]interface {}")).Once()
mockAction.On("Infof", "There is a newer %s version: %s\n", mock.AnythingOfType("[]interface {}")).Once()
// mockAction.On("Debugf", "Error marshaling app: %v\n", mock.AnythingOfType("[]interface {}")).Once()
mockAction.On("Debugf", "Error creating pull request: %v\n", mock.Anything).Return().Once()

// mockRepo.On("CreateNewBranch", mock.AnythingOfType("string")).Return(nil).Once()

mockWorktree := new(internal.MockWorktree)
mockWorktree.On("Checkout", mock.AnythingOfType("*git.CheckoutOptions")).Return(nil)
mockWorktree.On("Add", mock.AnythingOfType("string")).Return(plumbing.NewHash("0000000000000000000000000000000000000000"), nil)
mockWorktree.On("Commit", mock.AnythingOfType("string"), mock.AnythingOfType("*git.CommitOptions")).Return(plumbing.NewHash("0000000000000000000000000000000000000000"), nil)
mockRepo.On("Worktree").Return(mockWorktree, nil)

mockRef := plumbing.NewHashReference(plumbing.NewBranchReferenceName("master"), plumbing.NewHash("e5bd3914e2e596debea16f433f57875b5b90bcd6"))
mockRepo.On("Head").Return(mockRef, nil)

mockRepo.On("SetReference", mock.AnythingOfType("string"), mock.Anything).Return(nil)
mockRepo.On("Push", mock.AnythingOfType("*git.PushOptions")).Return(nil)

fileContent := []byte("spec:\n source:\n chart: chart1\n repoURL: https://test.local\n targetRevision: 0.1.2 \n")
mockOSInterface.On("ReadFile", mock.Anything).Return([]byte(fileContent), nil).Once()
mockOSInterface.On("WriteFile", "valid.yaml", []byte("spec:\n source:\n chart: chart1\n repoURL: https://test.local\n targetRevision: 1.7.0\n"), mock.AnythingOfType("fs.FileMode")).Return(nil)

err := processFile("valid.yaml", mockRepo, mockGitHubClient, cfg, mockAction, mockOSInterface)

assert.Error(t, err)
mockAction.AssertExpectations(t)
mockOSInterface.AssertExpectations(t)
mockRepo.AssertExpectations(t)
mockWorktree.AssertExpectations(t)
})
}
2 changes: 1 addition & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.22.0

require (
github.com/Masterminds/semver v1.5.0
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.11.0
github.com/google/go-github/v59 v59.0.0
github.com/jarcoal/httpmock v1.3.1
Expand All @@ -23,6 +22,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // 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/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand Down
Loading

0 comments on commit f470635

Please sign in to comment.