Skip to content

Commit

Permalink
Feature: Save output
Browse files Browse the repository at this point in the history
  • Loading branch information
caruccio committed Aug 8, 2024
1 parent d2e412d commit d16e417
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
5 changes: 5 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"os"
"regexp"
"strings"

Expand Down Expand Up @@ -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)
Expand Down
59 changes: 59 additions & 0 deletions approval_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"errors"
"os"
"testing"

"github.com/google/go-github/v43/github"
Expand Down Expand Up @@ -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)
}
}
})
}
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit d16e417

Please sign in to comment.