-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub_push.go
75 lines (62 loc) · 1.71 KB
/
github_push.go
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
package main
import (
"context"
"encoding/base64"
"errors"
"fmt"
"strings"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func pushToGithub(data, token string) error {
if data == "" {
return errors.New("params error")
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
c := "feat: add gocn news, date : " + time.Now().Format("2006-01-02")
sha := ""
content := &github.RepositoryContentFileOptions{
Message: &c,
SHA: &sha,
Committer: &github.CommitAuthor{
Name: github.String("lubanproj"),
Email: github.String("[email protected]"),
Login: github.String("lubanproj"),
},
Author: &github.CommitAuthor{
Name: github.String("lubanproj"),
Email: github.String("[email protected]"),
Login: github.String("lubanproj"),
},
Branch: github.String("master"),
}
op := &github.RepositoryContentGetOptions{}
repo, _, _, er := client.Repositories.GetContents(ctx, "lubanproj", "go_read", "README.md", op)
if er != nil || repo == nil {
fmt.Println("get github repositories error, date: ", time.Now())
return er
}
content.SHA = repo.SHA
decodeBytes, err := base64.StdEncoding.DecodeString(*repo.Content)
if err != nil {
fmt.Println("decode repo error, ",err)
return err
}
oldContentList := strings.Split(string(decodeBytes), "<br>")
if len(oldContentList) != 2 {
fmt.Println("README.md format error")
}
content.Content = []byte(oldContentList[0] + "<br>" + data + oldContentList[1])
_, _, err = client.Repositories.UpdateFile(ctx, "lubanproj", "go_read", "README.md", content)
if err != nil {
println(err)
return err
}
return nil
}