Skip to content

Add resource github_app_installation_repositories - #1376

Merged
kfcampbell merged 5 commits into
integrations:mainfrom
david-bain:add-app-repositories
Dec 5, 2022
Merged

kfcampbell merged 5 commits into
integrations:mainfrom
david-bain:add-app-repositories

Conversation

@david-bain

Copy link
Copy Markdown
Contributor

This resource allows multiple repositories to be passed in; which greatly improves the performance of the resource compared to the single repository version when needing to control state of multiple app installations with multiple repos, required in larger organisations.

The optimisation occurs as only a single call to get the list of repos is required per installation per read, regardless of the number of repositories being added.

  • Add resource_github_app_installation_repositories
  • Add tests
  • update docs

Resolves N/A


Behavior

Before the change?

  • resource_github_app_installation_repostiory was a 1:1 installation:repo; however in reality, many repositories can be added to an installations allow-list. This meant that an app installation allowlist with say 5 repositories required 5 resources to be created/managed. Inside that resource, there was some generic calls that related more to the installation so if an app installation had multiple repositories allowed, those exact same API calls are repeated multiple times, e.g. client.Apps.ListUserRepos().
  • This made managing app allow-lists this way VERY slow

After the change?

  • Creates a new resource that allows multiple repositories to be defined against an installation in a 1:Many fashion. Following from the above, this means that the installation API calls are only called once per resource and as there are much less resources now due to the grouping of repositories, the terraform runs MUCH faster.
  • In our organisation, it is 30x faster this way to plan

Other information

  • This resource has been running within our org on a fork for approximately 3 months and has been very successful.

Additional info

Pull request checklist

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Added the appropriate label for the given change

Does this introduce a breaking change?

Please see our docs on breaking changes to help!

  • Yes (Please add the Type: Breaking change label)
  • No

If Yes, what's the impact:

  • N/A

Pull request type

Please add the corresponding label for change this PR introduces:

  • Bugfix: Type: Bug
  • Feature/model/API additions: Type: Feature
  • Updates to docs or samples: Type: Documentation
  • Dependencies/code cleanup: Type: Maintenance

This resource allows multiple repositories to be passed in; which greatly improves the performance of the resource compared to the single repository version
when needing to control state of multiple app installations with multiple repos, required in larger organisations.

The optimisation occurs as only a single call to get the list of repos is required per installation per read, regardless of the number of respositories being added.

- Add resource_github_app_installation_repositories
- Add tests
@kfcampbell kfcampbell added the Type: Feature New feature or request label Nov 28, 2022

@kfcampbell kfcampbell 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.

Hi @david-bain, this is super cool and thanks for opening a PR to share it! I have a couple questions:

  • Do you mind adding a link to the new documentation in the github.erb file so the new doc is accessible?
  • What am I missing about running the integration tests? I've exported a valid APP_INSTALLATION_ID retrieved from my organization's app installation settings, but my tests fail with the following error:
    testing.go:705: Step 0 error: errors during apply:
        
        Error: GET https://api.github.com/user/installations/131977/repositories?per_page=100: 404 Not Found []

Do you think this could be permission-related?

  • Would you mind putting a note at the top of the docs for the new resource and resource_github_app_installation_repository that distinguishes between the purpose of each resource and describes when you might want to use one over the other?

Come to think of it, it may even be a good idea to deprecate resource_github_app_installation_repository soon in favor of the new resource.

* `installation_id` - (Required) The GitHub app installation id.
* `selected_repositories` - (Required) A list of repository names to install the app on.

~> **Note**: Due to GitHub API limitations, deleting this resource will leave one repository with the app installed. Manually uninstall the app after deleting the resource.

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.

How is the remaining repository determined? Is it deterministic?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See comment below, I'll update the docs to reflect

@david-bain david-bain Dec 1, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@kfcampbell
Re running the tests: I just pulled my branch and made sure to set GITHUB_TOKEN, GITHUB_ORGANISATION, and APP_INSTALLATION_ID and it ran fine.
I think I see your error. That ID in the path looks too small for an installation ID... I suspect you exported the App ID by accident. The installation ID is retrieved from the end of the web address when you navigate to a specific installation of an app.

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.

Oh my gosh you're totally right! Thanks for the help troubleshooting the testing.

@david-bain

Copy link
Copy Markdown
Contributor Author

Let me check into the above, I wrote this a while back on a fork so I need to refresh my memory regarding the remaining repo and tests.

Comment on lines +79 to +82
// Remove repositories that existed on GitHub but not selectedRepositories
// There is a github limitation that means we can't remove the last repository from an installation.
// Therefore, we skip the first and delete the rest. The app will then need to be uninstalled via the GUI
// as there is no current API endpoint for [un]installation. Ensure there is at least one repository remaining.

@david-bain david-bain Dec 1, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As per the comment here, I skip the first... which is based on the order of repositories that are returned by the the list accessible repos API call.
I guess I could sort it alphabetically so that it is always the first alphabetical by name that remains but not sure how useful that is.

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.

This seems like a reasonable call. It's definitely an instance where GitHub's behavior doesn't fit a tidy CRUD model.

@david-bain

Copy link
Copy Markdown
Contributor Author

Docs updated.
I believe I have addressed all your points. Please let me know if there is anything else.

@kfcampbell kfcampbell 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 making changes on this! I'll get this merged now and released in the near future.

@kfcampbell
kfcampbell merged commit 3db3e2f into integrations:main Dec 5, 2022
TheQueenIsDead pushed a commit to TheQueenIsDead/terraform-provider-github that referenced this pull request Dec 12, 2022
* Add resource github_app_installation_repositories

This resource allows multiple repositories to be passed in; which greatly improves the performance of the resource compared to the single repository version
when needing to control state of multiple app installations with multiple repos, required in larger organisations.

The optimisation occurs as only a single call to get the list of repos is required per installation per read, regardless of the number of respositories being added.

- Add resource_github_app_installation_repositories
- Add tests

* Update docs and link

Co-authored-by: Keegan Campbell <[email protected]>
kazaker pushed a commit to auto1-oss/terraform-provider-github that referenced this pull request Dec 28, 2022
* Add resource github_app_installation_repositories

This resource allows multiple repositories to be passed in; which greatly improves the performance of the resource compared to the single repository version
when needing to control state of multiple app installations with multiple repos, required in larger organisations.

The optimisation occurs as only a single call to get the list of repos is required per installation per read, regardless of the number of respositories being added.

- Add resource_github_app_installation_repositories
- Add tests

* Update docs and link

Co-authored-by: Keegan Campbell <[email protected]>
kfcampbell added a commit that referenced this pull request Jan 20, 2023
* feat: add new schema for check block resource under required_status_checks

* feat: iterate provided `check` blocks and build array of RequiredStatusCheck

* feat: set default app_id to -1

* feat: implement checks flattening for required status checks

