Skip to content

Commit

Permalink
Restructure source code (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverdlov93 authored Aug 9, 2023
1 parent c4e03f1 commit 6f91475
Show file tree
Hide file tree
Showing 235 changed files with 530 additions and 559 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ jobs:
with:
go-version: 1.20.x

# Generate mocks
- name: Generate mocks
run: go generate ./...

- name: Run Gosec Security Scanner
uses: securego/gosec@master
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/frogbot-scan-and-fix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ permissions:
pull-requests: write
security-events: write
jobs:
create-fix-pull-requests:
scan-repository:
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:

# Run tests
- name: Tests
run: go test ./... -v -race -timeout 30m -cover -coverprofile=covprofile -covermode=atomic
run: go test -p 1 ./... -v -race -timeout 30m -cover -coverprofile=covprofile -covermode=atomic
env:
JF_URL: ${{ secrets.PLATFORM_URL }}
JF_ACCESS_TOKEN: ${{ secrets.PLATFORM_ADMIN_TOKEN }}
Expand Down
4 changes: 2 additions & 2 deletions action/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ class Utils {
});
}
/**
* Execute frogbot create-fix-pull-requests command.
* Execute frogbot scan-repository command.
*/
static execCreateFixPullRequests() {
return __awaiter(this, void 0, void 0, function* () {
let res = yield (0, exec_1.exec)(Utils.getExecutableName(), ['create-fix-pull-requests']);
let res = yield (0, exec_1.exec)(Utils.getExecutableName(), ['scan-repository']);
if (res !== core.ExitCode.Success) {
throw new Error('Frogbot exited with exit code ' + res);
}
Expand Down
4 changes: 2 additions & 2 deletions action/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ export class Utils {
}

/**
* Execute frogbot create-fix-pull-requests command.
* Execute frogbot scan-repository command.
*/
public static async execCreateFixPullRequests() {
let res: number = await exec(Utils.getExecutableName(), ['create-fix-pull-requests']);
let res: number = await exec(Utils.getExecutableName(), ['scan-repository']);
if (res !== core.ExitCode.Success) {
throw new Error('Frogbot exited with exit code ' + res);
}
Expand Down
98 changes: 50 additions & 48 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package commands
import (
"errors"
"fmt"
"github.com/jfrog/frogbot/commands/scanpullrequest"
"github.com/jfrog/frogbot/commands/scanrepository"
"github.com/jfrog/frogbot/commands/utils"
"github.com/jfrog/froggit-go/vcsclient"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
Expand All @@ -17,16 +19,57 @@ type FrogbotCommand interface {
Run(config utils.RepoAggregator, client vcsclient.VcsClient) error
}

func GetCommands() []*clitool.Command {
return []*clitool.Command{
{
Name: utils.ScanPullRequest,
Aliases: []string{"spr"},
Usage: "Scans a pull request with JFrog Xray for security vulnerabilities.",
Action: func(ctx *clitool.Context) error {
return Exec(&scanpullrequest.ScanPullRequestCmd{}, ctx.Command.Name)
},
Flags: []clitool.Flag{},
},
{
Name: utils.ScanRepository,
Aliases: []string{"cfpr", "create-fix-pull-requests"},
Usage: "Scan the current branch and create pull requests with fixes if needed",
Action: func(ctx *clitool.Context) error {
return Exec(&scanrepository.ScanRepositoryCmd{}, ctx.Command.Name)
},
Flags: []clitool.Flag{},
},
{
Name: utils.ScanAllPullRequests,
Aliases: []string{"sprs", "scan-pull-requests"},
Usage: "Scans all the open pull requests within a single or multiple repositories with JFrog Xray for security vulnerabilities",
Action: func(ctx *clitool.Context) error {
return Exec(&scanpullrequest.ScanAllPullRequestsCmd{}, ctx.Command.Name)
},
Flags: []clitool.Flag{},
},
{
Name: utils.ScanMultipleRepositories,
Aliases: []string{"scan-and-fix-repos", "safr"},
Usage: "Scan single or multiple repositories and create pull requests with fixes if any security vulnerabilities are found",
Action: func(ctx *clitool.Context) error {
return Exec(&scanrepository.ScanMultipleRepositories{}, ctx.Command.Name)
},
Flags: []clitool.Flag{},
},
}
}

func Exec(command FrogbotCommand, commandName string) (err error) {
// Get frogbotUtils that contains the config, server, and VCS client
// Get frogbotDetails that contains the config, server, and VCS client
log.Info("Frogbot version:", utils.FrogbotVersion)
frogbotUtils, err := utils.GetFrogbotDetails(commandName)
frogbotDetails, err := utils.GetFrogbotDetails(commandName)
if err != nil {
return err
}

// Build the server configuration file
originalJfrogHomeDir, tempJFrogHomeDir, err := utils.BuildServerConfigFile(frogbotUtils.ServerDetails)
originalJfrogHomeDir, tempJFrogHomeDir, err := utils.BuildServerConfigFile(frogbotDetails.ServerDetails)
if err != nil {
return err
}
Expand All @@ -36,8 +79,8 @@ func Exec(command FrogbotCommand, commandName string) (err error) {

// Set releases remote repository env if needed
previousReleasesRepoEnv := os.Getenv(coreutils.ReleasesRemoteEnv)
if frogbotUtils.ReleasesRepo != "" {
if err = os.Setenv(coreutils.ReleasesRemoteEnv, fmt.Sprintf("frogbot/%s", frogbotUtils.ReleasesRepo)); err != nil {
if frogbotDetails.ReleasesRepo != "" {
if err = os.Setenv(coreutils.ReleasesRemoteEnv, fmt.Sprintf("frogbot/%s", frogbotDetails.ReleasesRepo)); err != nil {
return
}
defer func() {
Expand All @@ -47,11 +90,11 @@ func Exec(command FrogbotCommand, commandName string) (err error) {

// Send a usage report
usageReportSent := make(chan error)
go utils.ReportUsage(commandName, frogbotUtils.ServerDetails, usageReportSent)
go utils.ReportUsage(commandName, frogbotDetails.ServerDetails, usageReportSent)

// Invoke the command interface
log.Info(fmt.Sprintf("Running Frogbot %q command", commandName))
err = command.Run(frogbotUtils.Repositories, frogbotUtils.GitClient)
err = command.Run(frogbotDetails.Repositories, frogbotDetails.GitClient)

// Wait for a signal, letting us know that the usage reporting is done.
<-usageReportSent
Expand All @@ -61,44 +104,3 @@ func Exec(command FrogbotCommand, commandName string) (err error) {
}
return err
}

func GetCommands() []*clitool.Command {
return []*clitool.Command{
{
Name: utils.ScanPullRequest,
Aliases: []string{"spr"},
Usage: "Scans a pull request with JFrog Xray for security vulnerabilities.",
Action: func(ctx *clitool.Context) error {
return Exec(&ScanPullRequestCmd{}, ctx.Command.Name)
},
Flags: []clitool.Flag{},
},
{
Name: utils.CreateFixPullRequests,
Aliases: []string{"cfpr"},
Usage: "Scan the current branch and create pull requests with fixes if needed",
Action: func(ctx *clitool.Context) error {
return Exec(&CreateFixPullRequestsCmd{}, ctx.Command.Name)
},
Flags: []clitool.Flag{},
},
{
Name: utils.ScanPullRequests,
Aliases: []string{"sprs"},
Usage: "Scans all the open pull requests within a single or multiple repositories with JFrog Xray for security vulnerabilities",
Action: func(ctx *clitool.Context) error {
return Exec(&ScanAllPullRequestsCmd{}, ctx.Command.Name)
},
Flags: []clitool.Flag{},
},
{
Name: utils.ScanAndFixRepos,
Aliases: []string{"safr"},
Usage: "Scan single or multiple repositories and create pull requests with fixes if any security vulnerabilities are found",
Action: func(ctx *clitool.Context) error {
return Exec(&ScanAndFixRepositories{}, ctx.Command.Name)
},
Flags: []clitool.Flag{},
},
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package commands
package scanpullrequest

import (
"context"
Expand All @@ -11,7 +11,7 @@ import (
"github.com/jfrog/froggit-go/vcsclient"
)

var errPullRequestScan = "pull request %d in the %s repository returned the following error: \n%s"
var errPullRequestScan = "pull request #%d scan in the '%s' repository returned the following error:\n%s"

type ScanAllPullRequestsCmd struct {
}
Expand Down
Loading

0 comments on commit 6f91475

Please sign in to comment.