-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathaksk.go
139 lines (125 loc) · 3.69 KB
/
aksk.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
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
package main
import (
"bytes"
"context"
"flag"
"log"
"github.com/GitDataAI/jiaozifs/utils"
"github.com/GitDataAI/jiaozifs/api"
apiimpl "github.com/GitDataAI/jiaozifs/api/api_impl"
)
var url string
var ak string
var sk string
var repoName string
func init() {
flag.StringVar(&url, "url", "", "jiaozifs endpoint")
flag.StringVar(&ak, "ak", "", "jiaozifs ak")
flag.StringVar(&sk, "sk", "", "jiaozifs sk")
flag.StringVar(&repoName, "repo", "", "repo to create")
}
func main() {
flag.Parse()
log.Println(func(ctx context.Context) error {
cli, err := api.NewClient(url+apiimpl.APIV1Prefix, api.AkSkOption(ak, sk))
if err != nil {
return err
}
resp, err := cli.GetUserInfo(ctx)
userInfo, err := api.ParseGetUserInfoResponse(resp)
if err != nil {
return err
}
log.Println("User", userInfo.JSON200.Name, "Login")
//create repo
resp, err = cli.CreateRepository(ctx, api.CreateRepositoryJSONRequestBody{
Name: repoName,
})
if err != nil {
return err
}
repo, err := api.ParseCreateRepositoryResponse(resp)
if err != nil {
return err
}
log.Println("Create Repo", repo.JSON201.Name, "ID:", repo.JSON201.Id)
//create branch
branchName := "branch_test"
resp, err = cli.CreateBranch(ctx, userInfo.JSON200.Name, repo.JSON201.Name, api.CreateBranchJSONRequestBody{
Name: branchName,
Source: "main",
})
if err != nil {
return err
}
_, err = api.ParseCreateBranchResponse(resp)
if err != nil {
return err
}
log.Println("Create Branch", repo.JSON201.Name, "ID:", repo.JSON201.Id)
//create draft
resp, err = cli.GetWip(ctx, userInfo.JSON200.Name, repo.JSON201.Name, &api.GetWipParams{RefName: branchName})
if err != nil {
return err
}
log.Println("create draft for main branch")
// upload files to draft
resp, err = cli.UploadObjectWithBody(ctx, userInfo.JSON200.Name, repo.JSON201.Name, &api.UploadObjectParams{
RefName: branchName,
Path: "a.bin",
}, "application/octet-stream", bytes.NewBufferString("hello, world!!"))
if err != nil {
return err
}
uploadResult, err := api.ParseUploadObjectResponse(resp)
if err != nil {
return err
}
log.Println("upload a file size:", uploadResult.JSON201.SizeBytes, "checksum", uploadResult.JSON201.Checksum)
//commit draft
resp, err = cli.CommitWip(ctx, userInfo.JSON200.Name, repo.JSON201.Name, &api.CommitWipParams{
RefName: branchName,
Msg: "test",
})
if err != nil {
return err
}
commitResult, err := api.ParseCommitWipResponse(resp)
if err != nil {
return err
}
log.Println("commit changes. hash:", commitResult.JSON201.BaseCommit)
// merge branch
resp, err = cli.CreateMergeRequest(ctx, userInfo.JSON200.Name, repo.JSON201.Name, api.CreateMergeRequestJSONRequestBody{
Description: utils.String("create merge request test"),
SourceBranchName: branchName,
TargetBranchName: "main",
Title: "Merge: test",
})
if err != nil {
return err
}
mergeRequestResult, err := api.ParseCreateMergeRequestResponse(resp)
if err != nil {
return err
}
log.Println("create merge request merge id", mergeRequestResult.JSON201.Sequence)
_, err = cli.Merge(ctx, userInfo.JSON200.Name, repo.JSON201.Name, mergeRequestResult.JSON201.Sequence, api.MergeJSONRequestBody{
Msg: "merge it",
})
log.Println("merge success")
//read the file from main branch
resp, err = cli.GetObject(ctx, userInfo.JSON200.Name, repo.JSON201.Name, &api.GetObjectParams{
Type: api.RefTypeBranch,
RefName: "main",
Path: "a.bin",
Range: nil,
})
if err != nil {
return err
}
result, err := api.ParseGetObjectResponse(resp)
log.Println("get object content ", string(result.Body))
return nil
}(context.Background()))
}