|
| 1 | +// Copyright 2021 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 | +// newfilewithappauth demonstrates the functionality of GitHub's app authentication |
| 7 | +// methods by fetching an installation access token and reauthenticating to GitHub |
| 8 | +// with OAuth configurations. |
| 9 | +package main |
| 10 | + |
| 11 | +import ( |
| 12 | + "context" |
| 13 | + "io/ioutil" |
| 14 | + "log" |
| 15 | + "net/http" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/bradleyfalzon/ghinstallation/v2" |
| 19 | + "github.com/google/go-github/v41/github" |
| 20 | + "golang.org/x/oauth2" |
| 21 | +) |
| 22 | + |
| 23 | +func main() { |
| 24 | + const gitHost = "https://git.api.com" |
| 25 | + |
| 26 | + privatePem, err := ioutil.ReadFile("path/to/pem") |
| 27 | + if err != nil { |
| 28 | + log.Fatalf("failed to read pem: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + itr, err := ghinstallation.NewAppsTransport(http.DefaultTransport, 10, privatePem) |
| 32 | + if err != nil { |
| 33 | + log.Fatalf("faild to create app transport: %v\n", err) |
| 34 | + } |
| 35 | + itr.BaseURL = gitHost |
| 36 | + |
| 37 | + //create git client with app transport |
| 38 | + client, err := github.NewEnterpriseClient( |
| 39 | + gitHost, |
| 40 | + gitHost, |
| 41 | + &http.Client{ |
| 42 | + Transport: itr, |
| 43 | + Timeout: time.Second * 30, |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + log.Fatalf("faild to create git client for app: %v\n", err) |
| 47 | + } |
| 48 | + |
| 49 | + installations, _, err := client.Apps.ListInstallations(context.Background(), &github.ListOptions{}) |
| 50 | + if err != nil { |
| 51 | + log.Fatalf("failed to list installations: %v\n", err) |
| 52 | + } |
| 53 | + |
| 54 | + //capture our installationId for our app |
| 55 | + //we need this for the access token |
| 56 | + var installID int64 |
| 57 | + for _, val := range installations { |
| 58 | + installID = val.GetID() |
| 59 | + } |
| 60 | + |
| 61 | + token, _, err := client.Apps.CreateInstallationToken( |
| 62 | + context.Background(), |
| 63 | + installID, |
| 64 | + &github.InstallationTokenOptions{}) |
| 65 | + if err != nil { |
| 66 | + log.Fatalf("failed to create installation token: %v\n", err) |
| 67 | + } |
| 68 | + |
| 69 | + ts := oauth2.StaticTokenSource( |
| 70 | + &oauth2.Token{AccessToken: token.GetToken()}, |
| 71 | + ) |
| 72 | + oAuthClient := oauth2.NewClient(context.Background(), ts) |
| 73 | + |
| 74 | + //create new git hub client with accessToken |
| 75 | + apiClient, err := github.NewEnterpriseClient(gitHost, gitHost, oAuthClient) |
| 76 | + if err != nil { |
| 77 | + log.Fatalf("failed to create new git client with token: %v\n", err) |
| 78 | + } |
| 79 | + |
| 80 | + _, resp, err := apiClient.Repositories.CreateFile( |
| 81 | + context.Background(), |
| 82 | + "repoOwner", |
| 83 | + "sample-repo", |
| 84 | + "example/foo.txt", |
| 85 | + &github.RepositoryContentFileOptions{ |
| 86 | + Content: []byte("foo"), |
| 87 | + Message: github.String("sample commit"), |
| 88 | + SHA: nil, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + log.Fatalf("failed to create new file: %v\n", err) |
| 92 | + } |
| 93 | + |
| 94 | + log.Printf("file written status code: %v", resp.StatusCode) |
| 95 | +} |
0 commit comments