|
| 1 | +package ldapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/google/go-github/github" |
| 13 | + ldapi "github.com/launchdarkly/api-client-go/v13" |
| 14 | + lcr "github.com/launchdarkly/find-code-references-in-pull-request/config" |
| 15 | + gha "github.com/launchdarkly/find-code-references-in-pull-request/internal/github_actions" |
| 16 | + "github.com/launchdarkly/find-code-references-in-pull-request/internal/version" |
| 17 | + |
| 18 | + flags "github.com/launchdarkly/find-code-references-in-pull-request/internal/references" |
| 19 | +) |
| 20 | + |
| 21 | +func CreateFlagLinks(config *lcr.Config, flagsRef flags.ReferenceSummary, event *github.PullRequestEvent) { |
| 22 | + pr := event.PullRequest |
| 23 | + if pr == nil || pr.HTMLURL == nil || pr.ID == nil { |
| 24 | + gha.Debug("No pull request found in event") |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + numAdded := len(flagsRef.FlagsAdded) |
| 29 | + numRemoved := len(flagsRef.FlagsRemoved) |
| 30 | + |
| 31 | + for key, aliases := range flagsRef.FlagsAdded { |
| 32 | + message := buildLinkMessage(key, aliases, "added", numAdded, numRemoved) |
| 33 | + link := makeFlagLinkRep(event, key, message) |
| 34 | + postFlagLink(config, *link, key) |
| 35 | + } |
| 36 | + |
| 37 | + for key, aliases := range flagsRef.FlagsRemoved { |
| 38 | + action := "removed" |
| 39 | + if flagsRef.IsExtinct(key) { |
| 40 | + action = "extinct" |
| 41 | + } |
| 42 | + message := buildLinkMessage(key, aliases, action, numAdded, numRemoved) |
| 43 | + link := makeFlagLinkRep(event, key, message) |
| 44 | + postFlagLink(config, *link, key) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func postFlagLink(config *lcr.Config, link ldapi.FlagLinkPost, flagKey string) { |
| 49 | + requestBody, err := json.Marshal(link) |
| 50 | + if err != nil { |
| 51 | + gha.SetWarning("Failed to create flag link for %s", flagKey) |
| 52 | + gha.Debug("Unable to construct flag link payload") |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + url := fmt.Sprintf("%s/api/v2/flag-links/projects/%s/flags/%s", config.LdInstance, config.LdProject, flagKey) |
| 57 | + |
| 58 | + gha.Debug("[POST %s]\n\n%s", url, string(requestBody)) |
| 59 | + |
| 60 | + req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(requestBody)) |
| 61 | + if err != nil { |
| 62 | + gha.SetWarning("Could not to create flag link request") |
| 63 | + return |
| 64 | + } |
| 65 | + req.Header.Set("Content-Type", "application/json") |
| 66 | + req.Header.Set("LD-API-Version", "beta") |
| 67 | + req.Header.Set("Authorization", config.ApiToken) |
| 68 | + req.Header.Add("User-Agent", fmt.Sprintf("find-code-references-pr/%s", version.Version)) |
| 69 | + |
| 70 | + client := new(http.Client) |
| 71 | + resp, err := client.Do(req) |
| 72 | + if err != nil { |
| 73 | + gha.SetWarning("Failed to create flag link for %s", flagKey) |
| 74 | + gha.Debug("Error when sending flag link request:\n\n%s", err.Error()) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + defer resp.Body.Close() |
| 79 | + body, err := io.ReadAll(resp.Body) |
| 80 | + if err != nil { |
| 81 | + gha.Debug("Could not parse flag link request") |
| 82 | + } |
| 83 | + |
| 84 | + switch resp.StatusCode { |
| 85 | + case http.StatusCreated: |
| 86 | + gha.Log("[POST %s] Flag link created [url=%s]", url, *link.DeepLink) |
| 87 | + case http.StatusConflict: |
| 88 | + gha.Log("[POST %s] Flag link already exists [url=%s]", url, *link.DeepLink) |
| 89 | + default: |
| 90 | + gha.SetWarning("Failed to create flag link for %s", flagKey) |
| 91 | + gha.Log("[POST %s] Flag link request failed [status=%d]", url, resp.StatusCode) |
| 92 | + } |
| 93 | + |
| 94 | + gha.Debug("Response:\n\n%s", string(body)) |
| 95 | +} |
| 96 | + |
| 97 | +func makeFlagLinkRep(event *github.PullRequestEvent, flagKey, message string) *ldapi.FlagLinkPost { |
| 98 | + pr := event.PullRequest |
| 99 | + if pr == nil || pr.HTMLURL == nil || pr.ID == nil { |
| 100 | + return nil |
| 101 | + } |
| 102 | + |
| 103 | + metadata := map[string]string{ |
| 104 | + "message": message, |
| 105 | + "prNumber": strconv.Itoa(*pr.Number), |
| 106 | + "prTitle": *pr.Title, |
| 107 | + "prBody": *pr.Body, |
| 108 | + "state": *pr.State, |
| 109 | + "avatarUrl": *pr.User.AvatarURL, |
| 110 | + "repoName": *event.Repo.FullName, |
| 111 | + "repoUrl": *event.Repo.HTMLURL, |
| 112 | + } |
| 113 | + |
| 114 | + if pr.User.Name != nil { |
| 115 | + metadata["authorName"] = *pr.User.Name |
| 116 | + metadata["authorDisplayName"] = *pr.User.Name |
| 117 | + } else { |
| 118 | + metadata["authorDisplayName"] = *pr.User.Login |
| 119 | + metadata["authorLogin"] = *pr.User.Login |
| 120 | + } |
| 121 | + |
| 122 | + var timestamp *int64 |
| 123 | + if pr.CreatedAt != nil { |
| 124 | + m := pr.CreatedAt.UnixMilli() |
| 125 | + timestamp = &m |
| 126 | + } |
| 127 | + |
| 128 | + // TODO enable integration once capability is available |
| 129 | + // integration := "github" |
| 130 | + id := strconv.FormatInt(*pr.ID, 10) |
| 131 | + // key must be unique |
| 132 | + key := fmt.Sprintf("github-pr-%s-%s", id, flagKey) |
| 133 | + |
| 134 | + return &ldapi.FlagLinkPost{ |
| 135 | + DeepLink: pr.HTMLURL, |
| 136 | + Key: &key, |
| 137 | + // IntegrationKey: &integration, |
| 138 | + Timestamp: timestamp, |
| 139 | + Title: getLinkTitle(event), |
| 140 | + Description: pr.Body, |
| 141 | + Metadata: &metadata, |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func getLinkTitle(event *github.PullRequestEvent) *string { |
| 146 | + builder := new(strings.Builder) |
| 147 | + builder.WriteString(fmt.Sprintf("[%s]", *event.Repo.FullName)) |
| 148 | + |
| 149 | + pr := event.PullRequest |
| 150 | + if pr.Title != nil { |
| 151 | + builder.WriteString(" ") |
| 152 | + builder.WriteString(*pr.Title) |
| 153 | + if pr.Number != nil { |
| 154 | + builder.WriteString(fmt.Sprintf(" (#%d)", *pr.Number)) |
| 155 | + } |
| 156 | + } else if pr.Number != nil { |
| 157 | + builder.WriteString(fmt.Sprintf(" PR #%d", *pr.Number)) |
| 158 | + } else { |
| 159 | + builder.WriteString(" pull request") |
| 160 | + } |
| 161 | + |
| 162 | + title := builder.String() |
| 163 | + |
| 164 | + return &title |
| 165 | +} |
| 166 | + |
| 167 | +func buildLinkMessage(key string, aliases []string, action string, added, removed int) string { |
| 168 | + builder := new(strings.Builder) |
| 169 | + builder.WriteString(fmt.Sprintf("Flag %s", action)) |
| 170 | + if len(aliases) > 0 { |
| 171 | + builder.WriteString(fmt.Sprintf(" (aliases: %s)", strings.Join(aliases, ", "))) |
| 172 | + } |
| 173 | + |
| 174 | + if added > 0 { |
| 175 | + count := added |
| 176 | + if action == "added" { |
| 177 | + count-- |
| 178 | + } |
| 179 | + if count > 0 { |
| 180 | + builder.WriteString(fmt.Sprintf("\n\t- Added %d other flags", count)) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if removed > 0 { |
| 185 | + count := removed |
| 186 | + if action == "added" { |
| 187 | + count-- |
| 188 | + } |
| 189 | + if count > 0 { |
| 190 | + builder.WriteString(fmt.Sprintf("\n\t- Removed %d other flags", count)) |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + return builder.String() |
| 195 | +} |
0 commit comments