Skip to content

Commit

Permalink
rename and rework logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jazanne committed Feb 2, 2024
1 parent d93ac9f commit a81186c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 32 deletions.
8 changes: 4 additions & 4 deletions comments/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"html"
"html/template"
"log"
"reflect"
"sort"
"strings"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/google/go-github/github"
ldapi "github.com/launchdarkly/api-client-go/v13"
lcr "github.com/launchdarkly/find-code-references-in-pull-request/config"
gha "github.com/launchdarkly/find-code-references-in-pull-request/internal/github_actions"
refs "github.com/launchdarkly/find-code-references-in-pull-request/internal/references"
)

Expand Down Expand Up @@ -113,7 +113,7 @@ func BuildFlagComment(buildComment FlagComments, flagsRef refs.ReferenceSummary,

hash := md5.Sum([]byte(postedComments))
if existingComment != nil && strings.Contains(*existingComment.Body, hex.EncodeToString(hash[:])) {
log.Println("comment already exists")
gha.Log("comment already exists")
return ""
}

Expand All @@ -129,7 +129,7 @@ func ProcessFlags(flagsRef refs.ReferenceSummary, flags []ldapi.FeatureFlag, con
idx, _ := find(flags, flagKey)
createComment, err := githubFlagComment(flags[idx], flagAliases, true, false, config)
if err != nil {
log.Println(err)
gha.LogError(err)
}
buildComment.CommentsAdded = append(buildComment.CommentsAdded, createComment)
}
Expand All @@ -144,7 +144,7 @@ func ProcessFlags(flagsRef refs.ReferenceSummary, flags []ldapi.FeatureFlag, con
}
removedComment, err := githubFlagComment(flags[idx], flagAliases, false, extinct, config)
if err != nil {
log.Println(err)
gha.LogError(err)
}
buildComment.CommentsRemoved = append(buildComment.CommentsRemoved, removedComment)
}
Expand Down
2 changes: 1 addition & 1 deletion diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func ProcessDiffs(matcher lsearch.Matcher, contents []byte, builder *refs.Refere
elementMatcher := matcher.Elements[0]
for _, flagKey := range elementMatcher.FindMatches(line) {
aliasMatches := elementMatcher.FindAliases(line, flagKey)
gha.LogDebug("Found (%s) reference to flag %s with aliases %v", op, flagKey, aliasMatches)
gha.Debug("Found (%s) reference to flag %s with aliases %v", op, flagKey, aliasMatches)
builder.AddReference(flagKey, op, aliasMatches)
}
if builder.MaxReferences() {
Expand Down
2 changes: 1 addition & 1 deletion internal/aliases/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func GenerateAliases(opts options.Options, flagKeys []string, diffContents alias
return nil, err
}

gha.LogDebug("Generating aliases for removed files...")
gha.Debug("Generating aliases for removed files...")
filePatternAliases := getFilepatternAliases(opts.Aliases)
for _, flag := range flagKeys {
for _, alias := range filePatternAliases {
Expand Down
2 changes: 1 addition & 1 deletion internal/extinctions/extinctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func CheckExtinctions(opts options.Options, builder *refs.ReferenceSummaryBuilde
return err
}

gha.LogDebug("Searching for any remaining references to removed flags...")
gha.Debug("Searching for any remaining references to removed flags...")
references, err := ld_search.SearchForRefs(opts.Dir, matcher)
if err != nil {
return err
Expand Down
24 changes: 15 additions & 9 deletions internal/github_actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package github_actions

import (
"fmt"
"log"
"os"
)

func SetOutputOrLogError(name, value string) {
if err := SetOutput(name, value); err != nil {
LogError("Failed to set outputs.%s\n", name)
func SetOutput(name, value string) {
if err := setOutput(name, value); err != nil {
SetError("Failed to set outputs.%s\n", name)
}
}

func SetOutput(name, value string) error {
func setOutput(name, value string) error {
Debug("setting output %s=%s", name, value)
output := os.Getenv("GITHUB_OUTPUT")

f, err := os.OpenFile(output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
Expand All @@ -28,22 +30,26 @@ func MaskInput(input string) {
}

func Log(format string, a ...any) {
fmt.Println(fmt.Sprintf(format, a...))
log.Println(fmt.Sprintf(format, a...))
}

func LogNotice(format string, a ...any) {
func LogError(err error) {
log.Println(err)
}

func SetNotice(format string, a ...any) {
fmt.Printf("::notice::%s\n", fmt.Sprintf(format, a...))
}

func LogWarning(format string, a ...any) {
func SetWarning(format string, a ...any) {
fmt.Printf("::warning::%s\n", fmt.Sprintf(format, a...))
}

func LogError(format string, a ...any) {
func SetError(format string, a ...any) {
fmt.Printf("::error::%s\n", fmt.Sprintf(format, a...))
}

func LogDebug(format string, a ...any) {
func Debug(format string, a ...any) {
fmt.Printf("::debug::%s\n", fmt.Sprintf(format, a...))
}

Expand Down
2 changes: 1 addition & 1 deletion internal/ldclient/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func GetAllFlags(config *lcr.Config) ([]ldapi.FeatureFlag, error) {
gha.LogDebug("Fetching all flags for project")
gha.Debug("Fetching all flags for project")
params := url.Values{}
params.Add("env", config.LdEnvironment)
activeFlags, err := getFlags(config, params)
Expand Down
28 changes: 13 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"sort"
Expand Down Expand Up @@ -42,7 +41,7 @@ func main() {
failExit(err)

if len(flags) == 0 {
gha.LogNotice("No flags found in project %s", config.LdProject)
gha.SetNotice("No flags found in project %s", config.LdProject)
os.Exit(0)
}

Expand Down Expand Up @@ -73,8 +72,8 @@ func main() {

if config.CheckExtinctions {
if err := extinctions.CheckExtinctions(opts, builder); err != nil {
gha.LogWarning("Error checking for extinct flags")
log.Println(err)
gha.SetWarning("Error checking for extinct flags")
gha.LogError(err)
}
}
flagsRef := builder.Build()
Expand Down Expand Up @@ -102,7 +101,7 @@ func main() {
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 {
log.Println(err)
gha.LogError(err)
}

for _, comment := range comments {
Expand Down Expand Up @@ -154,7 +153,7 @@ func postGithubComment(ctx context.Context, flagsRef references.ReferenceSummary
}

func getDiffs(ctx context.Context, config *lcr.Config, prNumber int) ([]*diff.FileDiff, error) {
gha.LogDebug("Getting pull request diff...")
gha.Debug("Getting pull request diff...")
rawOpts := github.RawOptions{Type: github.Diff}
raw, resp, err := config.GHClient.PullRequests.GetRaw(ctx, config.Owner, config.Repo, prNumber, rawOpts)
if err != nil {
Expand All @@ -174,15 +173,14 @@ func getOptions(config *lcr.Config) (options.Options, error) {
viper.Set("dir", config.Workspace)
viper.Set("accessToken", config.ApiToken)

err := options.InitYAML()
if err != nil {
log.Println(err)
if err := options.InitYAML(); err != nil {
gha.LogError(err)
}
return options.GetOptions()
}

func setOutputs(config *lcr.Config, flagsRef references.ReferenceSummary) {
gha.LogDebug("Setting outputs...")
gha.Debug("Setting outputs...")
flagsModified := flagsRef.AddedKeys()
setOutputsForChangedFlags("modified", flagsModified)

Expand All @@ -202,17 +200,17 @@ func setOutputs(config *lcr.Config, flagsRef references.ReferenceSummary) {

func setOutputsForChangedFlags(modifier string, changedFlags []string) {
count := len(changedFlags)
gha.SetOutputOrLogError(fmt.Sprintf("any-%s", modifier), fmt.Sprintf("%t", count > 0))
gha.SetOutputOrLogError(fmt.Sprintf("%s-flags-count", modifier), fmt.Sprintf("%d", count))
gha.SetOutput(fmt.Sprintf("any-%s", modifier), fmt.Sprintf("%t", count > 0))
gha.SetOutput(fmt.Sprintf("%s-flags-count", modifier), fmt.Sprintf("%d", count))

sort.Strings(changedFlags)
gha.SetOutputOrLogError(fmt.Sprintf("%s-flags", modifier), strings.Join(changedFlags, " "))
gha.SetOutput(fmt.Sprintf("%s-flags", modifier), strings.Join(changedFlags, " "))
}

func failExit(err error) {
if err != nil {
gha.LogError(err.Error())
log.Println(err)
gha.LogError(err)
gha.SetError(err.Error())
os.Exit(1)
}
}

0 comments on commit a81186c

Please sign in to comment.