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

Fix eventual consistency issue with github_actions_environment_variable #2378

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions github/resource_github_actions_environment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import (
"log"
"net/http"
"net/url"
"strings"
"time"

"github.com/google/go-github/v63/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const (
retryDelay = 10 * time.Second
statusPending = "pending"
statusReady = "ready"
)

func resourceGithubActionsEnvironmentVariable() *schema.Resource {
return &schema.Resource{
Create: resourceGithubActionsEnvironmentVariableCreate,
Expand Down Expand Up @@ -79,6 +88,10 @@ func resourceGithubActionsEnvironmentVariableCreate(d *schema.ResourceData, meta
}

d.SetId(buildThreePartID(repoName, envName, name))

if _, err := waitEnvironmentVariableReady(ctx, client, owner, repoName, envName, name); err != nil {
return err
}
return resourceGithubActionsEnvironmentVariableRead(d, meta)
}

Expand Down Expand Up @@ -155,3 +168,36 @@ func resourceGithubActionsEnvironmentVariableDelete(d *schema.ResourceData, meta

return err
}

func waitEnvironmentVariableReady(ctx context.Context, client *github.Client, owner, repoName, envName, name string) (*github.ActionsVariable, error) {
const timeout = 5 * time.Minute
stateconf := &retry.StateChangeConf{
Delay: retryDelay,
Pending: []string{statusPending},
Refresh: statusEnvironmentVariable(ctx, client, owner, repoName, envName, name),
Target: []string{statusReady},
Timeout: timeout,
}

output, err := stateconf.WaitForStateContext(ctx)
if v, ok := output.(*github.ActionsVariable); ok {
return v, err
}
return nil, err
}

func statusEnvironmentVariable(ctx context.Context, client *github.Client, owner, repoName, envName, name string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
envVar, resp, err := client.Actions.GetEnvVariable(ctx, owner, repoName, envName, name)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
return nil, statusPending, nil
}
}
// GitHub API returns the environment variables in uppercase
if envVar.Name == strings.ToUpper(name) {
return envVar, statusReady, nil
}
return nil, statusPending, nil
}
}