Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Validate inputs that cannot be used together #160

Merged
merged 3 commits into from Jun 5, 2022

Conversation

M1kep
Copy link
Contributor

@M1kep M1kep commented May 30, 2022

No description provided.

@dawidd6
Copy link
Owner

dawidd6 commented May 31, 2022

Could you please explain how this code exactly works? Frankly I'm not really a JS expert and reading this gives me head scratching.

@M1kep
Copy link
Contributor Author

M1kep commented May 31, 2022

I've provided an explanation below, let me know if there's still any confusion. I'll also add a different version that may be easier to understand, which I can use instead:

Edit: Fixed

        uniqueInputSets.forEach((inputSet) => {
            /**
             * Given example object of:
             * const example = {
             *     "key1": "value1",
             *     "key2": "value2"
             * }
             *
             * Object.values(example) returns ["value1", "value2"]
             * Object.keys(example) returns ["key1", "key2"]
             *
             * (For each uniqueInputSet)
             * Get the values
             *      Guaranteed to be either the user input or '' (https://github.com/actions/toolkit/blob/c4ae214c261c0d684c6bcfe6632b773b82482bad/packages/core/src/core.ts#L130)
             *
             * These values are passed to the reduce method which takes a reducer(callback) and a starting value
             *      *Official Reduce Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
             * The reducer is called for each item in the array with the below inputs
             *      previousValue(Named occurrences in this case)
             *          This value is the value that was returned by the callback the last time it was called
             *          If it's the first item in the array, the then starting value is used(In this case 0)
             *      currentValue(Named val in this case)
             *          This is the value of the current element in the array
             *
             * The reducer below checks if the user provided input by checking if the value is not ''
             *      If the input was provided by a user, then we increment and return the occurrences variable
             *      which will then be passed into the callback for the next item in the array
             *
             */
            const occurrences = Object.values(inputSet).reduce((occurrences, val) => {
                if (val !== '') {
                    occurrences++
                }
                return occurrences
            },0)
            if(occurrences > 1) {
                throw new Error(`The following inputs cannot be used together: ${Object.keys(inputSet).join(", ")}`)
            }
        })

Alternate version which is probably the better option:

        uniqueInputSets.forEach((inputSet) => {
            const inputs = Object.values(inputSet)
            const providedInputs = inputs.filter(input => input !== '')
            if (providedInputs.length > 1) {
                throw new Error(`The following inputs cannot be used together: ${Object.keys(inputSet).join(", ")}`)
            }
        })

@dawidd6
Copy link
Owner

dawidd6 commented Jun 1, 2022

Much nicer, thanks. Yet I'm not sure about the second object. Workflow conclusion input has a default value set in action metadata yaml and it does not really collide with run ID, it is just not evaluated when run ID is provided.

@M1kep
Copy link
Contributor Author

M1kep commented Jun 1, 2022

Ah I see, I'll take a look in the AM at cleaning that up

@M1kep
Copy link
Contributor Author

M1kep commented Jun 5, 2022

I've removed the second block

@dawidd6
Copy link
Owner

dawidd6 commented Jun 5, 2022

Thanks

@dawidd6 dawidd6 merged commit 27fee4d into dawidd6:master Jun 5, 2022
iTrooz pushed a commit to iTrooz/action-download-artifact that referenced this pull request Jun 19, 2022
* feat: Validate inputs that cannot be used together

* Fix reduce callback

* Simplify inputSet validation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants