Skip to content

Commit 68ffc47

Browse files
committed
quality rules: repo origin check must not "crash" the process if not a Git repo (or no origin defined)
1 parent 82b5796 commit 68ffc47

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

cmd/bob/projecthomepage.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func openProjectHomepageEntrypoint() *cobra.Command {
2121
Args: cobra.NoArgs,
2222
Run: func(_ *cobra.Command, _ []string) {
2323
osutil.ExitIfError(func() error {
24-
repo, err := gitHubRepoRefFromGit()
24+
repo, err := gitHubRepoRefFromGitNonNil()
2525
if err != nil {
2626
return err
2727
}
@@ -36,18 +36,36 @@ func openProjectHomepageEntrypoint() *cobra.Command {
3636
}
3737
}
3838

39+
func gitHubRepoRefFromGitNonNil() (*githubRepoRef, error) {
40+
ref, err := gitHubRepoRefFromGit()
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
if ref == nil {
46+
return nil, errors.New("unable to resolve GitHub organization/repo name")
47+
}
48+
49+
return ref, err
50+
}
51+
52+
// NOTE: returns nil, nil if not a Git repo OR no origin specified OR not a GitHub origin
3953
func gitHubRepoRefFromGit() (*githubRepoRef, error) {
4054
conf, err := os.ReadFile(".git/config")
4155
if err != nil {
42-
return nil, err
56+
if os.IsNotExist(err) {
57+
return nil, nil
58+
} else { // some other error
59+
return nil, err
60+
}
4361
}
4462

4563
// dirty
4664
originParseRe := regexp.MustCompile(`url = [email protected]:(.+)/(.+).git`)
4765

4866
matches := originParseRe.FindStringSubmatch(string(conf))
4967
if matches == nil {
50-
return nil, errors.New("unable to resolve GitHub organization/repo name")
68+
return nil, nil
5169
}
5270

5371
org, repo := matches[1], matches[2]

cmd/bob/quality.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,15 @@ func conditionsPass(conditions []QualityRuleCondition) bool {
8787
panic("repo_origin needs to be in form '*foobar*'")
8888
}
8989

90-
originMatches := strings.Contains(githubURL(getGithubRepoRef()), originWildcard)
90+
ref := getGithubRepoRef()
91+
92+
originMatches := func() bool {
93+
if ref != nil {
94+
return strings.Contains(githubURL(*ref), originWildcard)
95+
} else {
96+
return false
97+
}
98+
}()
9199

92100
if !conditionPasses(originMatches) {
93101
return false
@@ -99,7 +107,7 @@ func conditionsPass(conditions []QualityRuleCondition) bool {
99107
}
100108

101109
// lazily read gitHubRepoRefFromGit() just once
102-
func getGithubRepoRef() githubRepoRef {
110+
func getGithubRepoRef() *githubRepoRef {
103111
if githubRepoRefSingleton == nil {
104112
var err error
105113
githubRepoRefSingleton, err = gitHubRepoRefFromGit()
@@ -108,7 +116,7 @@ func getGithubRepoRef() githubRepoRef {
108116
}
109117
}
110118

111-
return *githubRepoRefSingleton
119+
return githubRepoRefSingleton
112120
}
113121

114122
var githubRepoRefSingleton *githubRepoRef

0 commit comments

Comments
 (0)