Skip to content

Commit

Permalink
fix modified files fetching (#861)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorvezani authored Dec 22, 2023
1 parent 6ce1c84 commit d96aceb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
3 changes: 3 additions & 0 deletions plugins/ci/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 5.3.3
* Trim spaces from masterBranch before using it

## 5.3.2
* Only skip failed files instead of halting the process

Expand Down
11 changes: 11 additions & 0 deletions plugins/ci/pkg/ci/ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ func TestGetAllResources(t *testing.T) {
assert.Len(t, images, 3, "even though there are errors, we should still get the images")
assert.Len(t, resources, 7, "even though there are errors, we should still get the resources")
}

func TestTrimSpace(t *testing.T) {
assert.Equal(t, "hello", strings.TrimSpace("hello"))
assert.Equal(t, "hello", strings.TrimSpace(" hello "))
assert.Equal(t, "hello", strings.TrimSpace("\nhello\n"))
assert.Equal(t, "hello", strings.TrimSpace("\n hello \n"))
assert.Equal(t, "hello", strings.TrimSpace("\n hello"))
assert.Equal(t, "hello", strings.TrimSpace("hello\n"))
assert.Equal(t, "hello", strings.TrimSpace("\nhello"))
assert.Equal(t, "hello", strings.TrimSpace(" \n hello \n "))
}
42 changes: 25 additions & 17 deletions plugins/ci/pkg/ci/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type cmdExecutor func(cmd *exec.Cmd, message string) (string, error)
func getGitInfo(cmdExecutor cmdInDirExecutor, ciRunner models.CIRunnerVal, baseRepoPath, repoName, baseBranch string) (*gitInfo, error) {
var err error
var filesModified []string
repoName = strings.TrimSpace(repoName)
_, err = cmdExecutor(baseRepoPath, exec.Command("git", "config", "--global", "--add", "safe.directory", "/insights"), "marking directory as safe")
if err != nil {
logrus.Errorf("Unable to mark directory %s as safe: %v", baseRepoPath, err)
Expand All @@ -45,6 +46,7 @@ func getGitInfo(cmdExecutor cmdInDirExecutor, ciRunner models.CIRunnerVal, baseR
return nil, err
}
}
currentHash = strings.TrimSpace(currentHash)
logrus.Infof("Current hash: %s", currentHash)

var gitCommandFail bool
Expand All @@ -67,6 +69,7 @@ func getGitInfo(cmdExecutor cmdInDirExecutor, ciRunner models.CIRunnerVal, baseR
}
}
}
masterHash = strings.TrimSpace(masterHash)
logrus.Infof("Master hash: %s", masterHash)

commitMessage := os.Getenv("COMMIT_MESSAGE")
Expand All @@ -80,6 +83,7 @@ func getGitInfo(cmdExecutor cmdInDirExecutor, ciRunner models.CIRunnerVal, baseR
if len(commitMessage) > 100 {
commitMessage = commitMessage[:100] // Limit to 100 chars, double the length of github recommended length
}
commitMessage = strings.TrimSpace(commitMessage)
logrus.Infof("Commit message: %s", commitMessage)

branch := os.Getenv("BRANCH_NAME")
Expand All @@ -89,21 +93,24 @@ func getGitInfo(cmdExecutor cmdInDirExecutor, ciRunner models.CIRunnerVal, baseR
logrus.Warnf("Unable to get GIT branch name: %v", err)
gitCommandFail = true
}
files, err := cmdExecutor(baseRepoPath, exec.Command("git", "diff", "--name-only", "HEAD", masterHash), "modified files")
if err != nil {
logrus.Warnf("Unable to get git modified files: %v", err)
gitCommandFail = true
}
}
branch = strings.TrimSpace(branch)
logrus.Infof("Branch: %s", branch)

splitted := strings.Split(files, "\n")
for _, f := range splitted {
if len(f) > 0 {
filesModified = append(filesModified, f)
}
filesModifiedStr, err := cmdExecutor(baseRepoPath, exec.Command("git", "diff", "--name-only", "HEAD", masterHash), "getting modified files")
if err != nil {
logrus.Warnf("Unable to get git modified files: %v", err)
gitCommandFail = true
}

for _, mf := range strings.Split(filesModifiedStr, "\n") {
mf = strings.TrimSpace(mf)
if len(mf) > 0 {
filesModified = append(filesModified, mf)
}
}
logrus.Infof("Branch: %s", branch)
logrus.Infof("Files modified: %s", filesModified)

origin := os.Getenv("ORIGIN_URL")
if origin == "" {
origin, err = cmdExecutor(baseRepoPath, exec.Command("git", "remote", "get-url", "origin"), "getting origin url")
Expand All @@ -112,6 +119,7 @@ func getGitInfo(cmdExecutor cmdInDirExecutor, ciRunner models.CIRunnerVal, baseR
gitCommandFail = true
}
}
origin = strings.TrimSpace(origin)
logrus.Infof("Origin: %s", util.RemoveTokensAndPassword(origin))

if gitCommandFail {
Expand All @@ -125,12 +133,12 @@ func getGitInfo(cmdExecutor cmdInDirExecutor, ciRunner models.CIRunnerVal, baseR
logrus.Infof("Repo Name: %s", repoName)

return &gitInfo{
masterHash: strings.TrimSuffix(masterHash, "\n"),
currentHash: strings.TrimSuffix(currentHash, "\n"),
commitMessage: strings.TrimSuffix(commitMessage, "\n"),
branch: strings.TrimSuffix(branch, "\n"),
origin: strings.TrimSuffix(origin, "\n"),
repoName: strings.TrimSuffix(repoName, "\n"),
masterHash: masterHash,
currentHash: currentHash,
commitMessage: commitMessage,
branch: branch,
origin: origin,
repoName: repoName,
filesModified: filesModified,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/ci/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.3.2
5.3.3

0 comments on commit d96aceb

Please sign in to comment.