From 70e749b53a9bb3759c751cee87ba6317c2c8b611 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Thu, 17 Apr 2025 23:01:19 +0200 Subject: [PATCH] fix(github*): pass by correctly owner and repository (#54) Signed-off-by: Ettore Di Giacinto --- services/actions/githubprcreator.go | 39 ++++++++++++------- .../actions/githubrepositorygetallcontent.go | 10 ++--- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/services/actions/githubprcreator.go b/services/actions/githubprcreator.go index 909f0e8..a1790c7 100644 --- a/services/actions/githubprcreator.go +++ b/services/actions/githubprcreator.go @@ -18,38 +18,49 @@ type GithubPRCreator struct { func NewGithubPRCreator(config map[string]string) *GithubPRCreator { client := github.NewClient(nil).WithAuthToken(config["token"]) + defaultBranch := config["defaultBranch"] + if defaultBranch == "" { + defaultBranch = "main" // Default to "main" if not specified + } + return &GithubPRCreator{ client: client, token: config["token"], repository: config["repository"], owner: config["owner"], customActionName: config["customActionName"], - defaultBranch: config["defaultBranch"], + defaultBranch: defaultBranch, } } -func (g *GithubPRCreator) createOrUpdateBranch(ctx context.Context, branchName string) error { +func (g *GithubPRCreator) createOrUpdateBranch(ctx context.Context, branchName string, owner string, repository string) error { // Get the latest commit SHA from the default branch - ref, _, err := g.client.Git.GetRef(ctx, g.owner, g.repository, "refs/heads/"+g.defaultBranch) + ref, _, err := g.client.Git.GetRef(ctx, owner, repository, "refs/heads/"+g.defaultBranch) if err != nil { - return fmt.Errorf("failed to get reference: %w", err) + return fmt.Errorf("failed to get reference for default branch %s: %w", g.defaultBranch, err) } // Try to get the branch if it exists - _, resp, err := g.client.Git.GetRef(ctx, g.owner, g.repository, "refs/heads/"+branchName) + _, resp, err := g.client.Git.GetRef(ctx, owner, repository, "refs/heads/"+branchName) if err != nil { - // If branch doesn't exist, create it - if resp != nil && resp.StatusCode == 404 { + if resp == nil { + return fmt.Errorf("failed to check branch existence: %w", err) + } + + // If branch doesn't exist (404), create it + if resp.StatusCode == 404 { newRef := &github.Reference{ Ref: github.String("refs/heads/" + branchName), Object: &github.GitObject{SHA: ref.Object.SHA}, } - _, _, err = g.client.Git.CreateRef(ctx, g.owner, g.repository, newRef) + _, _, err = g.client.Git.CreateRef(ctx, owner, repository, newRef) if err != nil { return fmt.Errorf("failed to create branch: %w", err) } return nil } + + // For other errors, return the error return fmt.Errorf("failed to check branch existence: %w", err) } @@ -58,7 +69,7 @@ func (g *GithubPRCreator) createOrUpdateBranch(ctx context.Context, branchName s Ref: github.String("refs/heads/" + branchName), Object: &github.GitObject{SHA: ref.Object.SHA}, } - _, _, err = g.client.Git.UpdateRef(ctx, g.owner, g.repository, updateRef, true) + _, _, err = g.client.Git.UpdateRef(ctx, owner, repository, updateRef, true) if err != nil { return fmt.Errorf("failed to update branch: %w", err) } @@ -66,10 +77,10 @@ func (g *GithubPRCreator) createOrUpdateBranch(ctx context.Context, branchName s return nil } -func (g *GithubPRCreator) createOrUpdateFile(ctx context.Context, branch string, filePath string, content string, message string) error { +func (g *GithubPRCreator) createOrUpdateFile(ctx context.Context, branch string, filePath string, content string, message string, owner string, repository string) error { // Get the current file content if it exists var sha *string - fileContent, _, _, err := g.client.Repositories.GetContents(ctx, g.owner, g.repository, filePath, &github.RepositoryContentGetOptions{ + fileContent, _, _, err := g.client.Repositories.GetContents(ctx, owner, repository, filePath, &github.RepositoryContentGetOptions{ Ref: branch, }) if err == nil && fileContent != nil { @@ -77,7 +88,7 @@ func (g *GithubPRCreator) createOrUpdateFile(ctx context.Context, branch string, } // Create or update the file - _, _, err = g.client.Repositories.CreateFile(ctx, g.owner, g.repository, filePath, &github.RepositoryContentFileOptions{ + _, _, err = g.client.Repositories.CreateFile(ctx, owner, repository, filePath, &github.RepositoryContentFileOptions{ Message: &message, Content: []byte(content), Branch: &branch, @@ -118,14 +129,14 @@ func (g *GithubPRCreator) Run(ctx context.Context, params types.ActionParams) (t } // Create or update branch - err = g.createOrUpdateBranch(ctx, result.Branch) + err = g.createOrUpdateBranch(ctx, result.Branch, result.Owner, result.Repository) if err != nil { return types.ActionResult{}, fmt.Errorf("failed to create/update branch: %w", err) } // Create or update files for _, file := range result.Files { - err = g.createOrUpdateFile(ctx, result.Branch, file.Path, file.Content, fmt.Sprintf("Update %s", file.Path)) + err = g.createOrUpdateFile(ctx, result.Branch, file.Path, file.Content, fmt.Sprintf("Update %s", file.Path), result.Owner, result.Repository) if err != nil { return types.ActionResult{}, fmt.Errorf("failed to update file %s: %w", file.Path, err) } diff --git a/services/actions/githubrepositorygetallcontent.go b/services/actions/githubrepositorygetallcontent.go index 2cb844f..0dd386e 100644 --- a/services/actions/githubrepositorygetallcontent.go +++ b/services/actions/githubrepositorygetallcontent.go @@ -28,11 +28,11 @@ func NewGithubRepositoryGetAllContent(config map[string]string) *GithubRepositor } } -func (g *GithubRepositoryGetAllContent) getContentRecursively(ctx context.Context, path string) (string, error) { +func (g *GithubRepositoryGetAllContent) getContentRecursively(ctx context.Context, path string, owner string, repository string) (string, error) { var result strings.Builder // Get content at the current path - _, directoryContent, _, err := g.client.Repositories.GetContents(ctx, g.owner, g.repository, path, nil) + _, directoryContent, _, err := g.client.Repositories.GetContents(ctx, owner, repository, path, nil) if err != nil { return "", fmt.Errorf("error getting content at path %s: %w", path, err) } @@ -41,14 +41,14 @@ func (g *GithubRepositoryGetAllContent) getContentRecursively(ctx context.Contex for _, item := range directoryContent { if item.GetType() == "dir" { // Recursively get content for subdirectories - subContent, err := g.getContentRecursively(ctx, item.GetPath()) + subContent, err := g.getContentRecursively(ctx, item.GetPath(), owner, repository) if err != nil { return "", err } result.WriteString(subContent) } else if item.GetType() == "file" { // Get file content - fileContent, _, _, err := g.client.Repositories.GetContents(ctx, g.owner, g.repository, item.GetPath(), nil) + fileContent, _, _, err := g.client.Repositories.GetContents(ctx, owner, repository, item.GetPath(), nil) if err != nil { return "", fmt.Errorf("error getting file content for %s: %w", item.GetPath(), err) } @@ -89,7 +89,7 @@ func (g *GithubRepositoryGetAllContent) Run(ctx context.Context, params types.Ac result.Path = "." } - content, err := g.getContentRecursively(ctx, result.Path) + content, err := g.getContentRecursively(ctx, result.Path, result.Owner, result.Repository) if err != nil { return types.ActionResult{}, err }