You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repofiles_change_test.go 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/url"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "time"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. "code.gitea.io/gitea/models/unittest"
  12. "code.gitea.io/gitea/modules/contexttest"
  13. "code.gitea.io/gitea/modules/git"
  14. "code.gitea.io/gitea/modules/setting"
  15. api "code.gitea.io/gitea/modules/structs"
  16. files_service "code.gitea.io/gitea/services/repository/files"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func getCreateRepoFilesOptions(repo *repo_model.Repository) *files_service.ChangeRepoFilesOptions {
  20. return &files_service.ChangeRepoFilesOptions{
  21. Files: []*files_service.ChangeRepoFile{
  22. {
  23. Operation: "create",
  24. TreePath: "new/file.txt",
  25. ContentReader: strings.NewReader("This is a NEW file"),
  26. },
  27. },
  28. OldBranch: repo.DefaultBranch,
  29. NewBranch: repo.DefaultBranch,
  30. Message: "Creates new/file.txt",
  31. Author: nil,
  32. Committer: nil,
  33. }
  34. }
  35. func getUpdateRepoFilesOptions(repo *repo_model.Repository) *files_service.ChangeRepoFilesOptions {
  36. return &files_service.ChangeRepoFilesOptions{
  37. Files: []*files_service.ChangeRepoFile{
  38. {
  39. Operation: "update",
  40. TreePath: "README.md",
  41. SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  42. ContentReader: strings.NewReader("This is UPDATED content for the README file"),
  43. },
  44. },
  45. OldBranch: repo.DefaultBranch,
  46. NewBranch: repo.DefaultBranch,
  47. Message: "Updates README.md",
  48. Author: nil,
  49. Committer: nil,
  50. }
  51. }
  52. func getDeleteRepoFilesOptions(repo *repo_model.Repository) *files_service.ChangeRepoFilesOptions {
  53. return &files_service.ChangeRepoFilesOptions{
  54. Files: []*files_service.ChangeRepoFile{
  55. {
  56. Operation: "delete",
  57. TreePath: "README.md",
  58. SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  59. },
  60. },
  61. LastCommitID: "",
  62. OldBranch: repo.DefaultBranch,
  63. NewBranch: repo.DefaultBranch,
  64. Message: "Deletes README.md",
  65. Author: &files_service.IdentityOptions{
  66. Name: "Bob Smith",
  67. Email: "bob@smith.com",
  68. },
  69. Committer: nil,
  70. }
  71. }
  72. func getExpectedFileResponseForRepofilesDelete(u *url.URL) *api.FileResponse {
  73. // Just returns fields that don't change, i.e. fields with commit SHAs and dates can't be determined
  74. return &api.FileResponse{
  75. Content: nil,
  76. Commit: &api.FileCommitResponse{
  77. Author: &api.CommitUser{
  78. Identity: api.Identity{
  79. Name: "Bob Smith",
  80. Email: "bob@smith.com",
  81. },
  82. },
  83. Committer: &api.CommitUser{
  84. Identity: api.Identity{
  85. Name: "Bob Smith",
  86. Email: "bob@smith.com",
  87. },
  88. },
  89. Message: "Deletes README.md\n",
  90. },
  91. Verification: &api.PayloadCommitVerification{
  92. Verified: false,
  93. Reason: "gpg.error.not_signed_commit",
  94. Signature: "",
  95. Payload: "",
  96. },
  97. }
  98. }
  99. func getExpectedFileResponseForRepofilesCreate(commitID, lastCommitSHA string) *api.FileResponse {
  100. treePath := "new/file.txt"
  101. encoding := "base64"
  102. content := "VGhpcyBpcyBhIE5FVyBmaWxl"
  103. selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
  104. htmlURL := setting.AppURL + "user2/repo1/src/branch/master/" + treePath
  105. gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/103ff9234cefeee5ec5361d22b49fbb04d385885"
  106. downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
  107. return &api.FileResponse{
  108. Content: &api.ContentsResponse{
  109. Name: filepath.Base(treePath),
  110. Path: treePath,
  111. SHA: "103ff9234cefeee5ec5361d22b49fbb04d385885",
  112. LastCommitSHA: lastCommitSHA,
  113. Type: "file",
  114. Size: 18,
  115. Encoding: &encoding,
  116. Content: &content,
  117. URL: &selfURL,
  118. HTMLURL: &htmlURL,
  119. GitURL: &gitURL,
  120. DownloadURL: &downloadURL,
  121. Links: &api.FileLinksResponse{
  122. Self: &selfURL,
  123. GitURL: &gitURL,
  124. HTMLURL: &htmlURL,
  125. },
  126. },
  127. Commit: &api.FileCommitResponse{
  128. CommitMeta: api.CommitMeta{
  129. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + commitID,
  130. SHA: commitID,
  131. },
  132. HTMLURL: setting.AppURL + "user2/repo1/commit/" + commitID,
  133. Author: &api.CommitUser{
  134. Identity: api.Identity{
  135. Name: "User Two",
  136. Email: "user2@noreply.example.org",
  137. },
  138. Date: time.Now().UTC().Format(time.RFC3339),
  139. },
  140. Committer: &api.CommitUser{
  141. Identity: api.Identity{
  142. Name: "User Two",
  143. Email: "user2@noreply.example.org",
  144. },
  145. Date: time.Now().UTC().Format(time.RFC3339),
  146. },
  147. Parents: []*api.CommitMeta{
  148. {
  149. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  150. SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
  151. },
  152. },
  153. Message: "Updates README.md\n",
  154. Tree: &api.CommitMeta{
  155. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/trees/f93e3a1a1525fb5b91020da86e44810c87a2d7bc",
  156. SHA: "f93e3a1a1525fb5b91020git dda86e44810c87a2d7bc",
  157. },
  158. },
  159. Verification: &api.PayloadCommitVerification{
  160. Verified: false,
  161. Reason: "gpg.error.not_signed_commit",
  162. Signature: "",
  163. Payload: "",
  164. },
  165. }
  166. }
  167. func getExpectedFileResponseForRepofilesUpdate(commitID, filename, lastCommitSHA string) *api.FileResponse {
  168. encoding := "base64"
  169. content := "VGhpcyBpcyBVUERBVEVEIGNvbnRlbnQgZm9yIHRoZSBSRUFETUUgZmlsZQ=="
  170. selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + filename + "?ref=master"
  171. htmlURL := setting.AppURL + "user2/repo1/src/branch/master/" + filename
  172. gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/dbf8d00e022e05b7e5cf7e535de857de57925647"
  173. downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + filename
  174. return &api.FileResponse{
  175. Content: &api.ContentsResponse{
  176. Name: filename,
  177. Path: filename,
  178. SHA: "dbf8d00e022e05b7e5cf7e535de857de57925647",
  179. LastCommitSHA: lastCommitSHA,
  180. Type: "file",
  181. Size: 43,
  182. Encoding: &encoding,
  183. Content: &content,
  184. URL: &selfURL,
  185. HTMLURL: &htmlURL,
  186. GitURL: &gitURL,
  187. DownloadURL: &downloadURL,
  188. Links: &api.FileLinksResponse{
  189. Self: &selfURL,
  190. GitURL: &gitURL,
  191. HTMLURL: &htmlURL,
  192. },
  193. },
  194. Commit: &api.FileCommitResponse{
  195. CommitMeta: api.CommitMeta{
  196. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + commitID,
  197. SHA: commitID,
  198. },
  199. HTMLURL: setting.AppURL + "user2/repo1/commit/" + commitID,
  200. Author: &api.CommitUser{
  201. Identity: api.Identity{
  202. Name: "User Two",
  203. Email: "user2@noreply.example.org",
  204. },
  205. Date: time.Now().UTC().Format(time.RFC3339),
  206. },
  207. Committer: &api.CommitUser{
  208. Identity: api.Identity{
  209. Name: "User Two",
  210. Email: "user2@noreply.example.org",
  211. },
  212. Date: time.Now().UTC().Format(time.RFC3339),
  213. },
  214. Parents: []*api.CommitMeta{
  215. {
  216. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  217. SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
  218. },
  219. },
  220. Message: "Updates README.md\n",
  221. Tree: &api.CommitMeta{
  222. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/trees/f93e3a1a1525fb5b91020da86e44810c87a2d7bc",
  223. SHA: "f93e3a1a1525fb5b91020da86e44810c87a2d7bc",
  224. },
  225. },
  226. Verification: &api.PayloadCommitVerification{
  227. Verified: false,
  228. Reason: "gpg.error.not_signed_commit",
  229. Signature: "",
  230. Payload: "",
  231. },
  232. }
  233. }
  234. func TestChangeRepoFilesForCreate(t *testing.T) {
  235. // setup
  236. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  237. ctx, _ := contexttest.MockContext(t, "user2/repo1")
  238. ctx.SetParams(":id", "1")
  239. contexttest.LoadRepo(t, ctx, 1)
  240. contexttest.LoadRepoCommit(t, ctx)
  241. contexttest.LoadUser(t, ctx, 2)
  242. contexttest.LoadGitRepo(t, ctx)
  243. defer ctx.Repo.GitRepo.Close()
  244. repo := ctx.Repo.Repository
  245. doer := ctx.Doer
  246. opts := getCreateRepoFilesOptions(repo)
  247. // test
  248. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  249. // asserts
  250. assert.NoError(t, err)
  251. gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath())
  252. defer gitRepo.Close()
  253. commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch)
  254. lastCommit, _ := gitRepo.GetCommitByPath("new/file.txt")
  255. expectedFileResponse := getExpectedFileResponseForRepofilesCreate(commitID, lastCommit.ID.String())
  256. assert.NotNil(t, expectedFileResponse)
  257. if expectedFileResponse != nil {
  258. assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0])
  259. assert.EqualValues(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA)
  260. assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL)
  261. assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, filesResponse.Commit.Author.Email)
  262. assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, filesResponse.Commit.Author.Name)
  263. }
  264. })
  265. }
  266. func TestChangeRepoFilesForUpdate(t *testing.T) {
  267. // setup
  268. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  269. ctx, _ := contexttest.MockContext(t, "user2/repo1")
  270. ctx.SetParams(":id", "1")
  271. contexttest.LoadRepo(t, ctx, 1)
  272. contexttest.LoadRepoCommit(t, ctx)
  273. contexttest.LoadUser(t, ctx, 2)
  274. contexttest.LoadGitRepo(t, ctx)
  275. defer ctx.Repo.GitRepo.Close()
  276. repo := ctx.Repo.Repository
  277. doer := ctx.Doer
  278. opts := getUpdateRepoFilesOptions(repo)
  279. // test
  280. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  281. // asserts
  282. assert.NoError(t, err)
  283. gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath())
  284. defer gitRepo.Close()
  285. commit, _ := gitRepo.GetBranchCommit(opts.NewBranch)
  286. lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath)
  287. expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
  288. assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0])
  289. assert.EqualValues(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA)
  290. assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL)
  291. assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, filesResponse.Commit.Author.Email)
  292. assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, filesResponse.Commit.Author.Name)
  293. })
  294. }
  295. func TestChangeRepoFilesForUpdateWithFileMove(t *testing.T) {
  296. // setup
  297. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  298. ctx, _ := contexttest.MockContext(t, "user2/repo1")
  299. ctx.SetParams(":id", "1")
  300. contexttest.LoadRepo(t, ctx, 1)
  301. contexttest.LoadRepoCommit(t, ctx)
  302. contexttest.LoadUser(t, ctx, 2)
  303. contexttest.LoadGitRepo(t, ctx)
  304. defer ctx.Repo.GitRepo.Close()
  305. repo := ctx.Repo.Repository
  306. doer := ctx.Doer
  307. opts := getUpdateRepoFilesOptions(repo)
  308. opts.Files[0].FromTreePath = "README.md"
  309. opts.Files[0].TreePath = "README_new.md" // new file name, README_new.md
  310. // test
  311. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  312. // asserts
  313. assert.NoError(t, err)
  314. gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath())
  315. defer gitRepo.Close()
  316. commit, _ := gitRepo.GetBranchCommit(opts.NewBranch)
  317. lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath)
  318. expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
  319. // assert that the old file no longer exists in the last commit of the branch
  320. fromEntry, err := commit.GetTreeEntryByPath(opts.Files[0].FromTreePath)
  321. switch err.(type) {
  322. case git.ErrNotExist:
  323. // correct, continue
  324. default:
  325. t.Fatalf("expected git.ErrNotExist, got:%v", err)
  326. }
  327. toEntry, err := commit.GetTreeEntryByPath(opts.Files[0].TreePath)
  328. assert.NoError(t, err)
  329. assert.Nil(t, fromEntry) // Should no longer exist here
  330. assert.NotNil(t, toEntry) // Should exist here
  331. // assert SHA has remained the same but paths use the new file name
  332. assert.EqualValues(t, expectedFileResponse.Content.SHA, filesResponse.Files[0].SHA)
  333. assert.EqualValues(t, expectedFileResponse.Content.Name, filesResponse.Files[0].Name)
  334. assert.EqualValues(t, expectedFileResponse.Content.Path, filesResponse.Files[0].Path)
  335. assert.EqualValues(t, expectedFileResponse.Content.URL, filesResponse.Files[0].URL)
  336. assert.EqualValues(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA)
  337. assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL)
  338. })
  339. }
  340. // Test opts with branch names removed, should get same results as above test
  341. func TestChangeRepoFilesWithoutBranchNames(t *testing.T) {
  342. // setup
  343. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  344. ctx, _ := contexttest.MockContext(t, "user2/repo1")
  345. ctx.SetParams(":id", "1")
  346. contexttest.LoadRepo(t, ctx, 1)
  347. contexttest.LoadRepoCommit(t, ctx)
  348. contexttest.LoadUser(t, ctx, 2)
  349. contexttest.LoadGitRepo(t, ctx)
  350. defer ctx.Repo.GitRepo.Close()
  351. repo := ctx.Repo.Repository
  352. doer := ctx.Doer
  353. opts := getUpdateRepoFilesOptions(repo)
  354. opts.OldBranch = ""
  355. opts.NewBranch = ""
  356. // test
  357. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  358. // asserts
  359. assert.NoError(t, err)
  360. gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath())
  361. defer gitRepo.Close()
  362. commit, _ := gitRepo.GetBranchCommit(repo.DefaultBranch)
  363. lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath)
  364. expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
  365. assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0])
  366. })
  367. }
  368. func TestChangeRepoFilesForDelete(t *testing.T) {
  369. onGiteaRun(t, testDeleteRepoFiles)
  370. }
  371. func testDeleteRepoFiles(t *testing.T, u *url.URL) {
  372. // setup
  373. unittest.PrepareTestEnv(t)
  374. ctx, _ := contexttest.MockContext(t, "user2/repo1")
  375. ctx.SetParams(":id", "1")
  376. contexttest.LoadRepo(t, ctx, 1)
  377. contexttest.LoadRepoCommit(t, ctx)
  378. contexttest.LoadUser(t, ctx, 2)
  379. contexttest.LoadGitRepo(t, ctx)
  380. defer ctx.Repo.GitRepo.Close()
  381. repo := ctx.Repo.Repository
  382. doer := ctx.Doer
  383. opts := getDeleteRepoFilesOptions(repo)
  384. t.Run("Delete README.md file", func(t *testing.T) {
  385. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  386. assert.NoError(t, err)
  387. expectedFileResponse := getExpectedFileResponseForRepofilesDelete(u)
  388. assert.NotNil(t, filesResponse)
  389. assert.Nil(t, filesResponse.Files[0])
  390. assert.EqualValues(t, expectedFileResponse.Commit.Message, filesResponse.Commit.Message)
  391. assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, filesResponse.Commit.Author.Identity)
  392. assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, filesResponse.Commit.Committer.Identity)
  393. assert.EqualValues(t, expectedFileResponse.Verification, filesResponse.Verification)
  394. })
  395. t.Run("Verify README.md has been deleted", func(t *testing.T) {
  396. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  397. assert.Nil(t, filesResponse)
  398. expectedError := "repository file does not exist [path: " + opts.Files[0].TreePath + "]"
  399. assert.EqualError(t, err, expectedError)
  400. })
  401. }
  402. // Test opts with branch names removed, same results
  403. func TestChangeRepoFilesForDeleteWithoutBranchNames(t *testing.T) {
  404. onGiteaRun(t, testDeleteRepoFilesWithoutBranchNames)
  405. }
  406. func testDeleteRepoFilesWithoutBranchNames(t *testing.T, u *url.URL) {
  407. // setup
  408. unittest.PrepareTestEnv(t)
  409. ctx, _ := contexttest.MockContext(t, "user2/repo1")
  410. ctx.SetParams(":id", "1")
  411. contexttest.LoadRepo(t, ctx, 1)
  412. contexttest.LoadRepoCommit(t, ctx)
  413. contexttest.LoadUser(t, ctx, 2)
  414. contexttest.LoadGitRepo(t, ctx)
  415. defer ctx.Repo.GitRepo.Close()
  416. repo := ctx.Repo.Repository
  417. doer := ctx.Doer
  418. opts := getDeleteRepoFilesOptions(repo)
  419. opts.OldBranch = ""
  420. opts.NewBranch = ""
  421. t.Run("Delete README.md without Branch Name", func(t *testing.T) {
  422. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  423. assert.NoError(t, err)
  424. expectedFileResponse := getExpectedFileResponseForRepofilesDelete(u)
  425. assert.NotNil(t, filesResponse)
  426. assert.Nil(t, filesResponse.Files[0])
  427. assert.EqualValues(t, expectedFileResponse.Commit.Message, filesResponse.Commit.Message)
  428. assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, filesResponse.Commit.Author.Identity)
  429. assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, filesResponse.Commit.Committer.Identity)
  430. assert.EqualValues(t, expectedFileResponse.Verification, filesResponse.Verification)
  431. })
  432. }
  433. func TestChangeRepoFilesErrors(t *testing.T) {
  434. // setup
  435. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  436. ctx, _ := contexttest.MockContext(t, "user2/repo1")
  437. ctx.SetParams(":id", "1")
  438. contexttest.LoadRepo(t, ctx, 1)
  439. contexttest.LoadRepoCommit(t, ctx)
  440. contexttest.LoadUser(t, ctx, 2)
  441. contexttest.LoadGitRepo(t, ctx)
  442. defer ctx.Repo.GitRepo.Close()
  443. repo := ctx.Repo.Repository
  444. doer := ctx.Doer
  445. t.Run("bad branch", func(t *testing.T) {
  446. opts := getUpdateRepoFilesOptions(repo)
  447. opts.OldBranch = "bad_branch"
  448. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  449. assert.Error(t, err)
  450. assert.Nil(t, filesResponse)
  451. expectedError := "branch does not exist [name: " + opts.OldBranch + "]"
  452. assert.EqualError(t, err, expectedError)
  453. })
  454. t.Run("bad SHA", func(t *testing.T) {
  455. opts := getUpdateRepoFilesOptions(repo)
  456. origSHA := opts.Files[0].SHA
  457. opts.Files[0].SHA = "bad_sha"
  458. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  459. assert.Nil(t, filesResponse)
  460. assert.Error(t, err)
  461. expectedError := "sha does not match [given: " + opts.Files[0].SHA + ", expected: " + origSHA + "]"
  462. assert.EqualError(t, err, expectedError)
  463. })
  464. t.Run("new branch already exists", func(t *testing.T) {
  465. opts := getUpdateRepoFilesOptions(repo)
  466. opts.NewBranch = "develop"
  467. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  468. assert.Nil(t, filesResponse)
  469. assert.Error(t, err)
  470. expectedError := "branch already exists [name: " + opts.NewBranch + "]"
  471. assert.EqualError(t, err, expectedError)
  472. })
  473. t.Run("treePath is empty:", func(t *testing.T) {
  474. opts := getUpdateRepoFilesOptions(repo)
  475. opts.Files[0].TreePath = ""
  476. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  477. assert.Nil(t, filesResponse)
  478. assert.Error(t, err)
  479. expectedError := "path contains a malformed path component [path: ]"
  480. assert.EqualError(t, err, expectedError)
  481. })
  482. t.Run("treePath is a git directory:", func(t *testing.T) {
  483. opts := getUpdateRepoFilesOptions(repo)
  484. opts.Files[0].TreePath = ".git"
  485. filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  486. assert.Nil(t, filesResponse)
  487. assert.Error(t, err)
  488. expectedError := "path contains a malformed path component [path: " + opts.Files[0].TreePath + "]"
  489. assert.EqualError(t, err, expectedError)
  490. })
  491. t.Run("create file that already exists", func(t *testing.T) {
  492. opts := getCreateRepoFilesOptions(repo)
  493. opts.Files[0].TreePath = "README.md" // already exists
  494. fileResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts)
  495. assert.Nil(t, fileResponse)
  496. assert.Error(t, err)
  497. expectedError := "repository file already exists [path: " + opts.Files[0].TreePath + "]"
  498. assert.EqualError(t, err, expectedError)
  499. })
  500. })
  501. }