MCPcopy Index your code
hub / github.com/rilldata/rill / CreateGithubPullRequest

Method CreateGithubPullRequest

admin/server/github.go:255–313  ·  view source on GitHub ↗

CreateGithubPullRequest creates a Github PR from the specified branch to the project's primary branch.

(ctx context.Context, req *adminv1.CreateGithubPullRequestRequest)

Source from the content-addressed store, hash-verified

253
254// CreateGithubPullRequest creates a Github PR from the specified branch to the project's primary branch.
255func (s *Server) CreateGithubPullRequest(ctx context.Context, req *adminv1.CreateGithubPullRequestRequest) (*adminv1.CreateGithubPullRequestResponse, error) {
256 observability.AddRequestAttributes(ctx,
257 attribute.String("args.organization", req.Org),
258 attribute.String("args.project", req.Project),
259 attribute.String("args.branch", req.Branch),
260 )
261
262 if req.Branch == "" {
263 return nil, status.Error(codes.InvalidArgument, "branch must be specified")
264 }
265
266 proj, err := s.admin.DB.FindProjectByName(ctx, req.Org, req.Project)
267 if err != nil {
268 return nil, err
269 }
270
271 claims := auth.GetClaims(ctx)
272 if !claims.ProjectPermissions(ctx, proj.OrganizationID, proj.ID).ManageDev {
273 return nil, status.Error(codes.PermissionDenied, "does not have permission to create a github PR")
274 }
275
276 if proj.GitRemote == nil {
277 return nil, status.Error(codes.FailedPrecondition, "project is not connected to a github repository")
278 }
279
280 if proj.ManagedGitRepoID != nil {
281 return nil, status.Error(codes.FailedPrecondition, "cannot create github PR for a project connected to a managed git repository")
282 }
283
284 account, repo, ok := gitutil.SplitGithubRemote(*proj.GitRemote)
285 if !ok {
286 return nil, fmt.Errorf("invalid github url %q stored for project", *proj.GitRemote)
287 }
288
289 repoID, err := s.githubRepoIDForProject(ctx, proj)
290 if err != nil {
291 return nil, err
292 }
293 client := s.admin.Github.InstallationClient(*proj.GithubInstallationID, &repoID)
294
295 title := req.Title
296 if title == "" {
297 title = fmt.Sprintf("Update from %s", req.Branch)
298 }
299
300 pr, _, err := client.PullRequests.Create(ctx, account, repo, &github.NewPullRequest{
301 Title: github.Ptr(title),
302 Head: github.Ptr(req.Branch),
303 Base: github.Ptr(proj.PrimaryBranch),
304 Body: github.Ptr(req.Body),
305 })
306 if err != nil {
307 return nil, fmt.Errorf("failed to create github PR: %w", err)
308 }
309
310 return &adminv1.CreateGithubPullRequestResponse{
311 PrUrl: pr.GetHTMLURL(),
312 }, nil

Callers

nothing calls this directly

Calls 11

AddRequestAttributesFunction · 0.92
GetClaimsFunction · 0.92
SplitGithubRemoteFunction · 0.92
CreateMethod · 0.80
StringMethod · 0.65
FindProjectByNameMethod · 0.65
ProjectPermissionsMethod · 0.65
ErrorfMethod · 0.65
InstallationClientMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected