Skip to content

Commit

Permalink
Gomodsum flag - allow go.mod changes as well as go.sum (#4)
Browse files Browse the repository at this point in the history
* Adds gomodsum_only flag to allow go.{mod,sum} changes
* More tests.
* Compiled dist
  • Loading branch information
evantorrie authored Jun 23, 2020
1 parent 2e6292e commit 477fbfa
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 35 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ installed go on the Workflow runner.
**/go.mod
-tools/go.mod
# enforce action failure if any files modified as a result of the action
# are *not* `go.sum` or `go.mod` files
# defaults to true

gomodsum_only: true

# enforce action failure if any files modified as a result of the action
# are *not* `go.sum` files
# defaults to false

gosum_only: true
gosum_only: false
```
### Example
Expand All @@ -69,7 +75,6 @@ jobs:
gomods: |
**/go.mod
-tools/go.mod
gosum_only: true
- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Auto-fix go.sum discrepancies
Expand All @@ -80,13 +85,17 @@ jobs:
1. `gomods`
Defaults to `go.mod`, i.e. top level `go.mod` in repo.

2. `gosum_only`
2. `gomodsum_only`
Defaults to `true`

3. `gosum_only`
Defaults to `false`


### Outputs

TBD
1. `changedfiles`
Newline delimited set of git tracked files which changed during the action.

## Background

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ inputs:
description: 'fail if files other than go.sum are modified during tidy'
required: false
default: false
gomodsum_only:
description: 'fail if files other than go.{mod,sum} are modified during tidy'
required: false
default: false

outputs:
changedfiles: # id of output
Expand Down
53 changes: 38 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1911,26 +1911,22 @@ async function run() {
.filter(s => s !== "");
let dirs = await utils.findDirectories(filePatterns);
await gomodTidy(dirs);
let diffs = await checkGoSumOnly();
core.startGroup('Files changed');
let diffs = await gitDiffFiles();

core.startGroup('Diff Files');
for (const f of diffs) {
console.log(f);
console.log(` ${f}`);
}
core.endGroup();
core.startGroup('Diffs');
core.startGroup('Full diff');
await exec.exec('git', ['diff']);
core.endGroup();

const gosum_only = core.getInput('gosum_only').toLowerCase();
if (gosum_only === 'true' || gosum_only === 'enabled') {
// count number of files which end in go.sum
// \todo add check for full filename rather than suffix, i.e. ohgo.sum)
let gosums = diffs.filter(s => s.endsWith('go.sum'));
core.debug(`go.sums=${gosums}`);
if (diffs.length !== gosums.length) {
const msg = "Files other than go.sum were changed during go mod tidy!";
throw new Error(msg);
}
}
const gomodsum_only = core.getInput('gomodsum_only').toLowerCase();
let enabled = (s) => { return s === 'true' || s === 'enabled'; };
utils.checkModifiedFiles(diffs, enabled(gosum_only), enabled(gomodsum_only));
core.setOutput('changedfiles', diffs.join("\n"));
} catch (error) {
core.setFailed(error.message);
}
Expand All @@ -1949,7 +1945,7 @@ async function gomodTidy(dirs) {

// Run git status in each directory - to see what files have changed
// \todo check behaviour with git submodules
async function checkGoSumOnly() {
async function gitDiffFiles() {
core.debug(`git diff --name-only`);
let myOutput = '';
const options = {
Expand Down Expand Up @@ -1980,12 +1976,38 @@ module.exports = require("child_process");
/***/ 278:
/***/ (function(__unusedmodule, exports, __webpack_require__) {

const core = __webpack_require__(470);
const glob = __webpack_require__(281);
const path = __webpack_require__(622);

const hasIterationProtocol = variable =>
variable !== null && Symbol.iterator in Object(variable);

/**
* @param diffs array of filenames to examine
* @param gosum boolean check all files are go.sum only
* @param gomodsum boolean check all files are go.mod or go.sum only
* @returns {void}
* @throws string exeception if diffs do not satisfy constraints
*/
function checkModifiedFiles(diffs, gosum_only, gomodsum_only) {
if (!(gosum_only || gomodsum_only)) {
// then we really don't care about what files were modified
return;
}
// precondition diffs is a container that has a 'filter' operation
// \todo add check for full filename rather than suffix, i.e. ohgo.sum)
let gosums = diffs.filter(s => s.endsWith('go.sum'));
let gomods = diffs.filter(s => s.endsWith('go.mod'));
core.debug(`go.sums=${gosums}, go.mods=${gomods}, diffs=${diffs}`);
let desired = gosum_only ? gosums.length : (gosums.length + gomods.length);
if (diffs.length !== desired) {
const fileSpec = gosum_only ? 'go.sum' : 'go.{mod,sum}';
const msg = `Files other than ${fileSpec} were changed during \`go mod tidy\`!`;
throw new Error(msg);
}
}

/**
* @param patterns An Iterable container of globbale patterns
* @returns {array} array of paths matching the globs
Expand Down Expand Up @@ -2026,6 +2048,7 @@ function difference(setA, setB) {
return _difference;
}

exports.checkModifiedFiles = checkModifiedFiles;
exports.findDirectories = findDirectories;


Expand Down
26 changes: 11 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,22 @@ async function run() {
.filter(s => s !== "");
let dirs = await utils.findDirectories(filePatterns);
await gomodTidy(dirs);
let diffs = await checkGoSumOnly();
core.startGroup('Files changed');
let diffs = await gitDiffFiles();

core.startGroup('Diff Files');
for (const f of diffs) {
console.log(f);
console.log(` ${f}`);
}
core.endGroup();
core.startGroup('Diffs');
core.startGroup('Full diff');
await exec.exec('git', ['diff']);
core.endGroup();

const gosum_only = core.getInput('gosum_only').toLowerCase();
if (gosum_only === 'true' || gosum_only === 'enabled') {
// count number of files which end in go.sum
// \todo add check for full filename rather than suffix, i.e. ohgo.sum)
let gosums = diffs.filter(s => s.endsWith('go.sum'));
core.debug(`go.sums=${gosums}`);
if (diffs.length !== gosums.length) {
const msg = "Files other than go.sum were changed during go mod tidy!";
throw new Error(msg);
}
}
const gomodsum_only = core.getInput('gomodsum_only').toLowerCase();
let enabled = (s) => { return s === 'true' || s === 'enabled'; };
utils.checkModifiedFiles(diffs, enabled(gosum_only), enabled(gomodsum_only));
core.setOutput('changedfiles', diffs.join("\n"));
} catch (error) {
core.setFailed(error.message);
}
Expand All @@ -54,7 +50,7 @@ async function gomodTidy(dirs) {

// Run git status in each directory - to see what files have changed
// \todo check behaviour with git submodules
async function checkGoSumOnly() {
async function gitDiffFiles() {
core.debug(`git diff --name-only`);
let myOutput = '';
const options = {
Expand Down
31 changes: 30 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,37 @@ describe('test findDirectories', () => {

});

describe('checkModifiedFiles', () => {
const filesOnlyGosum = [ 'go.sum', 'tests/go.sum', 'foo/bar/go.sum' ];
const filesOnlyGosumGomod = [ 'go.mod', 'go.sum', 'foo/bar/go.mod', 'foo/bar/go.sum', 'bar/go.mod' ];
const filesAll = [ ...filesOnlyGosum, ...filesOnlyGosumGomod, 'main.go' ];

test('does not throw when both flags disabled', () => {
expect(() => utils.checkModifiedFiles(filesAll, false, false)).not.toThrow();
expect(() => utils.checkModifiedFiles(filesOnlyGosum, false, false)).not.toThrow();
expect(() => utils.checkModifiedFiles(filesOnlyGosumGomod, false, false)).not.toThrow();
});

test('throws on arbitrary files when either flag enabled', () => {
expect(() => utils.checkModifiedFiles(filesAll, true, false)).toThrow();
expect(() => utils.checkModifiedFiles(filesAll, false, true)).toThrow();
});

test('throws on go.mod files when gosum_only flag enabled', () => {
expect(() => utils.checkModifiedFiles(filesAll, true, false)).toThrow();
expect(() => utils.checkModifiedFiles(filesOnlyGosumGomod, true, false)).toThrow();
expect(() => utils.checkModifiedFiles(filesOnlyGosum, true, false)).not.toThrow();
});

test('does not throw on go.sum only files when gomodsum_only flag enabled', () => {
expect(() => utils.checkModifiedFiles(filesOnlyGosum, false, true)).not.toThrow();
expect(() => utils.checkModifiedFiles(filesOnlyGosumGomod, false, true)).not.toThrow();
});

});

// shows how the runner will run a javascript action with env / stdout protocol
test.skip('test runs', () => {
test('test runs', () => {
process.env['INPUT_GOMODS'] = 'go.mod';
const ip = path.join(__dirname, 'index.js');
console.log(cp.execSync(`node ${ip}`, { env: process.env }).toString());
Expand Down
27 changes: 27 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
const core = require('@actions/core');
const glob = require('@actions/glob');
const path = require('path');

const hasIterationProtocol = variable =>
variable !== null && Symbol.iterator in Object(variable);

/**
* @param diffs array of filenames to examine
* @param gosum boolean check all files are go.sum only
* @param gomodsum boolean check all files are go.mod or go.sum only
* @returns {void}
* @throws string exeception if diffs do not satisfy constraints
*/
function checkModifiedFiles(diffs, gosum_only, gomodsum_only) {
if (!(gosum_only || gomodsum_only)) {
// then we really don't care about what files were modified
return;
}
// precondition diffs is a container that has a 'filter' operation
// \todo add check for full filename rather than suffix, i.e. ohgo.sum)
let gosums = diffs.filter(s => s.endsWith('go.sum'));
let gomods = diffs.filter(s => s.endsWith('go.mod'));
core.debug(`go.sums=${gosums}, go.mods=${gomods}, diffs=${diffs}`);
let desired = gosum_only ? gosums.length : (gosums.length + gomods.length);
if (diffs.length !== desired) {
const fileSpec = gosum_only ? 'go.sum' : 'go.{mod,sum}';
const msg = `Files other than ${fileSpec} were changed during \`go mod tidy\`!`;
throw new Error(msg);
}
}

/**
* @param patterns An Iterable container of globbale patterns
* @returns {array} array of paths matching the globs
Expand Down Expand Up @@ -44,5 +70,6 @@ function difference(setA, setB) {
return _difference;
}

exports.checkModifiedFiles = checkModifiedFiles;
exports.findDirectories = findDirectories;

0 comments on commit 477fbfa

Please sign in to comment.