Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ const packageDetails = require(path.join(__dirname, "package.json"));
"-e, --allow-empty",
"allow workflows that do not contain any actions"
)
.option(
"-o, --only <owner/repo>",
"single action to pin e.g. mheap/debug-action. May be a glob e.g. mheap/*. May have version tag e.g. mheap/debug-action@v1.2.3"
)
.parse(process.argv);

const filename = program.args[0];
const filename = program.args[0];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run npm run lint-fix to ensure that formatting is correct


if (!filename) {
console.log("Usage: pin-github-action /path/to/workflow.yml");
Expand All @@ -37,11 +41,27 @@ const packageDetails = require(path.join(__dirname, "package.json"));
let allowed = program.opts().allow;
allowed = (allowed || "").split(",").filter((r) => r);
let ignoreShas = program.opts().ignoreShas;
let only = program.opts().only;

let [onlyOwner, onlyRepo] = [null,null];
let onlyVersion = null;
if ( only ){
let parts = only.split("/");
if ( parts.length != 2 ){
throw ( "Syntax for --only: account/repo or account/*");
}
[onlyOwner,onlyRepo] = parts;

parts = onlyRepo.split("@");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable shadowing here. Could we use a more descriptive (and different) variable name?

if( 2 == parts.length ){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parts.length == 2 here please, we don't use yoda conditions anywhere else

[onlyRepo,onlyVersion] = parts;
}
}

const input = fs.readFileSync(filename).toString();

let allowEmpty = program.opts().allowEmpty;
const output = await run(input, allowed, ignoreShas, allowEmpty, debug);
const output = await run(input, allowed, ignoreShas, allowEmpty, debug, onlyOwner, onlyRepo, onlyVersion);

fs.writeFileSync(filename, output.workflow);

Expand Down
4 changes: 2 additions & 2 deletions findRefOnGithub.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const github = new Octokit({
});

let debug = () => {};
module.exports = function (action, log) {
module.exports = function (action, log, onlyVersion) {
debug = log.extend("find-ref-on-github");
return new Promise(async function (resolve, reject) {
const owner = action.owner;
const repo = action.repo;
const pinned = action.pinnedVersion;
const pinned = onlyVersion ? onlyVersion : action.pinnedVersion;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test in findRefOnGitHub.test.js that covers this change?

const name = `${owner}/${repo}`;

let error;
Expand Down
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ module.exports = async function (
allowed,
ignoreShas,
allowEmpty,
debug
debug,
onlyOwner,
onlyRepo,
onlyVersion
) {
allowed = allowed || [];
ignoreShas = ignoreShas || false;
Expand All @@ -33,9 +36,24 @@ module.exports = async function (
continue;
}

if (onlyOwner && actions[i].owner !== onlyOwner ){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract this in to another file (e.g. checkSelectOnly.js) and write unit tests.

Example module: https://github.com/mheap/pin-github-action/blob/main/checkAllowedRepos.js
Example tests: https://github.com/mheap/pin-github-action/blob/main/checkIgnoredRepos.test.js

debug("skipping owner:",actions[i].owner)
continue;
}

if (onlyRepo && onlyRepo !== "*" && actions[i].repo !== onlyRepo ){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than checking * explicitly, use matcher like the existing functionality

debug("skipping repo:",actions[i].repo)
continue;
}

debug("pinning action:",action)

// Look up those actions on Github
const newVersion = await findRefOnGithub(actions[i], debug);
const newVersion = await findRefOnGithub(actions[i], debug, onlyVersion);
actions[i].newVersion = newVersion;
if( onlyVersion ){
actions[i].pinnedVersion = onlyVersion;
}

// Rewrite each action, replacing the uses block with a specific sha
workflow = replaceActions(workflow, actions[i]);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pin-github-action",
"version": "1.6.0",
"version": "1.6.1",
"description": "Pin your GitHub Actions to specific versions automatically!",
"main": "index.js",
"scripts": {
Expand Down