GetGithubPullRequest returns the status of the most recent PR for the specified branch, if any.
(ctx context.Context, req *adminv1.GetGithubPullRequestRequest)
| 314 | |
| 315 | // GetGithubPullRequest returns the status of the most recent PR for the specified branch, if any. |
| 316 | func (s *Server) GetGithubPullRequest(ctx context.Context, req *adminv1.GetGithubPullRequestRequest) (*adminv1.GetGithubPullRequestResponse, error) { |
| 317 | observability.AddRequestAttributes(ctx, |
| 318 | attribute.String("args.organization", req.Org), |
| 319 | attribute.String("args.project", req.Project), |
| 320 | attribute.String("args.branch", req.Branch), |
| 321 | ) |
| 322 | |
| 323 | if req.Branch == "" { |
| 324 | return nil, status.Error(codes.InvalidArgument, "branch must be specified") |
| 325 | } |
| 326 | |
| 327 | proj, err := s.admin.DB.FindProjectByName(ctx, req.Org, req.Project) |
| 328 | if err != nil { |
| 329 | return nil, err |
| 330 | } |
| 331 | |
| 332 | claims := auth.GetClaims(ctx) |
| 333 | if !claims.ProjectPermissions(ctx, proj.OrganizationID, proj.ID).ReadDev { |
| 334 | return nil, status.Error(codes.PermissionDenied, "does not have permission to read github PR") |
| 335 | } |
| 336 | |
| 337 | if proj.GitRemote == nil { |
| 338 | return nil, status.Error(codes.FailedPrecondition, "project is not connected to a github repository") |
| 339 | } |
| 340 | |
| 341 | if proj.ManagedGitRepoID != nil { |
| 342 | return nil, status.Error(codes.FailedPrecondition, "cannot get github PR for a project connected to a managed git repository") |
| 343 | } |
| 344 | |
| 345 | account, repo, ok := gitutil.SplitGithubRemote(*proj.GitRemote) |
| 346 | if !ok { |
| 347 | return nil, fmt.Errorf("invalid github url %q stored for project", *proj.GitRemote) |
| 348 | } |
| 349 | |
| 350 | repoID, err := s.githubRepoIDForProject(ctx, proj) |
| 351 | if err != nil { |
| 352 | return nil, err |
| 353 | } |
| 354 | client := s.admin.Github.InstallationClient(*proj.GithubInstallationID, &repoID) |
| 355 | |
| 356 | prs, _, err := client.PullRequests.List(ctx, account, repo, &github.PullRequestListOptions{ |
| 357 | Head: fmt.Sprintf("%s:%s", account, req.Branch), |
| 358 | State: "all", |
| 359 | Sort: "created", |
| 360 | Direction: "desc", |
| 361 | ListOptions: github.ListOptions{PerPage: 1}, |
| 362 | }) |
| 363 | if err != nil { |
| 364 | return nil, fmt.Errorf("failed to list github PRs: %w", err) |
| 365 | } |
| 366 | if len(prs) == 0 { |
| 367 | return &adminv1.GetGithubPullRequestResponse{}, nil |
| 368 | } |
| 369 | |
| 370 | pr := prs[0] |
| 371 | state := adminv1.GetGithubPullRequestResponse_STATE_UNSPECIFIED |
| 372 | switch strings.ToLower(pr.GetState()) { |
| 373 | case "open": |
nothing calls this directly
no test coverage detected