MCPcopy Create free account
hub / github.com/bluekeyes/patch2pr / prepareSourceRepo

Function prepareSourceRepo

cmd/patch2pr/main.go:323–364  ·  view source on GitHub ↗
(ctx context.Context, client *github.Client, opts *Options)

Source from the content-addressed store, hash-verified

321}
322
323func prepareSourceRepo(ctx context.Context, client *github.Client, opts *Options) (patch2pr.Repository, error) {
324 source := patch2pr.Repository{}
325 target := *opts.Repository
326
327 if !opts.Fork {
328 // If we're not using a fork, the source is the same as the target
329 return target, nil
330 }
331
332 user, _, err := client.Users.Get(ctx, "")
333 if err != nil {
334 return source, fmt.Errorf("get user failed: %w", err)
335 }
336
337 if opts.ForkRepository != nil {
338 source = *opts.ForkRepository
339 } else {
340 source = patch2pr.Repository{
341 Owner: user.GetLogin(),
342 Name: target.Name,
343 }
344 }
345
346 repo, _, err := client.Repositories.Get(ctx, source.Owner, source.Name)
347 switch {
348 case isCode(err, http.StatusNotFound):
349 isUserFork := user.GetLogin() == source.Owner
350 if err := createFork(ctx, client, source, target, isUserFork); err != nil {
351 return source, fmt.Errorf("forking repository failed: %w", err)
352 }
353
354 case err != nil:
355 return source, fmt.Errorf("get fork repository failed: %w", err)
356
357 default:
358 if !repo.GetFork() || repo.GetParent().GetFullName() != target.String() {
359 return source, fmt.Errorf("fork repository %q exists, but is not a fork of %q", source, target)
360 }
361 }
362
363 return source, nil
364}
365
366func createFork(ctx context.Context, client *github.Client, fork, parent patch2pr.Repository, isUserFork bool) error {
367 const (

Callers 1

executeFunction · 0.85

Calls 4

isCodeFunction · 0.85
createForkFunction · 0.85
GetMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected