From e9fc0bfa0d3b22091669c705a00feb10f9707f73 Mon Sep 17 00:00:00 2001 From: Pat Alwell Date: Fri, 24 Dec 2021 00:20:30 -0800 Subject: [PATCH 1/4] adding examples for app auth --- README.md | 8 ++- example/newfilewithappauth/main.go | 95 ++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 example/newfilewithappauth/main.go diff --git a/README.md b/README.md index 1eb9054c06d..cc1dbfc1846 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/example/newfilewithappauth/main.go b/example/newfilewithappauth/main.go new file mode 100644 index 00000000000..ea4a1b1eecc --- /dev/null +++ b/example/newfilewithappauth/main.go @@ -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" + "fmt" + "github.com/bradleyfalzon/ghinstallation" + "github.com/google/go-github/v41/github" + "golang.org/x/oauth2" + "io/ioutil" + "net/http" + "time" +) + +func main() { + + const gitHost = "https://git.api.com" + + privatePem, err := ioutil.ReadFile("path/to/pem") + if err != nil { + panic(err) + } + + itr, err := ghinstallation.NewAppsTransport(http.DefaultTransport, 10, privatePem) + if err != nil { + fmt.Printf("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 { + fmt.Printf("faild to create git client for app: %v\n", err) + } + + installations, _, err := client.Apps.ListInstallations(context.Background(), &github.ListOptions{}) + if err != nil { + fmt.Printf("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 { + fmt.Printf("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 { + fmt.Printf("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 { + fmt.Printf("failed to create new file: %v\n", err) + } + + fmt.Printf("file written: %v", resp.StatusCode) +} From 8bc9d23b26b3068ed7c002553d9bb6d2411fba4a Mon Sep 17 00:00:00 2001 From: Pat Alwell Date: Tue, 28 Dec 2021 16:54:44 -0800 Subject: [PATCH 2/4] mod tidy and tests --- example/newfilewithappauth/main.go | 30 +++++++++++++++--------------- go.mod | 3 +++ go.sum | 10 ++++++++++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/example/newfilewithappauth/main.go b/example/newfilewithappauth/main.go index ea4a1b1eecc..f7dd58388be 100644 --- a/example/newfilewithappauth/main.go +++ b/example/newfilewithappauth/main.go @@ -1,20 +1,20 @@ -//Copyright 2021 The go-github AUTHORS. All rights reserved. +// 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. +// 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. +// 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" - "fmt" "github.com/bradleyfalzon/ghinstallation" "github.com/google/go-github/v41/github" "golang.org/x/oauth2" "io/ioutil" + "log" "net/http" "time" ) @@ -25,12 +25,12 @@ func main() { privatePem, err := ioutil.ReadFile("path/to/pem") if err != nil { - panic(err) + log.Fatalf("failed to read pem: %v", err) } itr, err := ghinstallation.NewAppsTransport(http.DefaultTransport, 10, privatePem) if err != nil { - fmt.Printf("faild to create app transport: %v\n", err) + log.Fatalf("faild to create app transport: %v\n", err) } itr.BaseURL = gitHost @@ -43,12 +43,12 @@ func main() { Timeout: time.Second * 30, }) if err != nil { - fmt.Printf("faild to create git client for app: %v\n", err) + log.Fatalf("faild to create git client for app: %v\n", err) } installations, _, err := client.Apps.ListInstallations(context.Background(), &github.ListOptions{}) if err != nil { - fmt.Printf("failed to list installations: %v\n", err) + log.Fatalf("failed to list installations: %v\n", err) } //capture our installationId for our app @@ -63,7 +63,7 @@ func main() { installID, &github.InstallationTokenOptions{}) if err != nil { - fmt.Printf("failed to create installation token: %v\n", err) + log.Fatalf("failed to create installation token: %v\n", err) } ts := oauth2.StaticTokenSource( @@ -74,7 +74,7 @@ func main() { //create new git hub client with accessToken apiClient, err := github.NewEnterpriseClient(gitHost, gitHost, oAuthClient) if err != nil { - fmt.Printf("failed to create new git client with token: %v\n", err) + log.Fatalf("failed to create new git client with token: %v\n", err) } _, resp, err := apiClient.Repositories.CreateFile( @@ -88,8 +88,8 @@ func main() { SHA: nil, }) if err != nil { - fmt.Printf("failed to create new file: %v\n", err) + log.Fatalf("failed to create new file: %v\n", err) } - fmt.Printf("file written: %v", resp.StatusCode) + log.Printf("file written status code: %v", resp.StatusCode) } diff --git a/go.mod b/go.mod index 238a7a61692..efe02c76944 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 7e85c44db1f..b96190d3b04 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= From 1e97d601a49b8051678947bb29ddfb91ed8f16c5 Mon Sep 17 00:00:00 2001 From: Pat Alwell Date: Tue, 28 Dec 2021 17:09:34 -0800 Subject: [PATCH 3/4] linter and fmt --- example/newfilewithappauth/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/example/newfilewithappauth/main.go b/example/newfilewithappauth/main.go index f7dd58388be..61f0838dc8a 100644 --- a/example/newfilewithappauth/main.go +++ b/example/newfilewithappauth/main.go @@ -10,17 +10,17 @@ package main import ( "context" - "github.com/bradleyfalzon/ghinstallation" - "github.com/google/go-github/v41/github" - "golang.org/x/oauth2" "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") From 600c17ccb17f358de43b02266fb716e439368e0a Mon Sep 17 00:00:00 2001 From: Pat Alwell Date: Wed, 29 Dec 2021 09:42:21 -0800 Subject: [PATCH 4/4] bumping versions --- README.md | 2 +- example/newfilewithappauth/main.go | 2 +- go.mod | 6 +++--- go.sum | 16 ++++++---------- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index cc1dbfc1846..184fc1048b1 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ package. ```go import ( - "github.com/bradleyfalzon/ghinstallation" + "github.com/bradleyfalzon/ghinstallation/v2" "github.com/google/go-github/v41/github" ) diff --git a/example/newfilewithappauth/main.go b/example/newfilewithappauth/main.go index 61f0838dc8a..ef124c55617 100644 --- a/example/newfilewithappauth/main.go +++ b/example/newfilewithappauth/main.go @@ -15,7 +15,7 @@ import ( "net/http" "time" - "github.com/bradleyfalzon/ghinstallation" + "github.com/bradleyfalzon/ghinstallation/v2" "github.com/google/go-github/v41/github" "golang.org/x/oauth2" ) diff --git a/go.mod b/go.mod index efe02c76944..fba98e39f5f 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/google/go-github/v41 require ( - github.com/bradleyfalzon/ghinstallation v1.1.1 + github.com/bradleyfalzon/ghinstallation/v2 v2.0.3 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 @@ -10,9 +10,9 @@ require ( ) require ( - github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect + github.com/golang-jwt/jwt/v4 v4.0.0 // indirect github.com/golang/protobuf v1.3.2 // indirect - github.com/google/go-github/v29 v29.0.2 // indirect + github.com/google/go-github/v39 v39.0.0 // 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 diff --git a/go.sum b/go.sum index b96190d3b04..95ca91e2822 100644 --- a/go.sum +++ b/go.sum @@ -1,23 +1,20 @@ -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/bradleyfalzon/ghinstallation/v2 v2.0.3 h1:ywF/8q+GVpvlsEuvRb1SGSDQDUxntW1d4kFu/9q/YAE= +github.com/bradleyfalzon/ghinstallation/v2 v2.0.3/go.mod h1:tlgi+JWCXnKFx/Y4WtnDbZEINo31N5bcvnCoqieefmk= +github.com/golang-jwt/jwt/v4 v4.0.0 h1:RAqyYixv1p7uEnocuy8P1nru5wprCh/MH2BIlW5z5/o= +github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= 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-github/v39 v39.0.0 h1:pygGA5ySwxEez1N39GnDauD0PaWWuGgayudyZAc941s= +github.com/google/go-github/v39 v39.0.0/go.mod h1:C1s8C5aCC9L+JXIYpJM5GYytdX52vC1bLvHEF1IhBrE= 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= @@ -35,6 +32,5 @@ 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=