Просмотр исходного кода

Get contents of package.json seprately

Mohammed Nafees 4 лет назад
Родитель
Сommit
4491db1c15

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

@@ -74,8 +74,8 @@ func (c *GithubGetBuildpackHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 
 	res := &types.GetBuildpackResponse{}
 
-	nodeRuntime := buildpacks.NewAPINodeRuntime()
-	config := nodeRuntime.Detect(directoryContents)
+	nodeRuntime := buildpacks.NewAPINodeRuntime(client)
+	config := nodeRuntime.Detect(directoryContents, owner, name, repoContentOptions)
 	if config != nil {
 		res.Name = "Node.js"
 		res.Runtime = config["runtime"].(string)

+ 20 - 14
internal/integrations/buildpacks/api_nodejs.go

@@ -1,6 +1,7 @@
 package buildpacks
 
 import (
+	"context"
 	"encoding/json"
 	"strings"
 	"sync"
@@ -9,11 +10,14 @@ import (
 )
 
 type apiNodeRuntime struct {
-	wg sync.WaitGroup
+	ghClient *github.Client
+	wg       sync.WaitGroup
 }
 
-func NewAPINodeRuntime() *apiNodeRuntime {
-	return &apiNodeRuntime{}
+func NewAPINodeRuntime(client *github.Client) *apiNodeRuntime {
+	return &apiNodeRuntime{
+		ghClient: client,
+	}
 }
 
 func (runtime *apiNodeRuntime) detectYarn(results chan struct {
@@ -99,7 +103,11 @@ func (runtime *apiNodeRuntime) detectStandalone(results chan struct {
 	runtime.wg.Done()
 }
 
-func (runtime *apiNodeRuntime) Detect(directoryContent []*github.RepositoryContent) map[string]interface{} {
+func (runtime *apiNodeRuntime) Detect(
+	directoryContent []*github.RepositoryContent,
+	owner string, name string,
+	repoContentOptions github.RepositoryContentGetOptions,
+) map[string]interface{} {
 	results := make(chan struct {
 		string
 		bool
@@ -124,15 +132,13 @@ func (runtime *apiNodeRuntime) Detect(directoryContent []*github.RepositoryConte
 	if atLeastOne {
 		if detected[yarn] || detected[npm] {
 			// it is safe to assume that the project contains a package.json
-			var packageJSONRef *github.RepositoryContent
-			for i := 0; i < len(directoryContent); i++ {
-				name := directoryContent[i].GetName()
-				if name == "package.json" {
-					packageJSONRef = directoryContent[i]
-					break
-				}
-			}
-			content, err := packageJSONRef.GetContent()
+			fileContent, _, _, err := runtime.ghClient.Repositories.GetContents(
+				context.Background(),
+				owner,
+				name,
+				"package.json",
+				&repoContentOptions,
+			)
 			if err != nil {
 				// FIXME: log somewhere
 				return nil
@@ -143,7 +149,7 @@ func (runtime *apiNodeRuntime) Detect(directoryContent []*github.RepositoryConte
 					Node string `json:"node"`
 				} `json:"engines"`
 			}
-			err = json.NewDecoder(strings.NewReader(content)).Decode(&packageJSON)
+			err = json.NewDecoder(strings.NewReader(*fileContent.Content)).Decode(&packageJSON)
 			if err != nil {
 				// FIXME: log somewhere
 				return nil

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

@@ -53,5 +53,5 @@ type CLIRuntime interface {
 }
 
 type APIRuntime interface {
-	Detect([]*github.RepositoryContent) map[string]interface{}
+	Detect([]*github.RepositoryContent, string, string, github.RepositoryContentGetOptions) map[string]interface{}
 }