-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
105 lines (97 loc) · 2.83 KB
/
main.go
File metadata and controls
105 lines (97 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"context"
"encoding/base64"
"log"
"os"
"github.com/go-git/go-git/v5"
"github.com/jessevdk/go-flags"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
func main() {
var opts struct {
Repository string `short:"r" long:"repository" description:"the repository to push commits to" required:"true"`
BranchName string `short:"b" long:"branch" description:"the branch to push commits to" required:"true"`
Message string `short:"m" long:"message" description:"the commit message to use" default:"updated with github-signer"`
}
_, err := flags.Parse(&opts)
switch e := err.(type) {
case *flags.Error:
if e.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
case nil:
break
default:
log.Fatal(err)
}
r, err := git.PlainOpen(".")
if err != nil {
log.Fatalf("unable to open repository: %s", err)
}
w, err := r.Worktree()
if err != nil {
log.Fatalf("unable to open repository: %s", err)
}
rev, err := r.Head()
if err != nil {
log.Fatalf("unable to find HEAD revision: %s", err)
}
s, err := w.Status()
if err != nil {
log.Fatalf("unable to open repository: %s", err)
}
changes := &[]githubv4.FileAddition{}
deletes := &[]githubv4.FileDeletion{}
for name, status := range s {
if status.Worktree == git.Modified || status.Staging == git.Added || status.Staging == git.Modified {
log.Printf("adding %s", name)
b, _ := os.ReadFile(name)
content := base64.StdEncoding.EncodeToString(b)
*changes = append(*changes, githubv4.FileAddition{
Path: githubv4.String(name),
Contents: githubv4.Base64String(content),
})
} else if status.Staging == git.Deleted {
*deletes = append(*deletes, githubv4.FileDeletion{
Path: githubv4.String(name),
})
}
}
if (len(*changes) + len(*deletes)) == 0 {
log.Printf("no changes to commit, exiting")
os.Exit(0)
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := githubv4.NewClient(httpClient)
var m struct {
CreateCommitOnBranch struct {
Commit struct {
Url githubv4.ID
}
} `graphql:"createCommitOnBranch(input: $input)"`
}
input := githubv4.CreateCommitOnBranchInput{
Branch: githubv4.CommittableBranch{
RepositoryNameWithOwner: githubv4.NewString(githubv4.String(opts.Repository)),
BranchName: githubv4.NewString(githubv4.String(opts.BranchName)),
},
Message: githubv4.CommitMessage{Headline: githubv4.String(opts.Message)},
FileChanges: &githubv4.FileChanges{
Additions: changes,
Deletions: deletes,
},
ExpectedHeadOid: githubv4.GitObjectID(rev.Hash().String()),
}
err = client.Mutate(context.Background(), &m, input, nil)
if err != nil {
log.Fatalf("unable to mutate: %s", err)
}
log.Printf("mutation complete: %s", m.CreateCommitOnBranch.Commit.Url)
}