go-github
go-github copied to clipboard
Create commit in new branch without creating branch beforehand
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 ?
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 , i would like to work on this issue, please assign me this
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.
@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.
Any news ?
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