@@ -27,7 +27,7 @@ type CreateEnvironmentVariableRequest struct {
27
27
28
28
// CreateEnvironmentVariable will create a brand new environment variable if one does not exist.
29
29
func (c * Client ) CreateEnvironmentVariable (ctx context.Context , request CreateEnvironmentVariableRequest ) (e EnvironmentVariable , err error ) {
30
- url := fmt .Sprintf ("%s/v9 /projects/%s/env" , c .baseURL , request .ProjectID )
30
+ url := fmt .Sprintf ("%s/v10 /projects/%s/env" , c .baseURL , request .ProjectID )
31
31
if c .teamID (request .TeamID ) != "" {
32
32
url = fmt .Sprintf ("%s?teamId=%s" , url , c .teamID (request .TeamID ))
33
33
}
@@ -37,38 +37,38 @@ func (c *Client) CreateEnvironmentVariable(ctx context.Context, request CreateEn
37
37
"url" : url ,
38
38
"payload" : payload ,
39
39
})
40
+ var response CreateEnvironmentVariableResponse
40
41
err = c .doRequest (clientRequest {
41
42
ctx : ctx ,
42
43
method : "POST" ,
43
44
url : url ,
44
45
body : payload ,
45
- }, & e )
46
+ }, & response )
47
+
46
48
if conflictingEnv , isConflicting , err2 := conflictingEnvVar (err ); isConflicting {
47
49
if err2 != nil {
48
50
return e , err2
49
51
}
50
-
51
52
envs , err3 := c .ListEnvironmentVariables (ctx , request .TeamID , request .ProjectID )
52
- if err != nil {
53
+ if err3 != nil {
53
54
return e , fmt .Errorf ("%s: unable to list environment variables to detect conflict: %s" , err , err3 )
54
55
}
55
-
56
56
id , found := findConflictingEnvID (request .TeamID , request .ProjectID , conflictingEnv , envs )
57
57
if found {
58
58
return e , fmt .Errorf ("%w the conflicting environment variable ID is %s" , err , id )
59
59
}
60
60
}
61
+
61
62
if err != nil {
62
63
return e , fmt .Errorf ("%w - %s" , err , payload )
63
64
}
64
- // The API response returns an encrypted environment variable, but we want to return the decrypted version.
65
- e .Value = request .EnvironmentVariable .Value
66
- e .TeamID = c .teamID (request .TeamID )
67
- return e , err
65
+ response .Created .Value = request .EnvironmentVariable .Value
66
+ response .Created .TeamID = c .teamID (request .TeamID )
67
+ return response .Created , err
68
68
}
69
69
70
70
func (c * Client ) ListEnvironmentVariables (ctx context.Context , teamID , projectID string ) (envs []EnvironmentVariable , err error ) {
71
- url := fmt .Sprintf ("%s/v9 /projects/%s/env" , c .baseURL , projectID )
71
+ url := fmt .Sprintf ("%s/v10 /projects/%s/env" , c .baseURL , projectID )
72
72
if c .teamID (teamID ) != "" {
73
73
url = fmt .Sprintf ("%s?teamId=%s" , url , c .teamID (teamID ))
74
74
}
@@ -85,26 +85,39 @@ func (c *Client) ListEnvironmentVariables(ctx context.Context, teamID, projectID
85
85
}
86
86
87
87
func overlaps (s []string , e []string ) bool {
88
+ set := make (map [string ]struct {}, len (s ))
88
89
for _ , a := range s {
89
- for _ , b := range e {
90
- if a == b {
91
- return true
92
- }
90
+ set [a ] = struct {}{}
91
+ }
92
+
93
+ for _ , b := range e {
94
+ if _ , exists := set [b ]; exists {
95
+ return true
93
96
}
94
97
}
98
+
95
99
return false
96
100
}
97
101
98
102
func findConflictingEnvID (teamID , projectID string , envConflict EnvConflictError , envs []EnvironmentVariable ) (string , bool ) {
103
+ checkTargetOverlap := len (envConflict .Target ) != 0
104
+
99
105
for _ , env := range envs {
100
- if env .Key == envConflict .Key && overlaps ( env .Target , envConflict . Target ) && env . GitBranch = = envConflict .GitBranch {
101
- id := fmt . Sprintf ( "%s/%s" , projectID , env . ID )
102
- if teamID != "" {
103
- id = fmt . Sprintf ( "%s/%s" , teamID , id )
104
- }
105
- return id , true
106
+ if env .Key != envConflict .EnvVarKey || env .GitBranch ! = envConflict .GitBranch {
107
+ continue
108
+ }
109
+
110
+ if checkTargetOverlap && ! overlaps ( env . Target , envConflict . Target ) {
111
+ continue
106
112
}
113
+
114
+ id := fmt .Sprintf ("%s/%s" , projectID , env .ID )
115
+ if teamID != "" {
116
+ id = fmt .Sprintf ("%s/%s" , teamID , id )
117
+ }
118
+ return id , true
107
119
}
120
+
108
121
return "" , false
109
122
}
110
123
@@ -116,15 +129,28 @@ type CreateEnvironmentVariablesRequest struct {
116
129
117
130
type CreateEnvironmentVariablesResponse struct {
118
131
Created []EnvironmentVariable `json:"created"`
119
- Failed []struct {
120
- Error struct {
121
- Code string `json:"code"`
122
- Message string `json:"message"`
123
- Key string `json:"envVarKey"`
124
- GitBranch * string `json:"gitBranch"`
125
- Target []string `json:"target"`
126
- } `json:"error"`
127
- } `json:"failed"`
132
+ Failed []FailedItem `json:"failed"`
133
+ }
134
+
135
+ type FailedItem struct {
136
+ Error struct {
137
+ Action * string `json:"action,omitempty"`
138
+ Code string `json:"code"`
139
+ EnvVarID * string `json:"envVarId,omitempty"`
140
+ EnvVarKey * string `json:"envVarKey,omitempty"`
141
+ GitBranch * string `json:"gitBranch,omitempty"`
142
+ Key * string `json:"key,omitempty"`
143
+ Link * string `json:"link,omitempty"`
144
+ Message string `json:"message"`
145
+ Project * string `json:"project,omitempty"`
146
+ Target []string `json:"target,omitempty"`
147
+ Value * string `json:"value,omitempty"`
148
+ } `json:"error"`
149
+ }
150
+
151
+ type CreateEnvironmentVariableResponse struct {
152
+ Created EnvironmentVariable `json:"created"`
153
+ Failed []FailedItem `json:"failed"`
128
154
}
129
155
130
156
func (c * Client ) CreateEnvironmentVariables (ctx context.Context , request CreateEnvironmentVariablesRequest ) ([]EnvironmentVariable , error ) {
@@ -141,10 +167,6 @@ func (c *Client) CreateEnvironmentVariables(ctx context.Context, request CreateE
141
167
url = fmt .Sprintf ("%s?teamId=%s" , url , c .teamID (request .TeamID ))
142
168
}
143
169
payload := string (mustMarshal (request .EnvironmentVariables ))
144
- tflog .Info (ctx , "creating environment variables" , map [string ]interface {}{
145
- "url" : url ,
146
- "payload" : payload ,
147
- })
148
170
149
171
var response CreateEnvironmentVariablesResponse
150
172
err := c .doRequest (clientRequest {
@@ -165,15 +187,19 @@ func (c *Client) CreateEnvironmentVariables(ctx context.Context, request CreateE
165
187
for _ , failed := range response .Failed {
166
188
if failed .Error .Code == "ENV_CONFLICT" {
167
189
id , found := findConflictingEnvID (request .TeamID , request .ProjectID , EnvConflictError {
168
- Key : failed .Error .Key ,
190
+ Key : * failed .Error .EnvVarKey ,
169
191
Target : failed .Error .Target ,
170
192
GitBranch : failed .Error .GitBranch ,
171
193
}, envs )
172
194
if found {
173
195
err = fmt .Errorf ("%w, conflicting environment variable ID is %s" , err , id )
174
196
}
175
197
} else {
176
- err = fmt .Errorf ("failed to create environment variables, %s %s %s" , failed .Error .Message , failed .Error .Key , failed .Error .Target )
198
+ key := ""
199
+ if failed .Error .Key != nil {
200
+ key = * failed .Error .Key
201
+ }
202
+ err = fmt .Errorf ("failed to create environment variables, %s %s %s" , failed .Error .Message , key , failed .Error .Target )
177
203
}
178
204
}
179
205
return response .Created , err
@@ -198,7 +224,7 @@ type UpdateEnvironmentVariableRequest struct {
198
224
199
225
// UpdateEnvironmentVariable will update an existing environment variable to the latest information.
200
226
func (c * Client ) UpdateEnvironmentVariable (ctx context.Context , request UpdateEnvironmentVariableRequest ) (e EnvironmentVariable , err error ) {
201
- url := fmt .Sprintf ("%s/v9 /projects/%s/env/%s" , c .baseURL , request .ProjectID , request .EnvID )
227
+ url := fmt .Sprintf ("%s/v10 /projects/%s/env/%s" , c .baseURL , request .ProjectID , request .EnvID )
202
228
if c .teamID (request .TeamID ) != "" {
203
229
url = fmt .Sprintf ("%s?teamId=%s" , url , c .teamID (request .TeamID ))
204
230
}
@@ -262,7 +288,7 @@ func (c *Client) GetEnvironmentVariables(ctx context.Context, projectID, teamID
262
288
263
289
// GetEnvironmentVariable gets a singluar environment variable from Vercel based on its ID.
264
290
func (c * Client ) GetEnvironmentVariable (ctx context.Context , projectID , teamID , envID string ) (e EnvironmentVariable , err error ) {
265
- url := fmt .Sprintf ("%s/v1 /projects/%s/env/%s" , c .baseURL , projectID , envID )
291
+ url := fmt .Sprintf ("%s/v10 /projects/%s/env/%s" , c .baseURL , projectID , envID )
266
292
if c .teamID (teamID ) != "" {
267
293
url = fmt .Sprintf ("%s?teamId=%s" , url , c .teamID (teamID ))
268
294
}
0 commit comments