From d16e41712a00767ec000e21c26079476f928f63d Mon Sep 17 00:00:00 2001 From: Mateus Caruccio Date: Fri, 9 Aug 2024 00:27:05 +0100 Subject: [PATCH 1/2] Feature: Save output Fixes #131 --- action.yaml | 5 ++++ approval.go | 16 +++++++++++++ approval_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 6 +++++ 4 files changed, 86 insertions(+) diff --git a/action.yaml b/action.yaml index 7866c0b..3cc99bf 100644 --- a/action.yaml +++ b/action.yaml @@ -31,6 +31,11 @@ inputs: additional-denied-words: description: Comma separated list of words that can be used to deny beyond the defaults. default: '' +outputs: + issue-number: + description: The number of the issued created + issue-url: + description: The URL of the issued created runs: using: docker image: docker://ghcr.io/trstringer/manual-approval:1.9.1 diff --git a/approval.go b/approval.go index e2bb13f..6c1a8a2 100644 --- a/approval.go +++ b/approval.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "os" "regexp" "strings" @@ -97,6 +98,21 @@ Respond %s to continue workflow or %s to cancel.`, return nil } +func (a *approvalEnvironment) saveOutput(ctx context.Context) (bool, error) { + outputFile := os.Getenv("GITHUB_OUTPUT") + if outputFile == "" { + return false, nil + } + + output := []byte(fmt.Sprintf("issue-number=%v\nissue-url=%v\n", a.approvalIssueNumber, a.approvalIssue.GetURL())) + err := os.WriteFile(outputFile, output, 0644) + if err != nil { + return false, err + } + + return true, nil +} + func approvalFromComments(comments []*github.IssueComment, approvers []string, minimumApprovals int) (approvalStatus, error) { remainingApprovers := make([]string, len(approvers)) copy(remainingApprovers, approvers) diff --git a/approval_test.go b/approval_test.go index afc215f..0410479 100644 --- a/approval_test.go +++ b/approval_test.go @@ -1,6 +1,8 @@ package main import ( + "errors" + "os" "testing" "github.com/google/go-github/v43/github" @@ -427,3 +429,60 @@ func TestDeniedCommentBody(t *testing.T) { }) } } + +func TestSaveOutput(t *testing.T) { + testCases := []struct { + name string + approvalIssueNumber int + env_github_output string + isSuccess bool + }{ + { + name: "save_output_with_env", + approvalIssueNumber: 123, + env_github_output: "./output.txt", + isSuccess: true, + }, + { + name: "fail_save_output_without_env", + approvalIssueNumber: 123, + env_github_output: "", + isSuccess: false, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + os.Setenv("GITHUB_OUTPUT", testCase.env_github_output) + a := approvalEnvironment{ + client: nil, + repoFullName: "", + repo: "", + repoOwner: "", + runID: -1, + approvalIssueNumber: testCase.approvalIssueNumber, + issueTitle: "", + issueBody: "", + issueApprovers: nil, + minimumApprovals: 0, + } + + os.Remove(testCase.env_github_output) + actual, err := a.saveOutput(nil) + + if err != nil { + t.Fatalf("error creating output file: %v: %v", testCase.env_github_output, err) + } + + if actual != testCase.isSuccess { + t.Fatalf("expected %v but got %v", testCase.isSuccess, actual) + } + + if actual == true { + if _, err := os.Stat(testCase.env_github_output); errors.Is(err, os.ErrNotExist) { + t.Fatalf("expected create output file %v but it was not", testCase.env_github_output) + } + } + }) + } +} diff --git a/main.go b/main.go index 5aad15e..af1a649 100644 --- a/main.go +++ b/main.go @@ -193,6 +193,12 @@ func main() { os.Exit(1) } + _, err = apprv.saveOutput(ctx) + if err != nil { + fmt.Printf("error saving output: %v", err) + os.Exit(1) + } + killSignalChannel := make(chan os.Signal, 1) signal.Notify(killSignalChannel, os.Interrupt) From 4c0cdf52aec3cca92e9102aed82accee9a978131 Mon Sep 17 00:00:00 2001 From: Mateus Caruccio Date: Fri, 9 Aug 2024 15:30:40 +0100 Subject: [PATCH 2/2] Append-only to output --- approval.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/approval.go b/approval.go index 6c1a8a2..9482702 100644 --- a/approval.go +++ b/approval.go @@ -104,11 +104,17 @@ func (a *approvalEnvironment) saveOutput(ctx context.Context) (bool, error) { return false, nil } - output := []byte(fmt.Sprintf("issue-number=%v\nissue-url=%v\n", a.approvalIssueNumber, a.approvalIssue.GetURL())) - err := os.WriteFile(outputFile, output, 0644) + output := fmt.Sprintf("issue-number=%v\nissue-url=%v\n", a.approvalIssueNumber, a.approvalIssue.GetURL()) + f, err := os.OpenFile(outputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { return false, err } + defer f.Close() + + if _, err := f.WriteString(output); err != nil { + return false, err + } return true, nil }