@@ -17,7 +17,9 @@ import (
1717 "code.gitea.io/gitea/models/unittest"
1818 user_model "code.gitea.io/gitea/models/user"
1919 api "code.gitea.io/gitea/modules/structs"
20+ "code.gitea.io/gitea/modules/timeutil"
2021 "code.gitea.io/gitea/modules/util"
22+ webhook_module "code.gitea.io/gitea/modules/webhook"
2123 actions_service "code.gitea.io/gitea/services/actions"
2224
2325 "github.com/stretchr/testify/assert"
@@ -470,7 +472,7 @@ jobs:
470472 "appVersion" : "v1.22" ,
471473 })
472474 session .MakeRequest (t , req , http .StatusSeeOther )
473- runner .fetchNoTask (t ) // cannot fetch task since task2 is not completed
475+ runner .fetchNoTask (t ) // cannot fetch task because task2 is not completed
474476
475477 // run the workflow with appVersion=v1.22 and cancel=true
476478 req = NewRequestWithValues (t , "POST" , urlStr , map [string ]string {
@@ -499,39 +501,95 @@ func TestScheduleConcurrency(t *testing.T) {
499501
500502 apiRepo := createActionsTestRepo (t , token , "actions-concurrency" , false )
501503 repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : apiRepo .ID })
504+ httpContext := NewAPITestContext (t , user2 .Name , repo .Name , auth_model .AccessTokenScopeWriteRepository )
505+ defer doAPIDeleteRepository (httpContext )(t )
502506 runner := newMockRunner ()
503507 runner .registerAsRepoRunner (t , user2 .Name , repo .Name , "mock-runner" , []string {"ubuntu-latest" })
504508
505- // add a variable for test
506- req := NewRequestWithJSON (t , "POST" ,
507- fmt .Sprintf ("/api/v1/repos/%s/%s/actions/variables/cancel_schedule" , user2 .Name , repo .Name ), & api.CreateVariableOption {
508- Value : "false" ,
509- }).
510- AddTokenAuth (token )
511- MakeRequest (t , req , http .StatusNoContent )
512-
513509 wf1TreePath := ".gitea/workflows/schedule-concurrency.yml"
514510 wf1FileContent := `name: schedule-concurrency
515511on:
512+ push:
516513 schedule:
517- - cron: '0/5 * * * * '
514+ - cron: '@every 1m '
518515concurrency:
519516 group: schedule-concurrency
520- cancel-in-progress: ${{ vars.cancel_schedule == 'false ' }}
517+ cancel-in-progress: ${{ gitea.event_name == 'push ' }}
521518jobs:
522519 job:
523520 runs-on: ubuntu-latest
524521 steps:
525- - run: echo 'workflow dispatch job '
522+ - run: echo 'schedule workflow '
526523`
527524
528525 opts1 := getWorkflowCreateFileOptions (user2 , repo .DefaultBranch , fmt .Sprintf ("create %s" , wf1TreePath ), wf1FileContent )
529526 createWorkflowFile (t , token , user2 .Name , repo .Name , wf1TreePath , opts1 )
530527
528+ // fetch the task triggered by push
529+ task1 := runner .fetchTask (t )
530+ _ , _ , run1 := getTaskAndJobAndRunByTaskID (t , task1 .Id )
531+ assert .Equal (t , "schedule-concurrency" , run1 .ConcurrencyGroup )
532+ assert .True (t , run1 .ConcurrencyCancel )
533+ assert .Equal (t , string (webhook_module .HookEventPush ), run1 .TriggerEvent )
534+ assert .Equal (t , actions_model .StatusRunning , run1 .Status )
535+
536+ // trigger the task by schedule
537+ spec := unittest .AssertExistsAndLoadBean (t , & actions_model.ActionScheduleSpec {RepoID : repo .ID })
538+ spec .Next = timeutil .TimeStampNow () // manually update "Next"
539+ assert .NoError (t , actions_model .UpdateScheduleSpec (context .Background (), spec , "next" ))
540+ assert .NoError (t , actions_service .StartScheduleTasks (context .Background ()))
541+ runner .fetchNoTask (t ) // cannot fetch because task1 is not completed
542+ runner .execTask (t , task1 , & mockTaskOutcome {
543+ result : runnerv1 .Result_RESULT_SUCCESS ,
544+ })
545+ _ , _ , run1 = getTaskAndJobAndRunByTaskID (t , task1 .Id )
546+ assert .Equal (t , actions_model .StatusSuccess , run1 .Status )
547+ task2 := runner .fetchTask (t )
548+ _ , _ , run2 := getTaskAndJobAndRunByTaskID (t , task2 .Id )
549+ assert .Equal (t , "schedule-concurrency" , run2 .ConcurrencyGroup )
550+ assert .False (t , run2 .ConcurrencyCancel )
551+ assert .Equal (t , string (webhook_module .HookEventSchedule ), run2 .TriggerEvent )
552+ assert .Equal (t , actions_model .StatusRunning , run2 .Status )
553+
554+ // trigger the task by schedule again
555+ spec = unittest .AssertExistsAndLoadBean (t , & actions_model.ActionScheduleSpec {RepoID : repo .ID })
556+ spec .Next = timeutil .TimeStampNow () // manually update "Next"
557+ assert .NoError (t , actions_model .UpdateScheduleSpec (context .Background (), spec , "next" ))
531558 assert .NoError (t , actions_service .StartScheduleTasks (context .Background ()))
559+ runner .fetchNoTask (t ) // cannot fetch because task2 is not completed
560+ run3 := unittest .AssertExistsAndLoadBean (t , & actions_model.ActionRun {RepoID : repo .ID , Status : actions_model .StatusBlocked })
561+ assert .Equal (t , "schedule-concurrency" , run3 .ConcurrencyGroup )
562+ assert .False (t , run3 .ConcurrencyCancel )
563+ assert .Equal (t , string (webhook_module .HookEventSchedule ), run3 .TriggerEvent )
564+
565+ // trigger the task by push
566+ doAPICreateFile (httpContext , "doc.txt" , & api.CreateFileOptions {
567+ FileOptions : api.FileOptions {
568+ NewBranchName : "main" ,
569+ Message : "create doc.txt" ,
570+ Author : api.Identity {
571+ Name : user2 .Name ,
572+ Email : user2 .Email ,
573+ },
574+ Committer : api.Identity {
575+ Name : user2 .Name ,
576+ Email : user2 .Email ,
577+ },
578+ Dates : api.CommitDateOptions {
579+ Author : time .Now (),
580+ Committer : time .Now (),
581+ },
582+ },
583+ ContentBase64 : base64 .StdEncoding .EncodeToString ([]byte ("doc" )),
584+ })(t )
532585
533- httpContext := NewAPITestContext (t , user2 .Name , repo .Name , auth_model .AccessTokenScopeWriteRepository )
534- doAPIDeleteRepository (httpContext )(t )
586+ task4 := runner .fetchTask (t )
587+ _ , _ , run4 := getTaskAndJobAndRunByTaskID (t , task4 .Id )
588+ assert .Equal (t , "schedule-concurrency" , run4 .ConcurrencyGroup )
589+ assert .True (t , run4 .ConcurrencyCancel )
590+ assert .Equal (t , string (webhook_module .HookEventPush ), run4 .TriggerEvent )
591+ run3 = unittest .AssertExistsAndLoadBean (t , & actions_model.ActionRun {ID : run3 .ID })
592+ assert .Equal (t , actions_model .StatusCancelled , run3 .Status )
535593 })
536594}
537595
0 commit comments