Skip to content

Commit

Permalink
CLeanup bumper code
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperMalachowski committed Oct 18, 2024
1 parent 31a9207 commit d326514
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 341 deletions.
2 changes: 1 addition & 1 deletion cmd/image-autobumper/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ LABEL io.kyma-project.source=github.com/kyma-project/test-infra/cmd/image-autobu
# Copy the built Go app from the builder stage
COPY --from=builder /app/cmd/image-autobumper/main /image-autobumper

RUN apk add --no-cache ca-certificates git && \
RUN apk add --no-cache ca-certificates && \
chmod +x /image-autobumper
ENTRYPOINT ["/image-autobumper"]
127 changes: 0 additions & 127 deletions pkg/github/bumper/bumper.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package bumper

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"

"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -77,85 +72,13 @@ type PRHandler interface {
PRTitleBody() (string, string, error)
}

func Call(stdout, stderr io.Writer, cmd string, args ...string) error {
(&logrus.Logger{
Out: stderr,
Formatter: logrus.StandardLogger().Formatter,
Hooks: logrus.StandardLogger().Hooks,
Level: logrus.StandardLogger().Level,
}).WithField("cmd", cmd).
// The default formatting uses a space as separator, which is hard to read if an arg contains a space
WithField("args", fmt.Sprintf("['%s']", strings.Join(args, "', '"))).
Info("running command")

c := exec.Command(cmd, args...)
c.Stdout = stdout
c.Stderr = stderr
return c.Run()
}

func getTreeRef(stderr io.Writer, refname string) (string, error) {
revParseStdout := &bytes.Buffer{}
if err := Call(revParseStdout, stderr, gitCmd, "rev-parse", refname+":"); err != nil {
return "", fmt.Errorf("parse ref: %w", err)
}
fields := strings.Fields(revParseStdout.String())
if n := len(fields); n < 1 {
return "", errors.New("got no otput when trying to rev-parse")
}
return fields[0], nil
}

func gitStatus(stdout io.Writer, stderr io.Writer) (string, error) {
tmpRead, tmpWrite, err := os.Pipe()
if err != nil {
return "", err
}

if err := Call(tmpWrite, stderr, gitCmd, "status"); err != nil {
return "", fmt.Errorf("git status: %w", err)
}
tmpWrite.Close()
output, err := io.ReadAll(tmpRead)
if err != nil {
return "", err
}
stdout.Write(output)
return string(output), nil
}

func gitAdd(files []string, stdout, stderr io.Writer) error {
if err := Call(stdout, stderr, gitCmd, "add", strings.Join(files, " ")); err != nil {
return fmt.Errorf("git add: %w", err)
}

return nil
}

func gitCommit(name, email, message string, stdout, stderr io.Writer, signoff bool) error {
commitArgs := []string{"commit", "-m", message}
if name != "" && email != "" {
commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", name, email))
}
if signoff {
commitArgs = append(commitArgs, "--signoff")
}
if err := Call(stdout, stderr, gitCmd, commitArgs...); err != nil {
return fmt.Errorf("git commit: %w", err)
}
return nil
}

func gitPush(remote, remoteBranch string, repo *git.Repository, auth transport.AuthMethod, dryrun bool) error {
if _, err := repo.CreateRemote(&config.RemoteConfig{
Name: forkRemoteName,
URLs: []string{remote},
}); err != nil && err != git.ErrRemoteExists {
return fmt.Errorf("create remote: %w", err)
}
// if err := Call(stdout, stderr, gitCmd, "remote", "add", forkRemoteName, remote); err != nil {
// return fmt.Errorf("add remote: %w", err)
// }

remoteRef, err := repo.Reference(plumbing.ReferenceName(fmt.Sprintf("refs/remotes/%s/%s", forkRemoteName, remoteBranch)), true)
if err != nil && err != plumbing.ErrReferenceNotFound {
Expand All @@ -167,31 +90,12 @@ func gitPush(remote, remoteBranch string, repo *git.Repository, auth transport.A
remoteTreeRef = remoteRef.Hash().String()
}

// fetchStderr := &bytes.Buffer{}
// var remoteTreeRef string
// if err := Call(stdout, fetchStderr, gitCmd, "fetch", forkRemoteName, remoteBranch); err != nil {
// logrus.Info("fetchStderr is : ", fetchStderr.String())
// if !strings.Contains(strings.ToLower(fetchStderr.String()), fmt.Sprintf("couldn't find remote ref %s", remoteBranch)) {
// return fmt.Errorf("fetch from fork: %w", err)
// }
// } else {
// var err error
// remoteTreeRef, err = getTreeRef(stderr, fmt.Sprintf("refs/remotes/%s/%s", forkRemoteName, remoteBranch))
// if err != nil {
// return fmt.Errorf("get remote tree ref: %w", err)
// }
// }

localRef, err := repo.Head()
if err != nil {
return fmt.Errorf("get local ref: %w", err)
}

localTreeRef := localRef.Hash().String()
// localTreeRef, err := getTreeRef(stderr, "HEAD")
// if err != nil {
// return fmt.Errorf("get local tree ref: %w", err)
// }

if dryrun {
logrus.Info("[Dryrun] Skip git push with: ")
Expand All @@ -209,29 +113,12 @@ func gitPush(remote, remoteBranch string, repo *git.Repository, auth transport.A
}); err != nil {
return fmt.Errorf("push to remote: %w", err)
}
// if err := GitPush(forkRemoteName, remoteBranch, stdout, stderr, ""); err != nil {
// return err
// }
} else {
logrus.Info("Not pushing as up-to-date remote branch already exists")
}
return nil
}

