feat(cli): access team workspace collections and environments #4095
+2,744
−834
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR aims at bringing support for accessing collections and environments within team workspaces (support for personal workspace will be brought down the line) from the CLI by supplying their respective IDs. The following updates are made to the
test
command:The
test
command and the-e, --env
flag now support specifying an ID in addition to the file path.A new
--token
flag is added that takes in a personal access token for authorized access to the above resources.By default, the data is fetched from the Hoppscotch cloud instance. Alternatively, an SH instance can be specified using the new
--server
flag.Error codes
There are various scenarios where a workspace access attempt can fail under which the following error codes will be displayed in the CLI:
TOKEN_EXPIRED
- The supplied access token<access-token>
via the--token
flag has expired.TOKEN_INVALID
- The specified access token<access-token>
via the--token
flag is invalid or might have been deleted.INVALID_ID
- The user can supply a file path or ID for a collection (file path argument for thetest
command) / environment (-e, --env,
flag for thetest
command). If the ID was provided and the above error corresponds to the case in which an invalid ID is supplied or the resource is inaccessible to the user, being from a team that the user isn't part of. In the case of a file path, the above error corresponds to a case in which the supplied file path doesn't exist. We're clubbing both cases since there isn't a way to distinguish between b/w IDs and non-existent file paths if the network call aimed at workspace access fails.INVALID_SERVER_URL
There are multiple cases in which this error can happen:
/v1/access-tokens/{collection/environment}/{path/id}
with the network call resulting in a404
.application/json
, safeguarding against the case where the network call doesn't fail where the route is not valid as above and checking against the content type. For instance, supplying the FE instance URL.ERR_INVALID_URL
as the error code from the network call). For instance, missing a protocol (e.g.,http://
orhttps://
), having an invalid structure, or containing illegal characters.ENOTFOUND
as the error code from the network call). For instance, a typo in the domain name.SERVER_CONNECTION_REFUSED
- Found the server, but the server refused to connect (receivedECONNREFUSED
as the error code from the network call).Changes
test
command.getResourceContents()
helper function under ~/utils/getters.ts. It checks for the existence of a file path corresponding to the supplied value and proceeds with the network call if it isn't the case.HoppCollection
/HoppEnvironment
formats, and requires transformation before further processing. New helper functionstransformWorkspaceCollection()
&transformWorkspaceEnvironment()
are added under ~/utils/workspace-access.ts.readJsonFile
helper function under ~/utils/mutators.ts to include an additional parameterfileExistsInPath
used to throw an error if the file doesn't exist in path. A check for the same that existed previously is removed in favor of the same performed at an earlier point (proceed with the network call if there isn't a valid file path corresponding to the supplied path/ID).e2e
tests moved under__tests__/e2e
and unit tests under__tests__/unit
with the respective test data maintained underfixtures
for both directories. The pre-existing unit tests under__tests__/functions
are to be revisited separately (Tracked in HFE-326) and moved under__tests__/unit
.Closes HFE-502 HFE-503.
Steps to verify (SH)
Spin up the
selfhost-web
dev server and the BE container locally.Obtain a new personal access token to access the workspace resources. It can be done via the UI if checking out to feat: introduce personal access tokens for authorization #4094. Alternatively, send a post request to the
http://<localhost_server_url>/v1/access-tokens/create
endpoint with the following body:Choose a collection to run via the CLI or create one if none exists.
There'll be a UI flow introduced to obtain the collection ID (and active environment ID) straightaway from the UI within the collection tree. For now, grab the collection ID (match the name against the
title
field from the response within thedata.rootCollectionsOfTeam
field and pick the correspondingid
field value) of interest from theRootCollectionsOfTeam
GQL query.Navigate to the
hoppscotch-cli
package path locally and runpnpm build
. Run the collection via:Similarly, if running a collection that requires an environment, grab the environment ID (match the name against the
name
field from the response within thedata.team.teamEnvironments
field and pick the correspondingid
field value) from theGetTeamEnvironments
GQL query.Run the collection supplying the environment ID:
Both file path and ID can be supplied while specifying an access token. Hence, try out different combinations and the different error scenarios mentioned above.
Checks
Note to reviewers
E2e additions for the workspace access behavior (tests including the usage of
--token
&--server
flags) are skipped for now. It'll be enabled once a proper e2e test environment is set up locally. Unit tests are in place for the new functionality added.