Skip to content

Instantly share code, notes, and snippets.

@tomalec
Last active October 27, 2021 19:28
Show Gist options
  • Save tomalec/b8e9d13f268078120d6202efc8b0ded5 to your computer and use it in GitHub Desktop.
Save tomalec/b8e9d13f268078120d6202efc8b0ded5 to your computer and use it in GitHub Desktop.
Fetch a list of latest minor WC releases
customElements.define( 'wc-latest-releases', class extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
}
static get observedAttributes() { return ["limit"]; }
get limit() {
return Number(this.getAttribute("limit"));
}
async fetchRealeses() {
this.releases = null;
const request = await fetch('https://api.github.com/repos/woocommerce/woocommerce/releases');
const allRealeses = await request.json();
const minors = allRealeses.reduce((acc,release)=>{
const minor = release.tag_name.match(/(\d+\.\d+)/)[0];
let previous = acc.get(minor);
if ( ! previous || previous.published_at < release.published_at ) {
acc.set(minor, {
minor,
latest_tag_name: release.tag_name,
url: release.html_url,
published_at: release.published_at,
});
}
return acc;
}, new Map());
this.releases = Array.from(minors.values()).sort((a,b)=> b[0] - a[0]);
}
async connectedCallback() {
await this.fetchRealeses();
this.render();
}
attributeChangedCallback(attrName, oldVal, newVal) {
this.render();
}
render(){
if ( ! this.releases ) {
this.shadowRoot.innerHTML = 'Loading...';
} else {
this.shadowRoot.innerHTML = `<table>
<tr>
<th>Latest version</th>
<th>Released</th>
</tr>
${this.releases.slice(0, this.limit).map((release) => {
return `
<tr>
<td><a href="${release.url}">${release.latest_tag_name}</a></td>
<td>${release.published_at}</td>
</tr>
`;
}).join("\n")}
</table>`;
}
}
});
const request = await fetch('https://api.github.com/repos/woocommerce/woocommerce/releases');
const allRealeses = await request.json();
const minors = allRealeses.reduce((acc,release)=>{
const minor = release.tag_name.match(/(\d+\.\d+)/)[0];
let previous = acc.get(minor);
if ( ! previous || previous.published_at < release.published_at ) {
acc.set(minor, {
minor,
latest_tag_name: release.tag_name,
url: release.html_url,
published_at: release.published_at,
});
}
return acc;
}, new Map());
const latest4 = Array.from(minors.values()).sort((a,b)=> b[0] - a[0]).splice(0,4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment