Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ GitHub Apps authentication can be provided by the [ghinstallation](https://githu
package.

```go
import "github.com/bradleyfalzon/ghinstallation"
import (
"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/v41/github"
)

func main() {
// Wrap the shared transport for use with the integration ID 1 authenticating with installation ID 99.
Expand All @@ -137,6 +140,9 @@ func main() {
}
```

*Note*: In order to interact with certain APIs, for example writing a file to a repo, one must generate an installation token
using the installation ID of the GitHub app and authenticate with the OAuth method mentioned above. See the examples.

### Rate Limiting ###

GitHub imposes a rate limit on all API clients. Unauthenticated clients are
Expand Down
95 changes: 95 additions & 0 deletions example/newfilewithappauth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2021 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// newfilewithappauth demonstrates the functionality of GitHub's app authentication
// methods by fetching an installation access token and reauthenticating to GitHub
// with OAuth configurations.
package main

import (
"context"
"io/ioutil"
"log"
"net/http"
"time"

"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/v41/github"
"golang.org/x/oauth2"
)

func main() {
const gitHost = "https://git.api.com"

privatePem, err := ioutil.ReadFile("path/to/pem")
if err != nil {
log.Fatalf("failed to read pem: %v", err)
}

itr, err := ghinstallation.NewAppsTransport(http.DefaultTransport, 10, privatePem)
if err != nil {
log.Fatalf("faild to create app transport: %v\n", err)
}
itr.BaseURL = gitHost

//create git client with app transport
client, err := github.NewEnterpriseClient(
gitHost,
gitHost,
&http.Client{
Transport: itr,
Timeout: time.Second * 30,
})
if err != nil {
log.Fatalf("faild to create git client for app: %v\n", err)
}

installations, _, err := client.Apps.ListInstallations(context.Background(), &github.ListOptions{})
if err != nil {
log.Fatalf("failed to list installations: %v\n", err)
}

//capture our installationId for our app
//we need this for the access token
var installID int64
for _, val := range installations {
installID = val.GetID()
}

token, _, err := client.Apps.CreateInstallationToken(
context.Background(),
installID,
&github.InstallationTokenOptions{})
if err != nil {
log.Fatalf("failed to create installation token: %v\n", err)
}

ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token.GetToken()},
)
oAuthClient := oauth2.NewClient(context.Background(), ts)

//create new git hub client with accessToken
apiClient, err := github.NewEnterpriseClient(gitHost, gitHost, oAuthClient)
if err != nil {
log.Fatalf("failed to create new git client with token: %v\n", err)
}

_, resp, err := apiClient.Repositories.CreateFile(
context.Background(),
"repoOwner",
"sample-repo",
"example/foo.txt",
&github.RepositoryContentFileOptions{
Content: []byte("foo"),
Message: github.String("sample commit"),
SHA: nil,
})
if err != nil {
log.Fatalf("failed to create new file: %v\n", err)
}

log.Printf("file written status code: %v", resp.StatusCode)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/google/go-github/v41

require (
github.com/bradleyfalzon/ghinstallation v1.1.1
github.com/google/go-cmp v0.5.6
github.com/google/go-querystring v1.1.0
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
Expand All @@ -9,7 +10,9 @@ require (
)

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/google/go-github/v29 v29.0.2 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
github.com/bradleyfalzon/ghinstallation v1.1.1 h1:pmBXkxgM1WeF8QYvDLT5kuQiHMcmf+X015GI0KM/E3I=
github.com/bradleyfalzon/ghinstallation v1.1.1/go.mod h1:vyCmHTciHx/uuyN82Zc3rXN3X2KTK8nUTCrTMwAhcug=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-github/v29 v29.0.2 h1:opYN6Wc7DOz7Ku3Oh4l7prmkOMwEcQxpFtxdU8N8Pts=
github.com/google/go-github/v29 v29.0.2/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
Expand All @@ -26,5 +35,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=