@@ -408,6 +408,121 @@ func TestIntegration_Repository_SubmitEditChange(t *testing.T) {
408408 })
409409}
410410
411+ func TestIntegration_Repository_SubmitEditChange_labels (t * testing.T ) {
412+ label1 := fixturetest .New (_fixtures , "label1" , func () string { return randomString (8 ) }).Get (t )
413+ label2 := fixturetest .New (_fixtures , "label2" , func () string { return randomString (8 ) }).Get (t )
414+ label3 := fixturetest .New (_fixtures , "label3" , func () string { return randomString (8 ) }).Get (t )
415+
416+ branchFixture := fixturetest .New (_fixtures , "branch" , func () string {
417+ return randomString (8 )
418+ })
419+
420+ branchName := branchFixture .Get (t )
421+ t .Logf ("Creating branch: %s" , branchName )
422+
423+ var (
424+ gitRepo * git.Repository // only when _update is true
425+ gitWork * git.Worktree
426+ )
427+ if * _update {
428+ t .
Setenv (
"GIT_AUTHOR_EMAIL" ,
"[email protected] " )
429+ t .Setenv ("GIT_AUTHOR_NAME" , "gs-test[bot]" )
430+ t .
Setenv (
"GIT_COMMITTER_EMAIL" ,
"[email protected] " )
431+ t .Setenv ("GIT_COMMITTER_NAME" , "gs-test[bot]" )
432+
433+ output := ioutil .TestLogWriter (t , "[git] " )
434+
435+ ctx := t .Context ()
436+
437+ t .Logf ("Cloning test-repo..." )
438+ repoDir := t .TempDir ()
439+ cmd := exec .
Command (
"git" ,
"clone" ,
"[email protected] :abg/test-repo.git" ,
repoDir )
440+ cmd .Stdout = output
441+ cmd .Stdout = output
442+ require .NoError (t , cmd .Run (), "failed to clone test-repo" )
443+
444+ var err error
445+ gitWork , err = git .OpenWorktree (ctx , repoDir , git.OpenOptions {
446+ Log : silogtest .New (t ),
447+ })
448+ require .NoError (t , err , "failed to open git repo" )
449+ gitRepo = gitWork .Repository ()
450+
451+ require .NoError (t , gitRepo .CreateBranch (ctx , git.CreateBranchRequest {
452+ Name : branchName ,
453+ }), "could not create branch: %s" , branchName )
454+ require .NoError (t , gitWork .Checkout (ctx , branchName ),
455+ "could not checkout branch: %s" , branchName )
456+ require .NoError (t , os .WriteFile (
457+ filepath .Join (repoDir , branchName + ".txt" ),
458+ []byte (randomString (32 )),
459+ 0o644 ,
460+ ), "could not write file to branch" )
461+
462+ cmd = exec .Command ("git" , "add" , "." )
463+ cmd .Dir = repoDir
464+ cmd .Stdout = output
465+ cmd .Stderr = output
466+ require .NoError (t , cmd .Run (), "git add failed" )
467+ require .NoError (t , gitWork .Commit (ctx , git.CommitRequest {
468+ Message : "commit from test" ,
469+ }), "could not commit changes" )
470+
471+ t .Logf ("Pushing to origin" )
472+ require .NoError (t ,
473+ gitWork .Push (ctx , git.PushOptions {
474+ Remote : "origin" ,
475+ Refspec : git .Refspec (branchName ),
476+ }), "error pushing branch" )
477+
478+ t .Cleanup (func () {
479+ ctx := context .WithoutCancel (t .Context ())
480+ t .Logf ("Deleting remote branch: %s" , branchName )
481+ assert .NoError (t ,
482+ gitWork .Push (ctx , git.PushOptions {
483+ Remote : "origin" ,
484+ Refspec : git .Refspec (":" + branchName ),
485+ }), "error deleting branch" )
486+ })
487+ }
488+
489+ ctx := t .Context ()
490+ rec := newRecorder (t , t .Name ())
491+ ghc := newGitLabClient (rec .GetDefaultClient ())
492+ repo , err := gitlab .NewRepository (
493+ ctx , new (gitlab.Forge ), "abg" , "test-repo" , silogtest .New (t ), ghc , _testRepoID ,
494+ )
495+ require .NoError (t , err )
496+
497+ change , err := repo .SubmitChange (ctx , forge.SubmitChangeRequest {
498+ Subject : branchName ,
499+ Body : "Test PR" ,
500+ Base : "main" ,
501+ Head : branchName ,
502+ Labels : []string {label1 },
503+ })
504+ require .NoError (t , err , "error creating PR" )
505+ changeID := change .ID
506+
507+ t .Run ("AddNewLabel" , func (t * testing.T ) {
508+ require .NoError (t ,
509+ repo .EditChange (t .Context (), changeID , forge.EditChangeOptions {
510+ Labels : []string {label2 },
511+ }), "could not add labels to PR" )
512+ })
513+
514+ t .Run ("AddExistingLabel" , func (t * testing.T ) {
515+ require .NoError (t ,
516+ repo .EditChange (t .Context (), changeID , forge.EditChangeOptions {
517+ Labels : []string {label2 , label3 },
518+ }), "could not add existing label to PR" )
519+ })
520+
521+ gotLabels , err := repo .ChangeLabels (t .Context (), changeID )
522+ require .NoError (t , err , "could not get labels for PR" )
523+ assert .ElementsMatch (t , []string {label1 , label2 , label3 }, gotLabels )
524+ }
525+
411526func TestIntegration_Repository_comments (t * testing.T ) {
412527 ctx := t .Context ()
413528 rec := newRecorder (t , t .Name ())
0 commit comments