Skip to content

Commit 587300f

Browse files
monsagricollinmurd
andauthored
Ffeldberg/rel 4888/support enterprise instances (#147)
Adding support for instantiating a github enterprise client if the PR we are checking isn't in a regular GH account. Also adds some safety for nil pointer references and a fallback value for required flag link values. --------- Co-authored-by: collinmurd <collinmurdock22@gmail.com>
1 parent c49a2f6 commit 587300f

File tree

6 files changed

+49
-18
lines changed

6 files changed

+49
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
### Added
44

5+
- Add support for enterprise hosted github instances [More info](https://github.com/launchdarkly/find-code-references-in-pull-request/issues/102)
6+
57
### Changed
68

79
### Fixed
810

11+
- Potential null pointer error when creating flag links
12+
913
## 2.0.2
1014

1115
### Changed

config/config.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,27 @@ func ValidateInputandParse(ctx context.Context) (*Config, error) {
9898
config.CreateFlagLinks = createFlagLinks
9999
}
100100

101-
config.GHClient = getGithubClient(ctx)
101+
client, err := getGithubClient(ctx)
102+
if err != nil {
103+
return nil, err
104+
}
105+
config.GHClient = client
106+
102107
return &config, nil
103108
}
104109

105-
func getGithubClient(ctx context.Context) *github.Client {
110+
func getGithubClient(ctx context.Context) (*github.Client, error) {
106111
ts := oauth2.StaticTokenSource(
107112
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
108113
)
109114
tc := oauth2.NewClient(ctx, ts)
110-
return github.NewClient(tc)
115+
gc := github.NewClient(tc)
116+
117+
host := os.Getenv("GITHUB_SERVER_URL")
118+
if host != "https://github.com" && host != "" {
119+
gha.Log("Using GitHub Enterprise host. baseUrl: %s, uploadUrl: %s", host+"/api/v3/", host+"/api/uploads/")
120+
return gc.WithEnterpriseURLs(host+"/api/v3/", host+"/api/uploads/")
121+
}
122+
123+
return gc, nil
111124
}

internal/ldclient/flag_links.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
ldapi "github.com/launchdarkly/api-client-go/v15"
1515
lcr "github.com/launchdarkly/find-code-references-in-pull-request/config"
1616
gha "github.com/launchdarkly/find-code-references-in-pull-request/internal/github_actions"
17+
"github.com/launchdarkly/find-code-references-in-pull-request/internal/utils"
1718
"github.com/launchdarkly/find-code-references-in-pull-request/internal/version"
1819

1920
flags "github.com/launchdarkly/find-code-references-in-pull-request/internal/references"
@@ -101,23 +102,24 @@ func makeFlagLinkRep(event *github.PullRequestEvent, flagKey, message string) *l
101102
return nil
102103
}
103104

105+
authorLogin := utils.SafeString(pr.User.Login)
104106
metadata := map[string]string{
105107
"message": message,
106108
"prNumber": strconv.Itoa(*pr.Number),
107-
"prTitle": *pr.Title,
108-
"prBody": *pr.Body,
109-
"state": *pr.State,
110-
"avatarUrl": *pr.User.AvatarURL,
111-
"repoName": *event.Repo.FullName,
112-
"repoUrl": *event.Repo.HTMLURL,
109+
"prTitle": utils.SafeString(pr.Title),
110+
"prBody": utils.SafeString(pr.Body),
111+
"state": utils.SafeString(pr.State),
112+
"avatarUrl": utils.SafeString(pr.User.AvatarURL),
113+
"repoName": utils.SafeString(event.Repo.FullName),
114+
"repoUrl": utils.SafeString(event.Repo.HTMLURL),
113115
}
114116

115117
if pr.User.Name != nil {
116-
metadata["authorName"] = *pr.User.Name
117-
metadata["authorDisplayName"] = *pr.User.Name
118+
metadata["authorName"] = utils.SafeString(pr.User.Name)
119+
metadata["authorDisplayName"] = utils.SafeString(pr.User.Name)
118120
} else {
119-
metadata["authorDisplayName"] = *pr.User.Login
120-
metadata["authorLogin"] = *pr.User.Login
121+
metadata["authorDisplayName"] = authorLogin
122+
metadata["authorLogin"] = authorLogin
121123
}
122124

123125
var timestamp *int64
@@ -131,25 +133,30 @@ func makeFlagLinkRep(event *github.PullRequestEvent, flagKey, message string) *l
131133
// key must be unique
132134
key := fmt.Sprintf("github-pr-%s-%s", id, flagKey)
133135

136+
description := utils.SafeString(pr.Body)
137+
// Flag links require a description
138+
if description == "" {
139+
description = "Empty PR Body"
140+
}
134141
return &ldapi.FlagLinkPost{
135142
DeepLink: pr.HTMLURL,
136143
Key: &key,
137144
IntegrationKey: &integration,
138145
Timestamp: timestamp,
139146
Title: getLinkTitle(event),
140-
Description: pr.Body,
147+
Description: &description,
141148
Metadata: &metadata,
142149
}
143150
}
144151

145152
func getLinkTitle(event *github.PullRequestEvent) *string {
146153
builder := new(strings.Builder)
147-
builder.WriteString(fmt.Sprintf("[%s]", *event.Repo.FullName))
154+
builder.WriteString(fmt.Sprintf("[%s]", utils.SafeString(event.Repo.FullName)))
148155

149156
pr := event.PullRequest
150157
if pr.Title != nil {
151158
builder.WriteString(" ")
152-
builder.WriteString(*pr.Title)
159+
builder.WriteString(utils.SafeString(pr.Title))
153160
if pr.Number != nil {
154161
builder.WriteString(fmt.Sprintf(" (#%d)", *pr.Number))
155162
}

internal/utils/utils.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ func Dedupe(s []string) []string {
1414
}
1515
return ret
1616
}
17+
18+
func SafeString(s *string) string {
19+
if s != nil {
20+
return *s
21+
}
22+
return ""
23+
}

internal/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package version
22

3-
const Version = "2.0.2"
3+
const Version = "2.0.3-alpha"

testdata/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ show-widgets
55
show_widgets
66
mobile-app-promo-ios
77
mobile-app-promo-ios
8-
beta-ui
8+
beta-ui

0 commit comments

Comments
 (0)