Skip to content

Commit

Permalink
chore: update api request for flags (#42)
Browse files Browse the repository at this point in the history
- delete unused code
- lock to latest version of flags api, which guarantees all flags are
fetched
- update api-client-go to latest
- wrap error and failExit to make sure there's consistent err logging
- set user agent (if we use release we can have it update version like
it does in `ld-find-code-refs`
  • Loading branch information
jazanne authored Aug 3, 2023
1 parent 9abd947 commit df205fc
Show file tree
Hide file tree
Showing 411 changed files with 125,211 additions and 94 deletions.
49 changes: 0 additions & 49 deletions client/client.go

This file was deleted.

2 changes: 1 addition & 1 deletion comments/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
sprig "github.com/Masterminds/sprig/v3"

"github.com/google/go-github/github"
ldapi "github.com/launchdarkly/api-client-go/v7"
ldapi "github.com/launchdarkly/api-client-go/v13"
"github.com/launchdarkly/cr-flags/config"
lcr "github.com/launchdarkly/cr-flags/config"
lflags "github.com/launchdarkly/cr-flags/flags"
Expand Down
8 changes: 3 additions & 5 deletions comments/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import (
"strings"
"testing"

ldapi "github.com/launchdarkly/api-client-go/v7"
ldapi "github.com/launchdarkly/api-client-go/v13"
"github.com/launchdarkly/cr-flags/config"
lflags "github.com/launchdarkly/cr-flags/flags"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func ptr(v interface{}) *interface{} { return &v }
func ptr[T any](t T) *T { return &t }

type testFlagEnv struct {
Flag ldapi.FeatureFlag
Config config.Config
}

func newTestAccEnv() *testFlagEnv {

flag := createFlag("example-flag")
config := config.Config{
LdEnvironment: "production",
Expand All @@ -32,11 +31,10 @@ func newTestAccEnv() *testFlagEnv {
}

func createFlag(key string) ldapi.FeatureFlag {
href := "test"
environment := ldapi.FeatureFlagConfig{
EnvironmentName: "Production",
Site: ldapi.Link{
Href: &href,
Href: ptr("test"),
},
}
variationTrue := ldapi.Variation{
Expand Down
11 changes: 3 additions & 8 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@ package diff
import (
"testing"

ldapi "github.com/launchdarkly/api-client-go/v7"
ldapi "github.com/launchdarkly/api-client-go/v13"
"github.com/launchdarkly/cr-flags/config"
lflags "github.com/launchdarkly/cr-flags/flags"
lsearch "github.com/launchdarkly/ld-find-code-refs/v2/search"
"github.com/sourcegraph/go-diff/diff"
"github.com/stretchr/testify/assert"
)

func ptr(v interface{}) *interface{} { return &v }
func ptr[T any](t T) *T { return &t }

func createFlag(key string) ldapi.FeatureFlag {
variation := int32(0)
href := "test"
environment := ldapi.FeatureFlagConfig{
Site: ldapi.Link{
Href: &href,
},
Fallthrough: ldapi.VariationOrRolloutRep{
Variation: &variation,
Href: ptr("test"),
},
}
variationTrue := ldapi.Variation{
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ require (
require (
github.com/jstemmer/go-junit-report/v2 v2.0.0
github.com/kyoh86/richgo v0.3.12
github.com/launchdarkly/api-client-go/v7 v7.1.1
github.com/launchdarkly/api-client-go/v13 v13.0.0
github.com/launchdarkly/ld-find-code-refs/v2 v2.10.1-0.20230628134336-ed386199c5f4
)

require github.com/launchdarkly/api-client-go/v7 v7.1.1 // indirect

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
Expand Down Expand Up @@ -48,6 +50,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ github.com/kyoh86/richgo v0.3.12 h1:W66IRaaC1BWoCPIyI94t/PChWBQ38QFDRsF1nWu4904=
github.com/kyoh86/richgo v0.3.12/go.mod h1:op8jQyhnxSZQelHDyupswwJGerEDB6zkKfzCnuqAwx4=
github.com/kyoh86/xdg v1.2.0 h1:CERuT/ShdTDj+A2UaX3hQ3mOV369+Sj+wyn2nIRIIkI=
github.com/kyoh86/xdg v1.2.0/go.mod h1:/mg8zwu1+qe76oTFUBnyS7rJzk7LLC0VGEzJyJ19DHs=
github.com/launchdarkly/api-client-go/v13 v13.0.0 h1:DcNotcPkkvmCPI1CXAPfk5WEvuViov/Fo/5fbnD096w=
github.com/launchdarkly/api-client-go/v13 v13.0.0/go.mod h1:cQRkOAs0LGcfIs6RSsHNqwhzItUZooyhpqPv0hgiQZM=
github.com/launchdarkly/api-client-go/v7 v7.1.1 h1:3VBkFt9xHljMw5KDlVFDUogxfH78Y7GLVu8irBC8Gy8=
github.com/launchdarkly/api-client-go/v7 v7.1.1/go.mod h1:GVl1inKsWoKX3yLgdqrjxWw8k4ih0HlSmdnrhi5NNDs=
github.com/launchdarkly/json-patch v0.0.0-20180720210516-dd68d883319f h1:jfiPiz2hE/7mHv2NOS4cm07sSJCsKlbxmR7pzPhhvpU=
Expand Down Expand Up @@ -204,6 +206,7 @@ github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 h1:
github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9/go.mod h1:EHPiTAKtiFmrMldLUNswFwfZ2eJIYBHktdaUTZxYWRw=
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
37 changes: 37 additions & 0 deletions internal/ldclient/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ldapi

import (
"encoding/json"
"fmt"
"net/http"

ldapi "github.com/launchdarkly/api-client-go/v13"
lcr "github.com/launchdarkly/cr-flags/config"
"github.com/launchdarkly/cr-flags/internal/version"
)

func GetFlags(config *lcr.Config) ([]ldapi.FeatureFlag, error) {
url := fmt.Sprintf("%s/api/v2/flags/%s?env=%s", config.LdInstance, config.LdProject, config.LdEnvironment)
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return []ldapi.FeatureFlag{}, err
}
req.Header.Add("Authorization", config.ApiToken)
req.Header.Add("LD-API-Version", "20220603")
req.Header.Add("User-Agent", fmt.Sprintf("find-code-references-pr/%s", version.Version))

resp, err := client.Do(req)
if err != nil {
return []ldapi.FeatureFlag{}, err
}
defer resp.Body.Close()

flags := ldapi.FeatureFlags{}
err = json.NewDecoder(resp.Body).Decode(&flags)
if err != nil {
return []ldapi.FeatureFlag{}, err
}

return flags.Items, nil
}
3 changes: 3 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package version

const Version = "0.7.0-rc"
35 changes: 6 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ package main

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"sort"
"strings"

"github.com/pkg/errors"

"github.com/google/go-github/github"
ldapi "github.com/launchdarkly/api-client-go/v7"
ghc "github.com/launchdarkly/cr-flags/comments"
lcr "github.com/launchdarkly/cr-flags/config"
ldiff "github.com/launchdarkly/cr-flags/diff"
e "github.com/launchdarkly/cr-flags/errors"
lflags "github.com/launchdarkly/cr-flags/flags"
gha "github.com/launchdarkly/cr-flags/internal/github_actions"
ldclient "github.com/launchdarkly/cr-flags/internal/ldclient"
"github.com/launchdarkly/cr-flags/search"
"github.com/launchdarkly/ld-find-code-refs/v2/options"
"github.com/sourcegraph/go-diff/diff"
Expand All @@ -32,12 +33,12 @@ func main() {
eventPath := os.Getenv("GITHUB_EVENT_PATH")
event, err := parseEvent(eventPath)
if err != nil {
log.Printf("error parsing GitHub event payload at %q: %v", eventPath, err)
os.Exit(1)
err := errors.Wrap(err, fmt.Sprintf("error parsing GitHub event payload at %q", eventPath))
failExit(err)
}

// Query for flags
flags, err := getFlags(config)
flags, err := ldclient.GetFlags(config)
failExit(err)

if len(flags) == 0 {
Expand Down Expand Up @@ -87,30 +88,6 @@ func main() {
failExit(err)
}

func getFlags(config *lcr.Config) ([]ldapi.FeatureFlag, error) {
url := fmt.Sprintf("%s/api/v2/flags/%s?env=%s", config.LdInstance, config.LdProject, config.LdEnvironment)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return []ldapi.FeatureFlag{}, err
}
req.Header.Add("Authorization", config.ApiToken)

resp, err := client.Do(req)
if err != nil {
return []ldapi.FeatureFlag{}, err
}
defer resp.Body.Close()

flags := ldapi.FeatureFlags{}
err = json.NewDecoder(resp.Body).Decode(&flags)
if err != nil {
return []ldapi.FeatureFlag{}, err
}

return flags.Items, nil
}

func checkExistingComments(event *github.PullRequestEvent, config *lcr.Config, ctx context.Context) *github.IssueComment {
comments, _, err := config.GHClient.Issues.ListComments(ctx, config.Owner, config.Repo, *event.PullRequest.Number, nil)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"strings"

ldapi "github.com/launchdarkly/api-client-go/v7"
ldapi "github.com/launchdarkly/api-client-go/v13"
"github.com/launchdarkly/ld-find-code-refs/v2/aliases"
"github.com/launchdarkly/ld-find-code-refs/v2/options"
lsearch "github.com/launchdarkly/ld-find-code-refs/v2/search"
Expand Down
24 changes: 24 additions & 0 deletions vendor/github.com/launchdarkly/api-client-go/v13/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/launchdarkly/api-client-go/v13/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions vendor/github.com/launchdarkly/api-client-go/v13/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit df205fc

Please sign in to comment.