@@ -395,6 +395,100 @@ jobs:
395395 })
396396}
397397
398+ func TestWorkflowDispatchConcurrency (t * testing.T ) {
399+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
400+ user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
401+ session := loginUser (t , user2 .Name )
402+ token := getTokenForLoggedInUser (t , session , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
403+
404+ apiRepo := createActionsTestRepo (t , token , "actions-concurrency" , false )
405+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : apiRepo .ID })
406+ runner := newMockRunner ()
407+ runner .registerAsRepoRunner (t , user2 .Name , repo .Name , "mock-runner" , []string {"ubuntu-latest" })
408+
409+ wf1TreePath := ".gitea/workflows/workflow-dispatch-concurrency.yml"
410+ wf1FileContent := `name: workflow-dispatch-concurrency
411+ on:
412+ workflow_dispatch:
413+ inputs:
414+ appVersion:
415+ description: 'APP version'
416+ required: true
417+ default: 'v1.23'
418+ type: choice
419+ options:
420+ - v1.21
421+ - v1.22
422+ - v1.23
423+ cancel:
424+ description: 'Cancel running workflows'
425+ required: false
426+ type: boolean
427+ default: false
428+ concurrency:
429+ group: workflow-dispatch-${{ inputs.appVersion }}
430+ cancel-in-progress: ${{ inputs.cancel }}
431+ jobs:
432+ job:
433+ runs-on: ubuntu-latest
434+ steps:
435+ - run: echo 'workflow dispatch job'
436+ `
437+
438+ opts1 := getWorkflowCreateFileOptions (user2 , repo .DefaultBranch , fmt .Sprintf ("create %s" , wf1TreePath ), wf1FileContent )
439+ createWorkflowFile (t , token , user2 .Name , repo .Name , wf1TreePath , opts1 )
440+
441+ // run the workflow with appVersion=v1.21 and cancel=false
442+ urlStr := fmt .Sprintf ("/%s/%s/actions/run?workflow=%s" , user2 .Name , repo .Name , "workflow-dispatch-concurrency.yml" )
443+ req := NewRequestWithValues (t , "POST" , urlStr , map [string ]string {
444+ "_csrf" : GetUserCSRFToken (t , session ),
445+ "ref" : "refs/heads/main" ,
446+ "appVersion" : "v1.21" ,
447+ })
448+ session .MakeRequest (t , req , http .StatusSeeOther )
449+ task1 := runner .fetchTask (t )
450+ _ , _ , run1 := getTaskAndJobAndRunByTaskID (t , task1 .Id )
451+ assert .Equal (t , "workflow-dispatch-v1.21" , run1 .ConcurrencyGroup )
452+
453+ // run the workflow with appVersion=v1.22 and cancel=false
454+ req = NewRequestWithValues (t , "POST" , urlStr , map [string ]string {
455+ "_csrf" : GetUserCSRFToken (t , session ),
456+ "ref" : "refs/heads/main" ,
457+ "appVersion" : "v1.22" ,
458+ })
459+ session .MakeRequest (t , req , http .StatusSeeOther )
460+ task2 := runner .fetchTask (t )
461+ _ , _ , run2 := getTaskAndJobAndRunByTaskID (t , task2 .Id )
462+ assert .Equal (t , "workflow-dispatch-v1.22" , run2 .ConcurrencyGroup )
463+
464+ // run the workflow with appVersion=v1.22 and cancel=false again
465+ req = NewRequestWithValues (t , "POST" , urlStr , map [string ]string {
466+ "_csrf" : GetUserCSRFToken (t , session ),
467+ "ref" : "refs/heads/main" ,
468+ "appVersion" : "v1.22" ,
469+ })
470+ session .MakeRequest (t , req , http .StatusSeeOther )
471+ runner .fetchNoTask (t ) // cannot fetch task since task2 is not completed
472+
473+ // run the workflow with appVersion=v1.22 and cancel=true
474+ req = NewRequestWithValues (t , "POST" , urlStr , map [string ]string {
475+ "_csrf" : GetUserCSRFToken (t , session ),
476+ "ref" : "refs/heads/main" ,
477+ "appVersion" : "v1.22" ,
478+ "cancel" : "on" ,
479+ })
480+ session .MakeRequest (t , req , http .StatusSeeOther )
481+ task4 := runner .fetchTask (t )
482+ _ , _ , run4 := getTaskAndJobAndRunByTaskID (t , task4 .Id )
483+ assert .Equal (t , "workflow-dispatch-v1.22" , run4 .ConcurrencyGroup )
484+ _ , _ , run2 = getTaskAndJobAndRunByTaskID (t , task2 .Id )
485+ assert .True (t , run2 .Status .IsCancelled ())
486+
487+ httpContext := NewAPITestContext (t , user2 .Name , repo .Name , auth_model .AccessTokenScopeWriteRepository )
488+ doAPIDeleteRepository (httpContext )(t )
489+ })
490+ }
491+
398492func getTaskAndJobAndRunByTaskID (t * testing.T , taskID int64 ) (* actions_model.ActionTask , * actions_model.ActionRunJob , * actions_model.ActionRun ) {
399493 actionTask := unittest .AssertExistsAndLoadBean (t , & actions_model.ActionTask {ID : taskID })
400494 actionRunJob := unittest .AssertExistsAndLoadBean (t , & actions_model.ActionRunJob {ID : actionTask .JobID })
0 commit comments