feat: add ability to detect current project#242
Open
danudey wants to merge 11 commits intosemaphoreci:masterfrom
Open
feat: add ability to detect current project#242danudey wants to merge 11 commits intosemaphoreci:masterfrom
danudey wants to merge 11 commits intosemaphoreci:masterfrom
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The Semaphore CLI tool has the ability to detect the current project, but there are a few caveats:
originis the upstream remote; this is not a safe assumption because when creating a fork theghcli tool will rename the upstream remote toupstreamand set the new fork to beorigin.This means that if a user needs the current project ID (e.g. to trigger a pipeline via the API), they must get the list of all remote URLs themselves, get the list of all projects themselves, find a match between the two, and then call
sem get project <project name>to get the project's ID.This PR does a few things (updated from the previous implementation):
First, we add a new function
getAllGitRemotesAndProjects(), which does the following:git configSecondly, we also add a small helper function,
getGitHubBaseRemoteName(), which checks the repository's git configuration viagit configto see if the user has rungh repo set-defaultto choose a default remote.Thirdly, we refactor
InferProject()to callgetAllGitRemotesAndProjects()and then choose one of the results with the following detection logic:gh repo set-defaultand that remote is in the list, use thatoriginrepo, use that; if not but there's anupstreamrepo, use thatThe original
InferProject()simply chose theoriginremote and used that, so this new logic will cover those cases as well, and thus shouldn't cause issues with the rest of the codebase.Lastly, we add a new
sem get current_project(aliassem get cur) which simply prints out the project's metadata:Originally I was planning on highlighting the current project when a user did
sem get projectwithout an argument, but that ended up feeling messier than just having a separate command for it which a user could parse.Edit: Added a
--jsonflag tocurrent_projectto format the output as JSON (which includes far more data).Edit 2: Bumped golang version from 1.20 to 1.21 to get the
slicespackage (should go to 1.25 IMHO but this is a minimal change), and added-buildvcs=falseto work around git permissions warnings in docker container when runningmake buildEdit 3: Completely rewrote almost everything into a vastly simplified and yet more complicated implementation.