Skip to content

Commit 8705221

Browse files
committed
Add LatestVersion.vue
1 parent 2316757 commit 8705221

3 files changed

Lines changed: 108 additions & 1 deletion

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<script setup>
2+
// noinspection NpmUsedModulesInstalled
3+
import { ref } from 'vue'
4+
import VPButton from 'vitepress/dist/client/theme-default/components/VPButton.vue'
5+
6+
const props = defineProps({
7+
repo: { type: String, required: true },
8+
})
9+
const href = `https://github.com/${props.repo}/releases/latest`
10+
const src = `https://img.shields.io/github/v/release/${props.repo}?style=for-the-badge&logo=github&label=latest%20version`
11+
12+
const btnTheme = ref('brand')
13+
const shaOutput = ref(null)
14+
const myButton = ref(null)
15+
16+
function handleOutputClick(event) {
17+
if (event.target.value) {
18+
event.target.select()
19+
navigator.clipboard.writeText(event.target.value)
20+
}
21+
}
22+
23+
async function generateHash(event) {
24+
console.log('event:', event)
25+
console.log('myButton:', myButton)
26+
const el = myButton.value?.$el
27+
console.log('el:', el)
28+
if (!el) return console.error('Unable to access button element.')
29+
el.disabled = true
30+
btnTheme.value = 'alt'
31+
const release = await fetchData(`https://api.github.com/repos/${props.repo}/releases/latest`)
32+
console.log('release:', release)
33+
if (!release) return console.error('Unable to fetch latest release.')
34+
console.log('release.tag_name:', release.tag_name)
35+
const tag = await fetchData(`https://api.github.com/repos/${props.repo}/git/ref/tags/${release.tag_name}`)
36+
console.log('tag:', tag)
37+
if (!tag?.object?.sha) return console.error('Unable to fetch tag/sha.')
38+
console.log('tag.object.sha:', tag.object.sha)
39+
shaOutput.value = tag.object.sha
40+
await navigator.clipboard.writeText(tag.object.sha)
41+
}
42+
43+
/**
44+
* Fetch Data
45+
* @param {String} url
46+
* @return {Promise<Object>}
47+
*/
48+
async function fetchData(url) {
49+
const options = { headers: { 'X-GitHub-Api-Version': '2022-11-28' } }
50+
const response = await fetch(url, options)
51+
console.log('response.status:', response.status)
52+
if (!response.ok) return
53+
return await response.json()
54+
}
55+
</script>
56+
57+
<template>
58+
<div id="buttons">
59+
<VPButton text="Generate Commit Hash" @click="generateHash" :theme="btnTheme" ref="myButton" />
60+
<div>
61+
<a :href="href" target="_blank" rel="noopener">
62+
<img alt="GitHub Release" :src="src" />
63+
</a>
64+
</div>
65+
</div>
66+
<div :style="{ display: shaOutput ? 'block' : 'none' }">
67+
<input
68+
id="encodeOutput"
69+
placeholder="Encoded Results"
70+
v-model="shaOutput"
71+
type="text"
72+
@click="handleOutputClick"
73+
readonly
74+
/>
75+
<p>Hash Copied to Clipboard</p>
76+
</div>
77+
</template>
78+
79+
<style scoped>
80+
#buttons > * {
81+
margin-bottom: 0.5rem;
82+
}
83+
84+
@media (min-width: 640px) {
85+
#buttons {
86+
display: flex;
87+
align-items: center;
88+
justify-content: space-between;
89+
}
90+
}
91+
img {
92+
display: inline-block;
93+
}
94+
input {
95+
border: 1px solid var(--vp-c-gray-1);
96+
border-radius: 8px;
97+
padding: 6px;
98+
width: 100%;
99+
}
100+
p {
101+
color: var(--vp-c-success-1);
102+
margin: 0 0 8px 8px;
103+
font-weight: 300;
104+
}
105+
</style>

.vitepress/theme/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'virtual:group-icons.css'
44
import './custom.css'
55

66
import StackTable from './components/StackTable.vue'
7+
import LatestVersion from './components/LatestVersion.vue'
78

89
import CopyButton from '@cssnr/vitepress-plugin-copybutton'
910
import '@cssnr/vitepress-plugin-copybutton/style.css'
@@ -21,6 +22,7 @@ export default {
2122
enhanceApp({ app }) {
2223
app.component('Badge', VPBadge)
2324
app.component('StackTable', StackTable)
25+
app.component('LatestVersion', LatestVersion)
2426

2527
app.component('CB', CopyButton)
2628
app.component('Contributors', Contributors)

docs/guides/get-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The only 3 required inputs are [token](../docs/inputs.md#token), [url](../docs/i
2929

3030
Check out the [Rolling Tags](features.md#rolling-tags) for more tag options, however `@v1` is recommended.
3131

32-
[![Latest Version](https://img.shields.io/github/v/release/cssnr/portainer-stack-deploy-action?style=for-the-badge&logo=github&label=Latest%20Release)](https://github.com/cssnr/portainer-stack-deploy-action/releases)
32+
<LatestVersion repo="cssnr/portainer-stack-deploy-action" />
3333

3434
<div class="tip custom-block" style="padding-top: 8px">
3535

0 commit comments

Comments
 (0)