-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor commit local stack fetching
commit-id:7529635e
- Loading branch information
Showing
17 changed files
with
549 additions
and
486 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package config_parser | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
||
"github.com/ejoffe/rake" | ||
"github.com/ejoffe/spr/config" | ||
"github.com/ejoffe/spr/git" | ||
) | ||
|
||
func ParseConfig(gitcmd git.GitInterface) *config.Config { | ||
cfg := config.EmptyConfig() | ||
|
||
rake.LoadSources(cfg.Repo, | ||
rake.DefaultSource(), | ||
GitHubRemoteSource(cfg, gitcmd), | ||
rake.YamlFileSource(RepoConfigFilePath(gitcmd)), | ||
rake.YamlFileWriter(RepoConfigFilePath(gitcmd)), | ||
) | ||
if cfg.Repo.GitHubHost == "" { | ||
fmt.Println("unable to auto configure repository host - must be set manually in .spr.yml") | ||
os.Exit(2) | ||
} | ||
if cfg.Repo.GitHubRepoOwner == "" { | ||
fmt.Println("unable to auto configure repository owner - must be set manually in .spr.yml") | ||
os.Exit(3) | ||
} | ||
|
||
if cfg.Repo.GitHubRepoName == "" { | ||
fmt.Println("unable to auto configure repository name - must be set manually in .spr.yml") | ||
os.Exit(4) | ||
} | ||
|
||
rake.LoadSources(cfg.User, | ||
rake.DefaultSource(), | ||
rake.YamlFileSource(UserConfigFilePath()), | ||
) | ||
|
||
rake.LoadSources(cfg.Internal, | ||
rake.DefaultSource(), | ||
rake.YamlFileSource(InternalConfigFilePath()), | ||
) | ||
|
||
rake.LoadSources(cfg.User, | ||
rake.YamlFileWriter(UserConfigFilePath())) | ||
|
||
cfg.Internal.RunCount = cfg.Internal.RunCount + 1 | ||
|
||
rake.LoadSources(cfg.Internal, | ||
rake.YamlFileWriter(InternalConfigFilePath())) | ||
|
||
return cfg | ||
} | ||
|
||
func RepoConfigFilePath(gitcmd git.GitInterface) string { | ||
rootdir := gitcmd.RootDir() | ||
filepath := filepath.Clean(path.Join(rootdir, ".spr.yml")) | ||
return filepath | ||
} | ||
|
||
func UserConfigFilePath() string { | ||
rootdir, err := os.UserHomeDir() | ||
check(err) | ||
filepath := filepath.Clean(path.Join(rootdir, ".spr.yml")) | ||
return filepath | ||
} | ||
|
||
func InternalConfigFilePath() string { | ||
rootdir, err := os.UserHomeDir() | ||
check(err) | ||
filepath := filepath.Clean(path.Join(rootdir, ".spr.state")) | ||
return filepath | ||
} | ||
|
||
func GitHubRemoteSource(config *config.Config, gitcmd git.GitInterface) *remoteSource { | ||
return &remoteSource{ | ||
gitcmd: gitcmd, | ||
config: config, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package config_parser | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ejoffe/spr/config" | ||
"github.com/ejoffe/spr/git/mockgit" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetRepoDetailsFromRemote(t *testing.T) { | ||
type testCase struct { | ||
remote string | ||
githubHost string | ||
repoOwner string | ||
repoName string | ||
match bool | ||
} | ||
testCases := []testCase{ | ||
{"origin https://github.com/r2/d2.git (push)", "github.com", "r2", "d2", true}, | ||
{"origin https://github.com/r2/d2.git (fetch)", "", "", "", false}, | ||
{"origin https://github.com/r2/d2 (push)", "github.com", "r2", "d2", true}, | ||
|
||
{"origin ssh://[email protected]/r2/d2.git (push)", "github.com", "r2", "d2", true}, | ||
{"origin ssh://[email protected]/r2/d2.git (fetch)", "", "", "", false}, | ||
{"origin ssh://[email protected]/r2/d2 (push)", "github.com", "r2", "d2", true}, | ||
|
||
{"origin [email protected]:r2/d2.git (push)", "github.com", "r2", "d2", true}, | ||
{"origin [email protected]:r2/d2.git (fetch)", "", "", "", false}, | ||
{"origin [email protected]:r2/d2 (push)", "github.com", "r2", "d2", true}, | ||
|
||
{"origin [email protected]:r2/d2.git (push)", "gh.enterprise.com", "r2", "d2", true}, | ||
{"origin [email protected]:r2/d2.git (fetch)", "", "", "", false}, | ||
{"origin [email protected]:r2/d2 (push)", "gh.enterprise.com", "r2", "d2", true}, | ||
|
||
{"origin https://github.com/r2/d2-a.git (push)", "github.com", "r2", "d2-a", true}, | ||
{"origin https://github.com/r2/d2_a.git (push)", "github.com", "r2", "d2_a", true}, | ||
} | ||
for i, testCase := range testCases { | ||
t.Logf("Testing %v %q", i, testCase.remote) | ||
githubHost, repoOwner, repoName, match := getRepoDetailsFromRemote(testCase.remote) | ||
if githubHost != testCase.githubHost { | ||
t.Fatalf("Wrong \"githubHost\" returned for test case %v, expected %q, got %q", i, testCase.githubHost, githubHost) | ||
} | ||
if repoOwner != testCase.repoOwner { | ||
t.Fatalf("Wrong \"repoOwner\" returned for test case %v, expected %q, got %q", i, testCase.repoOwner, repoOwner) | ||
} | ||
if repoName != testCase.repoName { | ||
t.Fatalf("Wrong \"repoName\" returned for test case %v, expected %q, got %q", i, testCase.repoName, repoName) | ||
} | ||
if match != testCase.match { | ||
t.Fatalf("Wrong \"match\" returned for test case %v, expected %t, got %t", i, testCase.match, match) | ||
} | ||
} | ||
} | ||
|
||
func TestGitHubRemoteSource(t *testing.T) { | ||
mock := mockgit.NewMockGit(t) | ||
mock.ExpectRemote("https://github.com/r2/d2.git") | ||
|
||
expect := config.Config{ | ||
Repo: &config.RepoConfig{ | ||
GitHubRepoOwner: "r2", | ||
GitHubRepoName: "d2", | ||
GitHubHost: "github.com", | ||
RequireChecks: false, | ||
RequireApproval: false, | ||
GitHubRemote: "", | ||
GitHubBranch: "", | ||
MergeMethod: "", | ||
}, | ||
User: &config.UserConfig{ | ||
ShowPRLink: false, | ||
LogGitCommands: false, | ||
LogGitHubCalls: false, | ||
StatusBitsHeader: false, | ||
}, | ||
} | ||
|
||
actual := config.Config{ | ||
Repo: &config.RepoConfig{}, | ||
User: &config.UserConfig{}, | ||
} | ||
source := GitHubRemoteSource(&actual, mock) | ||
source.Load(nil) | ||
assert.Equal(t, expect, actual) | ||
} |
Oops, something went wrong.