Skip to content

Commit

Permalink
Merge branch 'main' of github.com:l3montree-dev/devguard
Browse files Browse the repository at this point in the history
  • Loading branch information
timbastin committed Dec 13, 2024
2 parents aeaa8c0 + c832e59 commit 920e119
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
15 changes: 12 additions & 3 deletions internal/core/integrations/gitlab_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,22 @@ func (g *gitlabIntegration) AutoSetup(ctx core.Context) error {
return errors.Wrap(err, "could not extract project id from repo id")
}

project, _, err := client.GetProject(ctx.Request().Context(), projectId)
if err != nil {
return errors.Wrap(err, "could not get project")
}
defaultBranch := project.DefaultBranch

//generate a random branch name
branchName := fmt.Sprintf("devguard-autosetup-%s", strconv.Itoa(generateFourDigitNumber()))

projectName, err := g.getRepoNameFromProjectId(ctx, projectId)
if err != nil {
return errors.Wrap(err, "could not get project name")
}

templatePath := getTemplatePath(ctx.QueryParam("scanType"))
err = setupAndPushPipeline(accessToken, gitlabUrl, projectName, templatePath)
err = setupAndPushPipeline(accessToken, gitlabUrl, projectName, templatePath, branchName)
if err != nil {
return errors.Wrap(err, "could not setup and push pipeline")
}
Expand All @@ -448,8 +457,8 @@ func (g *gitlabIntegration) AutoSetup(ctx core.Context) error {

//create a merge request
mr, _, err := client.CreateMergeRequest(ctx.Request().Context(), projectName, &gitlab.CreateMergeRequestOptions{
SourceBranch: gitlab.Ptr("devguard-autosetup"),
TargetBranch: gitlab.Ptr("main"),
SourceBranch: gitlab.Ptr(branchName),
TargetBranch: gitlab.Ptr(defaultBranch),
Title: gitlab.Ptr("Add devguard pipeline template"),
RemoveSourceBranch: gitlab.Ptr(true),
})
Expand Down
16 changes: 13 additions & 3 deletions internal/core/integrations/integration_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import (
"fmt"
"os"
"strings"
"time"

"math/rand"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)

func setupAndPushPipeline(accessToken string, gitlabUrl string, projectName string, templatePath string) error {
func setupAndPushPipeline(accessToken string, gitlabUrl string, projectName string, templatePath string, branchName string) error {
dir, err := os.MkdirTemp("", "repo-clone")
if err != nil {
return fmt.Errorf("could not create temporary directory: %v", err)
Expand All @@ -31,18 +35,19 @@ func setupAndPushPipeline(accessToken string, gitlabUrl string, projectName stri
return fmt.Errorf("could not clone repository: %v", err)
}
err = r.CreateBranch(&config.Branch{
Name: "devguard-autosetup",
Name: branchName,
})
if err != nil {
return fmt.Errorf("could not create branch: %v", err)
}

//go to the branch
w, err := r.Worktree()
if err != nil {
return fmt.Errorf("could not get worktree: %v", err)
}
err = w.Checkout(&git.CheckoutOptions{
Branch: "refs/heads/devguard-autosetup",
Branch: plumbing.NewBranchReferenceName(branchName),
Create: true,
})
if err != nil {
Expand Down Expand Up @@ -140,3 +145,8 @@ func addPipelineTemplate(content []byte, template string) string { //nolint:unus

return fileStr
}

func generateFourDigitNumber() int {
r := rand.New(rand.NewSource(time.Now().UnixNano())) //nolint:gosec // we don't need a secure random number here
return 1000 + r.Intn(9000)
}

0 comments on commit 920e119

Please sign in to comment.