这是indexloc提供的服务,不要输入任何密码
Skip to content

Support per-branch environment variables #20

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

Merged
merged 2 commits into from
Apr 22, 2022
Merged
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
11 changes: 6 additions & 5 deletions client/project_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ type GitRepository struct {
// EnvironmentVariable defines the information Vercel requires and surfaces about an environment variable
// that is associated with a project.
type EnvironmentVariable struct {
Key string `json:"key"`
Value string `json:"value"`
Target []string `json:"target"`
Type string `json:"type"`
ID string `json:"id,omitempty"`
Key string `json:"key"`
Value string `json:"value"`
Target []string `json:"target"`
GitBranch *string `json:"gitBranch,omitempty"`
Type string `json:"type"`
ID string `json:"id,omitempty"`
}

// CreateProjectRequest defines the information necessary to create a project.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ resource "vercel_project" "example" {

Optional:

- **git_branch** (String) The git branch of the environment variable.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% familiar with this API, but can you only specify git_branch for preview targets, or does it work across all different target possibilities?
e.g. what happens if I specified

target     = ["production", "preview"]
git_branch = "staging" 
key        = "foo"
value      = "bar" 

I'm wondering if it's worth adding some additional validation around that.
Granted the provider is currently pretty light on up-front validation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API is very strict; the branch has to actually exist, can't be your production branch etc. Lots of things we cannot validate here. You get good error messages if you try to do something impossible.

- **id** (String) The ID of the environment variable
- **key** (String) The name of the environment variable.
- **target** (Set of String) The environments that the environment variable should be present on. Valid targets are either `production`, `preview`, or `development`.
Expand Down
6 changes: 6 additions & 0 deletions vercel/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ terraform to your Deployment.
},
Required: true,
},
"git_branch": {
Description: "The git branch of the environment variable.",
Type: types.StringType,
Optional: true,
},
Comment on lines +76 to +80
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're also missing a test that includes the git_branch being used.

Copy link
Contributor Author

@BarnabyShearer BarnabyShearer Apr 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at the moment the git tests only actually work for you. I get

Could not create project, unexpected error: bad_request - To link a GitHub
        repository, you need to install the GitHub integration first.

Which I think is because I can only use my own repositories. However I have added some reasonable tests that will hopefully work. (You will need to crate a staging branch in your test repository).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yeah, makes sense. We should probably read an environment variable for the non-team git repository or something. I don't think we can keep it pointing at my git repos forever 🤣. One for later though!

"key": {
Description: "The name of the environment variable.",
Type: types.StringType,
Expand Down Expand Up @@ -247,6 +252,7 @@ func containsEnvVar(env []EnvironmentItem, v EnvironmentItem) bool {
for _, e := range env {
if e.Key == v.Key &&
e.Value == v.Value &&
e.GitBranch == v.GitBranch &&
len(e.Target) == len(v.Target) {
for i, t := range e.Target {
if t != v.Target[i] {
Expand Down
40 changes: 22 additions & 18 deletions vercel/resource_project_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ func parseEnvironment(vars []EnvironmentItem) []client.EnvironmentVariable {
}

out = append(out, client.EnvironmentVariable{
Key: e.Key.Value,
Value: e.Value.Value,
Target: target,
Type: "encrypted",
ID: e.ID.Value,
Key: e.Key.Value,
Value: e.Value.Value,
Target: target,
GitBranch: toStrPointer(e.GitBranch),
Type: "encrypted",
ID: e.ID.Value,
})
}
return out
Expand Down Expand Up @@ -74,10 +75,11 @@ func (p *Project) toUpdateProjectRequest(oldName string) client.UpdateProjectReq

// EnvironmentItem reflects the state terraform stores internally for a project's environment variable.
type EnvironmentItem struct {
Target []types.String `tfsdk:"target"`
Key types.String `tfsdk:"key"`
Value types.String `tfsdk:"value"`
ID types.String `tfsdk:"id"`
Target []types.String `tfsdk:"target"`
GitBranch types.String `tfsdk:"git_branch"`
Key types.String `tfsdk:"key"`
Value types.String `tfsdk:"value"`
ID types.String `tfsdk:"id"`
}

func (e *EnvironmentItem) toUpsertEnvironmentVariableRequest() client.UpsertEnvironmentVariableRequest {
Expand All @@ -86,11 +88,12 @@ func (e *EnvironmentItem) toUpsertEnvironmentVariableRequest() client.UpsertEnvi
target = append(target, t.Value)
}
return client.UpsertEnvironmentVariableRequest{
Key: e.Key.Value,
Value: e.Value.Value,
Target: target,
Type: "encrypted",
ID: e.ID.Value,
Key: e.Key.Value,
Value: e.Value.Value,
Target: target,
GitBranch: toStrPointer(e.GitBranch),
Type: "encrypted",
ID: e.ID.Value,
}
}

Expand Down Expand Up @@ -125,10 +128,11 @@ func convertResponseToProject(response client.ProjectResponse, tid types.String)
target = append(target, types.String{Value: t})
}
env = append(env, EnvironmentItem{
Key: types.String{Value: e.Key},
Value: types.String{Value: e.Value},
Target: target,
ID: types.String{Value: e.ID},
Key: types.String{Value: e.Key},
Value: types.String{Value: e.Value},
Target: target,
GitBranch: fromStringPointer(e.GitBranch),
ID: types.String{Value: e.ID},
})
}
teamID := types.String{Value: tid.Value}
Expand Down
44 changes: 44 additions & 0 deletions vercel/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ func TestAcc_ProjectWithGitRepository(t *testing.T) {
testAccProjectExists("vercel_project.test_git", ""),
resource.TestCheckResourceAttr("vercel_project.test_git", "git_repository.type", "github"),
resource.TestCheckResourceAttr("vercel_project.test_git", "git_repository.repo", "dglsparsons/test"),
resource.TestCheckTypeSetElemNestedAttrs("vercel_project.test_git", "environment.*", map[string]string{
"key": "foo",
"value": "bar",
"git_branch": "staging",
}),
),
},
{
Config: testAccProjectConfigWithGitRepoUpdated(projectSuffix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccProjectExists("vercel_project.test_git", ""),
resource.TestCheckTypeSetElemNestedAttrs("vercel_project.test_git", "environment.*", map[string]string{
"key": "foo",
"value": "bar2",
"git_branch": "staging",
}),
),
},
},
Expand Down Expand Up @@ -245,6 +261,34 @@ resource "vercel_project" "test_git" {
type = "github"
repo = "dglsparsons/test"
}
environment = [
{
key = "foo"
value = "bar"
target = ["preview"]
git_branch = "staging"
}
]
}
`, projectSuffix)
}

func testAccProjectConfigWithGitRepoUpdated(projectSuffix string) string {
return fmt.Sprintf(`
resource "vercel_project" "test_git" {
name = "test-acc-two-%s"
git_repository = {
type = "github"
repo = "dglsparsons/test"
}
environment = [
{
key = "foo"
value = "bar2"
target = ["preview"]
git_branch = "staging"
}
]
}
`, projectSuffix)
}
Expand Down