11import type { Core } from '@strapi/strapi' ;
22import fetch from 'node-fetch' ;
33
4+ const lastTriggered : Record < string | number , string > = { } ;
5+
46async function triggerGithub ( strapi : Core . Strapi ) {
57 const owner = process . env . GITHUB_OWNER ;
68 const repo = process . env . GITHUB_REPO ;
@@ -49,6 +51,40 @@ async function triggerGithub(strapi: Core.Strapi) {
4951 }
5052}
5153
54+ function shouldTriggerOnce ( event : any , strapi : Core . Strapi ) : boolean {
55+ const { result } = event ;
56+ const id = result ?. id ;
57+ const publishedAt = result ?. publishedAt ;
58+ const updatedAt = result ?. updatedAt ;
59+
60+ // se não estiver publicado, nem pensa em build
61+ if ( ! publishedAt ) {
62+ strapi . log . info (
63+ `[lifecycles global] id=${ id } ainda não publicado (publishedAt vazio), não disparando build.`
64+ ) ;
65+ return false ;
66+ }
67+
68+ if ( ! id || ! updatedAt ) {
69+ strapi . log . warn (
70+ '[lifecycles global] Sem id ou updatedAt no result, disparando build por segurança.'
71+ ) ;
72+ return true ;
73+ }
74+
75+ const last = lastTriggered [ id ] ;
76+
77+ if ( last === updatedAt ) {
78+ strapi . log . info (
79+ `[lifecycles global] Já disparei build para id=${ id } em updatedAt=${ updatedAt } , ignorando chamada duplicada.`
80+ ) ;
81+ return false ;
82+ }
83+
84+ lastTriggered [ id ] = updatedAt ;
85+ return true ;
86+ }
87+
5288export default {
5389 register ( ) { } ,
5490
@@ -66,33 +102,31 @@ export default {
66102 strapi . db . lifecycles . subscribe ( {
67103 models : [ 'api::show.show' ] ,
68104
69- // 👉 Não dispara nada na criação, só loga
70105 async afterCreate ( event ) {
71106 const { result } = event ;
72107 strapi . log . info (
73- `[lifecycles global] afterCreate(show) id=${ result ?. id } publishedAt=${ result ?. publishedAt } `
108+ `[lifecycles global] afterCreate(show) id=${ result ?. id } publishedAt=${ result ?. publishedAt } updatedAt= ${ result ?. updatedAt } `
74109 ) ;
110+
111+ if ( shouldTriggerOnce ( event , strapi ) ) {
112+ strapi . log . info (
113+ '[lifecycles global] afterCreate => registro publicado, disparando triggerGithub()'
114+ ) ;
115+ await triggerGithub ( strapi ) ;
116+ }
75117 } ,
76118
77- // 👉 Só dispara quando o registro ESTÁ publicado
78119 async afterUpdate ( event ) {
79120 const { result } = event ;
80-
81- const isPublished = ! ! result ?. publishedAt ;
82-
83121 strapi . log . info (
84- `[lifecycles global] afterUpdate(show) id=${ result ?. id } isPublished =${ isPublished } `
122+ `[lifecycles global] afterUpdate(show) id=${ result ?. id } publishedAt =${ result ?. publishedAt } updatedAt= ${ result ?. updatedAt } `
85123 ) ;
86124
87- if ( isPublished ) {
125+ if ( shouldTriggerOnce ( event , strapi ) ) {
88126 strapi . log . info (
89- '[lifecycles global] Registro publicado/atualizado , disparando triggerGithub()'
127+ '[lifecycles global] afterUpdate => registro publicado, disparando triggerGithub()'
90128 ) ;
91129 await triggerGithub ( strapi ) ;
92- } else {
93- strapi . log . info (
94- '[lifecycles global] Registro ainda não publicado, não disparando build.'
95- ) ;
96130 }
97131 } ,
98132 } ) ;
0 commit comments