diff --git a/main.go b/main.go index 11824e1..5685df3 100644 --- a/main.go +++ b/main.go @@ -63,7 +63,7 @@ func ParseActionConfig(ctx githubactions.GitHubContext) (*CreatePrConfig, error) } func main() { - fmt.Println("Starting action CreatePullRequest, main") + fmt.Println("Starting result CreatePullRequest, main") ctx, err := githubactions.Context() if err != nil { githubactions.Fatalf("could not get context: %v", err.Error()) @@ -77,7 +77,7 @@ func main() { githubactions.Fatalf("%v", err.Error()) } - pr, err := createPullRequest(ctx.ServerURL, token, *config) + pr, result, err := createPullRequest(ctx.ServerURL, token, *config) if err != nil { githubactions.Fatalf("Error while creating pr: %v", err.Error()) } @@ -85,8 +85,10 @@ func main() { fmt.Printf("Created PR with id %d\n", pr.ID) githubactions.SetOutput("pull-request-number", strconv.FormatInt(pr.Index, 10)) + githubactions.SetOutput("pull-request-result", result) githubactions.SetOutput("pull-request-url", pr.URL) + fmt.Println("Ending result CreatePullRequest, main") } // Agent which will perform changes on gitea @@ -114,14 +116,8 @@ type CreatePrConfig struct { Labels []string } -// branchHasOpenPullRequest checks if there is an open pull request with the given branch name in a repository. -// It uses the Gitea client to list all open pull requests in the repository and checks if the branch name matches any of them. -// If a match is found, it returns true indicating that the branch has an open pull request. -// If there is an error listing the pull requests, it returns true along with the error. -// If there are no open pull requests with the branch name, it returns false. -// The method takes a CreatePrConfig struct as a parameter which contains the organization, repository, and branch details. -// It returns a boolean indicating if there is an open pull request and an error if any occurred. -func (a *Agent) branchHasOpenPullRequest(config CreatePrConfig) (bool, error) { +// ExistingOpenPullRequest checks if there is an existing pull request for the same head branch and return it +func (a *Agent) ExistingOpenPullRequest(config CreatePrConfig) (bool, *gitea.PullRequest, error) { currentPage := 1 for currentPage != 0 { @@ -129,19 +125,19 @@ func (a *Agent) branchHasOpenPullRequest(config CreatePrConfig) (bool, error) { gitea.ListPullRequestsOptions{State: gitea.StateOpen, ListOptions: gitea.ListOptions{Page: currentPage}}) if err != nil { - return true, err + return false, nil, err } - for _, p := range pulls { - if p.Head.Name == config.HeadBranch { - return true, nil + for _, pull := range pulls { + if pull.Head.Name == config.HeadBranch { + return true, pull, nil } } currentPage = response.NextPage } - return false, nil + return false, nil, nil } // labelsFromString retrieves a list of labels from a repository based on the provided configuration. @@ -209,7 +205,7 @@ func (a *Agent) createPullRequestGitea(config CreatePrConfig) (*gitea.PullReques // createPullRequest takes in the API URL, access token, and configuration for creating a pull request. // It initializes a Gitea client using the API URL and access token. // It creates an instance of the Agent struct using the Gitea client. -// It checks if there is an open pull request with the given branch name in the repository using the branchHasOpenPullRequest method of Agent. +// It checks if there is an open pull request with the given branch name in the repository using the ExistingOpenPullRequest method of Agent. // If an open pull request already exists, it returns nil as the pull request and nil error. // If no open pull request exists, it creates a new pull request using the createPullRequestGitea method of Agent. // It returns the created pull request and nil error if successful. @@ -219,25 +215,25 @@ func (a *Agent) createPullRequestGitea(config CreatePrConfig) (*gitea.PullReques // - token: The access token for authenticating with the Gitea API. // - config: The configuration for creating the pull request, including the organization, repository, branch details, title, body, assignees, and labels. // It returns a pointer to the created pull request and an error. -func createPullRequest(apiUrl string, token string, config CreatePrConfig) (*gitea.PullRequest, error) { +func createPullRequest(apiUrl string, token string, config CreatePrConfig) (*gitea.PullRequest, string, error) { client, _ := gitea.NewClient(apiUrl, gitea.SetToken(token)) agent := &Agent{client: client} - has, err := agent.branchHasOpenPullRequest(config) + has, pull, err := agent.ExistingOpenPullRequest(config) if err != nil { - return nil, err + return nil, "error", err } if has { - return nil, errors.New("PR already exists for this head branch") + return pull, "existing", nil } pr, err := agent.createPullRequestGitea(config) if err != nil { - return nil, err + return nil, "error", err } - return pr, nil + return pr, "created", nil }