diff --git a/pkg/external/customtemplates/github.go b/pkg/external/customtemplates/github.go index ee30f34565..dc75c5e94e 100644 --- a/pkg/external/customtemplates/github.go +++ b/pkg/external/customtemplates/github.go @@ -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 diff --git a/pkg/external/customtemplates/github_test.go b/pkg/external/customtemplates/github_test.go index ab070c7a34..2f55800be6 100644 --- a/pkg/external/customtemplates/github_test.go +++ b/pkg/external/customtemplates/github_test.go @@ -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") }