(ctx context.Context, orgID, branch string, userID *string, gitRemote string)
| 2196 | } |
| 2197 | |
| 2198 | func (s *Server) githubOptsForRemote(ctx context.Context, orgID, branch string, userID *string, gitRemote string) (githubRepoID, instID *int64, mgdGitRepoID *string, primaryBranch string, resErr error) { |
| 2199 | isMgdGitRepo := true |
| 2200 | mgdGitRepo, err := s.admin.DB.FindManagedGitRepo(ctx, gitRemote) |
| 2201 | if err != nil { |
| 2202 | if !errors.Is(err, database.ErrNotFound) { |
| 2203 | return nil, nil, nil, "", err |
| 2204 | } |
| 2205 | isMgdGitRepo = false |
| 2206 | } |
| 2207 | if isMgdGitRepo { |
| 2208 | // rill managed git repo |
| 2209 | if mgdGitRepo.OrgID == nil || orgID != *mgdGitRepo.OrgID { |
| 2210 | return nil, nil, nil, "", status.Error(codes.PermissionDenied, "not allowed to access this managed git repo") |
| 2211 | } |
| 2212 | id, err := s.admin.Github.ManagedOrgInstallationID() |
| 2213 | if err != nil { |
| 2214 | return nil, nil, nil, "", err |
| 2215 | } |
| 2216 | |
| 2217 | // fetch github repo id from github |
| 2218 | // ideally this can be stored in managed git repo table but it is fine to fetch it from github during project creation/updation |
| 2219 | c := s.admin.Github.InstallationClient(id, nil) |
| 2220 | account, repo, ok := gitutil.SplitGithubRemote(gitRemote) |
| 2221 | if !ok { |
| 2222 | return nil, nil, nil, "", status.Error(codes.InvalidArgument, "invalid github url") |
| 2223 | } |
| 2224 | ghRepo, _, err := c.Repositories.Get(ctx, account, repo) |
| 2225 | if err != nil { |
| 2226 | return nil, nil, nil, "", fmt.Errorf("failed to get github repo: %w", err) |
| 2227 | } |
| 2228 | |
| 2229 | if branch == "" { |
| 2230 | branch = ghRepo.GetDefaultBranch() |
| 2231 | } |
| 2232 | return ghRepo.ID, &id, &mgdGitRepo.ID, branch, nil |
| 2233 | } |
| 2234 | // User managed github projects must be configured by a user so we can ensure that they're allowed to access the repo. |
| 2235 | if userID == nil { |
| 2236 | return nil, nil, nil, "", status.Error(codes.Unauthenticated, "not authenticated as a user") |
| 2237 | } |
| 2238 | |
| 2239 | // Check Github app is installed and caller has access on the repo |
| 2240 | ghRepoID, installationID, err := s.getAndCheckGithubInstallationID(ctx, gitRemote, *userID) |
| 2241 | if err != nil { |
| 2242 | return nil, nil, nil, "", err |
| 2243 | } |
| 2244 | return &ghRepoID, &installationID, nil, branch, nil |
| 2245 | } |
| 2246 | |
| 2247 | // githubRepoIDForProject returns the github repo id for a project stored in the database. |
| 2248 | // For older projects this may be nil since the github repo id was not stored. |
no test coverage detected