* Add resource github_app_installation_repositories (#1376)

* Add resource github_app_installation_repositories

This resource allows multiple repositories to be passed in; which greatly improves the performance of the resource compared to the single repository version
when needing to control state of multiple app installations with multiple repos, required in larger organisations.

The optimisation occurs as only a single call to get the list of repos is required per installation per read, regardless of the number of respositories being added.

- Add resource_github_app_installation_repositories
- Add tests

* Update docs and link

Co-authored-by: Keegan Campbell <[email protected]>

* feat: adds new branch protection options for last reviewer and locking branch (#1407)

Co-authored-by: Keegan Campbell <[email protected]>

* feat(github_release): adding github_release resource and tests (#1122)

* feat(github_release): adding github_release resource and tests

* feat(docs) adding github_release page to website docs

* chore: update changelog with this pr's new resource

* fix: adding node_id and release_id to resource attributes

* Update CHANGELOG.md

* Fix broken merge/build

Co-authored-by: Keegan Campbell <[email protected]>

* 🚧 Workflows have changed

Workflow changes have been made in the Octokit org repo. This PR is propagating those changes.

* Issue template tweak (#1422)

* Don't link to a real PR

* Wording tweak

* feat: allow branch protection check app_id to be null

* chore: change branch protection flatten function to use GetAppID sdk method

* feat: change branch protection v3 utils to flatten and expand contexts into checks

* feat: change checks from it's own resource to a list of strings

* chore: resolve incorrect merge of main

* chore: update deprecation notice on contexts array

* chore(docs): Update branch_protection_v3 docs to mention the new `checks` functionality

* fix: Initialise literal empty slice of RequiredStatusCheck to mitigate errors when passing nil to the sdk

* chore(lint): resolve gosimple S1082 violation (errors.New => fmt.Errorf)

* chore: remove unused code comment

Co-authored-by: David Bain <[email protected]>
Co-authored-by: Keegan Campbell <[email protected]>
Co-authored-by: Sean Smith <[email protected]>
Co-authored-by: Trent Millar <[email protected]>
Co-authored-by: Nick Floyd <[email protected]>
avidspartan1 pushed a commit to avidspartan1/terraform-provider-github that referenced this pull request Feb 5, 2024
* Add resource github_app_installation_repositories

This resource allows multiple repositories to be passed in; which greatly improves the performance of the resource compared to the single repository version
when needing to control state of multiple app installations with multiple repos, required in larger organisations.

The optimisation occurs as only a single call to get the list of repos is required per installation per read, regardless of the number of respositories being added.

- Add resource_github_app_installation_repositories
- Add tests

* Update docs and link

Co-authored-by: Keegan Campbell <[email protected]>
avidspartan1 pushed a commit to avidspartan1/terraform-provider-github that referenced this pull request Feb 5, 2024
* feat: add new schema for check block resource under required_status_checks

* feat: iterate provided `check` blocks and build array of RequiredStatusCheck

* feat: set default app_id to -1

* feat: implement checks flattening for required status checks

* Add resource github_app_installation_repositories (integrations#1376)

* Add resource github_app_installation_repositories

This resource allows multiple repositories to be passed in; which greatly improves the performance of the resource compared to the single repository version
when needing to control state of multiple app installations with multiple repos, required in larger organisations.

The optimisation occurs as only a single call to get the list of repos is required per installation per read, regardless of the number of respositories being added.

- Add resource_github_app_installation_repositories
- Add tests

* Update docs and link

Co-authored-by: Keegan Campbell <[email protected]>

* feat: adds new branch protection options for last reviewer and locking branch (integrations#1407)

Co-authored-by: Keegan Campbell <[email protected]>

* feat(github_release): adding github_release resource and tests (integrations#1122)

* feat(github_release): adding github_release resource and tests

* feat(docs) adding github_release page to website docs

* chore: update changelog with this pr's new resource

* fix: adding node_id and release_id to resource attributes

* Update CHANGELOG.md

* Fix broken merge/build

Co-authored-by: Keegan Campbell <[email protected]>

* 🚧 Workflows have changed

Workflow changes have been made in the Octokit org repo. This PR is propagating those changes.

* Issue template tweak (integrations#1422)

* Don't link to a real PR

* Wording tweak

* feat: allow branch protection check app_id to be null

* chore: change branch protection flatten function to use GetAppID sdk method

* feat: change branch protection v3 utils to flatten and expand contexts into checks

* feat: change checks from it's own resource to a list of strings

* chore: resolve incorrect merge of main

* chore: update deprecation notice on contexts array

* chore(docs): Update branch_protection_v3 docs to mention the new `checks` functionality

* fix: Initialise literal empty slice of RequiredStatusCheck to mitigate errors when passing nil to the sdk

* chore(lint): resolve gosimple S1082 violation (errors.New => fmt.Errorf)

* chore: remove unused code comment

Co-authored-by: David Bain <[email protected]>
Co-authored-by: Keegan Campbell <[email protected]>
Co-authored-by: Sean Smith <[email protected]>
Co-authored-by: Trent Millar <[email protected]>
Co-authored-by: Nick Floyd <[email protected]>
@AlfredoBall

AlfredoBall commented Jul 9, 2026 •

Copy link
Copy Markdown
  • Update: I wrote this comment in ignorance of the true intention of the permissions model, Git Hub Apps are not designed to manage what they didn't create or what they are given by a user / PAT. I won't delete it though because I think it's still informative and even more so that I was wrong.

I had blinders on thinking about fixing what isn't broken.

I wasn't thinking as much about the permissions as I was the automation, which I was also wrong about.


Architectural Post-Mortem: Structural Limitations of github_app_installation_repositories

Status: Unresolvable (Upstream API Constraint)


Summary

Managing GitHub App repository assignments through the github_app_installation_repositories resource using a machine-to-machine authentication model (app_auth, GitHub App installation tokens, or OpenID Connect) is not currently possible.

This limitation is not a defect in Terraform or the GitHub provider. Instead, it stems from how the GitHub REST API exposes repository installation management. The API endpoints required to explicitly add or remove repositories from an installation are only available through user-scoped routes that require user-delegated credentials.

As a result, a fully automated workflow using GitHub App authentication alone cannot perform the complete CRUD lifecycle for this resource.


API Endpoint Constraints

The github_app_installation_repositories resource synchronizes repository assignments by calling GitHub REST API endpoints whose availability depends on the authentication context.

Repository Assignment Endpoints

Explicitly assigning or removing repositories requires the following endpoints:

PUT    /user/installations/{installation_id}/repositories/{repository_id}
DELETE /user/installations/{installation_id}/repositories/{repository_id}

These endpoints are intentionally exposed only under the /user/ namespace.

According to GitHub's REST API documentation, they do not accept:

  • GitHub App installation access tokens
  • GitHub App user access tokens
  • Fine-grained personal access tokens

Instead, they require a user-delegated authentication context (typically a classic Personal Access Token with the necessary repository permissions).

Consequently, requests made through app_auth fail with:

403 Resource not accessible by integration

Missing Organization-Scoped Mutation Endpoint

GitHub provides organization-scoped endpoints for retrieving installation information, but no equivalent endpoint exists for modifying repository assignments.

For example, the following route does not exist:

PUT /orgs/{org}/installations/{installation_id}/repositories/{repository_id}

Because no organization-scoped mutation API exists, all repository assignment operations are forced through the user-scoped /user/ endpoints, preventing automation using GitHub App installation credentials alone.


Why legacy_client = false Does Not Help

Beginning with version 6.13.0, the GitHub provider introduced the new client implementation via:

legacy_client = false

This change modernized the provider's transport layer, including fixes for several issues such as stale ETag caching (HTTP 304 behavior) that could previously lead to state inconsistencies.

However, this architectural improvement does not affect authentication or API routing.

Regardless of which client implementation is used, the provider must still call the same /user/installations/... endpoints, where GitHub performs server-side authorization checks. Those requests are rejected before any provider-side logic can complete.

In other words, the limitation exists entirely on the GitHub API side rather than within the provider implementation.


Relational Contradictions in Platform Scoping

The hard block on explicit repository mapping creates an operational contradiction where the GitHub App's app_auth can create or destroy repositories, where the repository is automatically assigned to a GitHub App's repository list upon creation with an GitHub App auth context, yet it cannot toggle repository visibility through the API. This creates a conflict where the token can delete assets, but not change its own scope without deleting the underlying resource.

graph TD
    classDef allowed stroke:#00cc66,stroke-width:2px,fill:#e6ffed;
    classDef blocked stroke:#ff3333,stroke-width:2px,fill:#ffeef0;

    Key["GitHub App Private Key (.pem)"] -->|Authentication Context| AppAuth["app_auth"]
    
    AppAuth -->|POST /orgs/org/repos| Create["CREATE A NEW REPOSITORY<br><br>ALLOWED<br>Backend hooks automatically<br>APPEND it to App's scope."]
    
    AppAuth -->|DELETE /repos/owner/repo| Destroy["DELETE ENTIRE REPOSITORY<br><br>ALLOWED<br>App can permanently erase<br>corporate code and history<br>from existence."]
    
    AppAuth -->|DELETE /user/installations/...| Toggle["SOFTLY REMOVE FROM APP ACCESS LIST<br><br>HARD BLOCKED<br>API throws 403 Forbidden.<br>App token is forbidden from<br>simply hiding the repository."]

    class Create allowed;
    class Destroy allowed;
    class Toggle blocked;
Loading

Underlying Data Model

Conceptually, GitHub stores the relationship between an installation and its accessible repositories as a separate mapping rather than as an attribute of either object.

A repository contains metadata about the repository itself, while the installation maintains the list of repositories it can access.

Because this relationship belongs to the installation, modifying it requires installation-specific mutation endpoints. Since those endpoints are only exposed through the user-scoped API, repository assignments cannot currently be managed using installation tokens alone.


An Interesting Asymmetry

This API design creates an operational asymmetry when compared with the standard github_repository lifecycle.

Repository Creation

When a GitHub App creates a repository through:

POST /orgs/{org}/repos

GitHub automatically associates the new repository with the creating installation.

No additional API call is required.

Repository Deletion

The same GitHub App installation token can later delete that repository through:

DELETE /repos/{owner}/{repo}

Removing the repository also removes its installation association.

Repository Assignment

However, attempting to explicitly add or remove repository access while leaving the repository itself intact requires the user-scoped installation endpoints discussed above.

Thus, a GitHub App installation token can:

  • Create a repository.
  • Automatically gain access to it.
  • Delete the repository.

…but cannot explicitly change repository assignment for an existing repository.


Relevant Ecosystem References

The following discussions document various aspects of this API limitation.

Terraform GitHub Provider

Issue #2103

github_app_installation_repository does not work with fine-grained personal access tokens.

Issue #2431

Cross-organization repository lookup behavior when using user-scoped installation endpoints.

GitHub Community

Discussion #24549

Working across organizations with GitHub Apps.

Provider Modernization

PR #3448 — New GitHub Client

Introduced the legacy_client = false architecture. This modernized the provider's transport layer but does not change the underlying GitHub REST API constraints affecting github_app_installation_repositories.


Conclusion

The current GitHub REST API does not expose an organization-scoped mechanism for modifying GitHub App repository assignments.

Because the only available mutation endpoints require a user-delegated authentication context, the github_app_installation_repositories resource cannot support a complete CRUD lifecycle using a pure machine-to-machine authentication model (app_auth).

Unless GitHub introduces new installation-scoped mutation endpoints or expands support for GitHub App installation tokens on the existing endpoints, this limitation cannot be resolved within the Terraform provider itself.

For environments standardized on GitHub App authentication, repository assignment should therefore be treated as an external bootstrap or administrative operation rather than infrastructure that can be fully managed through Terraform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants