Skip to content

Conversation

chmouel
Copy link
Member

@chmouel chmouel commented Jul 1, 2025

  • Removed listing of all installations.
  • Fetched the installation directly using the repository URL.
  • Added fallback to find the organization installation if the repository installation is not found.
  • Enhanced error handling for token retrieval.

Changes

Submitter Checklist

  • 📝 Ensure your commit message is clear and informative. Refer to the How to write a git commit message guide. Include the commit message in the PR body rather than linking to an external site (e.g., Jira ticket).

  • ♽ Run make test lint before submitting a PR to avoid unnecessary CI processing. Consider installing pre-commit and running pre-commit install in the repository root for an efficient workflow.

  • ✨ We use linters to maintain clean and consistent code. Run make lint before submitting a PR. Some linters offer a --fix mode, executable with make fix-linters (ensure markdownlint and golangci-lint are installed).

  • 📖 Document any user-facing features or changes in behavior.

  • 🧪 While 100% coverage isn't required, we encourage unit tests for code changes where possible.

  • 🎁 If feasible, add an end-to-end test. See README for details.

  • 🔎 Address any CI test flakiness before merging, or provide a valid reason to bypass it (e.g., token rate limitations).

  • If adding a provider feature, fill in the following details:

    • GitHub App
    • GitHub Webhook
    • Gitea/Forgejo
    • GitLab
    • Bitbucket Cloud
    • Bitbucket Data Center

    (update the provider documentation accordingly)

@mathur07
Copy link
Contributor

This fix will help us resolve this issue: https://issues.redhat.com/browse/KONFLUX-5929

Copy link
Contributor

@enarha enarha left a comment

Choose a reason for hiding this comment

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

What's missing in this PR (why is it in draft mode)?

@chmouel
Copy link
Member Author

chmouel commented Aug 1, 2025

What's missing in this PR (why is it in draft mode)?

not enough time to work on it for now (and it wasn't prioritized) and generally we don't have enough reviewer in pac so i put thing as draft pr so people don't get stress until i am sure it work...

@mathur07
Copy link
Contributor

mathur07 commented Aug 1, 2025

@chmouel I can give my best effort to review it. @aThorp96 Can help us too in the review?

Copy link
Member

@aThorp96 aThorp96 left a comment

Choose a reason for hiding this comment

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

This is so much cleaner and I think may significantly reduce the number of API calls.

Also, it looks like these API calls are not being included in the pipelines_as_code_git_provider_api_request_count metrics. I don't think that necessarily needs to be clubbed into this PR, but we'll need to fix that as well

pathParts := strings.Split(strings.Trim(repoURL.Path, "/"), "/")
if len(pathParts) < 2 {
return "", "", 0, fmt.Errorf("invalid repository URL path: %s", repoURL.Path)
}
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick

Should this be checking for longer paths as well? I know Gitlab supports sub-projects but I don't think Github would support /foo/bar/baz. It technically does resolve for example https://github.com/openshift-pipelines//pipelines-as-code with the two slashes, but since the Repository CR's URL semantically has to match the URL sent in the Github event payload I don't think that // will be valid in the RepositoryCR's URL in the first place.

Suggested change
pathParts := strings.Split(strings.Trim(repoURL.Path, "/"), "/")
if len(pathParts) < 2 {
return "", "", 0, fmt.Errorf("invalid repository URL path: %s", repoURL.Path)
}
pathParts := strings.Split(strings.Trim(repoURL.Path, "/"), "/")
if len(pathParts) != 2 {
return "", "", 0, fmt.Errorf("invalid repository URL path: expected '<organization>/<repository>', got: %s", repoURL.Path)
}

Comment on lines +73 to 81
// Directly get the installation for the repository
installation, _, err := client.Apps.FindRepositoryInstallation(ctx, owner, repoName)
if err != nil {
// Fallback to finding organization installation if repository installation is not found
installation, _, err = client.Apps.FindOrganizationInstallation(ctx, owner)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Change needed:
The Installation could also be installed for the User as well, so we'll also need to check client.Apps.FindUserInstallation() as well. If only these methods all had the same signature we could just iterate through a list of the methods, alas 😖

installationID := *installation.ID
token, err := ip.ghClient.GetAppToken(ctx, ip.run.Clients.Kube, enterpriseHost, installationID, ip.namespace)
if err != nil {
logger.Warnf("Could not get a token for installation ID %d: %v", installationID, err)
Copy link
Member

Choose a reason for hiding this comment

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

Should this be an event instead of just a log? Not sure when events are desirable instead of just logs

Comment on lines +239 to +241
mux.HandleFunc(fmt.Sprintf("/orgs/%s/installation", orgName), func(w http.ResponseWriter, _ *http.Request) {
_, _ = fmt.Fprintf(w, `{"id": %d}`, wantID)
})
Copy link
Member

Choose a reason for hiding this comment

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

Do you mind adding a test case for the fallback(s) as well?

@chmouel
Copy link
Member Author

chmouel commented Aug 4, 2025

let's wait for #2199 to ship first to make sure we can instrument this

@chmouel
Copy link
Member Author

chmouel commented Aug 13, 2025

/gemini review

@chmouel chmouel force-pushed the refactor-improve-github-app-installation-id-retrie branch from 2fb6740 to b6b6a83 Compare August 13, 2025 16:17
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly improves the efficiency of retrieving the GitHub App installation ID by fetching it directly for the repository or organization, instead of listing all installations. The new logic is much cleaner and more direct. I've included a few suggestions to further enhance the implementation by removing some now-unused code and improving the robustness of the installation discovery mechanism.

- Removed listing of all installations.
- Fetched the installation directly using the repository URL.
- Added fallback to find the organization installation if the repository
  installation is not found.
- Enhanced error handling for token retrieval.
- Updated tests to reflect the changes.
- Eliminated the matchRepos method from Install struct as it was unused

refactor: Improve GitHub App installation ID retrieval
Signed-off-by: Chmouel Boudjnah <[email protected]>
@chmouel chmouel force-pushed the refactor-improve-github-app-installation-id-retrie branch from b6b6a83 to 75709dc Compare August 13, 2025 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants