github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/scanrepository/scanmultiplerepositories_test.go (about)

     1  package scanrepository
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"github.com/go-git/go-git/v5/plumbing"
     8  	"github.com/go-git/go-git/v5/plumbing/protocol/packp"
     9  	"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
    10  	"github.com/jfrog/frogbot/utils"
    11  	"github.com/jfrog/froggit-go/vcsclient"
    12  	"github.com/jfrog/froggit-go/vcsutils"
    13  	"github.com/stretchr/testify/assert"
    14  	"net/http"
    15  	"net/http/httptest"
    16  	"os"
    17  	"path/filepath"
    18  	"strings"
    19  	"testing"
    20  )
    21  
    22  var testScanMultipleRepositoriesConfigPath = filepath.Join("..", "testdata", "config", "frogbot-config-scan-multiple-repositories.yml")
    23  var testRepositories = []string{"pip-repo", "npm-repo", "mvn-repo"}
    24  
    25  func TestScanAndFixRepos(t *testing.T) {
    26  	serverParams, restoreEnv := utils.VerifyEnv(t)
    27  	defer restoreEnv()
    28  
    29  	baseWd, err := os.Getwd()
    30  	assert.NoError(t, err)
    31  
    32  	var port string
    33  	server := httptest.NewServer(createScanRepoGitHubHandler(t, &port, nil, testRepositories...))
    34  	defer server.Close()
    35  	port = server.URL[strings.LastIndex(server.URL, ":")+1:]
    36  	client, err := vcsclient.NewClientBuilder(vcsutils.GitHub).ApiEndpoint(server.URL).Token("123456").Build()
    37  	assert.NoError(t, err)
    38  
    39  	gitTestParams := utils.Git{
    40  		GitProvider: vcsutils.GitHub,
    41  		RepoOwner:   "jfrog",
    42  		VcsInfo: vcsclient.VcsInfo{
    43  			Token:       "123456",
    44  			APIEndpoint: server.URL,
    45  		},
    46  	}
    47  
    48  	configData, err := utils.ReadConfigFromFileSystem(testScanMultipleRepositoriesConfigPath)
    49  	assert.NoError(t, err)
    50  
    51  	testDir, cleanup := utils.CopyTestdataProjectsToTemp(t, "scanmultiplerepositories")
    52  	defer func() {
    53  		assert.NoError(t, os.Chdir(baseWd))
    54  		cleanup()
    55  	}()
    56  
    57  	utils.CreateDotGitWithCommit(t, testDir, port, testRepositories...)
    58  	configAggregator, err := utils.BuildRepoAggregator(configData, &gitTestParams, &serverParams, utils.ScanMultipleRepositories)
    59  	assert.NoError(t, err)
    60  
    61  	var cmd = ScanMultipleRepositories{dryRun: true, dryRunRepoPath: testDir}
    62  	assert.NoError(t, cmd.Run(configAggregator, client, utils.MockHasConnection()))
    63  }
    64  
    65  func createScanRepoGitHubHandler(t *testing.T, port *string, response interface{}, projectNames ...string) http.HandlerFunc {
    66  	return func(w http.ResponseWriter, r *http.Request) {
    67  		for _, projectName := range projectNames {
    68  			if r.RequestURI == fmt.Sprintf("/%s/info/refs?service=git-upload-pack", projectName) {
    69  				hash := plumbing.NewHash("5e3021cf22da163f0d312d8fcf299abaa79726fb")
    70  				capabilities := capability.NewList()
    71  				assert.NoError(t, capabilities.Add(capability.SymRef, "HEAD:/refs/heads/master"))
    72  				ar := &packp.AdvRefs{
    73  					References: map[string]plumbing.Hash{
    74  						"refs/heads/master": plumbing.NewHash("5e3021cf22da163f0d312d8fcf299abaa79726fb"),
    75  					},
    76  					Head:         &hash,
    77  					Capabilities: capabilities,
    78  				}
    79  				var buf bytes.Buffer
    80  				assert.NoError(t, ar.Encode(&buf))
    81  				_, err := w.Write(buf.Bytes())
    82  				assert.NoError(t, err)
    83  				w.WriteHeader(http.StatusOK)
    84  				return
    85  			}
    86  			if r.RequestURI == fmt.Sprintf("/repos/jfrog/%s/pulls", projectName) {
    87  				w.WriteHeader(http.StatusOK)
    88  				return
    89  			}
    90  			if r.RequestURI == fmt.Sprintf("/%s", projectName) {
    91  				file, err := os.ReadFile(fmt.Sprintf("%s.tar.gz", projectName))
    92  				assert.NoError(t, err)
    93  				_, err = w.Write(file)
    94  				assert.NoError(t, err)
    95  				return
    96  			}
    97  			if r.RequestURI == fmt.Sprintf("/repos/jfrog/%s/tarball/master", projectName) {
    98  				w.Header().Add("Location", fmt.Sprintf("http://127.0.0.1:%s/%s", *port, projectName))
    99  				w.WriteHeader(http.StatusFound)
   100  				_, err := w.Write([]byte{})
   101  				assert.NoError(t, err)
   102  				return
   103  			}
   104  			if r.RequestURI == fmt.Sprintf("/repos/jfrog/%s/commits?page=1&per_page=%d&sha=master", projectName, vcsutils.NumberOfCommitsToFetch) {
   105  				w.WriteHeader(http.StatusOK)
   106  				rawJson := "[\n  {\n    \"url\": \"https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n    \"sha\": \"6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n    \"node_id\": \"MDY6Q29tbWl0NmRjYjA5YjViNTc4NzVmMzM0ZjYxYWViZWQ2OTVlMmU0MTkzZGI1ZQ==\",\n    \"html_url\": \"https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n    \"comments_url\": \"https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments\",\n    \"commit\": {\n      \"url\": \"https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n      \"author\": {\n        \"name\": \"Monalisa Octocat\",\n        \"email\": \"support@github.com\",\n        \"date\": \"2011-04-14T16:00:49Z\"\n      },\n      \"committer\": {\n        \"name\": \"Monalisa Octocat\",\n        \"email\": \"support@github.com\",\n        \"date\": \"2011-04-14T16:00:49Z\"\n      },\n      \"message\": \"Fix all the bugs\",\n      \"tree\": {\n        \"url\": \"https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n        \"sha\": \"6dcb09b5b57875f334f61aebed695e2e4193db5e\"\n      },\n      \"comment_count\": 0,\n      \"verification\": {\n        \"verified\": false,\n        \"reason\": \"unsigned\",\n        \"signature\": null,\n        \"payload\": null\n      }\n    },\n    \"author\": {\n      \"login\": \"octocat\",\n      \"id\": 1,\n      \"node_id\": \"MDQ6VXNlcjE=\",\n      \"avatar_url\": \"https://github.com/images/error/octocat_happy.gif\",\n      \"gravatar_id\": \"\",\n      \"url\": \"https://api.github.com/users/octocat\",\n      \"html_url\": \"https://github.com/octocat\",\n      \"followers_url\": \"https://api.github.com/users/octocat/followers\",\n      \"following_url\": \"https://api.github.com/users/octocat/following{/other_user}\",\n      \"gists_url\": \"https://api.github.com/users/octocat/gists{/gist_id}\",\n      \"starred_url\": \"https://api.github.com/users/octocat/starred{/owner}{/repo}\",\n      \"subscriptions_url\": \"https://api.github.com/users/octocat/subscriptions\",\n      \"organizations_url\": \"https://api.github.com/users/octocat/orgs\",\n      \"repos_url\": \"https://api.github.com/users/octocat/repos\",\n      \"events_url\": \"https://api.github.com/users/octocat/events{/privacy}\",\n      \"received_events_url\": \"https://api.github.com/users/octocat/received_events\",\n      \"type\": \"User\",\n      \"site_admin\": false\n    },\n    \"committer\": {\n      \"login\": \"octocat\",\n      \"id\": 1,\n      \"node_id\": \"MDQ6VXNlcjE=\",\n      \"avatar_url\": \"https://github.com/images/error/octocat_happy.gif\",\n      \"gravatar_id\": \"\",\n      \"url\": \"https://api.github.com/users/octocat\",\n      \"html_url\": \"https://github.com/octocat\",\n      \"followers_url\": \"https://api.github.com/users/octocat/followers\",\n      \"following_url\": \"https://api.github.com/users/octocat/following{/other_user}\",\n      \"gists_url\": \"https://api.github.com/users/octocat/gists{/gist_id}\",\n      \"starred_url\": \"https://api.github.com/users/octocat/starred{/owner}{/repo}\",\n      \"subscriptions_url\": \"https://api.github.com/users/octocat/subscriptions\",\n      \"organizations_url\": \"https://api.github.com/users/octocat/orgs\",\n      \"repos_url\": \"https://api.github.com/users/octocat/repos\",\n      \"events_url\": \"https://api.github.com/users/octocat/events{/privacy}\",\n      \"received_events_url\": \"https://api.github.com/users/octocat/received_events\",\n      \"type\": \"User\",\n      \"site_admin\": false\n    },\n    \"parents\": [\n      {\n        \"url\": \"https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n        \"sha\": \"6dcb09b5b57875f334f61aebed695e2e4193db5e\"\n      }\n    ]\n  }\n]"
   107  				b := []byte(rawJson)
   108  				_, err := w.Write(b)
   109  				assert.NoError(t, err)
   110  				return
   111  			}
   112  			if r.RequestURI == fmt.Sprintf("/repos/jfrog/%v/code-scanning/sarifs", projectName) {
   113  				w.WriteHeader(http.StatusAccepted)
   114  				rawJson := "{\n  \"id\": \"47177e22-5596-11eb-80a1-c1e54ef945c6\",\n  \"url\": \"https://api.github.com/repos/octocat/hello-world/code-scanning/sarifs/47177e22-5596-11eb-80a1-c1e54ef945c6\"\n}"
   115  				b := []byte(rawJson)
   116  				_, err := w.Write(b)
   117  				assert.NoError(t, err)
   118  				return
   119  			}
   120  			if r.RequestURI == fmt.Sprintf("/repos/jfrog/%s/pulls?state=open", projectName) {
   121  				jsonResponse, err := json.Marshal(response)
   122  				assert.NoError(t, err)
   123  				_, err = w.Write(jsonResponse)
   124  				assert.NoError(t, err)
   125  				return
   126  			}
   127  			if r.RequestURI == fmt.Sprintf("/repos/jfrog/%s", projectName) {
   128  				jsonResponse := `{"id": 1296269,"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5","name": "Hello-World","full_name": "octocat/Hello-World","private": false,"description": "This your first repo!","ssh_url": "git@github.com:octocat/Hello-World.git","clone_url": "https://github.com/octocat/Hello-World.git","visibility": "public"}`
   129  				_, err := w.Write([]byte(jsonResponse))
   130  				assert.NoError(t, err)
   131  				return
   132  			}
   133  		}
   134  	}
   135  }