2121import java .util .LinkedHashMap ;
2222import java .util .List ;
2323import java .util .Map ;
24+ import java .util .regex .Matcher ;
25+ import java .util .regex .Pattern ;
2426
2527import com .fasterxml .jackson .core .JsonProcessingException ;
2628import com .fasterxml .jackson .core .type .TypeReference ;
3335import org .springframework .core .ParameterizedTypeReference ;
3436import org .springframework .http .RequestEntity ;
3537import org .springframework .http .ResponseEntity ;
38+ import org .springframework .util .Assert ;
3639import org .springframework .util .StringUtils ;
40+ import org .springframework .web .client .HttpClientErrorException ;
3741import org .springframework .web .client .RestTemplate ;
3842
3943/**
@@ -59,6 +63,8 @@ public class GithubQueries {
5963
6064 private static final String DEFAULT_SUPPORT_POLICY = "SPRING_BOOT" ;
6165
66+ private static final Pattern PROJECT_FILE = Pattern .compile ("project\\ /(.*)\\ /.*" );
67+
6268 private static final ParameterizedTypeReference <Map <String , Object >> STRING_OBJECT_MAP = new ParameterizedTypeReference <>() {
6369 };
6470
@@ -96,6 +102,95 @@ ProjectData getData() {
96102 return new ProjectData (projects , documentation , support , supportPolicy );
97103 }
98104
105+ ProjectData updateData (ProjectData data , List <String > changes ) {
106+ Assert .notNull (data , "Project data should not be null" );
107+ Map <String , Project > projects = new LinkedHashMap <>(data .project ());
108+ Map <String , List <ProjectDocumentation >> documentation = new LinkedHashMap <>(data .documentation ());
109+ Map <String , List <ProjectSupport >> support = new LinkedHashMap <>(data .support ());
110+ Map <String , String > supportPolicy = new LinkedHashMap <>(data .supportPolicy ());
111+ Map <String , Boolean > checkedProjects = new LinkedHashMap <>();
112+ try {
113+ changes .forEach ((change ) -> {
114+ ProjectFile file = ProjectFile .from (change );
115+ if (ProjectFile .OTHER .equals (file )) {
116+ return ;
117+ }
118+ updateData (change , file , projects , supportPolicy , documentation , support , checkedProjects );
119+ });
120+ }
121+ catch (Exception ex ) {
122+ logger .debug ("Could not update data due to '%s'" .formatted (ex .getMessage ()));
123+ }
124+ return new ProjectData (projects , documentation , support , supportPolicy );
125+ }
126+
127+ private void updateData (String change , ProjectFile file , Map <String , Project > projects ,
128+ Map <String , String > supportPolicy , Map <String , List <ProjectDocumentation >> documentation ,
129+ Map <String , List <ProjectSupport >> support , Map <String , Boolean > checkedprojects ) {
130+ Matcher matcher = PROJECT_FILE .matcher (change );
131+ if (!matcher .matches ()) {
132+ return ;
133+ }
134+ String slug = matcher .group (1 );
135+ if (checkedprojects .get (slug ) == null ) {
136+ checkedprojects .put (slug , doesProjectExist (slug ));
137+ }
138+ if (checkedprojects .get (slug )) {
139+ updateFromIndex (file , projects , supportPolicy , slug );
140+ updateDocumentation (file , documentation , slug );
141+ updateSupport (file , support , slug );
142+ return ;
143+ }
144+ projects .remove (slug );
145+ documentation .remove (slug );
146+ support .remove (slug );
147+ supportPolicy .remove (slug );
148+ }
149+
150+ private void updateSupport (ProjectFile file , Map <String , List <ProjectSupport >> support , String slug ) {
151+ if (ProjectFile .SUPPORT .equals (file )) {
152+ List <ProjectSupport > projectSupports = getProjectSupports (slug );
153+ support .put (slug , projectSupports );
154+ }
155+ }
156+
157+ private void updateDocumentation (ProjectFile file , Map <String , List <ProjectDocumentation >> documentation ,
158+ String slug ) {
159+ if (ProjectFile .DOCUMENTATION .equals (file )) {
160+ List <ProjectDocumentation > projectDocumentation = getProjectDocumentations (slug );
161+ documentation .put (slug , projectDocumentation );
162+ }
163+ }
164+
165+ private void updateFromIndex (ProjectFile file , Map <String , Project > projects , Map <String , String > supportPolicy ,
166+ String slug ) {
167+ if (ProjectFile .INDEX .equals (file )) {
168+ ResponseEntity <Map <String , Object >> response = getFile (slug , "index.md" );
169+ Project project = getProject (response , slug );
170+ if (project != null ) {
171+ projects .put (slug , project );
172+ }
173+ String policy = getProjectSupportPolicy (response , slug );
174+ supportPolicy .put (slug , policy );
175+ }
176+ }
177+
178+ private boolean doesProjectExist (String projectSlug ) {
179+ RequestEntity <Void > request = RequestEntity .get ("/project/{projectSlug}?ref=" + this .branch , projectSlug )
180+ .build ();
181+ try {
182+ this .restTemplate .exchange (request , STRING_OBJECT_MAP );
183+ }
184+ catch (Exception ex ) {
185+ if (ex instanceof HttpClientErrorException ) {
186+ if (((HttpClientErrorException ) ex ).getStatusCode ().value () == 404 ) {
187+ return false ;
188+ }
189+ }
190+ }
191+ return true ;
192+ }
193+
99194 private void populateData (Map <String , Object > project , Map <String , Project > projects ,
100195 Map <String , List <ProjectDocumentation >> documentation , Map <String , List <ProjectSupport >> support ,
101196 Map <String , String > supportPolicy ) {
@@ -188,4 +283,29 @@ private String getFileContent(ResponseEntity<Map<String, Object>> exchange) {
188283 return new String (contents );
189284 }
190285
286+ enum ProjectFile {
287+
288+ INDEX ,
289+
290+ SUPPORT ,
291+
292+ DOCUMENTATION ,
293+
294+ OTHER ;
295+
296+ static ProjectFile from (String fileName ) {
297+ if (fileName .contains ("index.md" )) {
298+ return INDEX ;
299+ }
300+ if (fileName .contains ("documentation.json" )) {
301+ return DOCUMENTATION ;
302+ }
303+ if (fileName .contains ("support.json" )) {
304+ return SUPPORT ;
305+ }
306+ return OTHER ;
307+ }
308+
309+ }
310+
191311}
0 commit comments