utils_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package detect
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/zricethezav/gitleaks/v8/cmd/scm"
  6. "github.com/zricethezav/gitleaks/v8/report"
  7. "github.com/zricethezav/gitleaks/v8/sources"
  8. )
  9. func Test_createScmLink(t *testing.T) {
  10. tests := map[string]struct {
  11. remote *sources.RemoteInfo
  12. finding report.Finding
  13. want string
  14. }{
  15. // None
  16. "no platform": {
  17. remote: &sources.RemoteInfo{
  18. Platform: scm.NoPlatform,
  19. Url: "",
  20. },
  21. want: "",
  22. },
  23. // GitHub
  24. "github - single line": {
  25. remote: &sources.RemoteInfo{
  26. Platform: scm.GitHubPlatform,
  27. Url: "https://github.com/gitleaks/test",
  28. },
  29. finding: report.Finding{
  30. Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
  31. File: "metrics/% of sales/.env",
  32. StartLine: 25,
  33. EndLine: 25,
  34. },
  35. want: "https://github.com/gitleaks/test/blob/20553ad96a4a080c94a54d677db97eed8ce2560d/metrics/%25%20of%20sales/.env#L25",
  36. },
  37. "github - multi line": {
  38. remote: &sources.RemoteInfo{
  39. Platform: scm.GitHubPlatform,
  40. Url: "https://github.com/gitleaks/test",
  41. },
  42. finding: report.Finding{
  43. Commit: "7bad9f7654cf9701b62400281748c0e8efd97666",
  44. File: "config.json",
  45. StartLine: 235,
  46. EndLine: 238,
  47. },
  48. want: "https://github.com/gitleaks/test/blob/7bad9f7654cf9701b62400281748c0e8efd97666/config.json#L235-L238",
  49. },
  50. "github - markdown": {
  51. remote: &sources.RemoteInfo{
  52. Platform: scm.GitHubPlatform,
  53. Url: "https://github.com/gitleaks/test",
  54. },
  55. finding: report.Finding{
  56. Commit: "1fc8961d172f39ffb671766e472aa76f8d713e87",
  57. File: "docs/guides/ecosystem/discordjs.MD",
  58. StartLine: 34,
  59. EndLine: 34,
  60. },
  61. want: "https://github.com/gitleaks/test/blob/1fc8961d172f39ffb671766e472aa76f8d713e87/docs/guides/ecosystem/discordjs.MD?plain=1#L34",
  62. },
  63. "github - jupyter notebook": {
  64. remote: &sources.RemoteInfo{
  65. Platform: scm.GitHubPlatform,
  66. Url: "https://github.com/gitleaks/test",
  67. },
  68. finding: report.Finding{
  69. Commit: "8f56bd2369595bcadbb007e88ba294630fb05c7b",
  70. File: "Cloud/IPYNB/Overlapping Recommendation algorithm _OCuLaR_.ipynb",
  71. StartLine: 293,
  72. EndLine: 293,
  73. },
  74. want: "https://github.com/gitleaks/test/blob/8f56bd2369595bcadbb007e88ba294630fb05c7b/Cloud/IPYNB/Overlapping%20Recommendation%20algorithm%20_OCuLaR_.ipynb?plain=1#L293",
  75. },
  76. // GitLab
  77. "gitlab - single line": {
  78. remote: &sources.RemoteInfo{
  79. Platform: scm.GitLabPlatform,
  80. Url: "https://gitlab.com/example-org/example-group/gitleaks",
  81. },
  82. finding: report.Finding{
  83. Commit: "213ffd1c9bfa906eb4c7731771132c58a4ca0139",
  84. File: ".gitlab-ci.yml",
  85. StartLine: 41,
  86. EndLine: 41,
  87. },
  88. want: "https://gitlab.com/example-org/example-group/gitleaks/blob/213ffd1c9bfa906eb4c7731771132c58a4ca0139/.gitlab-ci.yml#L41",
  89. },
  90. "gitlab - multi line": {
  91. remote: &sources.RemoteInfo{
  92. Platform: scm.GitLabPlatform,
  93. Url: "https://gitlab.com/example-org/example-group/gitleaks",
  94. },
  95. finding: report.Finding{
  96. Commit: "63410f74e23a4e51e1f60b9feb073b5d325af878",
  97. File: ".vscode/launchSettings.json",
  98. StartLine: 6,
  99. EndLine: 8,
  100. },
  101. want: "https://gitlab.com/example-org/example-group/gitleaks/blob/63410f74e23a4e51e1f60b9feb073b5d325af878/.vscode/launchSettings.json#L6-8",
  102. },
  103. // Azure DevOps
  104. "azuredevops - single line": {
  105. remote: &sources.RemoteInfo{
  106. Platform: scm.AzureDevOpsPlatform,
  107. Url: "https://dev.azure.com/exampleorganisation/exampleproject/_git/exampleRepository",
  108. },
  109. finding: report.Finding{
  110. Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
  111. File: "examplefile.json",
  112. StartLine: 25,
  113. EndLine: 25,
  114. },
  115. want: "https://dev.azure.com/exampleorganisation/exampleproject/_git/exampleRepository/commit/20553ad96a4a080c94a54d677db97eed8ce2560d?path=/examplefile.json&line=25&lineStartColumn=1&lineEndColumn=10000000&type=2&lineStyle=plain&_a=files",
  116. },
  117. // Azure DevOps
  118. "azuredevops - multi line": {
  119. remote: &sources.RemoteInfo{
  120. Platform: scm.AzureDevOpsPlatform,
  121. Url: "https://dev.azure.com/exampleorganisation/exampleproject/_git/exampleRepository",
  122. },
  123. finding: report.Finding{
  124. Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
  125. File: "examplefile.json",
  126. StartLine: 25,
  127. EndLine: 30,
  128. },
  129. want: "https://dev.azure.com/exampleorganisation/exampleproject/_git/exampleRepository/commit/20553ad96a4a080c94a54d677db97eed8ce2560d?path=/examplefile.json&line=25&lineEnd=30&lineStartColumn=1&lineEndColumn=10000000&type=2&lineStyle=plain&_a=files",
  130. },
  131. // Gitea
  132. "gitea - single line": {
  133. remote: &sources.RemoteInfo{
  134. Platform: scm.GiteaPlatform,
  135. Url: "https://gitea.com/exampleorganisation/exampleproject",
  136. },
  137. finding: report.Finding{
  138. Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
  139. File: "examplefile.json",
  140. StartLine: 25,
  141. EndLine: 25,
  142. },
  143. want: "https://gitea.com/exampleorganisation/exampleproject/src/commit/20553ad96a4a080c94a54d677db97eed8ce2560d/examplefile.json#L25",
  144. },
  145. "gitea- multi line": {
  146. remote: &sources.RemoteInfo{
  147. Platform: scm.GiteaPlatform,
  148. Url: "https://gitea.com/exampleorganisation/exampleproject",
  149. },
  150. finding: report.Finding{
  151. Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
  152. File: "examplefile.json",
  153. StartLine: 25,
  154. EndLine: 30,
  155. },
  156. want: "https://gitea.com/exampleorganisation/exampleproject/src/commit/20553ad96a4a080c94a54d677db97eed8ce2560d/examplefile.json#L25-L30",
  157. },
  158. "gitea - markdown": {
  159. remote: &sources.RemoteInfo{
  160. Platform: scm.GiteaPlatform,
  161. Url: "https://gitea.com/exampleorganisation/exampleproject",
  162. },
  163. finding: report.Finding{
  164. Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
  165. File: "Readme.md",
  166. StartLine: 34,
  167. EndLine: 34,
  168. },
  169. want: "https://gitea.com/exampleorganisation/exampleproject/src/commit/20553ad96a4a080c94a54d677db97eed8ce2560d/Readme.md?display=source#L34",
  170. },
  171. // bitbucket
  172. "bitbucket - single line": {
  173. remote: &sources.RemoteInfo{
  174. Platform: scm.BitbucketPlatform,
  175. Url: "https://bitbucket.org/exampleorganisation/exampleproject",
  176. },
  177. finding: report.Finding{
  178. Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
  179. File: "examplefile.json",
  180. StartLine: 25,
  181. EndLine: 25,
  182. },
  183. want: "https://bitbucket.org/exampleorganisation/exampleproject/src/20553ad96a4a080c94a54d677db97eed8ce2560d/examplefile.json#lines-25",
  184. },
  185. "bitbucket- multi line": {
  186. remote: &sources.RemoteInfo{
  187. Platform: scm.BitbucketPlatform,
  188. Url: "https://bitbucket.org/exampleorganisation/exampleproject",
  189. },
  190. finding: report.Finding{
  191. Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
  192. File: "examplefile.json",
  193. StartLine: 25,
  194. EndLine: 30,
  195. },
  196. want: "https://bitbucket.org/exampleorganisation/exampleproject/src/20553ad96a4a080c94a54d677db97eed8ce2560d/examplefile.json#lines-25:30",
  197. },
  198. }
  199. for name, tt := range tests {
  200. t.Run(name, func(t *testing.T) {
  201. actual := createScmLink(tt.remote, tt.finding)
  202. assert.Equal(t, tt.want, actual)
  203. })
  204. }
  205. }