go-github icon indicating copy to clipboard operation
go-github copied to clipboard

Create commit in new branch without creating branch beforehand

Open brondum opened this issue 3 years ago • 6 comments

Hi.

Is it possible to create a commit in a new branch without explicitly doing a client.Git.CreateRef. When creating the remote ref before doing the commit Github creates the branch with the same commitSHA as the base branch, which causes our CI to trigger a job on the same SHA, which is just waste of time.

How i am doing it today:

  • Get ref from source branch client.Git.GetRef
  • Create new branch: client.Git.GetRef
  • Update file: client.Repositories.UpdateFile

Any ideas ?

brondum avatar Feb 04 '21 06:02 brondum

Thanks, @brondum.

This is actually the perfect question for GitHub developer technical support: https://support.github.com/contact

If they can show you how to accomplish what you want using the GitHub v3 API and curl, then this repo should be able to help you accomplish the same thing from Go.

Please report your findings back here and we can take it from there. Good luck.

gmlewis avatar Feb 04 '21 13:02 gmlewis

@gmlewis , i would like to work on this issue, please assign me this

vikramzsingh avatar Feb 05 '21 17:02 vikramzsingh

Thanks for your enthusiasm, @vikramzsingh , but this issue is not yet ready to work on. GitHub support needs to be contacted and we need to hear back from them on how to proceed.

gmlewis avatar Feb 05 '21 21:02 gmlewis

@brondum - have you heard back from GitHub Tech Support on this issue? If so, please share your findings here.

Otherwise, this issue will be closed in a couple weeks if there is no further interest/activity.

gmlewis avatar Aug 06 '21 14:08 gmlewis

Any news ?

clementlecorre avatar Sep 22 '22 12:09 clementlecorre

I don't think it's possible to create a commit without creating the branch somehow, since that is not how Git works. However, you can programmatically create the remote branch, here is a snippet:

// Create remote branch from master
masterRef, _, _ := client.Git.GetRef(ctx, Owner, Repo, "refs/heads/master") // or "refs/heads/main"
branch := "my-branch"
refName := fmt.Sprintf("refs/heads/%s", branch)
branchRef := &github.Reference {
  Ref: &refName,
  Object: &github.GitObject{
    SHA: masterRef.Object.SHA,
  },
}
_, _, _ = client.Git.CreateRef(ctx, Owner, Repo, branchRef)
// handle errors

// Push file
msg := "some commit message"
var req github.RepositoryContentFileOptions
req.Content = fileBytes
req.Message = &msg
req.Branch = &branch
filePath := "/some/path/to/my-file.txt"
pushFileResp, resp, err := client.Repositories.CreateFile(ctx, Owner, Repo, filePath, &req)
// handle errors

russellrc-keebo avatar Jan 13 '23 17:01 russellrc-keebo