@@ -3,6 +3,7 @@ package pin
33import (
44 "context"
55 "fmt"
6+ "log"
67 "os"
78 "path/filepath"
89 "regexp"
@@ -29,7 +30,10 @@ func PinActions(inputYaml string, exemptedActions []string, pinToImmutable bool,
2930 for _ , step := range job .Steps {
3031 if len (step .Uses ) > 0 {
3132 localUpdated := false
32- out , localUpdated = PinAction (step .Uses , out , exemptedActions , pinToImmutable , actionCommitMap )
33+ out , localUpdated , err = PinAction (step .Uses , out , exemptedActions , pinToImmutable , actionCommitMap )
34+ if err != nil {
35+ return out , updated , err
36+ }
3337 updated = updated || localUpdated
3438 }
3539 }
@@ -38,29 +42,36 @@ func PinActions(inputYaml string, exemptedActions []string, pinToImmutable bool,
3842 return out , updated , nil
3943}
4044
41- func PinAction (action , inputYaml string , exemptedActions []string , pinToImmutable bool , actionCommitMap map [string ]string ) (string , bool ) {
42-
45+ func PinAction (action , inputYaml string , exemptedActions []string , pinToImmutable bool , actionCommitMap map [string ]string ) (string , bool , error ) {
4346 updated := false
47+
4448 if ! strings .Contains (action , "@" ) || strings .HasPrefix (action , "docker://" ) {
45- return inputYaml , updated // Cannot pin local actions and docker actions
49+ return inputYaml , updated , nil // Cannot pin local actions and docker actions
4650 }
4751
4852 if isAbsolute (action ) || (pinToImmutable && IsImmutableAction (action )) {
49- return inputYaml , updated
53+ return inputYaml , updated , nil
5054 }
5155 leftOfAt := strings .Split (action , "@" )
5256 tagOrBranch := leftOfAt [1 ]
5357
5458 // skip pinning for exempted actions
5559 if ActionExists (leftOfAt [0 ], exemptedActions ) {
56- return inputYaml , updated
60+ return inputYaml , updated , nil
5761 }
5862
5963 splitOnSlash := strings .Split (leftOfAt [0 ], "/" )
6064 owner := splitOnSlash [0 ]
6165 repo := splitOnSlash [1 ]
6266
63- PAT := os .Getenv ("PAT" )
67+ // use secure repo token
68+ PAT := os .Getenv ("SECURE_REPO_PAT" )
69+ if PAT == "" {
70+ PAT = os .Getenv ("PAT" )
71+ log .Println ("SECURE_REPO_PAT is not set, using PAT" )
72+ } else {
73+ log .Println ("SECURE_REPO_PAT is set" )
74+ }
6475
6576 ctx := context .Background ()
6677 ts := oauth2 .StaticTokenSource (
@@ -81,7 +92,7 @@ func PinAction(action, inputYaml string, exemptedActions []string, pinToImmutabl
8192 if ! semanticTagRegex .MatchString (tagOrBranch ) {
8293 tagOrBranch , err = getSemanticVersion (client , owner , repo , tagOrBranch , commitSHA )
8394 if err != nil {
84- return inputYaml , updated
95+ return inputYaml , updated , err
8596 }
8697 }
8798 break
@@ -92,11 +103,11 @@ func PinAction(action, inputYaml string, exemptedActions []string, pinToImmutabl
92103 if commitSHA == "" {
93104 commitSHA , _ , err = client .Repositories .GetCommitSHA1 (ctx , owner , repo , tagOrBranch , "" )
94105 if err != nil {
95- return inputYaml , updated
106+ return inputYaml , updated , err
96107 }
97108 tagOrBranch , err = getSemanticVersion (client , owner , repo , tagOrBranch , commitSHA )
98109 if err != nil {
99- return inputYaml , updated
110+ return inputYaml , updated , err
100111 }
101112
102113 }
@@ -130,7 +141,7 @@ func PinAction(action, inputYaml string, exemptedActions []string, pinToImmutabl
130141 inputYaml = actionRegex .ReplaceAllString (inputYaml , pinnedActionWithVersion + "$2" )
131142
132143 inputYaml , _ = removePreviousActionComments (pinnedActionWithVersion , inputYaml )
133- return inputYaml , ! strings .EqualFold (action , pinnedActionWithVersion )
144+ return inputYaml , ! strings .EqualFold (action , pinnedActionWithVersion ), nil
134145 }
135146
136147 updated = ! strings .EqualFold (action , fullPinned )
@@ -162,7 +173,7 @@ func PinAction(action, inputYaml string, exemptedActions []string, pinToImmutabl
162173 )
163174 inputYaml , _ = removePreviousActionComments (fullPinned , inputYaml )
164175
165- return inputYaml , updated
176+ return inputYaml , updated , nil
166177}
167178
168179// It may be that there was already a comment next to the action
@@ -263,3 +274,7 @@ func ActionExists(actionName string, patterns []string) bool {
263274 }
264275 return false
265276}
277+
278+ func UsingSecureRepoPAT () bool {
279+ return os .Getenv ("SECURE_REPO_PAT" ) != ""
280+ }
0 commit comments