2022-12-16 00:45:22 +08:00
package runner
import (
2023-01-20 04:49:11 +08:00
"context"
"errors"
2022-12-16 00:45:22 +08:00
"fmt"
2023-01-20 04:49:11 +08:00
"io/fs"
"os"
2022-12-16 00:45:22 +08:00
"path"
2023-01-20 04:49:11 +08:00
"regexp"
"strings"
"sync"
2022-12-16 00:45:22 +08:00
"github.com/nektos/act/pkg/common"
2023-01-20 04:49:11 +08:00
"github.com/nektos/act/pkg/common/git"
2022-12-16 00:45:22 +08:00
"github.com/nektos/act/pkg/model"
)
func newLocalReusableWorkflowExecutor ( rc * RunContext ) common . Executor {
2023-01-20 04:49:11 +08:00
return newReusableWorkflowExecutor ( rc , rc . Config . Workdir , rc . Run . Job ( ) . Uses )
2022-12-16 00:45:22 +08:00
}
func newRemoteReusableWorkflowExecutor ( rc * RunContext ) common . Executor {
2023-01-20 04:49:11 +08:00
uses := rc . Run . Job ( ) . Uses
2022-12-16 00:45:22 +08:00
2023-01-20 04:49:11 +08:00
remoteReusableWorkflow := newRemoteReusableWorkflow ( uses )
if remoteReusableWorkflow == nil {
return common . NewErrorExecutor ( fmt . Errorf ( "expected format {owner}/{repo}/.github/workflows/{filename}@{ref}. Actual '%s' Input string was not in a correct format" , uses ) )
2022-12-16 00:45:22 +08:00
}
2023-01-20 04:49:11 +08:00
remoteReusableWorkflow . URL = rc . Config . GitHubInstance
workflowDir := fmt . Sprintf ( "%s/%s" , rc . ActionCacheDir ( ) , strings . ReplaceAll ( uses , "/" , "-" ) )
return common . NewPipelineExecutor (
newMutexExecutor ( cloneIfRequired ( rc , * remoteReusableWorkflow , workflowDir ) ) ,
newReusableWorkflowExecutor ( rc , workflowDir , fmt . Sprintf ( "./.github/workflows/%s" , remoteReusableWorkflow . Filename ) ) ,
)
}
var (
executorLock sync . Mutex
)
2022-12-16 00:45:22 +08:00
2023-01-20 04:49:11 +08:00
func newMutexExecutor ( executor common . Executor ) common . Executor {
return func ( ctx context . Context ) error {
executorLock . Lock ( )
defer executorLock . Unlock ( )
2022-12-16 00:45:22 +08:00
2023-01-20 04:49:11 +08:00
return executor ( ctx )
2022-12-16 00:45:22 +08:00
}
2023-01-20 04:49:11 +08:00
}
func cloneIfRequired ( rc * RunContext , remoteReusableWorkflow remoteReusableWorkflow , targetDirectory string ) common . Executor {
return common . NewConditionalExecutor (
func ( ctx context . Context ) bool {
_ , err := os . Stat ( targetDirectory )
notExists := errors . Is ( err , fs . ErrNotExist )
return notExists
} ,
git . NewGitCloneExecutor ( git . NewGitCloneExecutorInput {
URL : remoteReusableWorkflow . CloneURL ( ) ,
Ref : remoteReusableWorkflow . Ref ,
Dir : targetDirectory ,
Token : rc . Config . Token ,
} ) ,
nil ,
)
}
func newReusableWorkflowExecutor ( rc * RunContext , directory string , workflow string ) common . Executor {
return func ( ctx context . Context ) error {
planner , err := model . NewWorkflowPlanner ( path . Join ( directory , workflow ) , true )
if err != nil {
return err
}
2022-12-16 00:45:22 +08:00
2023-01-20 04:49:11 +08:00
plan := planner . PlanEvent ( "workflow_call" )
runner , err := NewReusableWorkflowRunner ( rc )
if err != nil {
return err
}
return runner . NewPlanExecutor ( plan ) ( ctx )
}
2022-12-16 00:45:22 +08:00
}
func NewReusableWorkflowRunner ( rc * RunContext ) ( Runner , error ) {
runner := & runnerImpl {
config : rc . Config ,
eventJSON : rc . EventJSON ,
caller : & caller {
runContext : rc ,
} ,
}
return runner . configure ( )
}
2023-01-20 04:49:11 +08:00
type remoteReusableWorkflow struct {
URL string
Org string
Repo string
Filename string
Ref string
}
func ( r * remoteReusableWorkflow ) CloneURL ( ) string {
return fmt . Sprintf ( "https://%s/%s/%s" , r . URL , r . Org , r . Repo )
}
func newRemoteReusableWorkflow ( uses string ) * remoteReusableWorkflow {
// GitHub docs:
// https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_iduses
r := regexp . MustCompile ( ` ^([^/]+)/([^/]+)/.github/workflows/([^@]+)@(.*)$ ` )
matches := r . FindStringSubmatch ( uses )
if len ( matches ) != 5 {
return nil
}
return & remoteReusableWorkflow {
Org : matches [ 1 ] ,
Repo : matches [ 2 ] ,
Filename : matches [ 3 ] ,
Ref : matches [ 4 ] ,
URL : "github.com" ,
}
}