From deb1ba6843a793e8639ad083381801771d047621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 8 Mar 2024 12:13:40 +0100 Subject: [PATCH] Fix the way the environment variables are retrieved --- main.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index aac8261..3cce7f0 100644 --- a/main.go +++ b/main.go @@ -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()) }