-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add gh.sh wrapper for gh CLI commands in issue triage workflows #975
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
Merged
+106
−13
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| # Wrapper around gh CLI that only allows specific subcommands and flags. | ||
| # All commands are scoped to the current repository via GH_REPO or GITHUB_REPOSITORY. | ||
| # | ||
| # Usage: | ||
| # ./scripts/gh.sh issue view 123 | ||
| # ./scripts/gh.sh issue view 123 --comments | ||
| # ./scripts/gh.sh issue list --state open --limit 20 | ||
| # ./scripts/gh.sh search issues "search query" --limit 10 | ||
| # ./scripts/gh.sh label list --limit 100 | ||
|
|
||
| ALLOWED_FLAGS=(--comments --state --limit --label) | ||
| FLAGS_WITH_VALUES=(--state --limit --label) | ||
|
|
||
| SUB1="${1:-}" | ||
| SUB2="${2:-}" | ||
OctavianGuzu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| CMD="$SUB1 $SUB2" | ||
| case "$CMD" in | ||
| "issue view"|"issue list"|"search issues"|"label list") | ||
| ;; | ||
| *) | ||
| exit 1 | ||
OctavianGuzu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ;; | ||
| esac | ||
|
|
||
| shift 2 | ||
|
|
||
| # Separate flags from positional arguments | ||
| POSITIONAL=() | ||
| FLAGS=() | ||
| skip_next=false | ||
| for arg in "$@"; do | ||
| if [[ "$skip_next" == true ]]; then | ||
| FLAGS+=("$arg") | ||
| skip_next=false | ||
| elif [[ "$arg" == -* ]]; then | ||
| flag="${arg%%=*}" | ||
| matched=false | ||
| for allowed in "${ALLOWED_FLAGS[@]}"; do | ||
| if [[ "$flag" == "$allowed" ]]; then | ||
| matched=true | ||
| break | ||
| fi | ||
| done | ||
| if [[ "$matched" == false ]]; then | ||
| exit 1 | ||
| fi | ||
| FLAGS+=("$arg") | ||
| # If flag expects a value and isn't using = syntax, skip next arg | ||
| if [[ "$arg" != *=* ]]; then | ||
| for vflag in "${FLAGS_WITH_VALUES[@]}"; do | ||
| if [[ "$flag" == "$vflag" ]]; then | ||
| skip_next=true | ||
| break | ||
| fi | ||
| done | ||
| fi | ||
| else | ||
| POSITIONAL+=("$arg") | ||
| fi | ||
| done | ||
|
|
||
| REPO="${GH_REPO:-${GITHUB_REPOSITORY:-}}" | ||
|
|
||
| if [[ "$CMD" == "search issues" ]]; then | ||
| if [[ -z "$REPO" ]]; then | ||
| exit 1 | ||
| fi | ||
| QUERY="${POSITIONAL[0]:-}" | ||
| QUERY_LOWER=$(echo "$QUERY" | tr '[:upper:]' '[:lower:]') | ||
| if [[ "$QUERY_LOWER" == *"repo:"* || "$QUERY_LOWER" == *"org:"* || "$QUERY_LOWER" == *"user:"* ]]; then | ||
| exit 1 | ||
| fi | ||
| gh "$SUB1" "$SUB2" "$QUERY" --repo "$REPO" "${FLAGS[@]}" | ||
| else | ||
| # Reject URLs in positional args to prevent cross-repo access | ||
| for pos in "${POSITIONAL[@]}"; do | ||
| if [[ "$pos" == http://* || "$pos" == https://* ]]; then | ||
| exit 1 | ||
| fi | ||
| done | ||
| gh "$SUB1" "$SUB2" "${POSITIONAL[@]}" "${FLAGS[@]}" | ||
| fi | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.