|
1 | 1 | using Octokit; |
| 2 | +using Octokit.Helpers; |
2 | 3 |
|
3 | 4 | namespace DevExcelerateApi.Helpers |
4 | 5 | { |
5 | | - public class GitHubHelper: IGitHubHelper |
| 6 | + public class GitHubHelper : IGitHubHelper |
6 | 7 | { |
7 | | - public async Task CopyFilesAsync(IGitHubClient client, string sourceOwner, string sourceRepo, string sourcePath, string targetOwner, string targetRepo, string targetPath) |
| 8 | + private IGitHubClient? _githubClient; |
| 9 | + |
| 10 | + public void SetGitHubClient(IGitHubClient githubClient) |
8 | 11 | { |
9 | | - var sourceFiles = await client.Repository.Content.GetAllContents(sourceOwner, sourceRepo, sourcePath); |
| 12 | + _githubClient = githubClient ?? throw new ArgumentNullException(nameof(githubClient)); |
| 13 | + } |
| 14 | + |
| 15 | + public async Task CopyFilesAsync(string sourceOwner, string sourceRepo, string sourcePath, string targetOwner, string targetRepo, string targetPath) |
| 16 | + { |
| 17 | + if (_githubClient == null) |
| 18 | + { |
| 19 | + throw new InvalidOperationException("GitHub client is not configured."); |
| 20 | + } |
| 21 | + |
| 22 | + var sourceFiles = await _githubClient.Repository.Content.GetAllContents(sourceOwner, sourceRepo, sourcePath); |
10 | 23 |
|
11 | 24 | foreach (var file in sourceFiles) |
12 | 25 | { |
13 | 26 | if (file.Type == ContentType.File) |
14 | 27 | { |
15 | | - var fileContent = await client.Repository.Content.GetRawContent(sourceOwner, sourceRepo, file.Path); |
| 28 | + var fileContent = await _githubClient.Repository.Content.GetRawContent(sourceOwner, sourceRepo, file.Path); |
16 | 29 | var fileContentString = System.Text.Encoding.UTF8.GetString(fileContent); |
17 | 30 | var createFileRequest = new CreateFileRequest($"Copying {file.Name} from {sourceRepo}", fileContentString); |
18 | 31 |
|
19 | 32 | var targetFilePath = $"{targetPath}/{file.Name}"; |
20 | | - await client.Repository.Content.CreateFile(targetOwner, targetRepo, targetFilePath, createFileRequest); |
| 33 | + await _githubClient.Repository.Content.CreateFile(targetOwner, targetRepo, targetFilePath, createFileRequest); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public async Task<string> GetFileContentAsync(string owner, string repo, string path) |
| 39 | + { |
| 40 | + if (_githubClient == null) |
| 41 | + { |
| 42 | + throw new InvalidOperationException("GitHub client is not configured."); |
| 43 | + } |
| 44 | + |
| 45 | + var fileContent = await _githubClient.Repository.Content.GetRawContent(owner, repo, path); |
| 46 | + var fileContentString = System.Text.Encoding.UTF8.GetString(fileContent); |
| 47 | + return fileContentString; |
| 48 | + } |
| 49 | + |
| 50 | + public async Task<RepositoryContentChangeSet?> SaveFileAsync(string owner, string repo, string defaultBranchName, string branch, string path, string content) |
| 51 | + { |
| 52 | + if (_githubClient == null) |
| 53 | + { |
| 54 | + throw new InvalidOperationException("GitHub client is not configured."); |
| 55 | + } |
| 56 | + |
| 57 | + try |
| 58 | + { |
| 59 | + // Check if the file already exists |
| 60 | + var existingFile = await _githubClient.Repository.Content.GetAllContentsByRef(owner, repo, path, branch); |
| 61 | + |
| 62 | + if (existingFile != null && existingFile.Count > 0) |
| 63 | + { |
| 64 | + var updateFileRequest = new UpdateFileRequest($"Updating file {path}", content, existingFile[0].Sha, branch); |
| 65 | + return await _githubClient.Repository.Content.UpdateFile(owner, repo, path, updateFileRequest); |
21 | 66 | } |
22 | 67 | } |
| 68 | + catch (NotFoundException) |
| 69 | + { |
| 70 | + await ReferenceExtensions.CreateBranch(_githubClient.Git.Reference, owner, repo, branch, defaultBranchName); |
| 71 | + |
| 72 | + var createFileRequest = new CreateFileRequest($"Adding file {path}", content, branch); |
| 73 | + |
| 74 | + return await _githubClient.Repository.Content.CreateFile(owner, repo, path, createFileRequest); |
| 75 | + } |
| 76 | + |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + public async Task<PullRequest> CreatePullRequestAsync(string owner, string repo, string headBranch, string baseBranch, string title, string body) |
| 81 | + { |
| 82 | + if (_githubClient == null) |
| 83 | + { |
| 84 | + throw new InvalidOperationException("GitHub client is not configured."); |
| 85 | + } |
| 86 | + |
| 87 | + // Step 1: Check if a pull request already exists for the head branch |
| 88 | + var pullRequests = await _githubClient.PullRequest.GetAllForRepository(owner, repo, new PullRequestRequest |
| 89 | + { |
| 90 | + State = ItemStateFilter.Open, // Only check open pull requests |
| 91 | + Head = $"{owner}:{headBranch}" // Filter by the head branch |
| 92 | + }); |
| 93 | + |
| 94 | + if (pullRequests.Any()) |
| 95 | + { |
| 96 | + // If a pull request already exists, return the first one |
| 97 | + return pullRequests[0]; |
| 98 | + } |
| 99 | + |
| 100 | + // Step 2: Create a new pull request if none exists |
| 101 | + var newPullRequest = new NewPullRequest(title, headBranch, baseBranch) |
| 102 | + { |
| 103 | + Body = body |
| 104 | + }; |
| 105 | + |
| 106 | + return await _githubClient.PullRequest.Create(owner, repo, newPullRequest); |
23 | 107 | } |
24 | 108 | } |
25 | 109 | } |
0 commit comments