Debian Codegraph @

github/examples_test.go

raw
1 // Copyright 2016 The go-github AUTHORS. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 // These examples are inlined in godoc.
7
8 package github_test
9
10 import (
11 "context"
12 "fmt"
13 "log"
14
15 "github.com/google/go-github/v60/github"
16 )
17
18 func ExampleMarkdownService_Render() {
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.Render(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_ListByUser() {
53 client := github.NewClient(nil)
54
55 user := "willnorris"
56 opt := &github.RepositoryListByUserOptions{Type: "owner", Sort: "updated", Direction: "desc"}
57
58 ctx := context.Background()
59 repos, _, err := client.Repositories.ListByUser(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 // In this example we're creating a new file in a repository using the
69 // Contents API. Only 1 file per commit can be managed through that API.
70
71 // Note that authentication is needed here as you are performing a modification
72 // so you will need to modify the example to provide an oauth client to
73 // github.NewClient() instead of nil. See the following documentation for more
74 // information on how to authenticate with the client:
75 // https://godoc.org/github.com/google/go-github/github#hdr-Authentication
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 // Note: the file needs to be absent from the repository as you are not
82 // specifying a SHA reference here.
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("[email protected]")},
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 // Process users...
110 }
111 }
112
113 func ExamplePullRequestsService_Create() {
114 // In this example we're creating a PR and displaying the HTML url at the end.
115
116 // Note that authentication is needed here as you are performing a modification
117 // so you will need to modify the example to provide an oauth client to
118 // github.NewClient() instead of nil. See the following documentation for more
119 // information on how to authenticate with the client:
120 // https://godoc.org/github.com/google/go-github/github#hdr-Authentication
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 // This example shows how to get a team ID corresponding to a given team name.
143
144 // Note that authentication is needed here as you are performing a lookup on
145 // an organization's administrative configuration, so you will need to modify
146 // the example to provide an oauth client to github.NewClient() instead of nil.
147 // See the following documentation for more information on how to authenticate
148 // with the client:
149 // https://godoc.org/github.com/google/go-github/github#hdr-Authentication
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 }