-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
99 lines (89 loc) · 2.63 KB
/
github.go
File metadata and controls
99 lines (89 loc) · 2.63 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
package githubapi
import (
"bufio"
"context"
"fmt"
"github.com/google/go-github/v56/github"
"io"
"log"
"os"
)
func MakeClient(personalToken string) *github.Client {
client := github.NewClient(nil).WithAuthToken(personalToken)
return client
}
func ListCommitALL(ctx context.Context, PersonalToken, repoName, ownerName string) ([]*github.RepositoryCommit, error) {
commits, _, err := MakeClient(PersonalToken).Repositories.ListCommits(ctx, ownerName, repoName, nil)
if err != nil {
return nil, err
}
return commits, nil
}
func GetCommit(ctx context.Context, personalToken, repoName, ownerName, sha string) (*github.RepositoryCommit, error) {
commits, _, err := MakeClient(personalToken).Repositories.GetCommit(ctx, ownerName, repoName, sha, nil)
if err != nil {
return nil, err
}
return commits, nil
}
func GetBranch(ctx context.Context, personalToken, repoName, ownerName string) ([]*github.Branch, error) {
branches, _, err := MakeClient(personalToken).Repositories.ListBranches(ctx, ownerName, repoName, nil)
if err != nil {
return nil, err
}
return branches, nil
}
func ListRepositoriesOrg(ctx context.Context, personalToken, OrgName string) (dest []*github.Repository, err error) {
dest, _, err = MakeClient(personalToken).Repositories.ListByOrg(ctx, OrgName, nil)
if err != nil {
return nil, err
}
return
}
func ListRepositoriesOnlydDetail(ctx context.Context, personalToken, OrgName string) (dest []Repositories, err error) {
List, err := ListRepositoriesOrg(ctx, personalToken, OrgName)
if err != nil {
return nil, err
}
dest = make([]Repositories, 0, len(List))
for _, v := range List {
data := Repositories{
Name: v.Name,
FullName: v.FullName,
Homepage: v.Homepage,
}
dest = append(dest, data)
}
return
}
func UploadFileToRepository(val PushRepositories) (response *github.RepositoryContentResponse, err error) {
file, err := os.Open(val.Path)
if err != nil {
log.Fatalf("Gagal open file %s", err.Error())
return
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
fmt.Println(err)
return
}
bs := make([]byte, stat.Size())
_, err = bufio.NewReader(file).Read(bs)
if err != nil && err != io.EOF {
fmt.Println(err)
return
}
opts := &github.RepositoryContentFileOptions{
Message: github.String(val.Message),
Content: bs,
Branch: github.String(val.Branch),
Committer: &github.CommitAuthor{Name: github.String(val.Username), Email: github.String(val.Email)},
}
response, _, err = MakeClient(val.PersonalToken).Repositories.CreateFile(val.Context, val.OwnerName, val.Reponame, val.Path, opts)
if err != nil {
fmt.Printf("%+v", err.Error())
return
}
return
}