66 "errors"
77 "fmt"
88 "sort"
9- "strings"
109 "sync"
1110 "time"
1211
@@ -245,92 +244,6 @@ var (
245244 EventTypeRunFinish EventType = "runFinish"
246245)
247246
248- func getToolRefInput (prg * types.Program , ref types.ToolReference , input string ) (string , error ) {
249- if ref .Arg == "" {
250- return "" , nil
251- }
252-
253- targetArgs := prg .ToolSet [ref .ToolID ].Arguments
254- targetKeys := map [string ]string {}
255-
256- if ref .Arg == "*" {
257- return input , nil
258- }
259-
260- if targetArgs == nil {
261- return "" , nil
262- }
263-
264- for targetKey := range targetArgs .Properties {
265- targetKeys [strings .ToLower (targetKey )] = targetKey
266- }
267-
268- inputMap := map [string ]interface {}{}
269- outputMap := map [string ]interface {}{}
270-
271- _ = json .Unmarshal ([]byte (input ), & inputMap )
272- for k , v := range inputMap {
273- inputMap [strings .ToLower (k )] = v
274- }
275-
276- fields := strings .Fields (ref .Arg )
277-
278- for i := 0 ; i < len (fields ); i ++ {
279- field := fields [i ]
280- if field == "and" {
281- continue
282- }
283- if field == "as" {
284- i ++
285- continue
286- }
287-
288- var (
289- keyName string
290- val any
291- )
292-
293- if strings .HasPrefix (field , "$" ) {
294- key := strings .TrimPrefix (field , "$" )
295- key = strings .TrimPrefix (key , "{" )
296- key = strings .TrimSuffix (key , "}" )
297- val = inputMap [strings .ToLower (key )]
298- } else {
299- val = field
300- }
301-
302- if len (fields ) > i + 1 && fields [i + 1 ] == "as" {
303- keyName = strings .ToLower (fields [i + 2 ])
304- }
305-
306- if len (targetKeys ) == 0 {
307- return "" , fmt .Errorf ("can not assign arg to context because target tool [%s] has no defined args" , ref .ToolID )
308- }
309-
310- if keyName == "" {
311- if len (targetKeys ) != 1 {
312- return "" , fmt .Errorf ("can not assign arg to context because target tool [%s] has does not have one args. You must use \" as\" syntax to map the arg to a key %v" , ref .ToolID , targetKeys )
313- }
314- for k := range targetKeys {
315- keyName = k
316- }
317- }
318-
319- if targetKey , ok := targetKeys [strings .ToLower (keyName )]; ok {
320- outputMap [targetKey ] = val
321- } else {
322- return "" , fmt .Errorf ("can not assign arg to context because target tool [%s] has does not args [%s]" , ref .ToolID , keyName )
323- }
324- }
325-
326- if len (outputMap ) == 0 {
327- return "" , nil
328- }
329-
330- output , err := json .Marshal (outputMap )
331- return string (output ), err
332- }
333-
334247func (r * Runner ) getContext (callCtx engine.Context , state * State , monitor Monitor , env []string , input string ) (result []engine.InputContext , _ error ) {
335248 toolRefs , err := callCtx .Tool .GetToolsByType (callCtx .Program , types .ToolTypeContext )
336249 if err != nil {
@@ -343,7 +256,7 @@ func (r *Runner) getContext(callCtx engine.Context, state *State, monitor Monito
343256 continue
344257 }
345258
346- contextInput , err := getToolRefInput (callCtx .Program , toolRef , input )
259+ contextInput , err := types . GetToolRefInput (callCtx .Program , toolRef , input )
347260 if err != nil {
348261 return nil , err
349262 }
@@ -878,18 +791,9 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
878791 refresh bool
879792 )
880793
881- // Only try to look up the cred if the tool is on GitHub or has an alias.
882- // If it is a GitHub tool and has an alias, the alias overrides the tool name, so we use it as the credential name.
883- if isGitHubTool (toolName ) && credentialAlias == "" {
884- c , exists , err = r .credStore .Get (callCtx .Ctx , toolName )
885- if err != nil {
886- return nil , fmt .Errorf ("failed to get credentials for tool %s: %w" , toolName , err )
887- }
888- } else if credentialAlias != "" {
889- c , exists , err = r .credStore .Get (callCtx .Ctx , credentialAlias )
890- if err != nil {
891- return nil , fmt .Errorf ("failed to get credential %s: %w" , credentialAlias , err )
892- }
794+ c , exists , err = r .credStore .Get (callCtx .Ctx , credName )
795+ if err != nil {
796+ return nil , fmt .Errorf ("failed to get credentials for tool %s: %w" , toolName , err )
893797 }
894798
895799 if c == nil {
@@ -955,22 +859,17 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
955859 }
956860
957861 if ! resultCredential .Ephemeral {
958- // Only store the credential if the tool is on GitHub or has an alias, and the credential is non-empty.
959- if (isGitHubTool (toolName ) && callCtx .Program .ToolSet [ref .ToolID ].Source .Repo != nil ) || credentialAlias != "" {
960- if isEmpty {
961- log .Warnf ("Not saving empty credential for tool %s" , toolName )
862+ if isEmpty {
863+ log .Warnf ("Not saving empty credential for tool %s" , toolName )
864+ } else {
865+ if refresh {
866+ err = r .credStore .Refresh (callCtx .Ctx , resultCredential )
962867 } else {
963- if refresh {
964- err = r .credStore .Refresh (callCtx .Ctx , resultCredential )
965- } else {
966- err = r .credStore .Add (callCtx .Ctx , resultCredential )
967- }
968- if err != nil {
969- return nil , fmt .Errorf ("failed to save credential for tool %s: %w" , toolName , err )
970- }
868+ err = r .credStore .Add (callCtx .Ctx , resultCredential )
869+ }
870+ if err != nil {
871+ return nil , fmt .Errorf ("failed to save credential for tool %s: %w" , toolName , err )
971872 }
972- } else {
973- log .Warnf ("Not saving credential for tool %s - credentials will only be saved for tools from GitHub, or tools that use aliases." , toolName )
974873 }
975874 }
976875 } else {
@@ -992,7 +891,3 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
992891
993892 return env , nil
994893}
995-
996- func isGitHubTool (toolName string ) bool {
997- return strings .HasPrefix (toolName , "github.com" )
998- }
0 commit comments