forked from iwarapter/github-commiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
155 lines (138 loc) · 3.75 KB
/
main.go
File metadata and controls
155 lines (138 loc) · 3.75 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"context"
"encoding/base64"
"log"
"os"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/jessevdk/go-flags"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
type 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"`
PullRequest bool `short:"p" long:"prmake" description:"automatically raises a pull request if set"`
}
func main() {
ctx := context.Background()
var opts Opts
parser := flags.NewParser(&opts, flags.Default)
_, err := parser.Parse()
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)
}
repo, status, err := openRepository()
if err != nil {
log.Fatalf("unable to open repository: %s", err)
}
changes := addChanges(status)
client := createGhClient()
oid, repoId, err := getMainOid(ctx, client, opts)
if err != nil {
log.Fatalf("unable to lookup oid: %s", err)
}
branchExists, err := CheckBranchExists(ctx, client, opts)
if err != nil {
log.Fatalf("unable to lookup branch: %s", err)
}
var revision *plumbing.Reference
if !branchExists {
err = CreateBranch(ctx, client, opts, repoId, oid)
if err != nil {
log.Fatalf("unable to create branch: %s", err)
}
revision, err = repo.Head()
if err != nil {
log.Fatalf("unable to find HEAD revision: %s", err)
}
} else {
err = fetchRemote(repo)
if err != nil {
log.Printf("unable to fetch remote: %s", err)
}
refName := plumbing.ReferenceName("refs/remotes/origin/" + opts.BranchName)
revision, err = repo.Reference(refName, true)
if err != nil {
log.Fatalf("unable to find HEAD for branch %s: %s", opts.BranchName, err)
}
}
err = DoCommit(ctx, client, changes, opts, revision)
if err != nil {
log.Fatalf("unable to commit: %s", err)
}
if opts.PullRequest {
err = CreatePullRequest(ctx, client, opts, repoId)
if err != nil {
log.Fatalf("unable to create PR: %s", err)
}
}
}
func openRepository() (*git.Repository, git.Status, error) {
repo, err := git.PlainOpen(".")
if err != nil {
return nil, nil, err
}
worktree, err := repo.Worktree()
if err != nil {
return nil, nil, err
}
status, err := worktree.Status()
if err != nil {
return nil, nil, err
}
return repo, status, nil
}
func fetchRemote(repo *git.Repository) error {
remote, err := repo.Remote("origin")
if err != nil {
return err
}
tokenAuth := &http.TokenAuth{
Token: os.Getenv("GITHUB_TOKEN"),
}
err = remote.Fetch(&git.FetchOptions{Auth: tokenAuth})
if err != nil {
return err
}
return nil
}
func addChanges(status git.Status) *[]githubv4.FileAddition {
changes := &[]githubv4.FileAddition{}
for name, status := range status {
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),
})
}
}
if len(*changes) == 0 {
log.Printf("no changes to commit, exiting")
os.Exit(0)
}
return changes
}
func createGhClient() *githubv4.Client {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := githubv4.NewClient(httpClient)
return client
}