Skip to content

Commit

Permalink
restructure repo dir (#4293)
Browse files Browse the repository at this point in the history
* restructure repo dir

* fix path in test
  • Loading branch information
dogancanbakir authored Oct 30, 2023
1 parent 83681fb commit 1d0fbc0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 15 additions & 2 deletions pkg/external/customtemplates/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/external/customtemplates/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 1d0fbc0

Please sign in to comment.