Explorar el Código

update go-github to v41; wip on environments endpoints

Alexander Belanger hace 4 años
padre
commit
ae9e992564

+ 1 - 1
api/server/authz/git_installation.go

@@ -6,7 +6,7 @@ import (
 	"fmt"
 	"net/http"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"golang.org/x/oauth2"
 	"gorm.io/gorm"
 

+ 29 - 1
api/server/handlers/environment/create.go

@@ -15,6 +15,8 @@ import (
 	"github.com/porter-dev/porter/internal/integrations/ci/actions"
 	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/internal/models/integrations"
+	"github.com/porter-dev/porter/internal/oauth"
+	"golang.org/x/oauth2"
 )
 
 type CreateEnvironmentHandler struct {
@@ -59,7 +61,7 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 	}
 
 	// write Github actions files to the repo
-	client, err := getGithubClientFromEnvironment(c.Config(), env)
+	client, err := getGithubClientFromUserWithOAuth(c.Config(), user)
 
 	if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
@@ -123,3 +125,29 @@ func getGithubClientFromEnvironment(config *config.Config, env *models.Environme
 
 	return github.NewClient(&http.Client{Transport: itr}), nil
 }
+
+func getGithubClientFromUserWithOAuth(config *config.Config, user *models.User) (*github.Client, error) {
+	oauthInt, err := config.Repo.GithubAppOAuthIntegration().ReadGithubAppOauthIntegration(user.GithubAppIntegrationID)
+
+	if err != nil {
+		return nil, err
+	}
+
+	_, _, err = oauth.GetAccessToken(
+		oauthInt.SharedOAuthModel,
+		&config.GithubAppConf.Config,
+		oauth.MakeUpdateGithubAppOauthIntegrationFunction(oauthInt, config.Repo),
+	)
+
+	if err != nil {
+		return nil, err
+	}
+
+	client := github.NewClient(config.GithubAppConf.Client(oauth2.NoContext, &oauth2.Token{
+		AccessToken:  string(oauthInt.AccessToken),
+		RefreshToken: string(oauthInt.RefreshToken),
+		TokenType:    "Bearer",
+	}))
+
+	return client, nil
+}

+ 1 - 1
api/server/handlers/gitinstallation/get_accounts.go

@@ -6,7 +6,7 @@ import (
 	"sort"
 	"time"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"

+ 1 - 1
api/server/handlers/gitinstallation/get_buildpack.go

@@ -6,7 +6,7 @@ import (
 	"net/http"
 	"sync"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"

+ 1 - 1
api/server/handlers/gitinstallation/get_contents.go

@@ -4,7 +4,7 @@ import (
 	"context"
 	"net/http"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"

+ 1 - 1
api/server/handlers/gitinstallation/get_procfile.go

@@ -6,7 +6,7 @@ import (
 	"regexp"
 	"strings"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"

+ 3 - 1
api/server/handlers/gitinstallation/get_tarball_url.go

@@ -4,7 +4,7 @@ import (
 	"context"
 	"net/http"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
@@ -53,6 +53,7 @@ func (c *GithubGetTarballURLHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
 		owner,
 		name,
 		branch,
+		false,
 	)
 
 	if err != nil {
@@ -68,6 +69,7 @@ func (c *GithubGetTarballURLHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
 		&github.RepositoryContentGetOptions{
 			Ref: *branchResp.Commit.SHA,
 		},
+		false,
 	)
 
 	if err != nil {

+ 1 - 1
api/server/handlers/gitinstallation/helpers.go

@@ -5,7 +5,7 @@ import (
 	"net/url"
 
 	"github.com/bradleyfalzon/ghinstallation"
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
 	"github.com/porter-dev/porter/api/server/shared/config"

+ 1 - 1
api/server/handlers/gitinstallation/list.go

@@ -4,7 +4,7 @@ import (
 	"context"
 	"net/http"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"

+ 10 - 6
api/server/handlers/gitinstallation/list_branches.go

@@ -5,7 +5,7 @@ import (
 	"net/http"
 	"sync"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
@@ -43,8 +43,10 @@ func (c *GithubListBranchesHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 	}
 
 	// List all branches for a specified repo
-	allBranches, resp, err := client.Repositories.ListBranches(context.Background(), owner, name, &github.ListOptions{
-		PerPage: 100,
+	allBranches, resp, err := client.Repositories.ListBranches(context.Background(), owner, name, &github.BranchListOptions{
+		ListOptions: github.ListOptions{
+			PerPage: 100,
+		},
 	})
 
 	if err != nil {
@@ -63,9 +65,11 @@ func (c *GithubListBranchesHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		defer wg.Done()
 
 		for cp < numPages {
-			opts := &github.ListOptions{
-				Page:    cp,
-				PerPage: 100,
+			opts := &github.BranchListOptions{
+				ListOptions: github.ListOptions{
+					Page:    cp,
+					PerPage: 100,
+				},
 			}
 
 			branches, _, err := client.Repositories.ListBranches(context.Background(), owner, name, opts)

+ 5 - 3
api/server/handlers/gitinstallation/list_repos.go

@@ -5,7 +5,7 @@ import (
 	"net/http"
 	"sync"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
@@ -41,13 +41,15 @@ func (c *GithubListReposHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 		PerPage: 100,
 	}
 
-	allRepos, resp, err := client.Apps.ListRepos(context.Background(), opt)
+	repoList, resp, err := client.Apps.ListRepos(context.Background(), opt)
 
 	if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		return
 	}
 
+	allRepos := repoList.Repositories
+
 	// make workers to get pages concurrently
 	const WCOUNT = 5
 	numPages := resp.LastPage + 1
@@ -74,7 +76,7 @@ func (c *GithubListReposHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 			}
 
 			mu.Lock()
-			allRepos = append(allRepos, repos...)
+			allRepos = append(allRepos, repos.Repositories...)
 			mu.Unlock()
 
 			cp += WCOUNT

+ 1 - 1
api/server/handlers/gitinstallation/webhook.go

@@ -8,7 +8,7 @@ import (
 	"net/http"
 	"strings"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"

+ 1 - 1
api/server/handlers/user/github_callback.go

@@ -10,7 +10,7 @@ import (
 	"golang.org/x/oauth2"
 	"gorm.io/gorm"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authn"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"

+ 1 - 1
api/server/shared/config/loader/loader.go

@@ -163,7 +163,7 @@ func (e *EnvConfigLoader) LoadConfig() (res *config.Config, err error) {
 			res.GithubAppConf = oauth.NewGithubAppClient(&oauth.Config{
 				ClientID:     sc.GithubAppClientID,
 				ClientSecret: sc.GithubAppClientSecret,
-				Scopes:       []string{"read:user"},
+				Scopes:       []string{"read:user", "repo"},
 				BaseURL:      sc.ServerURL,
 			}, sc.GithubAppName, sc.GithubAppWebhookSecret, sc.GithubAppSecretPath, AppID)
 		}

+ 1 - 1
cli/cmd/github/release.go

@@ -12,7 +12,7 @@ import (
 	"runtime"
 	"strings"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 )
 
 // ZIPReleaseGetter retrieves a release from Github in ZIP format and downloads it

+ 0 - 1
go.mod

@@ -24,7 +24,6 @@ require (
 	github.com/go-playground/validator/v10 v10.3.0
 	github.com/go-redis/redis/v8 v8.11.0
 	github.com/go-test/deep v1.0.7
-	github.com/google/go-github v17.0.0+incompatible
 	github.com/google/go-github/v29 v29.0.3 // indirect
 	github.com/google/go-github/v41 v41.0.0
 	github.com/gorilla/schema v1.2.0

+ 0 - 2
go.sum

@@ -643,8 +643,6 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
 github.com/google/go-containerregistry v0.4.1/go.mod h1:Ct15B4yir3PLOP5jsy0GNeYVaIZs/MK/Jz5any1wFW0=
 github.com/google/go-containerregistry v0.5.1 h1:/+mFTs4AlwsJ/mJe8NDtKb7BxLtbZFpcn8vDsneEkwQ=
 github.com/google/go-containerregistry v0.5.1/go.mod h1:Ct15B4yir3PLOP5jsy0GNeYVaIZs/MK/Jz5any1wFW0=
-github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
-github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
 github.com/google/go-github/v29 v29.0.2/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E=
 github.com/google/go-github/v29 v29.0.3 h1:IktKCTwU//aFHnpA+2SLIi7Oo9uhAzgsdZNbcAqhgdc=
 github.com/google/go-github/v29 v29.0.3/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E=

+ 1 - 1
internal/integrations/buildpacks/go.go

@@ -3,7 +3,7 @@ package buildpacks
 import (
 	"sync"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 )
 
 type goRuntime struct {

+ 1 - 1
internal/integrations/buildpacks/nodejs.go

@@ -8,7 +8,7 @@ import (
 	"sync"
 
 	"github.com/Masterminds/semver/v3"
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 )
 
 var (

+ 1 - 1
internal/integrations/buildpacks/python.go

@@ -4,7 +4,7 @@ import (
 	"strings"
 	"sync"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 )
 
 type pythonRuntime struct {

+ 1 - 1
internal/integrations/buildpacks/ruby.go

@@ -8,7 +8,7 @@ import (
 	"strings"
 	"sync"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 )
 
 type rubyRuntime struct {

+ 1 - 1
internal/integrations/buildpacks/shared.go

@@ -1,7 +1,7 @@
 package buildpacks
 
 import (
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 )
 
 const (

+ 24 - 23
internal/integrations/ci/actions/preview.go

@@ -3,6 +3,7 @@ package actions
 import (
 	"context"
 	"fmt"
+	"net/http"
 	"strings"
 
 	"github.com/google/go-github/v41/github"
@@ -21,31 +22,31 @@ type EnvOpts struct {
 
 func SetupEnv(opts *EnvOpts) ([]byte, error) {
 	// create Github environment if it does not exist
-	// _, resp, err := opts.Client.Repositories.GetEnvironment(
-	// 	context.Background(),
-	// 	opts.GitRepoOwner,
-	// 	opts.GitRepoName,
-	// 	opts.EnvironmentName,
-	// )
-
-	// if resp.StatusCode == http.StatusNotFound {
-	// 	_, _, err := opts.Client.Repositories.CreateUpdateEnvironment(
-	// 		context.Background(),
-	// 		opts.GitRepoOwner,
-	// 		opts.GitRepoName,
-	// 		opts.EnvironmentName,
-	// 		&github.CreateUpdateEnvironment{},
-	// 	)
-
-	// 	if err != nil {
-	// 		return nil, err
-	// 	}
-	// } else if err != nil {
-	// 	return nil, err
-	// }
+	_, resp, err := opts.Client.Repositories.GetEnvironment(
+		context.Background(),
+		opts.GitRepoOwner,
+		opts.GitRepoName,
+		opts.EnvironmentName,
+	)
+
+	if resp.StatusCode == http.StatusNotFound {
+		_, _, err := opts.Client.Repositories.CreateUpdateEnvironment(
+			context.Background(),
+			opts.GitRepoOwner,
+			opts.GitRepoName,
+			opts.EnvironmentName,
+			&github.CreateUpdateEnvironment{},
+		)
+
+		if err != nil {
+			return nil, err
+		}
+	} else if err != nil {
+		return nil, err
+	}
 
 	// create porter token secret
-	err := createGithubSecret(
+	err = createGithubSecret(
 		opts.Client,
 		getPorterTokenSecretName(opts.ProjectID),
 		opts.PorterToken,