From 626bee5cac616c6f2fbc1bfa304a8f4941d41e56 Mon Sep 17 00:00:00 2001 From: Shane C Date: Fri, 6 Sep 2024 16:02:10 -0400 Subject: [PATCH] issue creation and editing, edit markdown output --- cmd/root.go | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 83d7fcd..d9d787b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -128,14 +128,14 @@ var rootCmd = &cobra.Command{ var markdownOutput strings.Builder markdownHandler := markdown.NewMarkdown(&markdownOutput) - markdownHandler.H1("GoSec Results:") - - markdownHandler.PlainText("
") - markdownHandler.PlainText("Results:\n") + markdownHandler.H1("GoSec Report:") + markdownHandler.PlainText("This report automatically updates each time the action runs.\n") if len(rows) == 0 { markdownHandler.PlainText("**Nothing Found! 🥳**") } else { + markdownHandler.PlainText("
") + markdownHandler.PlainText("Results:\n") if sevCountMap["high"] != 0 { markdownHandler.PlainText(fmt.Sprintf("🟥 **%d** high severity issues\n", sevCountMap["high"])) } @@ -156,17 +156,18 @@ var rootCmd = &cobra.Command{ AutoWrapText: false, AutoFormatHeaders: false, }) + markdownHandler.PlainText("
") } - markdownHandler.PlainText("
") - err = markdownHandler.Build() if err != nil { log.Fatalln(err) } + markdownOutputStr := markdownOutput.String() + if isAction { - action.AddStepSummary(markdownOutput.String()) + action.AddStepSummary(markdownOutputStr) } actionCtx, err := action.Context() @@ -181,15 +182,34 @@ var rootCmd = &cobra.Command{ repoOwner, repoName := actionCtx.Repo() - fmt.Println("hi") - - if _, _, err := forgeClient.CreateIssue(repoOwner, repoName, forgejo.CreateIssueOption{ - Title: "testing", - Body: markdownOutput.String(), - }); err != nil { + issues, _, err := forgeClient.ListIssues(forgejo.ListIssueOption{ + CreatedBy: "forgejo-actions", + KeyWord: "GoSec Report", + Type: forgejo.IssueTypeIssue, + }) + if err != nil { log.Fatalln(err) } + if len(issues) == 0 { + if _, _, err := forgeClient.CreateIssue(repoOwner, repoName, forgejo.CreateIssueOption{ + Title: "GoSec Report", + Body: markdownOutputStr, + }); err != nil { + log.Fatalln(err) + } + } else { + for _, issue := range issues { + if issue.Title == "GoSec Report" && strings.Contains(issue.Body, "GoSec Report:") { + if _, _, err := forgeClient.EditIssue(repoOwner, repoName, issue.ID, forgejo.EditIssueOption{ + Body: &markdownOutputStr, + }); err != nil { + log.Fatalln(err) + } + } + } + } + }, }