-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
73 lines (64 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const fetch = require("node-fetch")
const core = require('@actions/core');
let repoUrl, image, regex, maxAgeMinutes, username, password
try {
repoUrl = core.getInput("repo-url")
image = core.getInput("image")
regex = new RegExp(core.getInput("tag-regex"))
maxAgeMinutes = parseInt(core.getInput("max-age-minutes"))
username = core.getInput("username")
password = core.getInput("password")
} catch (err) {
const msg = "Failed to initialize action: " + err.message
core.setFailed(msg)
console.error(msg)
console.error(err)
process.exit(1)
}
fetch(`${repoUrl}/v2/users/login/`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ username, password })
})
.then(processResponse)
.then(({ token }) => {
return fetch(`${repoUrl}/v2/repositories/${image}/tags?page_size=100`, {
headers: {
"authorization": token
}
})
})
.then(processResponse)
.then(({ results }) => {
const tags =
results.sort(byDateAsc)
.filter(filterOldEntry)
.filter(el => regex.test(el.name))
.map(el => el.name)
.join(",")
core.setOutput("tags", tags)
})
.catch((err) => {
const msg = "Failed to scan Docker repository: " + err.message
core.setFailed(msg)
console.error(msg)
console.error(err)
process.exit(1)
})
function processResponse(res) {
if(res.status < 400) {
return res.json()
} else {
return Promise.reject(new Error(`Response from server: '${res.statusText}' (${res.status}) for URL ${res.url}`))
}
}
function byDateAsc(a, b) {
return new Date(a.last_updated) - new Date(b.last_updated)
}
function filterOldEntry(entry) {
const now = new Date()
const entryDate = new Date(entry.last_updated)
return (now - entryDate) < maxAgeMinutes * 60 * 1000
}