Last active
October 27, 2021 19:28
-
-
Save tomalec/b8e9d13f268078120d6202efc8b0ded5 to your computer and use it in GitHub Desktop.
Fetch a list of latest minor WC releases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>`; | |
} | |
} | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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