|
|
|
@@ -18,49 +18,38 @@ type GithubPRCreator struct {
|
|
|
|
func NewGithubPRCreator(config map[string]string) *GithubPRCreator {
|
|
|
|
func NewGithubPRCreator(config map[string]string) *GithubPRCreator {
|
|
|
|
client := github.NewClient(nil).WithAuthToken(config["token"])
|
|
|
|
client := github.NewClient(nil).WithAuthToken(config["token"])
|
|
|
|
|
|
|
|
|
|
|
|
defaultBranch := config["defaultBranch"]
|
|
|
|
|
|
|
|
if defaultBranch == "" {
|
|
|
|
|
|
|
|
defaultBranch = "main" // Default to "main" if not specified
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return &GithubPRCreator{
|
|
|
|
return &GithubPRCreator{
|
|
|
|
client: client,
|
|
|
|
client: client,
|
|
|
|
token: config["token"],
|
|
|
|
token: config["token"],
|
|
|
|
repository: config["repository"],
|
|
|
|
repository: config["repository"],
|
|
|
|
owner: config["owner"],
|
|
|
|
owner: config["owner"],
|
|
|
|
customActionName: config["customActionName"],
|
|
|
|
customActionName: config["customActionName"],
|
|
|
|
defaultBranch: defaultBranch,
|
|
|
|
defaultBranch: config["defaultBranch"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (g *GithubPRCreator) createOrUpdateBranch(ctx context.Context, branchName string, owner string, repository string) error {
|
|
|
|
func (g *GithubPRCreator) createOrUpdateBranch(ctx context.Context, branchName string) error {
|
|
|
|
// Get the latest commit SHA from the default branch
|
|
|
|
// Get the latest commit SHA from the default branch
|
|
|
|
ref, _, err := g.client.Git.GetRef(ctx, owner, repository, "refs/heads/"+g.defaultBranch)
|
|
|
|
ref, _, err := g.client.Git.GetRef(ctx, g.owner, g.repository, "refs/heads/"+g.defaultBranch)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get reference for default branch %s: %w", g.defaultBranch, err)
|
|
|
|
return fmt.Errorf("failed to get reference: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Try to get the branch if it exists
|
|
|
|
// Try to get the branch if it exists
|
|
|
|
_, resp, err := g.client.Git.GetRef(ctx, owner, repository, "refs/heads/"+branchName)
|
|
|
|
_, resp, err := g.client.Git.GetRef(ctx, g.owner, g.repository, "refs/heads/"+branchName)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
if resp == nil {
|
|
|
|
// If branch doesn't exist, create it
|
|
|
|
return fmt.Errorf("failed to check branch existence: %w", err)
|
|
|
|
if resp != nil && resp.StatusCode == 404 {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If branch doesn't exist (404), create it
|
|
|
|
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
|
|
|
|
newRef := &github.Reference{
|
|
|
|
newRef := &github.Reference{
|
|
|
|
Ref: github.String("refs/heads/" + branchName),
|
|
|
|
Ref: github.String("refs/heads/" + branchName),
|
|
|
|
Object: &github.GitObject{SHA: ref.Object.SHA},
|
|
|
|
Object: &github.GitObject{SHA: ref.Object.SHA},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, _, err = g.client.Git.CreateRef(ctx, owner, repository, newRef)
|
|
|
|
_, _, err = g.client.Git.CreateRef(ctx, g.owner, g.repository, newRef)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create branch: %w", err)
|
|
|
|
return fmt.Errorf("failed to create branch: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// For other errors, return the error
|
|
|
|
|
|
|
|
return fmt.Errorf("failed to check branch existence: %w", err)
|
|
|
|
return fmt.Errorf("failed to check branch existence: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -69,7 +58,7 @@ func (g *GithubPRCreator) createOrUpdateBranch(ctx context.Context, branchName s
|
|
|
|
Ref: github.String("refs/heads/" + branchName),
|
|
|
|
Ref: github.String("refs/heads/" + branchName),
|
|
|
|
Object: &github.GitObject{SHA: ref.Object.SHA},
|
|
|
|
Object: &github.GitObject{SHA: ref.Object.SHA},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, _, err = g.client.Git.UpdateRef(ctx, owner, repository, updateRef, true)
|
|
|
|
_, _, err = g.client.Git.UpdateRef(ctx, g.owner, g.repository, updateRef, true)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update branch: %w", err)
|
|
|
|
return fmt.Errorf("failed to update branch: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -77,10 +66,10 @@ func (g *GithubPRCreator) createOrUpdateBranch(ctx context.Context, branchName s
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (g *GithubPRCreator) createOrUpdateFile(ctx context.Context, branch string, filePath string, content string, message string, owner string, repository string) error {
|
|
|
|
func (g *GithubPRCreator) createOrUpdateFile(ctx context.Context, branch string, filePath string, content string, message string) error {
|
|
|
|
// Get the current file content if it exists
|
|
|
|
// Get the current file content if it exists
|
|
|
|
var sha *string
|
|
|
|
var sha *string
|
|
|
|
fileContent, _, _, err := g.client.Repositories.GetContents(ctx, owner, repository, filePath, &github.RepositoryContentGetOptions{
|
|
|
|
fileContent, _, _, err := g.client.Repositories.GetContents(ctx, g.owner, g.repository, filePath, &github.RepositoryContentGetOptions{
|
|
|
|
Ref: branch,
|
|
|
|
Ref: branch,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err == nil && fileContent != nil {
|
|
|
|
if err == nil && fileContent != nil {
|
|
|
|
@@ -88,7 +77,7 @@ func (g *GithubPRCreator) createOrUpdateFile(ctx context.Context, branch string,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create or update the file
|
|
|
|
// Create or update the file
|
|
|
|
_, _, err = g.client.Repositories.CreateFile(ctx, owner, repository, filePath, &github.RepositoryContentFileOptions{
|
|
|
|
_, _, err = g.client.Repositories.CreateFile(ctx, g.owner, g.repository, filePath, &github.RepositoryContentFileOptions{
|
|
|
|
Message: &message,
|
|
|
|
Message: &message,
|
|
|
|
Content: []byte(content),
|
|
|
|
Content: []byte(content),
|
|
|
|
Branch: &branch,
|
|
|
|
Branch: &branch,
|
|
|
|
@@ -129,14 +118,14 @@ func (g *GithubPRCreator) Run(ctx context.Context, params types.ActionParams) (t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create or update branch
|
|
|
|
// Create or update branch
|
|
|
|
err = g.createOrUpdateBranch(ctx, result.Branch, result.Owner, result.Repository)
|
|
|
|
err = g.createOrUpdateBranch(ctx, result.Branch)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return types.ActionResult{}, fmt.Errorf("failed to create/update branch: %w", err)
|
|
|
|
return types.ActionResult{}, fmt.Errorf("failed to create/update branch: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create or update files
|
|
|
|
// Create or update files
|
|
|
|
for _, file := range result.Files {
|
|
|
|
for _, file := range result.Files {
|
|
|
|
err = g.createOrUpdateFile(ctx, result.Branch, file.Path, file.Content, fmt.Sprintf("Update %s", file.Path), result.Owner, result.Repository)
|
|
|
|
err = g.createOrUpdateFile(ctx, result.Branch, file.Path, file.Content, fmt.Sprintf("Update %s", file.Path))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return types.ActionResult{}, fmt.Errorf("failed to update file %s: %w", file.Path, err)
|
|
|
|
return types.ActionResult{}, fmt.Errorf("failed to update file %s: %w", file.Path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|