Skip to content
Snippets Groups Projects

Draft: Add static source data fetching to projects widget

2 files
+ 51
7
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -11,19 +11,24 @@
@@ -11,19 +11,24 @@
* SPDX-License-Identifier: EPL-2.0
* SPDX-License-Identifier: EPL-2.0
*/
*/
import $, { type } from 'jquery';
import $ from 'jquery';
import parse from 'parse-link-header';
import parse from 'parse-link-header';
import { validateURL } from '../utils/utils';
import { validateURL } from '../utils/utils';
import 'isomorphic-fetch';
import 'isomorphic-fetch';
const getProjectsData = (url, projects = []) => {
const eclipseProjectsUrl = "https://projects.eclipse.org/projects/";
 
 
const throwFetchError = ({ status, statusText } = response) => {
 
throw `${status}: ${statusText}`
 
};
 
 
const getProjectsDataFromAPI = (url, projects = []) => {
if (validateURL(url)) {
if (validateURL(url)) {
return new Promise((resolve, reject) =>
return new Promise((resolve, reject) =>
fetch(url)
fetch(url)
.then((response) => {
.then((response) => {
if (response.status !== 200) {
if (response.status !== 200) throwFetchError(response);
throw `${response.status}: ${response.statusText}`;
}
response
response
.json()
.json()
.then((data) => {
.then((data) => {
@@ -39,7 +44,7 @@ const getProjectsData = (url, projects = []) => {
@@ -39,7 +44,7 @@ const getProjectsData = (url, projects = []) => {
projects[key].version = project.releases[0].name;
projects[key].version = project.releases[0].name;
}
}
if (typeof projects[key].website_url === "undefined" || projects[key].website_url === "") {
if (typeof projects[key].website_url === "undefined" || projects[key].website_url === "") {
projects[key].website_url = "https://projects.eclipse.org/projects/" + projects[key].project_id;
projects[key].website_url = eclipseProjectsUrl + projects[key].project_id;
}
}
});
});
@@ -56,4 +61,42 @@ const getProjectsData = (url, projects = []) => {
@@ -56,4 +61,42 @@ const getProjectsData = (url, projects = []) => {
}
}
};
};
 
const getProjectsDataFromStaticSource = (url) => {
 
if (!validateURL(url)) return;
 
 
return new Promise((resolve, reject) =>
 
fetch(url)
 
.then(response => {
 
if (response.status !== 200) throw `${response.status}: ${response.statusText}`
 
 
response
 
.json()
 
.then((projects) => {
 
$(projects).each(function (key) {
 
if (typeof projects[key].website_url === "undefined" || projects[key].website_url === "") {
 
projects[key].website_url = eclipseProjectsUrl + projects[key].project_id;
 
}
 
});
 
 
projects.sort((a, b) => a.name.localeCompare(b.name));
 
resolve(projects);
 
})
 
.catch(reject)
 
})
 
.catch((err) => reject(err))
 
)
 
}
 
 
export const getProjectsData = (url, isStaticSource) => {
 
let projects;
 
 
if (isStaticSource) {
 
projects = getProjectsDataFromStaticSource(url);
 
} else {
 
projects = getProjectsDataFromAPI(url);
 
}
 
 
return projects;
 
}
 
export default getProjectsData;
export default getProjectsData;
Loading