-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: code-refactoring #85
base: main
Are you sure you want to change the base?
Changes from 9 commits
a927028
870512a
afea9c7
12688e0
ab32ad7
36e964e
135bca7
22a0c6a
6959802
4590a56
87c7fd6
1e4829a
2bbe296
a02805a
e9b3856
2a6a05a
b23e4f3
32b9bd3
9543583
49f1498
8d9f6d5
8028b7e
bccadd8
7e70ef9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
package internal | ||
package internals | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,8 @@ import ( | |
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/devtron-labs/git-sensor/internal" | ||
"github.com/devtron-labs/git-sensor/internal/sql" | ||
"github.com/devtron-labs/git-sensor/internals" | ||
"github.com/devtron-labs/git-sensor/internals/sql" | ||
"github.com/devtron-labs/git-sensor/util" | ||
"go.uber.org/zap" | ||
"os" | ||
|
@@ -29,8 +29,6 @@ type GitManager interface { | |
OpenRepoPlain(checkoutPath string) (*GitRepository, error) | ||
// Init initializes a git repo | ||
Init(gitCtx GitContext, rootDir string, remoteUrl string, isBare bool) error | ||
// FetchDiffStatBetweenCommits returns the file stats reponse on executing git action | ||
FetchDiffStatBetweenCommits(gitCtx GitContext, oldHash string, newHash string, rootDir string) (response, errMsg string, err error) | ||
} | ||
|
||
// GitManagerBase Base methods which will be available to all implementation of the parent interface | ||
|
@@ -43,16 +41,21 @@ type GitManagerBase interface { | |
Checkout(gitCtx GitContext, rootDir, branch string) (response, errMsg string, err error) | ||
// ConfigureSshCommand configures ssh in git repo | ||
ConfigureSshCommand(gitCtx GitContext, rootDir string, sshPrivateKeyPath string) (response, errMsg string, err error) | ||
// FetchDiffStatBetweenCommits returns the file stats reponse on executing git action | ||
FetchDiffStatBetweenCommits(gitCtx GitContext, oldHash string, newHash string, rootDir string) (response, errMsg string, err error) | ||
// LogMergeBase get the commit diff between using a merge base strategy | ||
LogMergeBase(gitCtx GitContext, rootDir, from string, to string) ([]*Commit, error) | ||
CreateCmdWithContext(ctx GitContext, name string, arg ...string) (*exec.Cmd, context.CancelFunc) | ||
RunCommandWithCred(cmd *exec.Cmd, userName, password string) (response, errMsg string, err error) | ||
RunCommand(cmd *exec.Cmd) (response, errMsg string, err error) | ||
} | ||
type GitManagerBaseImpl struct { | ||
logger *zap.SugaredLogger | ||
conf *internal.Configuration | ||
conf *internals.Configuration | ||
commandTimeoutMap map[string]int | ||
} | ||
|
||
func NewGitManagerBaseImpl(logger *zap.SugaredLogger, config *internal.Configuration) *GitManagerBaseImpl { | ||
func NewGitManagerBaseImpl(logger *zap.SugaredLogger, config *internals.Configuration) *GitManagerBaseImpl { | ||
|
||
commandTimeoutMap, err := parseCmdTimeoutJson(config) | ||
if err != nil { | ||
|
@@ -62,7 +65,7 @@ func NewGitManagerBaseImpl(logger *zap.SugaredLogger, config *internal.Configura | |
return &GitManagerBaseImpl{logger: logger, conf: config, commandTimeoutMap: commandTimeoutMap} | ||
} | ||
|
||
func parseCmdTimeoutJson(config *internal.Configuration) (map[string]int, error) { | ||
func parseCmdTimeoutJson(config *internals.Configuration) (map[string]int, error) { | ||
commandTimeoutMap := make(map[string]int) | ||
var err error | ||
if config.CliCmdTimeoutJson != "" { | ||
|
@@ -75,41 +78,25 @@ type GitManagerImpl struct { | |
GitManager | ||
} | ||
|
||
func NewGitManagerImpl(configuration *internal.Configuration, | ||
func NewGitManagerImpl(configuration *internals.Configuration, | ||
cliGitManager GitCliManager, | ||
goGitManager GoGitSDKManager) GitManagerImpl { | ||
goGitManager GoGitSDKManager) *GitManagerImpl { | ||
|
||
if configuration.UseGitCli { | ||
return GitManagerImpl{cliGitManager} | ||
} | ||
return GitManagerImpl{goGitManager} | ||
} | ||
|
||
func (impl *GitManagerImpl) OpenNewRepo(gitCtx GitContext, location string, url string) (*GitRepository, error) { | ||
|
||
r, err := impl.OpenRepoPlain(location) | ||
if err != nil { | ||
err = os.RemoveAll(location) | ||
if err != nil { | ||
return r, fmt.Errorf("error in cleaning checkout path: %s", err) | ||
} | ||
err = impl.Init(gitCtx, location, url, true) | ||
if err != nil { | ||
return r, fmt.Errorf("err in git init: %s", err) | ||
} | ||
r, err = impl.OpenRepoPlain(location) | ||
if err != nil { | ||
return r, fmt.Errorf("err in git init: %s", err) | ||
return &GitManagerImpl{ | ||
cliGitManager, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to have a look |
||
} | ||
} | ||
return r, nil | ||
return &GitManagerImpl{ | ||
goGitManager, | ||
} | ||
} | ||
|
||
func (impl *GitManagerBaseImpl) Fetch(gitCtx GitContext, rootDir string) (response, errMsg string, err error) { | ||
impl.logger.Debugw("git fetch ", "location", rootDir) | ||
cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", "-C", rootDir, "fetch", "origin", "--tags", "--force") | ||
defer cancel() | ||
output, errMsg, err := impl.runCommandWithCred(cmd, gitCtx.Username, gitCtx.Password) | ||
output, errMsg, err := impl.RunCommandWithCred(cmd, gitCtx.Username, gitCtx.Password) | ||
impl.logger.Debugw("fetch output", "root", rootDir, "opt", output, "errMsg", errMsg, "error", err) | ||
return output, errMsg, err | ||
} | ||
|
@@ -118,7 +105,7 @@ func (impl *GitManagerBaseImpl) Checkout(gitCtx GitContext, rootDir, branch stri | |
impl.logger.Debugw("git checkout ", "location", rootDir) | ||
cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", "-C", rootDir, "checkout", branch, "--force") | ||
defer cancel() | ||
output, errMsg, err := impl.runCommand(cmd) | ||
output, errMsg, err := impl.RunCommand(cmd) | ||
impl.logger.Debugw("checkout output", "root", rootDir, "opt", output, "errMsg", errMsg, "error", err) | ||
return output, errMsg, err | ||
} | ||
|
@@ -135,7 +122,7 @@ func (impl *GitManagerBaseImpl) LogMergeBase(gitCtx GitContext, rootDir, from st | |
impl.logger.Debugw("git", cmdArgs) | ||
cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", cmdArgs...) | ||
defer cancel() | ||
output, errMsg, err := impl.runCommand(cmd) | ||
output, errMsg, err := impl.RunCommand(cmd) | ||
impl.logger.Debugw("root", rootDir, "opt", output, "errMsg", errMsg, "error", err) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -148,16 +135,16 @@ func (impl *GitManagerBaseImpl) LogMergeBase(gitCtx GitContext, rootDir, from st | |
return commits, nil | ||
} | ||
|
||
func (impl *GitManagerBaseImpl) runCommandWithCred(cmd *exec.Cmd, userName, password string) (response, errMsg string, err error) { | ||
func (impl *GitManagerBaseImpl) RunCommandWithCred(cmd *exec.Cmd, userName, password string) (response, errMsg string, err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why public |
||
cmd.Env = append(os.Environ(), | ||
fmt.Sprintf("GIT_ASKPASS=%s", GIT_ASK_PASS), | ||
fmt.Sprintf("GIT_USERNAME=%s", userName), | ||
fmt.Sprintf("GIT_PASSWORD=%s", password), | ||
) | ||
return impl.runCommand(cmd) | ||
return impl.RunCommand(cmd) | ||
} | ||
|
||
func (impl *GitManagerBaseImpl) runCommand(cmd *exec.Cmd) (response, errMsg string, err error) { | ||
func (impl *GitManagerBaseImpl) RunCommand(cmd *exec.Cmd) (response, errMsg string, err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why public |
||
cmd.Env = append(cmd.Env, "HOME=/dev/null") | ||
outBytes, err := cmd.CombinedOutput() | ||
if err != nil { | ||
|
@@ -183,7 +170,7 @@ func (impl *GitManagerBaseImpl) ConfigureSshCommand(gitCtx GitContext, rootDir s | |
coreSshCommand := fmt.Sprintf("ssh -i %s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no", sshPrivateKeyPath) | ||
cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", "-C", rootDir, "config", "core.sshCommand", coreSshCommand) | ||
defer cancel() | ||
output, errMsg, err := impl.runCommand(cmd) | ||
output, errMsg, err := impl.RunCommand(cmd) | ||
impl.logger.Debugw("configure ssh command output ", "root", rootDir, "opt", output, "errMsg", errMsg, "error", err) | ||
return output, errMsg, err | ||
} | ||
|
@@ -280,7 +267,7 @@ func (impl *GitManagerBaseImpl) FetchDiffStatBetweenCommits(gitCtx GitContext, o | |
cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", "-C", rootDir, "diff", "--numstat", oldHash, newHash) | ||
defer cancel() | ||
|
||
output, errMsg, err := impl.runCommandWithCred(cmd, gitCtx.Username, gitCtx.Password) | ||
output, errMsg, err := impl.RunCommandWithCred(cmd, gitCtx.Username, gitCtx.Password) | ||
impl.logger.Debugw("root", rootDir, "opt", output, "errMsg", errMsg, "error", err) | ||
return output, errMsg, err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
internal
directory - is a special name that do not allow others to import any package under this directory. So after renaming it tointernals
- anyone can import this packages. I am almost sure that this is not a main goal of this PR.