Skip to content

Commit

Permalink
fix: changes to make it work
Browse files Browse the repository at this point in the history
  • Loading branch information
omri2001 committed Dec 2, 2024
1 parent 7f8b51c commit e04c95b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 63 deletions.
12 changes: 1 addition & 11 deletions cmd/piper/piper.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package main

import (
rookout "github.com/Rookout/GoSDK"
"github.com/quickube/piper/pkg/clients"
"github.com/quickube/piper/pkg/conf"
"github.com/quickube/piper/pkg/event_handler"
"github.com/quickube/piper/pkg/git_provider"
"github.com/quickube/piper/pkg/server"
"github.com/quickube/piper/pkg/utils"
workflowHandler "github.com/quickube/piper/pkg/workflow_handler"
"golang.org/x/net/context"
"log"
Expand All @@ -21,14 +19,6 @@ func main() {
log.Panicf("failed to load the configuration for Piper, error: %v", err)
}

if cfg.RookoutConfig.Token != "" {
labels := utils.StringToMap(cfg.RookoutConfig.Labels)
err = rookout.Start(rookout.RookOptions{Token: cfg.RookoutConfig.Token, Labels: labels})
if err != nil {
log.Printf("failed to start Rookout, error: %v\n", err)
}
}

err = cfg.WorkflowsConfig.WorkflowsSpecLoad("/piper-config/..data")
if err != nil {
log.Panicf("Failed to load workflow spec configuration, error: %v", err)
Expand All @@ -53,4 +43,4 @@ func main() {
defer stop()
event_handler.Start(ctx, stop, cfg, globalClients)
server.Start(ctx, stop, cfg, globalClients)
}
}
19 changes: 17 additions & 2 deletions pkg/event_handler/github_event_notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ var workflowTranslationToBitbucketMap = map[string]string{
"Error": "STOPPED",
}

var workflowTranslationToGitlabMap = map[string]string{
"": "pending",
"Pending": "pending",
"Running": "pending",
"Succeeded": "passed",
"Failed": "failed",
"Error": "failed",
}

type githubNotifier struct {
cfg *conf.GlobalConfig
clients *clients.Clients
Expand Down Expand Up @@ -72,13 +81,19 @@ func (gn *githubNotifier) translateWorkflowStatus(status string, workflowName st
case "github":
result, ok := workflowTranslationToGithubMap[status]
if !ok {
return "", fmt.Errorf("failed to translate workflow status to github stasuts for %s status: %s", workflowName, status)
return "", fmt.Errorf("failed to translate workflow status to github status for %s status: %s", workflowName, status)
}
return result, nil
case "bitbucket":
result, ok := workflowTranslationToBitbucketMap[status]
if !ok {
return "", fmt.Errorf("failed to translate workflow status to bitbucket stasuts for %s status: %s", workflowName, status)
return "", fmt.Errorf("failed to translate workflow status to bitbucket status for %s status: %s", workflowName, status)
}
return result, nil
case "gitlab":
result, ok := workflowTranslationToGitlabMap[status]
if !ok {
return "", fmt.Errorf("failed to translate workflow status to gitlab status for %s status: %s", workflowName, status)
}
return result, nil
}
Expand Down
57 changes: 39 additions & 18 deletions pkg/git_provider/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,42 @@ func NewGitlabClient(cfg *conf.GlobalConfig) (Client, error) {
if cfg.GitProviderConfig.Url != "" {
options = append(options, gitlab.WithBaseURL(cfg.GitProviderConfig.Url))
}

client, err := gitlab.NewClient(cfg.GitProviderConfig.Token, options...)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to authenticate user: %v", err)
}

err = ValidateGitlabPermissions(ctx, client, cfg)
if err != nil {
return nil, fmt.Errorf("failed to validate permissions: %v", err)
}

group, resp, err := client.Groups.GetGroup(cfg.GitProviderConfig.OrgName, nil)
if err != nil {
return nil, fmt.Errorf("failed to get organization: %v", err)
}
orgType := "Organization"
if cfg.GitProviderConfig.OrgLevelWebhook {
group, resp, err := client.Groups.GetGroup(cfg.GitProviderConfig.OrgName, nil)
if err != nil {
return nil, fmt.Errorf("failed to get organization: %v", err)
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to get organization data %s", resp.Status)
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to get organization data %s", resp.Status)
cfg.GitProviderConfig.OrgID = int64(group.ID)
} else {
orgType = "User"
user, resp, err := client.Users.CurrentUser()
if err != nil {
return nil, fmt.Errorf("failed to get user info: %v", err)
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to get user data %s", resp.Status)
}
cfg.GitProviderConfig.OrgID = int64(user.ID)
}

cfg.GitProviderConfig.OrgID = int64(group.ID)
log.Printf("Org ID is: %d\n", cfg.OrgID)
log.Printf("%s ID is: %d\n", orgType, cfg.OrgID)

return &GitlabClientImpl{
client: client,
Expand All @@ -61,8 +75,8 @@ func (c *GitlabClientImpl) ListFiles(ctx *context.Context, repo string, branch s
Ref: &branch,
Path: &path}

projectName := fmt.Sprintf("%s/%s", c.cfg.GitProviderConfig.OrgName, repo)
dirFiles, resp, err := c.client.Repositories.ListTree(projectName, opt, gitlab.WithContext(*ctx))
projectId := GetProjectId(ctx, c, &repo)
dirFiles, resp, err := c.client.Repositories.ListTree(projectId, opt, gitlab.WithContext(*ctx))

if err != nil {
return nil, err
Expand All @@ -78,8 +92,9 @@ func (c *GitlabClientImpl) ListFiles(ctx *context.Context, repo string, branch s

func (c *GitlabClientImpl) GetFile(ctx *context.Context, repo string, branch string, path string) (*CommitFile, error) {
var commitFile CommitFile
projectId := GetProjectId(ctx, c, &repo)

fileContent, resp, err := c.client.RepositoryFiles.GetFile(repo, path, &gitlab.GetFileOptions{Ref: &branch}, gitlab.WithContext(*ctx))
fileContent, resp, err := c.client.RepositoryFiles.GetFile(projectId, path, &gitlab.GetFileOptions{Ref: &branch}, gitlab.WithContext(*ctx))
if err != nil {
return &commitFile, err
}
Expand Down Expand Up @@ -158,7 +173,10 @@ func (c *GitlabClientImpl) SetWebhook(ctx *context.Context, repo *string) (*Hook
log.Printf("edited webhook for %s: %s\n", c.cfg.GitProviderConfig.OrgName, gitlabHook.URL)
}
} else {
respHook, ok := IsProjectWebhookEnabled(ctx, c, *repo)
projectId := GetProjectId(ctx, c, repo)
log.Printf("project id is: %d\n", projectId)
respHook, ok := IsProjectWebhookEnabled(ctx, c, projectId)

if !ok {
addProjectHookOpts := gitlab.AddProjectHookOptions{
URL: gitlab.Ptr(c.cfg.GitProviderConfig.WebhookURL),
Expand All @@ -168,8 +186,7 @@ func (c *GitlabClientImpl) SetWebhook(ctx *context.Context, repo *string) (*Hook
ReleasesEvents: gitlab.Ptr(true),
TagPushEvents: gitlab.Ptr(true),
}

gitlabHook, resp, err := c.client.Projects.AddProjectHook(*repo, &addProjectHookOpts, gitlab.WithContext(*ctx))
gitlabHook, resp, err := c.client.Projects.AddProjectHook(projectId, &addProjectHookOpts, gitlab.WithContext(*ctx))
if err != nil {
return nil, err
}
Expand All @@ -186,7 +203,7 @@ func (c *GitlabClientImpl) SetWebhook(ctx *context.Context, repo *string) (*Hook
ReleasesEvents: gitlab.Ptr(true),
TagPushEvents: gitlab.Ptr(true),
}
gitlabHook, resp, err := c.client.Projects.EditProjectHook(*repo, respHook.ID, &editProjectHookOpts, gitlab.WithContext(*ctx))
gitlabHook, resp, err := c.client.Projects.EditProjectHook(projectId, respHook.ID, &editProjectHookOpts, gitlab.WithContext(*ctx))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -218,7 +235,11 @@ func (c *GitlabClientImpl) UnsetWebhook(ctx *context.Context, hook *HookWithStat
resp, err := c.client.Projects.DeleteProjectHook(*hook.RepoName, int(hook.HookID), gitlab.WithContext(*ctx))

if err != nil {
return fmt.Errorf("failed to delete project level webhhok for %s, API call returned %d. %s", *hook.RepoName, resp.StatusCode, err)
statusCode := "unknown"
if resp != nil {
statusCode = fmt.Sprintf("%d", resp.StatusCode)
}
return fmt.Errorf("failed to delete project level webhhok for %s, API call returned %s. %s", *hook.RepoName, statusCode, err)
}

if resp.StatusCode != 204 {
Expand Down
48 changes: 16 additions & 32 deletions pkg/git_provider/gitlab_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"fmt"
"io"
"log"
"net/http"
"strings"

Expand All @@ -20,11 +21,12 @@ func ValidateGitlabPermissions(ctx context.Context, client *gitlab.Client, cfg *
repoAdminScopes := []string{"api"}
repoGranularScopes := []string{"write_repository", "read_api"}

scopes, err := getGitlabScopes(ctx, client)

token, _, err := client.PersonalAccessTokens.GetSinglePersonalAccessToken()
if err != nil {
return fmt.Errorf("failed to get scopes: %v", err)
}
scopes := token.Scopes

if len(scopes) == 0 {
return fmt.Errorf("permissions error: no scopes found for the gitlab client")
}
Expand All @@ -39,33 +41,6 @@ func ValidateGitlabPermissions(ctx context.Context, client *gitlab.Client, cfg *
return fmt.Errorf("permissions error: %v is not a valid scope for the project level permissions", scopes)
}

func getGitlabScopes(ctx context.Context, client *gitlab.Client) ([]string, error) {

user, resp, err := client.Users.CurrentUser(gitlab.WithContext(ctx))
if err != nil {
return nil, err
}
if resp.StatusCode == 400 {
return nil, err
}
a := gitlab.ListPersonalAccessTokensOptions{
UserID: &user.ID,
}
accessTokens, resp, err := client.PersonalAccessTokens.ListPersonalAccessTokens(&a)
fmt.Println(accessTokens)
if err != nil {
return nil, err
}
if resp.StatusCode == 400 {
return nil, err
}

scopes := accessTokens[0].Scopes
fmt.Println("Gitlab Token Scopes are:", scopes)

return scopes, nil
}

func IsGroupWebhookEnabled(ctx *context.Context, c *GitlabClientImpl) (*gitlab.GroupHook, bool) {
emptyHook := gitlab.GroupHook{}
hooks, resp, err := c.client.Groups.ListGroupHooks(c.cfg.GitProviderConfig.OrgName, nil, gitlab.WithContext(*ctx))
Expand All @@ -86,10 +61,10 @@ func IsGroupWebhookEnabled(ctx *context.Context, c *GitlabClientImpl) (*gitlab.G
return &emptyHook, false
}

func IsProjectWebhookEnabled(ctx *context.Context, c *GitlabClientImpl, project string) (*gitlab.ProjectHook, bool) {
func IsProjectWebhookEnabled(ctx *context.Context, c *GitlabClientImpl, projectId int) (*gitlab.ProjectHook, bool) {
emptyHook := gitlab.ProjectHook{}
projectFullName := fmt.Sprintf("%s/%s", c.cfg.GitProviderConfig.OrgName, project)
hooks, resp, err := c.client.Projects.ListProjectHooks(projectFullName, nil, gitlab.WithContext(*ctx))

hooks, resp, err := c.client.Projects.ListProjectHooks(projectId, nil, gitlab.WithContext(*ctx))
if err != nil {
return &emptyHook, false
}
Expand Down Expand Up @@ -117,6 +92,15 @@ func ExtractLabelsId(labels []*gitlab.EventLabel) []string {
return returnLabelsList
}

func GetProjectId(ctx *context.Context, c *GitlabClientImpl, repo *string) int {
projectFullName := fmt.Sprintf("%s/%s", c.cfg.GitProviderConfig.OrgName, *repo)
IProject, _, err := c.client.Projects.GetProject(projectFullName, nil, gitlab.WithContext(*ctx))
if err != nil {
log.Fatalf("Failed to get project: %v", err)
}
return IProject.ID
}

func ValidatePayload(r *http.Request, secret []byte) ([]byte, error) {
payload, err := io.ReadAll(r.Body)
if err != nil {
Expand Down

0 comments on commit e04c95b

Please sign in to comment.