// GitPush push the changes to the given remote and branch.
func GitPush(remote, remoteBranch string, stdout, stderr io.Writer, workingDir string) error {
logrus.Info("Pushing to remote...")
gc := GitCommand{
baseCommand: gitCmd,
args: []string{"push", "-f", remote, fmt.Sprintf("HEAD:%s", remoteBranch)},
workingDir: workingDir,
}
if err := gc.Call(stdout, stderr); err != nil {
return fmt.Errorf("%s: %w", gc.getCommand(), err)
}
return nil
}

// UpdatePullRequestWithLabels updates with github client "gc" the PR of github repo org/repo
// with "title" and "body" of PR matching author and headBranch from "source" to "baseBranch" with labels
func UpdatePullRequestWithLabels(gc github.Client, org, repo, title, body, source, baseBranch,
Expand Down Expand Up @@ -375,14 +262,6 @@ func processGitHub(o *Options, prh PRHandler) error {
logrus.Info("No changes, quitting.")
return nil
}
// resp, err := gitStatus(stdout, stderr)
// if err != nil {
// return fmt.Errorf("git status: %w", err)
// }
// if strings.Contains(resp, "nothing to commit, working tree clean") {
// fmt.Println("No changes, quitting.")
// return nil
// }

logrus.Info("Adding changes to the stage")
for _, file := range filesToBeAdded {
Expand All @@ -397,9 +276,6 @@ func processGitHub(o *Options, prh PRHandler) error {
return fmt.Errorf("add file %s to the worktree: %w", file, err)
}
}
// if err := gitAdd(filesToBeAdded, stdout, stderr); err != nil {
// return fmt.Errorf("add changes to commit %w", err)
// }

logrus.Infof("Committing changes with message: %s", commitMsg)
commitOpts := &git.CommitOptions{
Expand All @@ -412,9 +288,6 @@ func processGitHub(o *Options, prh PRHandler) error {
if _, err := workTree.Commit(commitMsg, commitOpts); err != nil {
return fmt.Errorf("commit changes to the remote branch: %w", err)
}
// if err := gitCommit(o.GitName, o.GitEmail, commitMsg, stdout, stderr, o.Signoff); err != nil {
// return fmt.Errorf("commit changes to the remote branch: %w", err)
// }
}

remote := fmt.Sprintf("https://%s:%s@%s/%s/%s.git", o.GitHubLogin, string(secret.GetTokenGenerator(o.GitHubToken)()), githubHost, o.GitHubLogin, o.RemoteName)
Expand Down
70 changes: 0 additions & 70 deletions pkg/github/bumper/bumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ package bumper

import (
"os"
"path/filepath"
"strings"
"testing"

"k8s.io/test-infra/prow/config/secret"
)

func TestValidateOptions(t *testing.T) {
Expand Down Expand Up @@ -119,73 +116,6 @@ func writeToFile(t *testing.T, path, content string) {
}
}

func TestCallWithWriter(t *testing.T) {
dir := t.TempDir()

file1 := filepath.Join(dir, "secret1")
file2 := filepath.Join(dir, "secret2")

writeToFile(t, file1, "abc")
writeToFile(t, file2, "xyz")

if err := secret.Add(file1, file2); err != nil {
t.Errorf("failed to start secrets agent; %v", err)
}

var fakeOut fakeWriter
var fakeErr fakeWriter

stdout := CensoredWriter{Delegate: &fakeOut, Censor: secret.Censor}
stderr := CensoredWriter{Delegate: &fakeErr, Censor: secret.Censor}

testCases := []struct {
description string
command string
args []string
expectedOut string
expectedErr string
}{
{
description: "no secret in stdout are working well",
command: "echo",
args: []string{"-n", "aaa: 123"},
expectedOut: "aaa: 123",
},
{
description: "secret in stdout are censored",
command: "echo",
args: []string{"-n", "abc: 123"},
expectedOut: "XXX: 123",
},
{
description: "secret in stderr are censored",
command: "ls",
args: []string{"/tmp/file-not-exist/abc/xyz/file-not-exist"},
expectedErr: "/tmp/file-not-exist/XXX/XXX/file-not-exist",
},
{
description: "no secret in stderr are working well",
command: "ls",
args: []string{"/tmp/file-not-exist/aaa/file-not-exist"},
expectedErr: "/tmp/file-not-exist/aaa/file-not-exist",
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
fakeOut.result = []byte{}
fakeErr.result = []byte{}
_ = Call(stdout, stderr, tc.command, tc.args...)
if full, want := string(fakeOut.result), tc.expectedOut; !strings.Contains(full, want) {
t.Errorf("stdout does not contain %q, got %q", full, want)
}
if full, want := string(fakeErr.result), tc.expectedErr; !strings.Contains(full, want) {
t.Errorf("stderr does not contain %q, got %q", full, want)
}
})
}
}

func TestGetAssignment(t *testing.T) {
cases := []struct {
description string
Expand Down
43 changes: 0 additions & 43 deletions pkg/github/bumper/utils.go

This file was deleted.

Loading

0 comments on commit d326514

Please sign in to comment.