Skip to content

Commit

Permalink
Ffeldberg/rel 4888/support enterprise instances (#147)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
monsagri and collinmurd authored Jan 16, 2025
1 parent c49a2f6 commit 587300f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

### Added

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

### Changed

### Fixed

- Potential null pointer error when creating flag links

## 2.0.2

### Changed
Expand Down
19 changes: 16 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,27 @@ func ValidateInputandParse(ctx context.Context) (*Config, error) {
config.CreateFlagLinks = createFlagLinks
}

config.GHClient = getGithubClient(ctx)
client, err := getGithubClient(ctx)
if err != nil {
return nil, err
}
config.GHClient = client

return &config, nil
}

func getGithubClient(ctx context.Context) *github.Client {
func getGithubClient(ctx context.Context) (*github.Client, error) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
return github.NewClient(tc)
gc := github.NewClient(tc)

host := os.Getenv("GITHUB_SERVER_URL")
if host != "https://github.com" && host != "" {
gha.Log("Using GitHub Enterprise host. baseUrl: %s, uploadUrl: %s", host+"/api/v3/", host+"/api/uploads/")
return gc.WithEnterpriseURLs(host+"/api/v3/", host+"/api/uploads/")
}

return gc, nil
}
33 changes: 20 additions & 13 deletions internal/ldclient/flag_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
ldapi "github.com/launchdarkly/api-client-go/v15"
lcr "github.com/launchdarkly/find-code-references-in-pull-request/config"
gha "github.com/launchdarkly/find-code-references-in-pull-request/internal/github_actions"
"github.com/launchdarkly/find-code-references-in-pull-request/internal/utils"
"github.com/launchdarkly/find-code-references-in-pull-request/internal/version"

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

authorLogin := utils.SafeString(pr.User.Login)
metadata := map[string]string{
"message": message,
"prNumber": strconv.Itoa(*pr.Number),
"prTitle": *pr.Title,
"prBody": *pr.Body,
"state": *pr.State,
"avatarUrl": *pr.User.AvatarURL,
"repoName": *event.Repo.FullName,
"repoUrl": *event.Repo.HTMLURL,
"prTitle": utils.SafeString(pr.Title),
"prBody": utils.SafeString(pr.Body),
"state": utils.SafeString(pr.State),
"avatarUrl": utils.SafeString(pr.User.AvatarURL),
"repoName": utils.SafeString(event.Repo.FullName),
"repoUrl": utils.SafeString(event.Repo.HTMLURL),
}

if pr.User.Name != nil {
metadata["authorName"] = *pr.User.Name
metadata["authorDisplayName"] = *pr.User.Name
metadata["authorName"] = utils.SafeString(pr.User.Name)
metadata["authorDisplayName"] = utils.SafeString(pr.User.Name)
} else {
metadata["authorDisplayName"] = *pr.User.Login
metadata["authorLogin"] = *pr.User.Login
metadata["authorDisplayName"] = authorLogin
metadata["authorLogin"] = authorLogin
}

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

description := utils.SafeString(pr.Body)
// Flag links require a description
if description == "" {
description = "Empty PR Body"
}
return &ldapi.FlagLinkPost{
DeepLink: pr.HTMLURL,
Key: &key,
IntegrationKey: &integration,
Timestamp: timestamp,
Title: getLinkTitle(event),
Description: pr.Body,
Description: &description,
Metadata: &metadata,
}
}

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

pr := event.PullRequest
if pr.Title != nil {
builder.WriteString(" ")
builder.WriteString(*pr.Title)
builder.WriteString(utils.SafeString(pr.Title))
if pr.Number != nil {
builder.WriteString(fmt.Sprintf(" (#%d)", *pr.Number))
}
Expand Down
7 changes: 7 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ func Dedupe(s []string) []string {
}
return ret
}

func SafeString(s *string) string {
if s != nil {
return *s
}
return ""
}
2 changes: 1 addition & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package version

const Version = "2.0.2"
const Version = "2.0.3-alpha"
2 changes: 1 addition & 1 deletion testdata/test
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ show-widgets
show_widgets
mobile-app-promo-ios
mobile-app-promo-ios
beta-ui
beta-ui

0 comments on commit 587300f

Please sign in to comment.