11import type { Logger } from 'pino' ;
22
3+ import { log as logDb } from '@/backend/db/log' ;
4+ import { getLogs , mainLogger } from '@/backend/utils/logger' ;
5+
6+ const event = 'git-squash-updates' ;
7+ const logger = mainLogger . child ( { event } ) ;
8+
39const SQUASH_UPDATE_MESSAGE = process . env . SQUASH_UPDATE_MESSAGE || 'Update dependencies' ;
4- const SQUASH__DEPS_DAYS_AGO = Number . parseInt ( process . env . SQUASH_DAYS_AGO || '5' ) ;
10+ const SQUASH_DEPS_DAYS_AGO = Number . parseInt ( process . env . SQUASH_DAYS_AGO || '5' ) ;
511const SQUASH_MAX_UPDATE_COMMITS = Number . parseInt ( process . env . SQUASH_MAX_UPDATE_COMMITS || '5' ) ;
612
713const buildMessage = ( subject : string , body ?: string ) => {
@@ -143,7 +149,8 @@ const pushToRemote = async (
143149
144150export const squashUpdates = async (
145151 sshRun : ( cmd : string ) => Promise < { stdout : string } > ,
146- logger : Logger
152+ jobId : number ,
153+ hostId : number
147154) => {
148155 logger . info ( 'Squashing dependency update commits' ) ;
149156
@@ -153,12 +160,10 @@ export const squashUpdates = async (
153160 return ;
154161 }
155162
156- // Keep squashing while the previous commit is from an automated author
157- let continueSquashing = true ;
158163 let loopCount = 0 ;
159164 const MAX_SQUASH_LOOPS = 50 ; // To prevent infinite loops
160165
161- while ( continueSquashing && loopCount < MAX_SQUASH_LOOPS ) {
166+ while ( loopCount < MAX_SQUASH_LOOPS ) {
162167 loopCount ++ ;
163168 logger . info ( `Squash loop iteration ${ loopCount } ` ) ;
164169
@@ -169,11 +174,12 @@ export const squashUpdates = async (
169174 const prevCommit = await getCommitMetadata ( sshRun , 1 ) ;
170175
171176 const isAutomatedAuthor = / d e p e n d a b o t | r e n o v a t e / i. test ( prevCommit . authorName ) ;
177+ const X_DAYS_AGO = Date . now ( ) - SQUASH_DEPS_DAYS_AGO * 24 * 60 * 60 * 1000 ;
172178
173- // If previous commit is not from automated author, stop squashing
174- if ( ! isAutomatedAuthor ) {
175- continueSquashing = false ;
176- } else {
179+ if (
180+ isAutomatedAuthor ||
181+ ( prevCommit . timestamp > X_DAYS_AGO && prevCommit . subject . includes ( SQUASH_UPDATE_MESSAGE ) )
182+ ) {
177183 // Previous commit is from automated author, squash them together
178184 logger . info ( `Squashing last 2 commits together (previous author: ${ prevCommit . authorName } )` ) ;
179185 await squashLastTwoCommits (
@@ -183,25 +189,21 @@ export const squashUpdates = async (
183189 prevCommit . body ,
184190 prevCommit . authorDate
185191 ) ;
192+ continue ;
186193 }
194+
195+ if ( ! lastCommit . subject . includes ( SQUASH_UPDATE_MESSAGE ) ) {
196+ logger . info ( 'Updating current commit message only (previous is old or not an update commit)' ) ;
197+ await updateCurrentCommit ( sshRun , lastCommit . subject , lastCommit . body ) ;
198+ }
199+
200+ break ;
187201 }
188202
189203 if ( loopCount >= MAX_SQUASH_LOOPS ) {
190204 logger . warn ( `Reached maximum squash loop limit of ${ MAX_SQUASH_LOOPS } ` ) ;
191205 }
192206
193- // Get current commit info after squashing
194- const lastCommit = await getCommitMetadata ( sshRun , 0 ) ;
195- const prevCommit = await getCommitMetadata ( sshRun , 1 ) ;
196-
197- const DAYS_AGO = Date . now ( ) - SQUASH__DEPS_DAYS_AGO * 24 * 60 * 60 * 1000 ;
198-
199- // If previous commit is old or doesn't start with "Update dependencies", just update current commit
200- if ( prevCommit . timestamp < DAYS_AGO || ! prevCommit . subject . includes ( SQUASH_UPDATE_MESSAGE ) ) {
201- logger . info ( 'Updating current commit message only (previous is old or not an update commit)' ) ;
202- await updateCurrentCommit ( sshRun , lastCommit . subject , lastCommit . body ) ;
203- }
204-
205207 // After squashing, check if we've exceeded the max number of update commits
206208 const checkCommitCount = SQUASH_MAX_UPDATE_COMMITS + 1 ;
207209 const recentCommits = await sshRun ( `git log -${ checkCommitCount } --format="%s"` ) ;
@@ -219,4 +221,8 @@ export const squashUpdates = async (
219221
220222 await pushToRemote ( sshRun , logger ) ;
221223 logger . info ( 'Finished squashing dependency update commits' ) ;
224+
225+ getLogs ( event ) . map ( async ( log ) => {
226+ await logDb . create ( { jobId, hostId, ...log } ) ;
227+ } ) ;
222228} ;
0 commit comments