Skip to content

fix: add plan-time validation to key_prefix in autolink reference resource (#3176) - #3277

Merged
stevehipwell merged 2 commits into
integrations:mainfrom
LeC-D:fix/autolink-key-prefix-validation
Jun 30, 2026
Merged

stevehipwell merged 2 commits into
integrations:mainfrom
LeC-D:fix/autolink-key-prefix-validation

Conversation

@LeC-D

@LeC-D LeC-D commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #3176

Problem

terraform plan accepts any value for key_prefix in github_repository_autolink_reference, but terraform apply can fail with a 422 from the GitHub API if the value is invalid:

key_prefix must not end with a number
key_prefix must only contain letters, numbers, or .-_+=:/#

This creates a confusing plan/apply inconsistency where the plan shows a clean diff but apply fails.

Fix

Added ValidateDiagFunc to the key_prefix schema field using a regexp that mirrors GitHub's API constraints:

  • Allowed characters: letters, digits, and .-_+=:/#
  • Must not end with a digit
ValidateDiagFunc: validation.ToDiagFunc(validation.StringMatch(
    regexp.MustCompile(`^[a-zA-Z0-9.=+:/#_-]*[a-zA-Z.=+:/#_-]$`),
    "must only contain letters, numbers, or .-_+=:/# and must not end with a number",
)),

Both regexp and validation were already imported in this file (used by target_url_template). No new dependencies needed.

Testing

Existing acceptance tests use key_prefix values like TEST1-, TEST2-, etc. which all pass the new validation. Invalid values like PTFY25 (ends with digit) will now be caught at plan time.

@github-actions

Copy link
Copy Markdown

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

@github-actions github-actions Bot added the Type: Bug Something isn't working as documented label Mar 15, 2026
austenstone

This comment was marked as duplicate.

@austenstone austenstone left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Left a concrete regression-test suggestion so the fix is covered at plan time as well as in the schema.

@LeC-D

LeC-D commented Mar 16, 2026

Copy link
Copy Markdown
Contributor Author

Good call! Added a resource.UnitTest sub-test in resource_github_repository_autolink_reference_test.go that passes key_prefix = "PTFY25" (ends with a digit) and asserts a plan-time error matching must not end with a number. This locks in the validation behavior without requiring a real GitHub account.

@LeC-D
LeC-D force-pushed the fix/autolink-key-prefix-validation branch from d721ddc to 49a967c Compare March 18, 2026 22:02
@LeC-D

LeC-D commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on upstream main to bring the branch up to date.

@LeC-D

LeC-D commented Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

Applied @deiga's review feedback: split the key_prefix validation into two separate StringMatch validators inside validation.All() — one for allowed characters (^[a-zA-Z0-9.=+:/#_-]+$) and one for the digit-suffix check (\D$). Error messages are now more targeted. Thanks for the suggestion!

@deiga deiga added this to the v6.12.0 Release milestone Mar 20, 2026
@LeC-D
LeC-D force-pushed the fix/autolink-key-prefix-validation branch from 3e5df52 to b1015f6 Compare March 24, 2026 14:06
@LeC-D

LeC-D commented Mar 24, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on upstream main (2026-03-24) to bring the branch back up to date.

@LeC-D

LeC-D commented Mar 26, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on upstream main (2026-03-26) to bring the branch back up to date.

@LeC-D

LeC-D commented Mar 28, 2026

Copy link
Copy Markdown
Contributor Author

@deiga The CHANGES_REQUESTED feedback has been addressed in the latest commit (b1015f6) — the key_prefix validation is now split into two separate StringMatch validators inside validation.All() as suggested. Would appreciate a re-review when you get a chance!

@LeC-D

LeC-D commented Mar 28, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on upstream main (2026-03-28) to bring the branch back up to date.

deiga
deiga previously approved these changes Mar 28, 2026
@LeC-D

LeC-D commented Mar 28, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on upstream main (2026-03-28) to bring the branch back up to date.

@deiga
deiga removed the request for review from robert-crandall May 6, 2026 12:36
@deiga
deiga removed the request for review from stevehipwell May 17, 2026 13:24
@deiga

deiga commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

@LeC-D Sorry that this has taken long to get to. Would you be available to rebase this (possibly multiple times) during the next week? We're trying to get 6.13.0 out and will try to merge as many ~ready PRs as is reasonable

@stevehipwell stevehipwell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@LeC-D please could you rebase this PR?

@stevehipwell

Copy link
Copy Markdown
Collaborator

@LeC-D are you in a position to continue working on this?

@LeC-D
LeC-D force-pushed the fix/autolink-key-prefix-validation branch from b1015f6 to 83fc917 Compare June 27, 2026 02:03
@LeC-D

LeC-D commented Jun 27, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on upstream main (2026-06-26) to bring the branch back up to date. Ready when you are!

…egrations#3176)

GitHub's API enforces that key_prefix:
- must only contain letters, numbers, or .-_+=:/#
- must not end with a number

Without client-side validation, terraform plan succeeds with an invalid
key_prefix (e.g. 'PTFY25') but terraform apply fails with a 422 from
the GitHub API, giving a confusing plan/apply inconsistency.

Added ValidateDiagFunc using a regexp that mirrors the API constraints,
so invalid key_prefix values are caught at plan time.
Adds a plan-time validation test asserting that a key_prefix ending
in a digit (e.g. PTFY25) is rejected before any API call is made.
Addresses review comment from @austenstone.
@LeC-D
LeC-D force-pushed the fix/autolink-key-prefix-validation branch from 83fc917 to 7ba7d52 Compare June 30, 2026 01:21
@deiga
deiga requested a review from Copilot June 30, 2026 03:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

These provider review instructions are being used.

This PR fixes #3176 by adding plan-time validation to the key_prefix attribute of the github_repository_autolink_reference resource. Previously, terraform plan accepted any value while terraform apply could fail with a 422 from the GitHub API for invalid prefixes (e.g. ending in a digit or using disallowed characters), creating a confusing plan/apply inconsistency. The change surfaces the error at plan time instead.

Changes:

  • Added a ValidateDiagFunc using validation.StringMatch with a regexp mirroring GitHub's key_prefix constraints (allowed characters and "must not end with a number").
  • Added a unit test asserting a key_prefix ending in a digit (PTFY25) is rejected at plan time.

I verified the regexp's allowed character set (. = + : / # _ -) matches GitHub's documented set (.-_+=:/#) exactly and that the trailing character class excludes digits, so it neither over- nor under-restricts relative to the API's stated rules. Because GitHub already enforces these constraints at apply, existing valid resources cannot be newly rejected. The regexp and validation imports are already present, the ValidateDiagFunc/StringMatch usage is consistent with existing schema fields (e.g. resource_github_repository.go:42), and the test's ExpectError substring matches the StringMatch error message. The schema Description is unchanged, so no docs regeneration is required.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
github/resource_github_repository_autolink_reference.go Adds ValidateDiagFunc regexp validation to the key_prefix schema attribute to catch invalid values at plan time.
github/resource_github_repository_autolink_reference_test.go Adds a plan-time validation unit test rejecting a key_prefix that ends with a digit.

@stevehipwell stevehipwell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@robert-crandall robert-crandall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for tackling this - moving the failure from apply-time 422 to plan-time is a really nice UX win. The regex matches GitHub's documented constraints exactly, and I like that you covered it with a fast unit test using ExpectError. Looks good to me!

@stevehipwell
stevehipwell merged commit 3d13ee2 into integrations:main Jun 30, 2026
10 checks passed
@deiga deiga mentioned this pull request Aug 9, 2026
2 of 5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Bug Something isn't working as documented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: autolink allows numbers during plan, but fails during apply

6 participants