github.com/google/go-github/v33@v33.0.0/github/examples_test.go (about) 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/v33/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 output, _, err := client.Markdown(context.Background(), input, opt) 25 if err != nil { 26 fmt.Println(err) 27 } 28 29 fmt.Println(output) 30 } 31 32 func ExampleRepositoriesService_GetReadme() { 33 client := github.NewClient(nil) 34 35 readme, _, err := client.Repositories.GetReadme(context.Background(), "google", "go-github", nil) 36 if err != nil { 37 fmt.Println(err) 38 return 39 } 40 41 content, err := readme.GetContent() 42 if err != nil { 43 fmt.Println(err) 44 return 45 } 46 47 fmt.Printf("google/go-github README:\n%v\n", content) 48 } 49 50 func ExampleRepositoriesService_List() { 51 client := github.NewClient(nil) 52 53 user := "willnorris" 54 opt := &github.RepositoryListOptions{Type: "owner", Sort: "updated", Direction: "desc"} 55 56 repos, _, err := client.Repositories.List(context.Background(), user, opt) 57 if err != nil { 58 fmt.Println(err) 59 } 60 61 fmt.Printf("Recently updated repositories by %q: %v", user, github.Stringify(repos)) 62 } 63 64 func ExampleRepositoriesService_CreateFile() { 65 // In this example we're creating a new file in a repository using the 66 // Contents API. Only 1 file per commit can be managed through that API. 67 68 // Note that authentication is needed here as you are performing a modification 69 // so you will need to modify the example to provide an oauth client to 70 // github.NewClient() instead of nil. See the following documentation for more 71 // information on how to authenticate with the client: 72 // https://godoc.org/github.com/google/go-github/github#hdr-Authentication 73 client := github.NewClient(nil) 74 75 ctx := context.Background() 76 fileContent := []byte("This is the content of my file\nand the 2nd line of it") 77 78 // Note: the file needs to be absent from the repository as you are not 79 // specifying a SHA reference here. 80 opts := &github.RepositoryContentFileOptions{ 81 Message: github.String("This is my commit message"), 82 Content: fileContent, 83 Branch: github.String("master"), 84 Committer: &github.CommitAuthor{Name: github.String("FirstName LastName"), Email: github.String("user@example.com")}, 85 } 86 _, _, err := client.Repositories.CreateFile(ctx, "myOrganization", "myRepository", "myNewFile.md", opts) 87 if err != nil { 88 fmt.Println(err) 89 return 90 } 91 } 92 93 func ExampleUsersService_ListAll() { 94 client := github.NewClient(nil) 95 opts := &github.UserListOptions{} 96 for { 97 users, _, err := client.Users.ListAll(context.Background(), opts) 98 if err != nil { 99 log.Fatalf("error listing users: %v", err) 100 } 101 if len(users) == 0 { 102 break 103 } 104 opts.Since = *users[len(users)-1].ID 105 // Process users... 106 } 107 } 108 109 func ExamplePullRequestsService_Create() { 110 // In this example we're creating a PR and displaying the HTML url at the end. 111 112 // Note that authentication is needed here as you are performing a modification 113 // so you will need to modify the example to provide an oauth client to 114 // github.NewClient() instead of nil. See the following documentation for more 115 // information on how to authenticate with the client: 116 // https://godoc.org/github.com/google/go-github/github#hdr-Authentication 117 client := github.NewClient(nil) 118 119 newPR := &github.NewPullRequest{ 120 Title: github.String("My awesome pull request"), 121 Head: github.String("branch_to_merge"), 122 Base: github.String("master"), 123 Body: github.String("This is the description of the PR created with the package `github.com/google/go-github/github`"), 124 MaintainerCanModify: github.Bool(true), 125 } 126 127 pr, _, err := client.PullRequests.Create(context.Background(), "myOrganization", "myRepository", newPR) 128 if err != nil { 129 fmt.Println(err) 130 return 131 } 132 133 fmt.Printf("PR created: %s\n", pr.GetHTMLURL()) 134 } 135 136 func ExampleTeamsService_ListTeams() { 137 // This example shows how to get a team ID corresponding to a given team name. 138 139 // Note that authentication is needed here as you are performing a lookup on 140 // an organization's administrative configuration, so you will need to modify 141 // the example to provide an oauth client to github.NewClient() instead of nil. 142 // See the following documentation for more information on how to authenticate 143 // with the client: 144 // https://godoc.org/github.com/google/go-github/github#hdr-Authentication 145 client := github.NewClient(nil) 146 147 teamName := "Developers team" 148 ctx := context.Background() 149 opts := &github.ListOptions{} 150 151 for { 152 teams, resp, err := client.Teams.ListTeams(ctx, "myOrganization", opts) 153 if err != nil { 154 fmt.Println(err) 155 return 156 } 157 for _, t := range teams { 158 if t.GetName() == teamName { 159 fmt.Printf("Team %q has ID %d\n", teamName, t.GetID()) 160 return 161 } 162 } 163 if resp.NextPage == 0 { 164 break 165 } 166 opts.Page = resp.NextPage 167 } 168 169 fmt.Printf("Team %q was not found\n", teamName) 170 }