1
2
3
4
5
6
7
8 package github_test
9
10 import (
11 "context"
12 "fmt"
13 "log"
14
15 "github.com/google/go-github/v45/github"
16 )
17
18 func ExampleClient_Markdown() {
19 client := github.NewClient(nil)
20
21 input := "# heading #\n\nLink to issue #1"
22 opt := &github.MarkdownOptions{Mode: "gfm", Context: "google/go-github"}
23
24 ctx := context.Background()
25 output, _, err := client.Markdown(ctx, input, opt)
26 if err != nil {
27 fmt.Println(err)
28 }
29
30 fmt.Println(output)
31 }
32
33 func ExampleRepositoriesService_GetReadme() {
34 client := github.NewClient(nil)
35
36 ctx := context.Background()
37 readme, _, err := client.Repositories.GetReadme(ctx, "google", "go-github", nil)
38 if err != nil {
39 fmt.Println(err)
40 return
41 }
42
43 content, err := readme.GetContent()
44 if err != nil {
45 fmt.Println(err)
46 return
47 }
48
49 fmt.Printf("google/go-github README:\n%v\n", content)
50 }
51
52 func ExampleRepositoriesService_List() {
53 client := github.NewClient(nil)
54
55 user := "willnorris"
56 opt := &github.RepositoryListOptions{Type: "owner", Sort: "updated", Direction: "desc"}
57
58 ctx := context.Background()
59 repos, _, err := client.Repositories.List(ctx, user, opt)
60 if err != nil {
61 fmt.Println(err)
62 }
63
64 fmt.Printf("Recently updated repositories by %q: %v", user, github.Stringify(repos))
65 }
66
67 func ExampleRepositoriesService_CreateFile() {
68
69
70
71
72
73
74
75
76 client := github.NewClient(nil)
77
78 ctx := context.Background()
79 fileContent := []byte("This is the content of my file\nand the 2nd line of it")
80
81
82
83 opts := &github.RepositoryContentFileOptions{
84 Message: github.String("This is my commit message"),
85 Content: fileContent,
86 Branch: github.String("master"),
87 Committer: &github.CommitAuthor{Name: github.String("FirstName LastName"), Email: github.String("user@example.com")},
88 }
89 _, _, err := client.Repositories.CreateFile(ctx, "myOrganization", "myRepository", "myNewFile.md", opts)
90 if err != nil {
91 fmt.Println(err)
92 return
93 }
94 }
95
96 func ExampleUsersService_ListAll() {
97 client := github.NewClient(nil)
98 opts := &github.UserListOptions{}
99 for {
100 ctx := context.Background()
101 users, _, err := client.Users.ListAll(ctx, opts)
102 if err != nil {
103 log.Fatalf("error listing users: %v", err)
104 }
105 if len(users) == 0 {
106 break
107 }
108 opts.Since = *users[len(users)-1].ID
109
110 }
111 }
112
113 func ExamplePullRequestsService_Create() {
114
115
116
117
118
119
120
121 client := github.NewClient(nil)
122
123 newPR := &github.NewPullRequest{
124 Title: github.String("My awesome pull request"),
125 Head: github.String("branch_to_merge"),
126 Base: github.String("master"),
127 Body: github.String("This is the description of the PR created with the package `github.com/google/go-github/github`"),
128 MaintainerCanModify: github.Bool(true),
129 }
130
131 ctx := context.Background()
132 pr, _, err := client.PullRequests.Create(ctx, "myOrganization", "myRepository", newPR)
133 if err != nil {
134 fmt.Println(err)
135 return
136 }
137
138 fmt.Printf("PR created: %s\n", pr.GetHTMLURL())
139 }
140
141 func ExampleTeamsService_ListTeams() {
142
143
144
145
146
147
148
149
150 client := github.NewClient(nil)
151
152 teamName := "Developers team"
153 ctx := context.Background()
154 opts := &github.ListOptions{}
155
156 for {
157 teams, resp, err := client.Teams.ListTeams(ctx, "myOrganization", opts)
158 if err != nil {
159 fmt.Println(err)
160 return
161 }
162 for _, t := range teams {
163 if t.GetName() == teamName {
164 fmt.Printf("Team %q has ID %d\n", teamName, t.GetID())
165 return
166 }
167 }
168 if resp.NextPage == 0 {
169 break
170 }
171 opts.Page = resp.NextPage
172 }
173
174 fmt.Printf("Team %q was not found\n", teamName)
175 }
176
View as plain text