Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Save output #132

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 22 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,27 @@ 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 := 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
}

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