Skip to content

Commit ba2ec73

Browse files
fix: repository credential fields should be computed (#774)
Signed-off-by: Blake Pettersson <[email protected]>
1 parent 988d1a7 commit ba2ec73

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

internal/provider/model_repository.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,17 @@ func repositorySchemaAttributes() map[string]schema.Attribute {
124124
"githubapp_id": schema.StringAttribute{
125125
MarkdownDescription: "ID of the GitHub app used to access the repo.",
126126
Optional: true,
127+
Computed: true,
127128
},
128129
"githubapp_installation_id": schema.StringAttribute{
129130
MarkdownDescription: "The installation ID of the GitHub App used to access the repo.",
130131
Optional: true,
132+
Computed: true,
131133
},
132134
"githubapp_enterprise_base_url": schema.StringAttribute{
133135
MarkdownDescription: "GitHub API URL for GitHub app authentication.",
134136
Optional: true,
137+
Computed: true,
135138
},
136139
"githubapp_private_key": schema.StringAttribute{
137140
MarkdownDescription: "Private key data (PEM) for authentication via GitHub app.",
@@ -215,16 +218,24 @@ func (m *repositoryModel) updateFromAPI(repo *v1alpha1.Repository) *repositoryMo
215218

216219
if repo.GitHubAppEnterpriseBaseURL != "" {
217220
m.GitHubAppEnterpriseBaseURL = types.StringValue(repo.GitHubAppEnterpriseBaseURL)
221+
} else if m.GitHubAppEnterpriseBaseURL.IsUnknown() {
222+
// If unknown and API didn't return a value, set to null
223+
m.GitHubAppEnterpriseBaseURL = types.StringNull()
218224
}
219225

220-
// Handle GitHub App ID conversion
221226
if repo.GithubAppId > 0 {
222227
m.GitHubAppID = types.StringValue(strconv.FormatInt(repo.GithubAppId, 10))
228+
} else if m.GitHubAppID.IsUnknown() {
229+
// If unknown and API didn't return a value, set to null
230+
m.GitHubAppID = types.StringNull()
223231
}
224232

225233
// Handle GitHub App Installation ID conversion
226234
if repo.GithubAppInstallationId > 0 {
227235
m.GitHubAppInstallationID = types.StringValue(strconv.FormatInt(repo.GithubAppInstallationId, 10))
236+
} else if m.GitHubAppInstallationID.IsUnknown() {
237+
// If unknown and API didn't return a value, set to null
238+
m.GitHubAppInstallationID = types.StringNull()
228239
}
229240

230241
return m

internal/provider/resource_repository_credentials_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,86 @@ func TestAccArgoCDRepositoryCredentials_GitHubAppConsistency(t *testing.T) {
275275
})
276276
}
277277

278+
func TestAccArgoCDRepositoryCredentials_GitHubAppWithRepositoryInheritance(t *testing.T) {
279+
sshPrivateKey, err := generateSSHPrivateKey()
280+
assert.NoError(t, err)
281+
282+
// Use the local test repository infrastructure to avoid external dependencies
283+
// while still testing GitHub App credential inheritance
284+
config := testAccArgoCDRepositoryCredentialsGitHubAppWithRepository(
285+
286+
"[email protected]:~/project-1.git",
287+
"123456",
288+
"987654321",
289+
"https://ghe.example.com/api/v3",
290+
sshPrivateKey,
291+
)
292+
293+
resource.Test(t, resource.TestCase{
294+
PreCheck: func() { testAccPreCheck(t) },
295+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
296+
Steps: []resource.TestStep{
297+
{
298+
Config: config,
299+
Check: resource.ComposeAggregateTestCheckFunc(
300+
// Check repository credentials - verify GitHub App fields are set
301+
resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "url", "[email protected]"),
302+
resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "githubapp_id", "123456"),
303+
resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "githubapp_installation_id", "987654321"),
304+
//resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "githubapp_enterprise_base_url", "https://ghe.example.com/api/v3"),
305+
// Check repository that inherits credentials
306+
resource.TestCheckResourceAttr("argocd_repository.repo", "repo", "[email protected]:~/project-1.git"),
307+
),
308+
},
309+
{
310+
// Apply the same configuration again to test for consistency (Issue #697)
311+
// This should NOT produce "inconsistent result after apply" errors
312+
// This is the key test - without the updateFromAPI fix, this step would fail
313+
Config: config,
314+
Check: resource.ComposeAggregateTestCheckFunc(
315+
// Verify repository credentials fields remain stable across applies
316+
resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "url", "[email protected]"),
317+
resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "githubapp_id", "123456"),
318+
resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "githubapp_installation_id", "987654321"),
319+
//resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "githubapp_enterprise_base_url", "https://ghe.example.com/api/v3"),
320+
// Verify repository remains stable
321+
resource.TestCheckResourceAttr("argocd_repository.repo", "repo", "[email protected]:~/project-1.git"),
322+
),
323+
},
324+
{
325+
// Apply a third time to ensure continued consistency
326+
Config: config,
327+
Check: resource.ComposeAggregateTestCheckFunc(
328+
resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "url", "[email protected]"),
329+
resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "githubapp_id", "123456"),
330+
resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "githubapp_installation_id", "987654321"),
331+
//resource.TestCheckResourceAttr("argocd_repository_credentials.githubapp", "githubapp_enterprise_base_url", "https://ghe.example.com/api/v3"),
332+
resource.TestCheckResourceAttr("argocd_repository.repo", "repo", "[email protected]:~/project-1.git"),
333+
),
334+
},
335+
},
336+
})
337+
}
338+
339+
func testAccArgoCDRepositoryCredentialsGitHubAppWithRepository(credsUrl, repoUrl, id, installID, enterpriseBaseURL, _ string) string {
340+
return fmt.Sprintf(`
341+
resource "argocd_repository_credentials" "githubapp" {
342+
url = "%s"
343+
githubapp_id = "%s"
344+
githubapp_installation_id = "%s"
345+
#githubapp_enterprise_base_url = "%s"
346+
ssh_private_key = "-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW\nQyNTUxOQAAACCGe6Vx0gbKqKCI0wIplfgK5JBjCDO3bhtU3sZfLoeUZgAAAJB9cNEifXDR\nIgAAAAtzc2gtZWQyNTUxOQAAACCGe6Vx0gbKqKCI0wIplfgK5JBjCDO3bhtU3sZfLoeUZg\nAAAEAJeUrObjoTbGO1Sq4TXHl/j4RJ5aKMC1OemWuHmLK7XYZ7pXHSBsqooIjTAimV+Ark\nkGMIM7duG1Texl8uh5RmAAAAC3Rlc3RAYXJnb2NkAQI=\n-----END OPENSSH PRIVATE KEY-----"
347+
}
348+
349+
resource "argocd_repository" "repo" {
350+
repo = "%s"
351+
type = "git"
352+
insecure = true
353+
depends_on = [argocd_repository_credentials.githubapp]
354+
}
355+
`, credsUrl, id, installID, enterpriseBaseURL, repoUrl)
356+
}
357+
278358
func testCheckMultipleResourceAttr(name, key, value string, count int) resource.TestCheckFunc {
279359
return func(s *terraform.State) error {
280360
for i := 0; i < count; i++ {

0 commit comments

Comments
 (0)