| 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 |
"log" |
| 14 |
"net/http" |
| 15 |
"os" |
| 16 |
"time" |
| 17 |
|
| 18 |
"github.com/bradleyfalzon/ghinstallation/v2" |
| 19 |
"github.com/google/go-github/v60/github" |
| 20 |
) |
| 21 |
|
| 22 |
func main() { |
| 23 |
const gitHost = "https://git.api.com" |
| 24 |
|
| 25 |
privatePem, err := os.ReadFile("path/to/pem") |
| 26 |
if err != nil { |
| 27 |
log.Fatalf("failed to read pem: %v", err) |
| 28 |
} |
| 29 |
|
| 30 |
itr, err := ghinstallation.NewAppsTransport(http.DefaultTransport, 10, privatePem) |
| 31 |
if err != nil { |
| 32 |
log.Fatalf("failed to create app transport: %v\n", err) |
| 33 |
} |
| 34 |
itr.BaseURL = gitHost |
| 35 |
|
| 36 |
//create git client with app transport |
| 37 |
client, err := github.NewClient( |
| 38 |
&http.Client{ |
| 39 |
Transport: itr, |
| 40 |
Timeout: time.Second * 30, |
| 41 |
}, |
| 42 |
).WithEnterpriseURLs(gitHost, gitHost) |
| 43 |
|
| 44 |
if err != nil { |
| 45 |
log.Fatalf("failed to create git client for app: %v\n", err) |
| 46 |
} |
| 47 |
|
| 48 |
installations, _, err := client.Apps.ListInstallations(context.Background(), &github.ListOptions{}) |
| 49 |
if err != nil { |
| 50 |
log.Fatalf("failed to list installations: %v\n", err) |
| 51 |
} |
| 52 |
|
| 53 |
//capture our installationId for our app |
| 54 |
//we need this for the access token |
| 55 |
var installID int64 |
| 56 |
for _, val := range installations { |
| 57 |
installID = val.GetID() |
| 58 |
} |
| 59 |
|
| 60 |
token, _, err := client.Apps.CreateInstallationToken( |
| 61 |
context.Background(), |
| 62 |
installID, |
| 63 |
&github.InstallationTokenOptions{}) |
| 64 |
if err != nil { |
| 65 |
log.Fatalf("failed to create installation token: %v\n", err) |
| 66 |
} |
| 67 |
|
| 68 |
apiClient, err := github.NewClient(nil).WithAuthToken( |
| 69 |
token.GetToken(), |
| 70 |
).WithEnterpriseURLs(gitHost, gitHost) |
| 71 |
if err != nil { |
| 72 |
log.Fatalf("failed to create new git client with token: %v\n", err) |
| 73 |
} |
| 74 |
|
| 75 |
_, resp, err := apiClient.Repositories.CreateFile( |
| 76 |
context.Background(), |
| 77 |
"repoOwner", |
| 78 |
"sample-repo", |
| 79 |
"example/foo.txt", |
| 80 |
&github.RepositoryContentFileOptions{ |
| 81 |
Content: []byte("foo"), |
| 82 |
Message: github.String("sample commit"), |
| 83 |
SHA: nil, |
| 84 |
}) |
| 85 |
if err != nil { |
| 86 |
log.Fatalf("failed to create new file: %v\n", err) |
| 87 |
} |
| 88 |
|
| 89 |
log.Printf("file written status code: %v", resp.StatusCode) |
| 90 |
} |