PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Octokit.Tests.Integration/Clients/RepositoryCommitsClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 506 lines | 407 code | 95 blank | 4 comment | 4 complexity | 91f84099d520bd9f531af7c8cf01b0d4 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Octokit;
  6. using Octokit.Tests.Integration;
  7. using Xunit;
  8. using Octokit.Tests.Integration.Helpers;
  9. public class RepositoryCommitsClientTests
  10. {
  11. public class TestsWithExistingRepositories
  12. {
  13. readonly IRepositoryCommitsClient _fixture;
  14. const int octokitNetRepositoryId = 7528679;
  15. const string octokitNetRepositoryOwner = "octokit";
  16. const string octokitNetRepositorName = "octokit.net";
  17. const int reactiveGitRepositoryId = 22718025;
  18. const string reactiveGitRepositoryOwner = "shiftkey";
  19. const string reactiveGitRepositorName = "ReactiveGit";
  20. public TestsWithExistingRepositories()
  21. {
  22. var client = Helper.GetAuthenticatedClient();
  23. _fixture = client.Repository.Commit;
  24. }
  25. [IntegrationTest]
  26. public async Task CanGetMergeBaseCommit()
  27. {
  28. var compareResult = await _fixture.Compare(octokitNetRepositoryOwner, octokitNetRepositorName, "65a22f4d2cff94a286ac3e96440c810c5509196f", "65a22f4d2cff94a286ac3e96440c810c5509196f");
  29. Assert.NotNull(compareResult.MergeBaseCommit);
  30. }
  31. [IntegrationTest]
  32. public async Task CanGetMergeBaseCommitWithRepositoryId()
  33. {
  34. var compareResult = await _fixture.Compare(octokitNetRepositoryId, "65a22f4d2cff94a286ac3e96440c810c5509196f", "65a22f4d2cff94a286ac3e96440c810c5509196f");
  35. Assert.NotNull(compareResult.MergeBaseCommit);
  36. }
  37. [IntegrationTest]
  38. public async Task CanGetCommit()
  39. {
  40. var commit = await _fixture.Get(octokitNetRepositoryOwner, octokitNetRepositorName, "65a22f4d2cff94a286ac3e96440c810c5509196f");
  41. Assert.NotNull(commit);
  42. }
  43. [IntegrationTest]
  44. public async Task CanGetCommitWithRepositoryId()
  45. {
  46. var commit = await _fixture.Get(octokitNetRepositoryId, "65a22f4d2cff94a286ac3e96440c810c5509196f");
  47. Assert.NotNull(commit);
  48. }
  49. [IntegrationTest]
  50. public async Task CanGetCommitWithFiles()
  51. {
  52. var commit = await _fixture.Get(octokitNetRepositoryOwner, octokitNetRepositorName, "65a22f4d2cff94a286ac3e96440c810c5509196f");
  53. Assert.True(commit.Files.Any(file => file.Filename.EndsWith("IConnection.cs")));
  54. }
  55. [IntegrationTest]
  56. public async Task CanGetCommitWithFilesWithRepositoryId()
  57. {
  58. var commit = await _fixture.Get(octokitNetRepositoryId, "65a22f4d2cff94a286ac3e96440c810c5509196f");
  59. Assert.True(commit.Files.Any(file => file.Filename.EndsWith("IConnection.cs")));
  60. }
  61. [IntegrationTest]
  62. public async Task CanGetListOfCommits()
  63. {
  64. var list = await _fixture.GetAll(reactiveGitRepositoryOwner, reactiveGitRepositorName);
  65. Assert.NotEmpty(list);
  66. }
  67. [IntegrationTest]
  68. public async Task CanGetListOfCommitsWithRepositoryId()
  69. {
  70. var list = await _fixture.GetAll(reactiveGitRepositoryId);
  71. Assert.NotEmpty(list);
  72. }
  73. [IntegrationTest]
  74. public async Task CanGetCorrectCountOfCommitsWithoutStart()
  75. {
  76. var options = new ApiOptions
  77. {
  78. PageSize = 5,
  79. PageCount = 1
  80. };
  81. var commits = await _fixture.GetAll(reactiveGitRepositoryOwner, reactiveGitRepositorName, options);
  82. Assert.Equal(5, commits.Count);
  83. }
  84. [IntegrationTest]
  85. public async Task CanGetCorrectCountOfCommitsWithoutStartWithRepositoryId()
  86. {
  87. var options = new ApiOptions
  88. {
  89. PageSize = 5,
  90. PageCount = 1
  91. };
  92. var commits = await _fixture.GetAll(reactiveGitRepositoryId, options);
  93. Assert.Equal(5, commits.Count);
  94. }
  95. [IntegrationTest]
  96. public async Task CanGetCorrectCountOfCommitsWithStart()
  97. {
  98. var options = new ApiOptions
  99. {
  100. PageSize = 5,
  101. PageCount = 1,
  102. StartPage = 2
  103. };
  104. var commits = await _fixture.GetAll(reactiveGitRepositoryOwner, reactiveGitRepositorName, options);
  105. Assert.Equal(5, commits.Count);
  106. }
  107. [IntegrationTest]
  108. public async Task CanGetCorrectCountOfCommitsWithStartWithRepositoryId()
  109. {
  110. var options = new ApiOptions
  111. {
  112. PageSize = 5,
  113. PageCount = 1,
  114. StartPage = 2
  115. };
  116. var commits = await _fixture.GetAll(reactiveGitRepositoryId, options);
  117. Assert.Equal(5, commits.Count);
  118. }
  119. [IntegrationTest]
  120. public async Task ReturnsDistinctResultsBasedOnStart()
  121. {
  122. var startOptions = new ApiOptions
  123. {
  124. PageSize = 5,
  125. PageCount = 1
  126. };
  127. var skipStartOptions = new ApiOptions
  128. {
  129. PageSize = 5,
  130. PageCount = 1,
  131. StartPage = 2
  132. };
  133. var firstCommit = await _fixture.GetAll(reactiveGitRepositoryOwner, reactiveGitRepositorName, startOptions);
  134. var secondCommit = await _fixture.GetAll(reactiveGitRepositoryOwner, reactiveGitRepositorName, skipStartOptions);
  135. Assert.NotEqual(firstCommit[0].Sha, secondCommit[0].Sha);
  136. Assert.NotEqual(firstCommit[1].Sha, secondCommit[1].Sha);
  137. Assert.NotEqual(firstCommit[2].Sha, secondCommit[2].Sha);
  138. Assert.NotEqual(firstCommit[3].Sha, secondCommit[3].Sha);
  139. Assert.NotEqual(firstCommit[4].Sha, secondCommit[4].Sha);
  140. }
  141. [IntegrationTest]
  142. public async Task ReturnsDistinctResultsBasedOnStartWithRepositoryId()
  143. {
  144. var startOptions = new ApiOptions
  145. {
  146. PageSize = 5,
  147. PageCount = 1
  148. };
  149. var skipStartOptions = new ApiOptions
  150. {
  151. PageSize = 5,
  152. PageCount = 1,
  153. StartPage = 2
  154. };
  155. var firstCommit = await _fixture.GetAll(reactiveGitRepositoryId, startOptions);
  156. var secondCommit = await _fixture.GetAll(reactiveGitRepositoryId, skipStartOptions);
  157. Assert.NotEqual(firstCommit[0].Sha, secondCommit[0].Sha);
  158. Assert.NotEqual(firstCommit[1].Sha, secondCommit[1].Sha);
  159. Assert.NotEqual(firstCommit[2].Sha, secondCommit[2].Sha);
  160. Assert.NotEqual(firstCommit[3].Sha, secondCommit[3].Sha);
  161. Assert.NotEqual(firstCommit[4].Sha, secondCommit[4].Sha);
  162. }
  163. [IntegrationTest]
  164. public async Task CanGetListOfCommitsBySha()
  165. {
  166. var request = new CommitRequest { Sha = "08b363d45d6ae8567b75dfa45c032a288584afd4" };
  167. var list = await _fixture.GetAll(octokitNetRepositoryOwner, octokitNetRepositorName, request);
  168. Assert.NotEmpty(list);
  169. }
  170. [IntegrationTest]
  171. public async Task CanGetListOfCommitsByShaWithRepositoryId()
  172. {
  173. var request = new CommitRequest { Sha = "08b363d45d6ae8567b75dfa45c032a288584afd4" };
  174. var list = await _fixture.GetAll(octokitNetRepositoryId, request);
  175. Assert.NotEmpty(list);
  176. }
  177. [IntegrationTest]
  178. public async Task CanGetListOfCommitsByPath()
  179. {
  180. var request = new CommitRequest { Path = "Octokit.Reactive/IObservableGitHubClient.cs" };
  181. var list = await _fixture.GetAll(octokitNetRepositoryOwner, octokitNetRepositorName, request);
  182. Assert.NotEmpty(list);
  183. }
  184. [IntegrationTest]
  185. public async Task CanGetListOfCommitsByPathWithRepositoryId()
  186. {
  187. var request = new CommitRequest { Path = "Octokit.Reactive/IObservableGitHubClient.cs" };
  188. var list = await _fixture.GetAll(octokitNetRepositoryId, request);
  189. Assert.NotEmpty(list);
  190. }
  191. [IntegrationTest]
  192. public async Task CanGetListOfCommitsByAuthor()
  193. {
  194. var request = new CommitRequest { Author = "kzu" };
  195. var list = await _fixture.GetAll(octokitNetRepositoryOwner, octokitNetRepositorName, request);
  196. Assert.NotEmpty(list);
  197. }
  198. [IntegrationTest]
  199. public async Task CanGetListOfCommitsByAuthorWithRepositoryId()
  200. {
  201. var request = new CommitRequest { Author = "kzu" };
  202. var list = await _fixture.GetAll(octokitNetRepositoryId, request);
  203. Assert.NotEmpty(list);
  204. }
  205. [IntegrationTest]
  206. public async Task CanGetListOfCommitsByDateRange()
  207. {
  208. var offset = new TimeSpan(1, 0, 0);
  209. var since = new DateTimeOffset(2014, 1, 1, 0, 0, 0, offset);
  210. var until = new DateTimeOffset(2014, 1, 8, 0, 0, 0, offset);
  211. var request = new CommitRequest { Since = since, Until = until };
  212. var list = await _fixture.GetAll(octokitNetRepositoryOwner, octokitNetRepositorName, request);
  213. Assert.NotEmpty(list);
  214. }
  215. [IntegrationTest]
  216. public async Task CanGetListOfCommitsByDateRangeWithRepositoryId()
  217. {
  218. var offset = new TimeSpan(1, 0, 0);
  219. var since = new DateTimeOffset(2014, 1, 1, 0, 0, 0, offset);
  220. var until = new DateTimeOffset(2014, 1, 8, 0, 0, 0, offset);
  221. var request = new CommitRequest { Since = since, Until = until };
  222. var list = await _fixture.GetAll(octokitNetRepositoryId, request);
  223. Assert.NotEmpty(list);
  224. }
  225. [IntegrationTest]
  226. public async Task CanGetCommitWithRenamedFiles()
  227. {
  228. var commit = await _fixture.Get(octokitNetRepositoryOwner, octokitNetRepositorName, "997e955f38eb0c2c36e55b1588455fa857951dbf");
  229. Assert.True(commit.Files
  230. .Where(file => file.Status == "renamed")
  231. .All(file => string.IsNullOrEmpty(file.PreviousFileName) == false));
  232. }
  233. [IntegrationTest]
  234. public async Task CanGetCommitWithRenamedFilesWithRepositoryId()
  235. {
  236. var commit = await _fixture.Get(octokitNetRepositoryId, "997e955f38eb0c2c36e55b1588455fa857951dbf");
  237. Assert.True(commit.Files
  238. .Where(file => file.Status == "renamed")
  239. .All(file => string.IsNullOrEmpty(file.PreviousFileName) == false));
  240. }
  241. [IntegrationTest]
  242. public async Task CanGetSha1()
  243. {
  244. var sha1 = await _fixture.GetSha1(octokitNetRepositoryOwner, octokitNetRepositorName, "master");
  245. Assert.NotNull(sha1);
  246. }
  247. [IntegrationTest]
  248. public async Task CanGetSha1WithRepositoryId()
  249. {
  250. var sha1 = await _fixture.GetSha1(octokitNetRepositoryId, "master");
  251. Assert.NotNull(sha1);
  252. }
  253. }
  254. public class TestsWithNewRepository : IDisposable
  255. {
  256. private readonly IGitHubClient _github;
  257. private readonly IRepositoryCommitsClient _fixture;
  258. private readonly RepositoryContext _context;
  259. public TestsWithNewRepository()
  260. {
  261. _github = Helper.GetAuthenticatedClient();
  262. _fixture = _github.Repository.Commit;
  263. _context = _github.CreateRepositoryContext("source-repo").Result;
  264. }
  265. [IntegrationTest]
  266. public async Task CanCompareReferences()
  267. {
  268. await CreateTheWorld();
  269. var result = await _fixture.Compare(Helper.UserName, _context.RepositoryName, "master", "my-branch");
  270. Assert.Equal(1, result.TotalCommits);
  271. Assert.Equal(1, result.Commits.Count);
  272. Assert.Equal(1, result.AheadBy);
  273. Assert.Equal(0, result.BehindBy);
  274. }
  275. [IntegrationTest]
  276. public async Task CanCompareReferencesWithRepositoryId()
  277. {
  278. await CreateTheWorld();
  279. var result = await _fixture.Compare(_context.Repository.Id, "master", "my-branch");
  280. Assert.Equal(1, result.TotalCommits);
  281. Assert.Equal(1, result.Commits.Count);
  282. Assert.Equal(1, result.AheadBy);
  283. Assert.Equal(0, result.BehindBy);
  284. }
  285. [IntegrationTest]
  286. public async Task CanCompareReferencesOtherWayRound()
  287. {
  288. await CreateTheWorld();
  289. var result = await _fixture.Compare(Helper.UserName, _context.RepositoryName, "my-branch", "master");
  290. Assert.Equal(0, result.TotalCommits);
  291. Assert.Equal(0, result.Commits.Count);
  292. Assert.Equal(0, result.AheadBy);
  293. Assert.Equal(1, result.BehindBy);
  294. }
  295. [IntegrationTest]
  296. public async Task CanCompareReferencesOtherWayRoundWithRepositoryId()
  297. {
  298. await CreateTheWorld();
  299. var result = await _fixture.Compare(_context.Repository.Id, "my-branch", "master");
  300. Assert.Equal(0, result.TotalCommits);
  301. Assert.Equal(0, result.Commits.Count);
  302. Assert.Equal(0, result.AheadBy);
  303. Assert.Equal(1, result.BehindBy);
  304. }
  305. [IntegrationTest]
  306. public async Task ReturnsUrlsToResources()
  307. {
  308. await CreateTheWorld();
  309. var result = await _fixture.Compare(Helper.UserName, _context.RepositoryName, "my-branch", "master");
  310. Assert.NotNull(result.DiffUrl);
  311. Assert.NotNull(result.HtmlUrl);
  312. Assert.NotNull(result.PatchUrl);
  313. Assert.NotNull(result.PermalinkUrl);
  314. }
  315. [IntegrationTest]
  316. public async Task ReturnsUrlsToResourcesWithRepositoryId()
  317. {
  318. await CreateTheWorld();
  319. var result = await _fixture.Compare(_context.Repository.Id, "my-branch", "master");
  320. Assert.NotNull(result.DiffUrl);
  321. Assert.NotNull(result.HtmlUrl);
  322. Assert.NotNull(result.PatchUrl);
  323. Assert.NotNull(result.PermalinkUrl);
  324. }
  325. [IntegrationTest]
  326. public async Task CanCompareUsingSha()
  327. {
  328. await CreateTheWorld();
  329. var master = await _github.Git.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/master");
  330. var branch = await _github.Git.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/my-branch");
  331. var result = await _fixture.Compare(Helper.UserName, _context.RepositoryName, master.Object.Sha, branch.Object.Sha);
  332. Assert.Equal(1, result.Commits.Count);
  333. Assert.Equal(1, result.AheadBy);
  334. Assert.Equal(0, result.BehindBy);
  335. }
  336. [IntegrationTest]
  337. public async Task CanCompareUsingShaWithRepositoryId()
  338. {
  339. await CreateTheWorld();
  340. var master = await _github.Git.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/master");
  341. var branch = await _github.Git.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/my-branch");
  342. var result = await _fixture.Compare(_context.Repository.Id, master.Object.Sha, branch.Object.Sha);
  343. Assert.Equal(1, result.Commits.Count);
  344. Assert.Equal(1, result.AheadBy);
  345. Assert.Equal(0, result.BehindBy);
  346. }
  347. [IntegrationTest]
  348. public async Task GetSha1FromRepository()
  349. {
  350. var reference = await CreateTheWorld();
  351. var sha1 = await _fixture.GetSha1(Helper.UserName, _context.RepositoryName, "my-branch");
  352. Assert.Equal(reference.Object.Sha, sha1);
  353. }
  354. [IntegrationTest]
  355. public async Task GetSha1FromRepositoryWithRepositoryId()
  356. {
  357. var reference = await CreateTheWorld();
  358. var sha1 = await _fixture.GetSha1(_context.Repository.Id, "my-branch");
  359. Assert.Equal(reference.Object.Sha, sha1);
  360. }
  361. async Task<Reference> CreateTheWorld()
  362. {
  363. var master = await _github.Git.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/master");
  364. // create new commit for master branch
  365. var newMasterTree = await CreateTree(new Dictionary<string, string> { { "README.md", "Hello World!" } });
  366. var newMaster = await CreateCommit("baseline for pull request", newMasterTree.Sha, master.Object.Sha);
  367. // update master
  368. await _github.Git.Reference.Update(Helper.UserName, _context.RepositoryName, "heads/master", new ReferenceUpdate(newMaster.Sha));
  369. // create new commit for feature branch
  370. var featureBranchTree = await CreateTree(new Dictionary<string, string> { { "README.md", "I am overwriting this blob with something new" } });
  371. var newFeature = await CreateCommit("this is the commit to merge into the pull request", featureBranchTree.Sha, newMaster.Sha);
  372. // create branch
  373. return await _github.Git.Reference.Create(Helper.UserName, _context.RepositoryName, new NewReference("refs/heads/my-branch", newFeature.Sha));
  374. }
  375. async Task<TreeResponse> CreateTree(IDictionary<string, string> treeContents)
  376. {
  377. var collection = new List<NewTreeItem>();
  378. foreach (var c in treeContents)
  379. {
  380. var baselineBlob = new NewBlob
  381. {
  382. Content = c.Value,
  383. Encoding = EncodingType.Utf8
  384. };
  385. var baselineBlobResult = await _github.Git.Blob.Create(Helper.UserName, _context.RepositoryName, baselineBlob);
  386. collection.Add(new NewTreeItem
  387. {
  388. Type = TreeType.Blob,
  389. Mode = FileMode.File,
  390. Path = c.Key,
  391. Sha = baselineBlobResult.Sha
  392. });
  393. }
  394. var newTree = new NewTree();
  395. foreach (var item in collection)
  396. {
  397. newTree.Tree.Add(item);
  398. }
  399. return await _github.Git.Tree.Create(Helper.UserName, _context.RepositoryName, newTree);
  400. }
  401. async Task<Commit> CreateCommit(string message, string sha, string parent)
  402. {
  403. var newCommit = new NewCommit(message, sha, parent);
  404. return await _github.Git.Commit.Create(Helper.UserName, _context.RepositoryName, newCommit);
  405. }
  406. public void Dispose()
  407. {
  408. _context.Dispose();
  409. }
  410. }
  411. }