From 587300ffc8ce465162f6e0b16c010c903f73294a Mon Sep 17 00:00:00 2001 From: Fabian Date: Thu, 16 Jan 2025 14:04:50 +0100 Subject: [PATCH] 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 --- CHANGELOG.md | 4 ++++ config/config.go | 19 ++++++++++++++++--- internal/ldclient/flag_links.go | 33 ++++++++++++++++++++------------- internal/utils/utils.go | 7 +++++++ internal/version/version.go | 2 +- testdata/test | 2 +- 6 files changed, 49 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d1ea1e1..1fbdfb6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/config.go b/config/config.go index 20c554a0..07cc7669 100644 --- a/config/config.go +++ b/config/config.go @@ -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 } diff --git a/internal/ldclient/flag_links.go b/internal/ldclient/flag_links.go index 8c9fc584..6555f2b6 100644 --- a/internal/ldclient/flag_links.go +++ b/internal/ldclient/flag_links.go @@ -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" @@ -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 @@ -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)) } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 9c5375a8..a07ec9a0 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -14,3 +14,10 @@ func Dedupe(s []string) []string { } return ret } + +func SafeString(s *string) string { + if s != nil { + return *s + } + return "" +} diff --git a/internal/version/version.go b/internal/version/version.go index eacf5007..61697b03 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -1,3 +1,3 @@ package version -const Version = "2.0.2" +const Version = "2.0.3-alpha" diff --git a/testdata/test b/testdata/test index 613b7907..4de0a8af 100644 --- a/testdata/test +++ b/testdata/test @@ -5,4 +5,4 @@ show-widgets show_widgets mobile-app-promo-ios mobile-app-promo-ios -beta-ui +beta-ui \ No newline at end of file