@@ -76,6 +76,34 @@ func PinAction(action, inputYaml string) (string, bool) {
7676 pinnedAction := fmt .Sprintf ("%s@%s # %s" , leftOfAt [0 ], commitSHA , tagOrBranch )
7777 updated = ! strings .EqualFold (action , pinnedAction )
7878 inputYaml = strings .ReplaceAll (inputYaml , action , pinnedAction )
79+ yamlWithPreviousActionCommentsRemoved , wasModified := removePreviousActionComments (pinnedAction , inputYaml )
80+ if wasModified {
81+ return yamlWithPreviousActionCommentsRemoved , updated
82+ }
83+ return inputYaml , updated
84+ }
85+
86+ // It may be that there was already a comment next to the action
87+ // In this case we want to remove the earlier comment
88+ // we add a comment with the Action version so dependabot/ renovatebot can update it
89+ // if there was no comment next to any action, updated will be false
90+ func removePreviousActionComments (pinnedAction , inputYaml string ) (string , bool ) {
91+ updated := false
92+ stringParts := strings .Split (inputYaml , pinnedAction )
93+ if len (stringParts ) > 1 {
94+ inputYaml = ""
95+ inputYaml = stringParts [0 ]
96+ for idx := 1 ; idx < len (stringParts ); idx ++ {
97+ trimmedString := strings .SplitN (stringParts [idx ], "\n " , 2 )
98+ if len (trimmedString ) > 1 {
99+ if strings .Contains (trimmedString [0 ], "#" ) {
100+ updated = true
101+ }
102+ inputYaml = inputYaml + pinnedAction + "\n " + trimmedString [1 ]
103+ }
104+ }
105+ }
106+
79107 return inputYaml , updated
80108}
81109
0 commit comments