MCPcopy Index your code
hub / github.com/google/go-github / pushCommit

Function pushCommit

example/commitpr/main.go:136–179  ·  view source on GitHub ↗

pushCommit creates the commit in the given reference using the given tree.

(ref *github.Reference, tree *github.Tree)

Source from the content-addressed store, hash-verified

134
135// pushCommit creates the commit in the given reference using the given tree.
136func pushCommit(ref *github.Reference, tree *github.Tree) (err error) {
137 // Get the parent commit to attach the commit to.
138 parent, _, err := client.Repositories.GetCommit(ctx, *sourceOwner, *sourceRepo, *ref.Object.SHA, nil)
139 if err != nil {
140 return err
141 }
142 // This is not always populated, but is needed.
143 parent.Commit.SHA = parent.SHA
144
145 // Create the commit using the tree.
146 date := time.Now()
147 author := &github.CommitAuthor{Date: &github.Timestamp{Time: date}, Name: authorName, Email: authorEmail}
148 commit := github.Commit{Author: author, Message: commitMessage, Tree: tree, Parents: []*github.Commit{parent.Commit}}
149 opts := github.CreateCommitOptions{}
150 if *privateKey != "" {
151 armoredBlock, e := os.ReadFile(*privateKey)
152 if e != nil {
153 return e
154 }
155 keyring, e := openpgp.ReadArmoredKeyRing(bytes.NewReader(armoredBlock))
156 if e != nil {
157 return e
158 }
159 if len(keyring) != 1 {
160 return errors.New("expected exactly one key in the keyring")
161 }
162 key := keyring[0]
163 opts.Signer = github.MessageSignerFunc(func(w io.Writer, r io.Reader) error {
164 return openpgp.ArmoredDetachSign(w, key, r, nil)
165 })
166 }
167 newCommit, _, err := client.Git.CreateCommit(ctx, *sourceOwner, *sourceRepo, commit, &opts)
168 if err != nil {
169 return err
170 }
171
172 // Attach the commit to the master branch.
173 ref.Object.SHA = newCommit.SHA
174 _, _, err = client.Git.UpdateRef(ctx, *sourceOwner, *sourceRepo, *ref.Ref, github.UpdateRef{
175 SHA: *newCommit.SHA,
176 Force: github.Ptr(false),
177 })
178 return err
179}
180
181// createPR creates a pull request. Based on: https://pkg.go.dev/github.com/google/go-github/v88/github#example-PullRequestsService-Create
182func createPR() (err error) {

Callers 1

mainFunction · 0.85

Calls 5

MessageSignerFuncFuncType · 0.92
PtrFunction · 0.92
CreateCommitMethod · 0.80
UpdateRefMethod · 0.80
GetCommitMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…