getRef returns the commit branch reference object if it exists or creates it from the base branch before returning it.
()
| 65 | // getRef returns the commit branch reference object if it exists or creates it |
| 66 | // from the base branch before returning it. |
| 67 | func getRef() (ref *github.Reference, err error) { |
| 68 | if ref, _, err = client.Git.GetRef(ctx, *sourceOwner, *sourceRepo, branchRef(*commitBranch)); err == nil { |
| 69 | return ref, nil |
| 70 | } |
| 71 | |
| 72 | // We consider that an error means the branch has not been found and needs to |
| 73 | // be created. |
| 74 | if *commitBranch == *baseBranch { |
| 75 | return nil, errors.New("the commit branch does not exist but `-base-branch` is the same as `-commit-branch`") |
| 76 | } |
| 77 | |
| 78 | if *baseBranch == "" { |
| 79 | return nil, errors.New("the `-base-branch` should not be set to an empty string when the branch specified by `-commit-branch` does not exists") |
| 80 | } |
| 81 | |
| 82 | var baseRef *github.Reference |
| 83 | if baseRef, _, err = client.Git.GetRef(ctx, *sourceOwner, *sourceRepo, branchRef(*baseBranch)); err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | newRef := github.CreateRef{Ref: branchRef(*commitBranch), SHA: *baseRef.Object.SHA} |
| 87 | ref, _, err = client.Git.CreateRef(ctx, *sourceOwner, *sourceRepo, newRef) |
| 88 | return ref, err |
| 89 | } |
| 90 | |
| 91 | // branchRef generates the fully qualified git reference for the given branch name. |
| 92 | func branchRef(name string) string { |