()
| 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", err) |
| 33 | } |
| 34 | itr.BaseURL = gitHost |
| 35 | |
| 36 | // create git client with app transport |
| 37 | client, err := github.NewClient(github.WithHTTPClient(&http.Client{ |
| 38 | Transport: itr, |
| 39 | Timeout: time.Second * 30, |
| 40 | }), github.WithEnterpriseURLs(gitHost, gitHost)) |
| 41 | if err != nil { |
| 42 | log.Fatalf("failed to create git client for app: %v", err) |
| 43 | } |
| 44 | |
| 45 | installations, _, err := client.Apps.ListInstallations(context.Background(), &github.ListOptions{}) |
| 46 | if err != nil { |
| 47 | log.Fatalf("failed to list installations: %v", err) |
| 48 | } |
| 49 | |
| 50 | // capture our installationId for our app |
| 51 | // we need this for the access token |
| 52 | var installID int64 |
| 53 | for _, val := range installations { |
| 54 | installID = val.GetID() |
| 55 | } |
| 56 | |
| 57 | token, _, err := client.Apps.CreateInstallationToken( |
| 58 | context.Background(), |
| 59 | installID, |
| 60 | &github.InstallationTokenOptions{}) |
| 61 | if err != nil { |
| 62 | log.Fatalf("failed to create installation token: %v", err) |
| 63 | } |
| 64 | |
| 65 | apiClient, err := github.NewClient(github.WithAuthToken(token.GetToken()), github.WithEnterpriseURLs(gitHost, gitHost)) |
| 66 | if err != nil { |
| 67 | log.Fatalf("failed to create new git client with token: %v", err) |
| 68 | } |
| 69 | |
| 70 | _, resp, err := apiClient.Repositories.CreateFile( |
| 71 | context.Background(), |
| 72 | "repoOwner", |
| 73 | "sample-repo", |
| 74 | "example/foo.txt", |
| 75 | &github.RepositoryContentFileOptions{ |
| 76 | Content: []byte("foo"), |
| 77 | Message: github.Ptr("sample commit"), |
| 78 | SHA: nil, |
| 79 | }) |
nothing calls this directly
no test coverage detected
searching dependent graphs…