|
| 1 | +package azuredevops |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/helixml/helix/api/pkg/ptr" |
| 8 | + "github.com/microsoft/azure-devops-go-api/azuredevops/v7/git" |
| 9 | +) |
| 10 | + |
| 11 | +func (c *AzureDevOpsClient) CreatePullRequest(ctx context.Context, repositoryName string, title string, description string, sourceBranch string, targetBranch string, project string) (*git.GitPullRequest, error) { |
| 12 | + gitClient, err := git.NewClient(ctx, c.connection) |
| 13 | + if err != nil { |
| 14 | + return nil, fmt.Errorf("failed to create Azure DevOps client: %w", err) |
| 15 | + } |
| 16 | + |
| 17 | + sourceRefName := fmt.Sprintf("refs/heads/%s", sourceBranch) |
| 18 | + targetRefName := fmt.Sprintf("refs/heads/%s", targetBranch) |
| 19 | + |
| 20 | + gitPullRequestToCreate := &git.GitPullRequest{ |
| 21 | + Title: &title, |
| 22 | + Description: &description, |
| 23 | + SourceRefName: &sourceRefName, |
| 24 | + TargetRefName: &targetRefName, |
| 25 | + } |
| 26 | + |
| 27 | + supportsIterations := true |
| 28 | + |
| 29 | + pr, err := gitClient.CreatePullRequest(ctx, git.CreatePullRequestArgs{ |
| 30 | + GitPullRequestToCreate: gitPullRequestToCreate, |
| 31 | + RepositoryId: &repositoryName, |
| 32 | + Project: &project, |
| 33 | + SupportsIterations: &supportsIterations, |
| 34 | + }) |
| 35 | + if err != nil { |
| 36 | + return nil, fmt.Errorf("failed to create pull request: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + return pr, nil |
| 40 | +} |
| 41 | + |
| 42 | +// ListPullRequests lists pull requests for a given repository name and project |
| 43 | +// repositoryName is the name of the repository |
| 44 | +// project is the name of the project in Azure DevOps |
| 45 | +func (c *AzureDevOpsClient) ListPullRequests(ctx context.Context, repositoryName string, project string) ([]git.GitPullRequest, error) { |
| 46 | + gitClient, err := git.NewClient(ctx, c.connection) |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("failed to create Azure DevOps client: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + // repoID, err := uuid.Parse(repositoryID) |
| 52 | + // if err != nil { |
| 53 | + // return nil, fmt.Errorf("failed to parse repository ID: %w", err) |
| 54 | + // } |
| 55 | + |
| 56 | + prs, err := gitClient.GetPullRequests(ctx, git.GetPullRequestsArgs{ |
| 57 | + SearchCriteria: &git.GitPullRequestSearchCriteria{ |
| 58 | + Status: ptr.To(git.PullRequestStatusValues.Active), |
| 59 | + }, |
| 60 | + RepositoryId: &repositoryName, |
| 61 | + Project: &project, |
| 62 | + }) |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("failed to list pull requests: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + return *prs, nil |
| 68 | +} |
0 commit comments