Fix the way the environment variables are retrieved

This commit is contained in:
Julien Fastré 2024-03-08 12:13:40 +01:00
parent f802f2b6eb
commit deb1ba6843
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
1 changed files with 12 additions and 12 deletions

24
main.go
View File

@ -5,16 +5,13 @@ import (
"errors"
"fmt"
"github.com/sethvargo/go-githubactions"
"os"
"slices"
"strconv"
"strings"
)
func ParseActionConfig() (*CreatePrConfig, error) {
ctx, err := githubactions.Context()
if err != nil {
return nil, nil
}
func ParseActionConfig(ctx githubactions.GitHubContext) (*CreatePrConfig, error) {
base := githubactions.GetInput("base")
if base == "" {
return nil, errors.New("base branch name cannot be empty")
@ -47,10 +44,10 @@ func ParseActionConfig() (*CreatePrConfig, error) {
var head string
headRaw := githubactions.GetInput("head")
if headRaw == "" {
if githubactions.GetInput("GITHUB_REF_TYPE") != "branch" {
if os.Getenv("GITHUB_REF_TYPE") != "branch" {
return nil, fmt.Errorf("set the \"head\" parameter or work from a branch: only branch can create a pull request: %v given as parameter GITHUB_REF_TYPE", githubactions.GetInput("GITHUB_REF_TYPE"))
}
head = githubactions.GetInput("GITHUB_REF_NAME")
head = os.Getenv("GITHUB_REF_NAME")
} else {
head = headRaw
}
@ -69,17 +66,20 @@ func ParseActionConfig() (*CreatePrConfig, error) {
func main() {
fmt.Println("Starting action CreatePullRequest, main")
ctx, err := githubactions.Context()
if err != nil {
githubactions.Fatalf("could not get context: %v", err.Error())
}
token := githubactions.GetInput("GITHUB_TOKEN")
apiUrl := githubactions.GetInput("GITHUB_API_URL")
fmt.Printf("Api url is %v\n", apiUrl)
token := os.Getenv("GITHUB_TOKEN")
fmt.Printf("Api url is %v\n", ctx.ServerURL)
config, err := ParseActionConfig()
config, err := ParseActionConfig(*ctx)
if err != nil {
githubactions.Fatalf("%v", err.Error())
}
pr, err := createPullRequest(apiUrl, token, *config)
pr, err := createPullRequest(ctx.ServerURL, token, *config)
if err != nil {
githubactions.Fatalf("Error while creating pr: %v", err.Error())
}