Set creates or updates the reference to point to sha. If force is true and the reference exists, Set updates it even if the update is not a fast-forward.
(ctx context.Context, sha string, force bool)
| 34 | // the reference exists, Set updates it even if the update is not a |
| 35 | // fast-forward. |
| 36 | func (r *Reference) Set(ctx context.Context, sha string, force bool) error { |
| 37 | // Test for existence because update and create return 422 responses if the |
| 38 | // the ref is missing or exists, respectively. The same code is also used |
| 39 | // for other errors like passing a bad SHA, so our only other option is to |
| 40 | // parse the string message, which is fragile. |
| 41 | var exists bool |
| 42 | if _, _, err := r.client.Git.GetRef(ctx, r.owner, r.repo, r.ref); err != nil { |
| 43 | if rerr, ok := err.(*github.ErrorResponse); !ok || rerr.Response.StatusCode != 404 { |
| 44 | return fmt.Errorf("get ref failed: %w", err) |
| 45 | } |
| 46 | } else { |
| 47 | exists = true |
| 48 | } |
| 49 | |
| 50 | if exists { |
| 51 | if _, _, err := r.client.Git.UpdateRef(ctx, r.owner, r.repo, r.ref, github.UpdateRef{ |
| 52 | SHA: sha, |
| 53 | Force: github.Ptr(force), |
| 54 | }); err != nil { |
| 55 | return fmt.Errorf("update ref failed: %w", err) |
| 56 | } |
| 57 | } else { |
| 58 | if _, _, err := r.client.Git.CreateRef(ctx, r.owner, r.repo, github.CreateRef{ |
| 59 | Ref: r.ref, |
| 60 | SHA: sha, |
| 61 | }); err != nil { |
| 62 | return fmt.Errorf("create ref failed: %w", err) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | // PullRequest create a new pull request for the reference. The reference must |
| 70 | // be a branch (start with "refs/heads/".) The pull request takes values from |
no outgoing calls