Skip to content

Commit

Permalink
Initial Release
Browse files Browse the repository at this point in the history
changed name to make more sense
  • Loading branch information
Daniel Dickerson committed Mar 21, 2022
1 parent 4fb3bbe commit 36fac97
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 65 deletions.
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"singleQuote": true,
"tabWidth": 2,
"arrowParens": "always"
}
8 changes: 2 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ inputs:
required: false
default: https://mcr.microsoft.com
outputs:
tags:
description: >
A comma-separated list of all image tags that have changed in the specified time frame, sorted by date in
ascending order (oldest first)
image:
images:
description: >
The image
A stringified array of images
runs:
using: node16
main: dist/index.js
Expand Down
76 changes: 49 additions & 27 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4443,45 +4443,67 @@ function limitVersions(tags, versionLimit) {



let repoUrl, index_image, regex, versionLimit;
let repoUrl, index_images, regex, versionLimit;
let imageArray = [];
let outputImages = [];
let promises = [];
try {
repoUrl = core.getInput("repo-url");
index_image = core.getInput("image");
regex = new RegExp(core.getInput("tag-regex"));
versionLimit = core.getInput("version-limit");
repoUrl = core.getInput('repo-url');
index_images = core.getInput('images');
regex = new RegExp(core.getInput('tag-regex'));
versionLimit = core.getInput('version-limit');
} catch (err) {
const msg = "Failed to initialize action: " + err.message;
const msg = 'Failed to initialize action: ' + err.message;
core.setFailed(msg);
console.error(msg);
console.error(err);
process.exit(1);
}

const processResponse = (res) => {
if (res.status < 400) {
return res.json();
} else {
throw new Error(
`Response from server: '${res.statusText}' (${res.status}) for URL ${res.url}`
);
}
if (res.status < 400) {
return res.json();
} else {
throw new Error(
`Response from server: '${res.statusText}' (${res.status}) for URL ${res.url}`
);
}
};

lib(`${repoUrl}/v2/${index_image}/tags/list`)
.then(response => processResponse(response))
.then(json => {
let { tags } = json;
let filteredTags = tags.filter(tag => regex.test(tag));
let limitedTags = limitVersions(filteredTags, versionLimit);
core.setOutput("tags", limitedTags);
})
.catch(err => {
const msg = "Failed to scan Docker repository: " + err.message;
core.setFailed(msg);
console.error(msg);
console.error(err);
process.exit(1);
try {
imageArray = JSON.parse(index_images);
} catch (err) {
imageArray.push(index_images);
}


imageArray.forEach((image) => {
promises.push(
lib(`${repoUrl}/v2/${image}/tags/list`)
.then((response) => processResponse(response))
.then((json) => {
let { tags } = json;
let filteredTags = tags.filter(tag => regex.test(tag));
let limitedTags = limitVersions(filteredTags, versionLimit);
let fullNames = limitedTags.map((tag) => `${image}:${tag}`);
outputImages.push(...fullNames);
})
.catch((err) => {
const msg = 'Failed to scan Docker repository: ' + err.message;
core.setFailed(msg);
console.error(msg);
console.error(err);
process.exit(1);
})
)
});

Promise.all(promises).then(() => {
console.log(`Images discovered:\n${outputImages.join('\n')}`);
core.setOutput('images', outputImages);
});



})();

65 changes: 38 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,65 @@
import fetch from 'node-fetch';
import core from '@actions/core';
import limitTags from "./modules/limitTags.js";
import limitTags from './modules/limitTags.js';

let repoUrl, images, regex, versionLimit;
let imageArray = [];
let outputTags = [];
let outputImages = [];
let promises = [];
try {
repoUrl = core.getInput("repo-url");
images = core.getInput("image");
regex = new RegExp(core.getInput("tag-regex"));
versionLimit = core.getInput("version-limit");
repoUrl = core.getInput('repo-url');
images = core.getInput('images');
regex = new RegExp(core.getInput('tag-regex'));
versionLimit = core.getInput('version-limit');
} catch (err) {
const msg = "Failed to initialize action: " + err.message;
const msg = 'Failed to initialize action: ' + err.message;
core.setFailed(msg);
console.error(msg);
console.error(err);
process.exit(1);
}

const processResponse = (res) => {
if (res.status < 400) {
return res.json();
} else {
throw new Error(
`Response from server: '${res.statusText}' (${res.status}) for URL ${res.url}`
);
}
if (res.status < 400) {
return res.json();
} else {
throw new Error(
`Response from server: '${res.statusText}' (${res.status}) for URL ${res.url}`
);
}
};

try {
imageArray = JSON.parse(images);
} catch (err) {
imageArray.push(images);
}
imageArray.forEach(element => {
fetch(`${repoUrl}/v2/${images}/tags/list`)


imageArray.forEach((image) => {
promises.push(
fetch(`${repoUrl}/v2/${image}/tags/list`)
.then((response) => processResponse(response))
.then((json) => {
let { tags } = json;
let filteredTags = tags.filter((tag) => regex.test(tag));
let limitedTags = limitTags(filteredTags, versionLimit);
outputTags.concat(limitedTags);
let { tags } = json;
let filteredTags = tags.filter(tag => regex.test(tag));
let limitedTags = limitTags(filteredTags, versionLimit);
let fullNames = limitedTags.map((tag) => `${image}:${tag}`);
outputImages.push(...fullNames);
})
.catch((err) => {
const msg = "Failed to scan Docker repository: " + err.message;
core.setFailed(msg);
console.error(msg);
console.error(err);
process.exit(1);
});
const msg = 'Failed to scan Docker repository: ' + err.message;
core.setFailed(msg);
console.error(msg);
console.error(err);
process.exit(1);
})
)
});

core.setOutput("tags", outputTags);
Promise.all(promises).then(() => {
console.log(`Images discovered:\n${outputImages.join('\n')}`);
core.setOutput('images', outputImages);
});


9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"name": "scan-docker-tags-action",
"version": "1.0.1",
"description": "A GitHub action for scanning Docker repositories for recently-updated tags",
"name": "scan-mcr-docker-images-action",
"version": "0.1.1",
"description": "A GitHub action for scanning MCR Docker repositories for tags",
"main": "index.js",
"type": "module",
"author": "",
"license": "MIT",
"scripts": {
"build": "ncc build index.js --license licenses.txt",
"test": "node test.js"
"build": "ncc build index.js --license licenses.txt -o dist"
},
"dependencies": {
"@actions/core": "^1.2.6",
Expand Down

0 comments on commit 36fac97

Please sign in to comment.