Skip to content

Commit 6537a52

Browse files
committed
Fixes merge drift issues with both org secrets and actions secrets
1 parent fe3ced8 commit 6537a52

File tree

3 files changed

+96
-13
lines changed

3 files changed

+96
-13
lines changed

github/resource_github_actions_organization_secret.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,11 @@ func resourceGithubActionsOrganizationSecretRead(d *schema.ResourceData, meta in
228228
if updatedAt, ok := d.GetOk("updated_at"); ok && destroyOnDrift && updatedAt != secret.UpdatedAt.String() {
229229
log.Printf("[INFO] The secret %s has been externally updated in GitHub", d.Id())
230230
d.SetId("")
231-
} else if !ok {
232-
if err = d.Set("updated_at", secret.UpdatedAt.String()); err != nil {
233-
return err
234-
}
231+
}
232+
233+
// Always update the timestamp to prevent repeated drift detection
234+
if err = d.Set("updated_at", secret.UpdatedAt.String()); err != nil {
235+
return err
235236
}
236237

237238
return nil
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package github
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
// Test for the organization secret drift detection fix
10+
func TestGithubActionsOrganizationSecretDriftDetectionFix(t *testing.T) {
11+
t.Run("always updates timestamp regardless of drift detection", func(t *testing.T) {
12+
// This test verifies the fix for the issue where updated_at was not
13+
// being set when drift was detected, causing repeated drift detection
14+
15+
d := schema.TestResourceDataRaw(t, resourceGithubActionsOrganizationSecret().Schema, map[string]interface{}{
16+
"secret_name": "test-secret",
17+
"plaintext_value": "test-value",
18+
"visibility": "private",
19+
"destroy_on_drift": true,
20+
"updated_at": "2023-01-01T00:00:00Z", // Old timestamp
21+
})
22+
d.SetId("test-secret")
23+
24+
// Simulate the updated_at logic from the read function
25+
// This is what the actual GitHub API would return (newer timestamp)
26+
newTimestamp := "2023-01-01T12:00:00Z"
27+
28+
// Simulate the drift detection logic from resourceGithubActionsOrganizationSecretRead
29+
destroyOnDrift := d.Get("destroy_on_drift").(bool)
30+
if updatedAt, ok := d.GetOk("updated_at"); ok && destroyOnDrift && updatedAt != newTimestamp {
31+
// This would log the drift and clear the ID
32+
d.SetId("")
33+
}
34+
35+
// This is the key fix - always update the timestamp
36+
err := d.Set("updated_at", newTimestamp)
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
41+
// Verify that the timestamp was updated even though drift was detected
42+
if d.Get("updated_at").(string) != newTimestamp {
43+
t.Error("Expected updated_at to be set to new timestamp after drift detection")
44+
}
45+
46+
// Verify that the ID was cleared due to drift detection
47+
if d.Id() != "" {
48+
t.Error("Expected ID to be cleared due to drift detection with destroy_on_drift=true")
49+
}
50+
})
51+
52+
t.Run("does not clear ID when destroy_on_drift is false", func(t *testing.T) {
53+
d := schema.TestResourceDataRaw(t, resourceGithubActionsOrganizationSecret().Schema, map[string]interface{}{
54+
"secret_name": "test-secret",
55+
"plaintext_value": "test-value",
56+
"visibility": "private",
57+
"destroy_on_drift": false,
58+
"updated_at": "2023-01-01T00:00:00Z", // Old timestamp
59+
})
60+
d.SetId("test-secret")
61+
62+
newTimestamp := "2023-01-01T12:00:00Z"
63+
64+
// Simulate the drift detection logic
65+
destroyOnDrift := d.Get("destroy_on_drift").(bool)
66+
if updatedAt, ok := d.GetOk("updated_at"); ok && destroyOnDrift && updatedAt != newTimestamp {
67+
d.SetId("")
68+
}
69+
70+
// Always update the timestamp
71+
err := d.Set("updated_at", newTimestamp)
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
76+
// Verify that the ID was NOT cleared when destroy_on_drift=false
77+
if d.Id() != "test-secret" {
78+
t.Error("Expected ID to remain when destroy_on_drift=false")
79+
}
80+
81+
// Verify that the timestamp was still updated
82+
if d.Get("updated_at").(string) != newTimestamp {
83+
t.Error("Expected updated_at to be updated even when destroy_on_drift=false")
84+
}
85+
})
86+
}

github/resource_github_actions_secret.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,11 @@ func resourceGithubActionsSecretRead(d *schema.ResourceData, meta interface{}) e
170170
if updatedAt, ok := d.GetOk("updated_at"); ok && destroyOnDrift && updatedAt != secret.UpdatedAt.String() {
171171
log.Printf("[INFO] The secret %s has been externally updated in GitHub", d.Id())
172172
d.SetId("")
173-
} else if updatedAt, ok := d.GetOk("updated_at"); ok && !destroyOnDrift && updatedAt != secret.UpdatedAt.String() {
174-
log.Printf("[INFO] The secret %s has been externally updated in GitHub, updating timestamp without recreating", d.Id())
175-
if err = d.Set("updated_at", secret.UpdatedAt.String()); err != nil {
176-
return err
177-
}
178-
} else if !ok {
179-
if err = d.Set("updated_at", secret.UpdatedAt.String()); err != nil {
180-
return err
181-
}
173+
}
174+
175+
// Always update the timestamp to prevent repeated drift detection
176+
if err = d.Set("updated_at", secret.UpdatedAt.String()); err != nil {
177+
return err
182178
}
183179

184180
return nil

0 commit comments

Comments
 (0)