restructure repo dir (#4293)

* restructure repo dir

* fix path in test
dev
Dogan Can Bakir 2023-10-30 18:30:51 +03:00 committed by GitHub
parent 83681fb308
commit 1d0fbc0b62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
fileutil "github.com/projectdiscovery/utils/file"
folderutil "github.com/projectdiscovery/utils/folder"
"golang.org/x/oauth2"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)
@ -85,10 +86,22 @@ func NewGitHubProviders(options *types.Options) ([]*customTemplateGitHubRepo, er
githubToken: options.GitHubToken,
}
providers = append(providers, customTemplateRepo)
customTemplateRepo.restructureRepoDir()
}
return providers, nil
}
func (customTemplateRepo *customTemplateGitHubRepo) restructureRepoDir() {
customGitHubTemplatesDirectory := config.DefaultConfig.CustomGitHubTemplatesDirectory
oldRepoClonePath := filepath.Join(customGitHubTemplatesDirectory, customTemplateRepo.reponame+"-"+customTemplateRepo.owner)
newRepoClonePath := customTemplateRepo.getLocalRepoClonePath(customGitHubTemplatesDirectory)
if fileutil.FolderExists(oldRepoClonePath) && !fileutil.FolderExists(newRepoClonePath) {
_ = folderutil.SyncDirectory(oldRepoClonePath, newRepoClonePath)
}
}
// getOwnerAndRepo returns the owner, repo, err from the given string
// e.g., it takes input projectdiscovery/nuclei-templates and
// returns owner => projectdiscovery, repo => nuclei-templates
@ -154,9 +167,9 @@ func (ctr *customTemplateGitHubRepo) pullChanges(repoPath, githubToken string) e
return nil
}
// All Custom github repos are cloned in the format of 'reponame-owner' for uniqueness
// All Custom github repos are cloned in the format of 'owner/reponame' for uniqueness
func (ctr *customTemplateGitHubRepo) getLocalRepoClonePath(downloadPath string) string {
return filepath.Join(downloadPath, ctr.reponame+"-"+ctr.owner)
return filepath.Join(downloadPath, ctr.owner, ctr.reponame)
}
// returns the auth object with username and github token as password

View File

@ -30,6 +30,6 @@ func TestDownloadCustomTemplatesFromGitHub(t *testing.T) {
ctm.Download(context.Background())
require.DirExists(t, filepath.Join(templatesDirectory, "github", "nuclei-templates-projectdiscovery"), "cloned directory does not exists")
require.DirExists(t, filepath.Join(templatesDirectory, "github", "nuclei-templates-ehsandeep"), "cloned directory does not exists")
require.DirExists(t, filepath.Join(templatesDirectory, "github", "projectdiscovery", "nuclei-templates"), "cloned directory does not exists")
require.DirExists(t, filepath.Join(templatesDirectory, "github", "ehsandeep", "nuclei-templates"), "cloned directory does not exists")
}