diff --git a/CODEOWNERS b/.github/CODEOWNERS similarity index 100% rename from CODEOWNERS rename to .github/CODEOWNERS diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml new file mode 100644 index 000000000..3974737aa --- /dev/null +++ b/.github/workflows/deployment.yml @@ -0,0 +1,346 @@ +name: Deployment + +concurrency: + group: ${{ github.workflow }}-${{ github.ref_name }} + cancel-in-progress: true + +permissions: + contents: write + +on: + workflow_dispatch: + inputs: + tag_name: + required: true + type: string + environment: + default: production + type: environment + go_version: + default: "1.19" + type: string + platforms: + default: "linux,macos,windows" + type: string + release: + description: "Whether to create a GitHub Release" + type: boolean + default: true + +jobs: + linux: + runs-on: ubuntu-latest + environment: ${{ inputs.environment }} + if: contains(inputs.platforms, 'linux') + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: ${{ inputs.go_version }} + - name: Install GoReleaser + uses: goreleaser/goreleaser-action@v4 + with: + version: "~1.17.1" + install-only: true + - name: Build release binaries + env: + TAG_NAME: ${{ inputs.tag_name }} + run: script/release --local "$TAG_NAME" --platform linux + - name: Generate web manual pages + run: | + go run ./cmd/gen-docs --website --doc-path dist/manual + tar -czvf dist/manual.tar.gz -C dist -- manual + - uses: actions/upload-artifact@v3 + with: + name: linux + if-no-files-found: error + retention-days: 7 + path: | + dist/*.tar.gz + dist/*.rpm + dist/*.deb + + macos: + runs-on: macos-latest + environment: ${{ inputs.environment }} + if: contains(inputs.platforms, 'macos') + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: ${{ inputs.go_version }} + - name: Configure macOS signing + if: inputs.environment == 'production' + env: + APPLE_DEVELOPER_ID: ${{ vars.APPLE_DEVELOPER_ID }} + APPLE_APPLICATION_CERT: ${{ secrets.APPLE_APPLICATION_CERT }} + APPLE_APPLICATION_CERT_PASSWORD: ${{ secrets.APPLE_APPLICATION_CERT_PASSWORD }} + run: | + keychain="$RUNNER_TEMP/buildagent.keychain" + keychain_password="password1" + + security create-keychain -p "$keychain_password" "$keychain" + security default-keychain -s "$keychain" + security unlock-keychain -p "$keychain_password" "$keychain" + + base64 -D <<<"$APPLE_APPLICATION_CERT" > "$RUNNER_TEMP/cert.p12" + security import "$RUNNER_TEMP/cert.p12" -k "$keychain" -P "$APPLE_APPLICATION_CERT_PASSWORD" -T /usr/bin/codesign + security set-key-partition-list -S "apple-tool:,apple:,codesign:" -s -k "$keychain_password" "$keychain" + rm "$RUNNER_TEMP/cert.p12" + - name: Install GoReleaser + uses: goreleaser/goreleaser-action@v4 + with: + version: "~1.17.1" + install-only: true + - name: Build release binaries + env: + TAG_NAME: ${{ inputs.tag_name }} + APPLE_DEVELOPER_ID: ${{ vars.APPLE_DEVELOPER_ID }} + run: script/release --local "$TAG_NAME" --platform macos + - name: Notarize macOS archives + if: inputs.environment == 'production' + env: + APPLE_ID: ${{ vars.APPLE_ID }} + APPLE_ID_PASSWORD: ${{ secrets.APPLE_ID_PASSWORD }} + APPLE_DEVELOPER_ID: ${{ vars.APPLE_DEVELOPER_ID }} + run: | + shopt -s failglob + script/sign dist/gh_*_macOS_*.zip + - uses: actions/upload-artifact@v3 + with: + name: macos + if-no-files-found: error + retention-days: 7 + path: | + dist/*.tar.gz + dist/*.zip + + windows: + runs-on: windows-latest + environment: ${{ inputs.environment }} + if: contains(inputs.platforms, 'windows') + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: ${{ inputs.go_version }} + - name: Obtain signing certificate + id: obtain_cert + if: inputs.environment == 'production' + shell: bash + run: | + base64 -d <<<"$CERT_CONTENTS" > ./cert.pfx + printf "cert-file=%s\n" ".\\cert.pfx" >> $GITHUB_OUTPUT + env: + CERT_CONTENTS: ${{ secrets.WINDOWS_CERT_PFX }} + - name: Install GoReleaser + uses: goreleaser/goreleaser-action@v4 + with: + version: "~1.17.1" + install-only: true + - name: Build release binaries + shell: bash + env: + TAG_NAME: ${{ inputs.tag_name }} + CERT_FILE: ${{ steps.obtain_cert.outputs.cert-file }} + CERT_PASSWORD: ${{ secrets.WINDOWS_CERT_PASSWORD }} + run: script/release --local "$TAG_NAME" --platform windows + - name: Set up MSBuild + id: setupmsbuild + uses: microsoft/setup-msbuild@v1.3.1 + - name: Build MSI + shell: bash + env: + MSBUILD_PATH: ${{ steps.setupmsbuild.outputs.msbuildPath }} + run: | + for ZIP_FILE in dist/gh_*_windows_*.zip; do + MSI_NAME="$(basename "$ZIP_FILE" ".zip")" + MSI_VERSION="$(cut -d_ -f2 <<<"$MSI_NAME" | cut -d- -f1)" + case "$MSI_NAME" in + *_386 ) + source_dir="$PWD/dist/windows_windows_386" + platform="x86" + ;; + *_amd64 ) + source_dir="$PWD/dist/windows_windows_amd64_v1" + platform="x64" + ;; + *_arm64 ) + echo "skipping building MSI for arm64 because WiX 3.11 doesn't support it: https://github.com/wixtoolset/issues/issues/6141" >&2 + continue + #source_dir="$PWD/dist/windows_windows_arm64" + #platform="arm64" + ;; + * ) + printf "unsupported architecture: %s\n" "$MSI_NAME" >&2 + exit 1 + ;; + esac + "${MSBUILD_PATH}\MSBuild.exe" ./build/windows/gh.wixproj -p:SourceDir="$source_dir" -p:OutputPath="$PWD/dist" -p:OutputName="$MSI_NAME" -p:ProductVersion="${MSI_VERSION#v}" -p:Platform="$platform" + done + - name: Sign MSI + if: inputs.environment == 'production' + shell: pwsh + run: | + Get-ChildItem -Path .\dist -Filter *.msi | ForEach-Object { + .\script\sign $_.FullName + } + env: + CERT_FILE: ${{ steps.obtain_cert.outputs.cert-file }} + CERT_PASSWORD: ${{ secrets.WINDOWS_CERT_PASSWORD }} + - uses: actions/upload-artifact@v3 + with: + name: windows + if-no-files-found: error + retention-days: 7 + path: | + dist/*.zip + dist/*.msi + + release: + runs-on: ubuntu-latest + needs: [linux, macos, windows] + if: inputs.release + steps: + - name: Checkout cli/cli + uses: actions/checkout@v3 + - name: Merge built artifacts + uses: actions/download-artifact@v3 + - name: Checkout documentation site + uses: actions/checkout@v3 + with: + repository: github/cli.github.com + path: site + fetch-depth: 0 + token: ${{ secrets.SITE_DEPLOY_PAT }} + - name: Update site man pages + env: + GIT_COMMITTER_NAME: cli automation + GIT_AUTHOR_NAME: cli automation + GIT_COMMITTER_EMAIL: noreply@github.com + GIT_AUTHOR_EMAIL: noreply@github.com + TAG_NAME: ${{ inputs.tag_name }} + run: | + git -C site rm 'manual/gh*.md' 2>/dev/null || true + tar -xzvf linux/manual.tar.gz -C site + git -C site add 'manual/gh*.md' + sed -i.bak -E "s/(assign version = )\".+\"/\1\"${TAG_NAME#v}\"/" site/index.html + rm -f site/index.html.bak + git -C site add index.html + git -C site diff --quiet --cached || git -C site commit -m "gh ${TAG_NAME#v}" + - name: Prepare release assets + env: + TAG_NAME: ${{ inputs.tag_name }} + run: | + shopt -s failglob + rm -rf dist + mkdir dist + mv -v {linux,macos,windows}/gh_* dist/ + - name: Install packaging dependencies + run: sudo apt-get install -y rpm reprepro + - name: Set up GPG + if: inputs.environment == 'production' + env: + GPG_PUBKEY: ${{ secrets.GPG_PUBKEY }} + GPG_KEY: ${{ secrets.GPG_KEY }} + GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} + GPG_KEYGRIP: ${{ secrets.GPG_KEYGRIP }} + run: | + base64 -d <<<"$GPG_PUBKEY" | gpg --import --no-tty --batch --yes + base64 -d <<<"$GPG_KEY" | gpg --import --no-tty --batch --yes + echo "allow-preset-passphrase" > ~/.gnupg/gpg-agent.conf + gpg-connect-agent RELOADAGENT /bye + /usr/lib/gnupg2/gpg-preset-passphrase --preset "$GPG_KEYGRIP" <<<"$GPG_PASSPHRASE" + - name: Sign RPMs + if: inputs.environment == 'production' + run: | + cp script/rpmmacros ~/.rpmmacros + rpmsign --addsign dist/*.rpm + - name: Run createrepo + env: + GPG_SIGN: ${{ inputs.environment == 'production' }} + run: | + mkdir -p site/packages/rpm + cp dist/*.rpm site/packages/rpm/ + ./script/createrepo.sh + cp -r dist/repodata site/packages/rpm/ + pushd site/packages/rpm + [ "$GPG_SIGN" = "false" ] || gpg --yes --detach-sign --armor repodata/repomd.xml + popd + - name: Run reprepro + env: + GPG_SIGN: ${{ inputs.environment == 'production' }} + # We are no longer adding to the distribution list. + # All apt distributions should use "stable" according to our install documentation. + # In the future we will remove legacy distributions listed here. + RELEASES: "cosmic eoan disco groovy focal stable oldstable testing sid unstable buster bullseye stretch jessie bionic trusty precise xenial hirsute impish kali-rolling" + run: | + mkdir -p upload + [ "$GPG_SIGN" = "true" ] || sed -i.bak '/^SignWith:/d' script/distributions + for release in $RELEASES; do + for file in dist/*.deb; do + reprepro --confdir="+b/script" includedeb "$release" "$file" + done + done + cp -a dists/ pool/ upload/ + mkdir -p site/packages + cp -a upload/* site/packages/ + - name: Create the release + env: + # In non-production environments, the assets will not have been signed + DO_PUBLISH: ${{ inputs.environment == 'production' }} + TAG_NAME: ${{ inputs.tag_name }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + shopt -s failglob + pushd dist + shasum -a 256 gh_* > checksums.txt + mv checksums.txt gh_${TAG_NAME#v}_checksums.txt + popd + release_args=( + "$TAG_NAME" + --title "GitHub CLI ${TAG_NAME#v}" + --target "$GITHUB_SHA" + --generate-notes + ) + if [[ $TAG_NAME == *-* ]]; then + release_args+=( --prerelease ) + else + release_args+=( --discussion-category "General" ) + fi + guard="echo" + [ "$DO_PUBLISH" = "false" ] || guard="" + script/label-assets dist/gh_* | xargs $guard gh release create "${release_args[@]}" -- + - name: Publish site + env: + DO_PUBLISH: ${{ inputs.environment == 'production' && !contains(inputs.tag_name, '-') }} + TAG_NAME: ${{ inputs.tag_name }} + GIT_COMMITTER_NAME: cli automation + GIT_AUTHOR_NAME: cli automation + GIT_COMMITTER_EMAIL: noreply@github.com + GIT_AUTHOR_EMAIL: noreply@github.com + working-directory: ./site + run: | + git add packages + git commit -m "Add rpm and deb packages for $TAG_NAME" + if [ "$DO_PUBLISH" = "true" ]; then + git push + else + git log --oneline @{upstream}.. + git diff --name-status @{upstream}.. + fi + - name: Bump homebrew-core formula + uses: mislav/bump-homebrew-formula-action@v2 + if: inputs.environment == 'production' && !contains(inputs.tag_name, '-') + with: + formula-name: gh + tag-name: ${{ inputs.tag_name }} + push-to: cli/homebrew-core + env: + COMMITTER_TOKEN: ${{ secrets.HOMEBREW_PR_PAT }} diff --git a/.github/workflows/releases.yml b/.github/workflows/releases.yml deleted file mode 100644 index 7ed0c3fa9..000000000 --- a/.github/workflows/releases.yml +++ /dev/null @@ -1,222 +0,0 @@ -name: goreleaser - -on: - push: - tags: - - "v*" - -permissions: - contents: write # publishing releases - repository-projects: write # move cards between columns - -jobs: - goreleaser: - runs-on: ubuntu-20.04 - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - name: Set up Go 1.19 - uses: actions/setup-go@v4 - with: - go-version: 1.19 - - name: Generate changelog - id: changelog - run: | - echo "tag-name=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT - gh api repos/$GITHUB_REPOSITORY/releases/generate-notes \ - -f tag_name="${GITHUB_REF#refs/tags/}" \ - -f target_commitish=trunk \ - -q .body > CHANGELOG.md - env: - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - - name: Install osslsigncode - run: sudo apt-get install -y osslsigncode - - name: Obtain signing cert - run: | - cert="$(mktemp -t cert.XXX)" - base64 -d <<<"$CERT_CONTENTS" > "$cert" - echo "CERT_FILE=$cert" >> $GITHUB_ENV - env: - CERT_CONTENTS: ${{ secrets.WINDOWS_CERT_PFX }} - - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v4 - with: - version: "~1.13.1" - args: release --release-notes=CHANGELOG.md - env: - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - GORELEASER_CURRENT_TAG: ${{steps.changelog.outputs.tag-name}} - CERT_PASSWORD: ${{secrets.WINDOWS_CERT_PASSWORD}} - - name: Checkout documentation site - uses: actions/checkout@v3 - with: - repository: github/cli.github.com - path: site - fetch-depth: 0 - ssh-key: ${{secrets.SITE_SSH_KEY}} - - name: Update site man pages - env: - GIT_COMMITTER_NAME: cli automation - GIT_AUTHOR_NAME: cli automation - GIT_COMMITTER_EMAIL: noreply@github.com - GIT_AUTHOR_EMAIL: noreply@github.com - run: make site-bump - - name: Move project cards - continue-on-error: true - env: - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - PENDING_COLUMN: 8189733 - DONE_COLUMN: 7110130 - run: | - api() { gh api -H 'accept: application/vnd.github.inertia-preview+json' "$@"; } - api-write() { [[ $GITHUB_REF == *-* ]] && echo "skipping: api $*" || api "$@"; } - cards=$(api --paginate projects/columns/$PENDING_COLUMN/cards | jq ".[].id") - for card in $cards; do - api-write --silent projects/columns/cards/$card/moves -f position=top -F column_id=$DONE_COLUMN - done - echo "moved ${#cards[@]} cards to the Done column" - - name: Install packaging dependencies - run: sudo apt-get install -y rpm reprepro - - name: Set up GPG - run: | - echo "${{secrets.GPG_PUBKEY}}" | base64 -d | gpg --import --no-tty --batch --yes - echo "${{secrets.GPG_KEY}}" | base64 -d | gpg --import --no-tty --batch --yes - echo "allow-preset-passphrase" > ~/.gnupg/gpg-agent.conf - gpg-connect-agent RELOADAGENT /bye - echo "${{secrets.GPG_PASSPHRASE}}" | /usr/lib/gnupg2/gpg-preset-passphrase --preset "${{secrets.GPG_KEYGRIP}}" - - name: Sign RPMs - run: | - cp script/rpmmacros ~/.rpmmacros - rpmsign --addsign dist/*.rpm - - name: Run createrepo - run: | - mkdir -p site/packages/rpm - cp dist/*.rpm site/packages/rpm/ - ./script/createrepo.sh - cp -r dist/repodata site/packages/rpm/ - pushd site/packages/rpm - gpg --yes --detach-sign --armor repodata/repomd.xml - popd - - name: Run reprepro - env: - # We are no longer adding to the distribution list. - # All apt distributions should use "stable" according to our install documentation. - # In the future we will remove legacy distributions listed here. - RELEASES: "cosmic eoan disco groovy focal stable oldstable testing sid unstable buster bullseye stretch jessie bionic trusty precise xenial hirsute impish kali-rolling" - run: | - mkdir -p upload - for release in $RELEASES; do - for file in dist/*.deb; do - reprepro --confdir="+b/script" includedeb "$release" "$file" - done - done - cp -a dists/ pool/ upload/ - mkdir -p site/packages - cp -a upload/* site/packages/ - - name: Publish site - env: - GIT_COMMITTER_NAME: cli automation - GIT_AUTHOR_NAME: cli automation - GIT_COMMITTER_EMAIL: noreply@github.com - GIT_AUTHOR_EMAIL: noreply@github.com - working-directory: ./site - run: | - git add packages - git commit -m "Add rpm and deb packages for ${GITHUB_REF#refs/tags/}" - if [[ $GITHUB_REF == *-* ]]; then - git log --oneline @{upstream}.. - git diff --name-status @{upstream}.. - else - git push - fi - - msi: - needs: goreleaser - runs-on: windows-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Download gh.exe - id: download_exe - shell: bash - run: | - hub release download "${GITHUB_REF#refs/tags/}" -i '*windows_amd64*.zip' - printf "zip=%s\n" *.zip >> $GITHUB_OUTPUT - unzip -o *.zip && rm -v *.zip - env: - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - - name: Prepare PATH - id: setupmsbuild - uses: microsoft/setup-msbuild@v1.3.1 - - name: Build MSI - id: buildmsi - shell: bash - env: - ZIP_FILE: ${{ steps.download_exe.outputs.zip }} - MSBUILD_PATH: ${{ steps.setupmsbuild.outputs.msbuildPath }} - run: | - name="$(basename "$ZIP_FILE" ".zip")" - version="$(echo -e ${GITHUB_REF#refs/tags/v} | sed s/-.*$//)" - "${MSBUILD_PATH}\MSBuild.exe" ./build/windows/gh.wixproj -p:SourceDir="$PWD" -p:OutputPath="$PWD" -p:OutputName="$name" -p:ProductVersion="$version" - - name: Obtain signing cert - id: obtain_cert - shell: bash - run: | - base64 -d <<<"$CERT_CONTENTS" > ./cert.pfx - printf "cert-file=%s\n" ".\\cert.pfx" >> $GITHUB_OUTPUT - env: - CERT_CONTENTS: ${{ secrets.WINDOWS_CERT_PFX }} - - name: Sign MSI - env: - CERT_FILE: ${{ steps.obtain_cert.outputs.cert-file }} - EXE_FILE: ${{ steps.buildmsi.outputs.msi }} - CERT_PASSWORD: ${{ secrets.WINDOWS_CERT_PASSWORD }} - run: .\script\signtool sign /d "GitHub CLI" /f $env:CERT_FILE /p $env:CERT_PASSWORD /fd sha256 /tr http://timestamp.digicert.com /v $env:EXE_FILE - - name: Upload MSI - shell: bash - run: | - tag_name="${GITHUB_REF#refs/tags/}" - hub release edit "$tag_name" -m "" -a "$MSI_FILE" - release_url="$(gh api repos/:owner/:repo/releases -q ".[]|select(.tag_name==\"${tag_name}\")|.url")" - publish_args=( -F draft=false ) - if [[ $GITHUB_REF != *-* ]]; then - publish_args+=( -f discussion_category_name="$DISCUSSION_CATEGORY" ) - fi - gh api -X PATCH "$release_url" "${publish_args[@]}" - env: - MSI_FILE: ${{ steps.buildmsi.outputs.msi }} - DISCUSSION_CATEGORY: General - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - - name: Bump homebrew-core formula - uses: mislav/bump-homebrew-formula-action@v2 - if: "!contains(github.ref, '-')" # skip prereleases - with: - formula-name: gh - env: - COMMITTER_TOKEN: ${{ secrets.UPLOAD_GITHUB_TOKEN }} - - name: Checkout scoop bucket - uses: actions/checkout@v3 - with: - repository: cli/scoop-gh - path: scoop-gh - fetch-depth: 0 - token: ${{secrets.UPLOAD_GITHUB_TOKEN}} - - name: Bump scoop bucket - shell: bash - run: | - hub release download "${GITHUB_REF#refs/tags/}" -i '*_checksums.txt' - script/scoop-gen "${GITHUB_REF#refs/tags/}" ./scoop-gh/gh.json < *_checksums.txt - git -C ./scoop-gh commit -m "gh ${GITHUB_REF#refs/tags/}" gh.json - if [[ $GITHUB_REF == *-* ]]; then - git -C ./scoop-gh show -m - else - git -C ./scoop-gh push - fi - env: - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - GIT_COMMITTER_NAME: cli automation - GIT_AUTHOR_NAME: cli automation - GIT_COMMITTER_EMAIL: noreply@github.com - GIT_AUTHOR_EMAIL: noreply@github.com diff --git a/.gitignore b/.gitignore index 905701534..b1e8e1fa8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,17 @@ /bin +/share/bash-completion/completions +/share/fish/vendor_completions.d /share/man/man1 +/share/zsh/site-functions /gh-cli .envrc /dist /site .github/**/node_modules /CHANGELOG.md +/.goreleaser.generated.yml /script/build +/script/build.exe # VS Code .vscode diff --git a/.goreleaser.yml b/.goreleaser.yml index d67d023ae..f441156e3 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -7,56 +7,76 @@ release: before: hooks: - - go mod tidy - - make manpages GH_VERSION={{.Version}} + - >- + {{ if eq .Runtime.Goos "windows" }}echo{{ end }} make manpages GH_VERSION={{.Version}} + - >- + {{ if ne .Runtime.Goos "linux" }}echo{{ end }} make completions builds: - - <<: &build_defaults - binary: bin/gh - main: ./cmd/gh - ldflags: - - -s -w -X github.com/cli/cli/v2/internal/build.Version={{.Version}} -X github.com/cli/cli/v2/internal/build.Date={{time "2006-01-02"}} - id: macos + - id: macos #build:macos goos: [darwin] goarch: [amd64, arm64] + hooks: + post: + - cmd: ./script/sign '{{ .Path }}' + output: true + binary: bin/gh + main: ./cmd/gh + ldflags: + - -s -w -X github.com/cli/cli/v2/internal/build.Version={{.Version}} -X github.com/cli/cli/v2/internal/build.Date={{time "2006-01-02"}} - - <<: *build_defaults - id: linux + - id: linux #build:linux goos: [linux] goarch: [386, arm, amd64, arm64] env: - CGO_ENABLED=0 + binary: bin/gh + main: ./cmd/gh + ldflags: + - -s -w -X github.com/cli/cli/v2/internal/build.Version={{.Version}} -X github.com/cli/cli/v2/internal/build.Date={{time "2006-01-02"}} - - <<: *build_defaults - id: windows + - id: windows #build:windows goos: [windows] goarch: [386, amd64, arm64] hooks: post: - - cmd: ./script/sign-windows-executable.sh '{{ .Path }}' - output: false + - cmd: >- + {{ if eq .Runtime.Goos "windows" }}.\script\sign{{ else }}./script/sign{{ end }} '{{ .Path }}' + output: true + binary: bin/gh + main: ./cmd/gh + ldflags: + - -s -w -X github.com/cli/cli/v2/internal/build.Version={{.Version}} -X github.com/cli/cli/v2/internal/build.Date={{time "2006-01-02"}} archives: - - id: nix - builds: [macos, linux] - <<: &archive_defaults - name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" + - id: linux-archive + builds: [linux] + name_template: "gh_{{ .Version }}_linux_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" wrap_in_directory: true - replacements: - darwin: macOS format: tar.gz + rlcp: true files: - LICENSE - ./share/man/man1/gh*.1 - - id: windows + - id: macos-archive + builds: [macos] + name_template: "gh_{{ .Version }}_macOS_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" + wrap_in_directory: true + format: zip + rlcp: true + files: + - LICENSE + - ./share/man/man1/gh*.1 + - id: windows-archive builds: [windows] - <<: *archive_defaults + name_template: "gh_{{ .Version }}_windows_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" wrap_in_directory: false format: zip + rlcp: true files: - LICENSE -nfpms: +nfpms: #build:linux - license: MIT maintainer: GitHub homepage: https://github.com/cli/cli @@ -70,3 +90,9 @@ nfpms: contents: - src: "./share/man/man1/gh*.1" dst: "/usr/share/man/man1" + - src: "./share/bash-completion/completions/gh" + dst: "/usr/share/bash-completion/completions/gh" + - src: "./share/fish/vendor_completions.d/gh.fish" + dst: "/usr/share/fish/vendor_completions.d/gh.fish" + - src: "./share/zsh/site-functions/_gh" + dst: "/usr/share/zsh/site-functions/_gh" diff --git a/Makefile b/Makefile index 46d40a7a9..7dac0d290 100644 --- a/Makefile +++ b/Makefile @@ -6,26 +6,37 @@ CGO_LDFLAGS ?= $(filter -g -L% -l% -O%,${LDFLAGS}) export CGO_LDFLAGS EXE = -ifeq ($(GOOS),windows) +ifeq ($(shell go env GOOS),windows) EXE = .exe endif ## The following tasks delegate to `script/build.go` so they can be run cross-platform. .PHONY: bin/gh$(EXE) -bin/gh$(EXE): script/build - @script/build $@ +bin/gh$(EXE): script/build$(EXE) + @script/build$(EXE) $@ -script/build: script/build.go +script/build$(EXE): script/build.go +ifeq ($(EXE),) GOOS= GOARCH= GOARM= GOFLAGS= CGO_ENABLED= go build -o $@ $< +else + go build -o $@ $< +endif .PHONY: clean -clean: script/build - @script/build $@ +clean: script/build$(EXE) + @$< $@ .PHONY: manpages -manpages: script/build - @script/build $@ +manpages: script/build$(EXE) + @$< $@ + +.PHONY: completions +completions: bin/gh$(EXE) + mkdir -p ./share/bash-completion/completions ./share/fish/vendor_completions.d ./share/zsh/site-functions + bin/gh$(EXE) completion -s bash > ./share/bash-completion/completions/gh + bin/gh$(EXE) completion -s fish > ./share/fish/vendor_completions.d/gh.fish + bin/gh$(EXE) completion -s zsh > ./share/zsh/site-functions/_gh # just a convenience task around `go test` .PHONY: test @@ -60,15 +71,25 @@ endif DESTDIR := prefix := /usr/local bindir := ${prefix}/bin -mandir := ${prefix}/share/man +datadir := ${prefix}/share +mandir := ${datadir}/man .PHONY: install -install: bin/gh manpages +install: bin/gh manpages completions install -d ${DESTDIR}${bindir} install -m755 bin/gh ${DESTDIR}${bindir}/ install -d ${DESTDIR}${mandir}/man1 install -m644 ./share/man/man1/* ${DESTDIR}${mandir}/man1/ + install -d ${DESTDIR}${datadir}/bash-completion/completions + install -m644 ./share/bash-completion/completions/gh ${DESTDIR}${datadir}/bash-completion/completions/gh + install -d ${DESTDIR}${datadir}/fish/vendor_completions.d + install -m644 ./share/fish/vendor_completions.d/gh.fish ${DESTDIR}${datadir}/fish/vendor_completions.d/gh.fish + install -d ${DESTDIR}${datadir}/zsh/site-functions + install -m644 ./share/zsh/site-functions/_gh ${DESTDIR}${datadir}/zsh/site-functions/_gh .PHONY: uninstall uninstall: rm -f ${DESTDIR}${bindir}/gh ${DESTDIR}${mandir}/man1/gh.1 ${DESTDIR}${mandir}/man1/gh-*.1 + rm -f ${DESTDIR}${datadir}/bash-completion/completions/gh + rm -f ${DESTDIR}${datadir}/fish/vendor_completions.d/gh.fish + rm -f ${DESTDIR}${datadir}/zsh/site-functions/_gh diff --git a/README.md b/README.md index 38c44c788..9ffc6d720 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ For more information, see [Linux & BSD installation](./docs/install_linux.md). | `winget install --id GitHub.cli` | `winget upgrade --id GitHub.cli` | > **Note** -> The Windows installer modifies your PATH. When using Windows Terminal, you will need to **open a new window** for the changes to take affect. (Simply opening a new tab will _not_ be sufficient.) +> The Windows installer modifies your PATH. When using Windows Terminal, you will need to **open a new window** for the changes to take effect. (Simply opening a new tab will _not_ be sufficient.) #### scoop diff --git a/api/client.go b/api/client.go index 0447051cc..e32856554 100644 --- a/api/client.go +++ b/api/client.go @@ -11,8 +11,7 @@ import ( "strings" "github.com/cli/cli/v2/internal/ghinstance" - "github.com/cli/go-gh" - ghAPI "github.com/cli/go-gh/pkg/api" + ghAPI "github.com/cli/go-gh/v2/pkg/api" ) const ( @@ -40,11 +39,11 @@ func (c *Client) HTTP() *http.Client { } type GraphQLError struct { - ghAPI.GQLError + *ghAPI.GraphQLError } type HTTPError struct { - ghAPI.HTTPError + *ghAPI.HTTPError scopesSuggestion string } @@ -57,7 +56,7 @@ func (err HTTPError) ScopesSuggestion() string { func (c Client) GraphQL(hostname string, query string, variables map[string]interface{}, data interface{}) error { opts := clientOptions(hostname, c.http.Transport) opts.Headers[graphqlFeatures] = features - gqlClient, err := gh.GQLClient(&opts) + gqlClient, err := ghAPI.NewGraphQLClient(opts) if err != nil { return err } @@ -69,7 +68,7 @@ func (c Client) GraphQL(hostname string, query string, variables map[string]inte func (c Client) Mutate(hostname, name string, mutation interface{}, variables map[string]interface{}) error { opts := clientOptions(hostname, c.http.Transport) opts.Headers[graphqlFeatures] = features - gqlClient, err := gh.GQLClient(&opts) + gqlClient, err := ghAPI.NewGraphQLClient(opts) if err != nil { return err } @@ -81,7 +80,7 @@ func (c Client) Mutate(hostname, name string, mutation interface{}, variables ma func (c Client) Query(hostname, name string, query interface{}, variables map[string]interface{}) error { opts := clientOptions(hostname, c.http.Transport) opts.Headers[graphqlFeatures] = features - gqlClient, err := gh.GQLClient(&opts) + gqlClient, err := ghAPI.NewGraphQLClient(opts) if err != nil { return err } @@ -93,7 +92,7 @@ func (c Client) Query(hostname, name string, query interface{}, variables map[st func (c Client) QueryWithContext(ctx context.Context, hostname, name string, query interface{}, variables map[string]interface{}) error { opts := clientOptions(hostname, c.http.Transport) opts.Headers[graphqlFeatures] = features - gqlClient, err := gh.GQLClient(&opts) + gqlClient, err := ghAPI.NewGraphQLClient(opts) if err != nil { return err } @@ -103,7 +102,7 @@ func (c Client) QueryWithContext(ctx context.Context, hostname, name string, que // REST performs a REST request and parses the response. func (c Client) REST(hostname string, method string, p string, body io.Reader, data interface{}) error { opts := clientOptions(hostname, c.http.Transport) - restClient, err := gh.RESTClient(&opts) + restClient, err := ghAPI.NewRESTClient(opts) if err != nil { return err } @@ -112,7 +111,7 @@ func (c Client) REST(hostname string, method string, p string, body io.Reader, d func (c Client) RESTWithNext(hostname string, method string, p string, body io.Reader, data interface{}) (string, error) { opts := clientOptions(hostname, c.http.Transport) - restClient, err := gh.RESTClient(&opts) + restClient, err := ghAPI.NewRESTClient(opts) if err != nil { return "", err } @@ -157,14 +156,14 @@ func HandleHTTPError(resp *http.Response) error { return handleResponse(ghAPI.HandleHTTPError(resp)) } -// handleResponse takes a ghAPI.HTTPError or ghAPI.GQLError and converts it into an +// handleResponse takes a ghAPI.HTTPError or ghAPI.GraphQLError and converts it into an // HTTPError or GraphQLError respectively. func handleResponse(err error) error { if err == nil { return nil } - var restErr ghAPI.HTTPError + var restErr *ghAPI.HTTPError if errors.As(err, &restErr) { return HTTPError{ HTTPError: restErr, @@ -175,10 +174,10 @@ func handleResponse(err error) error { } } - var gqlErr ghAPI.GQLError + var gqlErr *ghAPI.GraphQLError if errors.As(err, &gqlErr) { return GraphQLError{ - GQLError: gqlErr, + GraphQLError: gqlErr, } } diff --git a/api/http_client.go b/api/http_client.go index 3fb83974e..691f99481 100644 --- a/api/http_client.go +++ b/api/http_client.go @@ -9,8 +9,7 @@ import ( "github.com/cli/cli/v2/internal/ghinstance" "github.com/cli/cli/v2/utils" - "github.com/cli/go-gh" - ghAPI "github.com/cli/go-gh/pkg/api" + ghAPI "github.com/cli/go-gh/v2/pkg/api" ) type tokenGetter interface { @@ -55,7 +54,7 @@ func NewHTTPClient(opts HTTPClientOptions) (*http.Client, error) { clientOpts.CacheTTL = opts.CacheTTL } - client, err := gh.HTTPClient(&clientOpts) + client, err := ghAPI.NewHTTPClient(clientOpts) if err != nil { return nil, err } diff --git a/api/pull_request_test.go b/api/pull_request_test.go index 2e4fa73b1..6c15cdddd 100644 --- a/api/pull_request_test.go +++ b/api/pull_request_test.go @@ -2,42 +2,414 @@ package api import ( "encoding/json" + "fmt" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -func TestPullRequest_ChecksStatus(t *testing.T) { - pr := PullRequest{} +func TestChecksStatus_NoCheckRunsOrStatusContexts(t *testing.T) { + t.Parallel() + payload := ` + { "statusCheckRollup": { "nodes": [] } } + ` + var pr PullRequest + require.NoError(t, json.Unmarshal([]byte(payload), &pr)) + + expectedChecksStatus := PullRequestChecksStatus{ + Pending: 0, + Failing: 0, + Passing: 0, + Total: 0, + } + require.Equal(t, expectedChecksStatus, pr.ChecksStatus()) +} + +func TestChecksStatus_SummarisingCheckRuns(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + payload string + expectedChecksStatus PullRequestChecksStatus + }{ + { + name: "QUEUED is treated as Pending", + payload: singleCheckRunWithStatus("QUEUED"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + { + name: "IN_PROGRESS is treated as Pending", + payload: singleCheckRunWithStatus("IN_PROGRESS"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + { + name: "WAITING is treated as Pending", + payload: singleCheckRunWithStatus("WAITING"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + { + name: "PENDING is treated as Pending", + payload: singleCheckRunWithStatus("PENDING"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + { + name: "REQUESTED is treated as Pending", + payload: singleCheckRunWithStatus("REQUESTED"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + { + name: "COMPLETED with no conclusion is treated as Pending", + payload: singleCheckRunWithStatus("COMPLETED"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + { + name: "COMPLETED / STARTUP_FAILURE is treated as Pending", + payload: singleCompletedCheckRunWithConclusion("STARTUP_FAILURE"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + { + name: "COMPLETED / STALE is treated as Pending", + payload: singleCompletedCheckRunWithConclusion("STALE"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + { + name: "COMPLETED / SUCCESS is treated as Passing", + payload: singleCompletedCheckRunWithConclusion("SUCCESS"), + expectedChecksStatus: PullRequestChecksStatus{Passing: 1, Total: 1}, + }, + { + name: "COMPLETED / NEUTRAL is treated as Passing", + payload: singleCompletedCheckRunWithConclusion("NEUTRAL"), + expectedChecksStatus: PullRequestChecksStatus{Passing: 1, Total: 1}, + }, + { + name: "COMPLETED / SKIPPED is treated as Passing", + payload: singleCompletedCheckRunWithConclusion("SKIPPED"), + expectedChecksStatus: PullRequestChecksStatus{Passing: 1, Total: 1}, + }, + { + name: "COMPLETED / ACTION_REQUIRED is treated as Failing", + payload: singleCompletedCheckRunWithConclusion("ACTION_REQUIRED"), + expectedChecksStatus: PullRequestChecksStatus{Failing: 1, Total: 1}, + }, + { + name: "COMPLETED / TIMED_OUT is treated as Failing", + payload: singleCompletedCheckRunWithConclusion("TIMED_OUT"), + expectedChecksStatus: PullRequestChecksStatus{Failing: 1, Total: 1}, + }, + { + name: "COMPLETED / CANCELLED is treated as Failing", + payload: singleCompletedCheckRunWithConclusion("CANCELLED"), + expectedChecksStatus: PullRequestChecksStatus{Failing: 1, Total: 1}, + }, + { + name: "COMPLETED / CANCELLED is treated as Failing", + payload: singleCompletedCheckRunWithConclusion("CANCELLED"), + expectedChecksStatus: PullRequestChecksStatus{Failing: 1, Total: 1}, + }, + { + name: "COMPLETED / FAILURE is treated as Failing", + payload: singleCompletedCheckRunWithConclusion("FAILURE"), + expectedChecksStatus: PullRequestChecksStatus{Failing: 1, Total: 1}, + }, + { + name: "Unrecognized Status are treated as Pending", + payload: singleCheckRunWithStatus("AnUnrecognizedStatusJustForThisTest"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + { + name: "Unrecognized Conclusions are treated as Pending", + payload: singleCompletedCheckRunWithConclusion("AnUnrecognizedConclusionJustForThisTest"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + var pr PullRequest + require.NoError(t, json.Unmarshal([]byte(tt.payload), &pr)) + + require.Equal(t, tt.expectedChecksStatus, pr.ChecksStatus()) + }) + } +} + +func TestChecksStatus_SummarisingStatusContexts(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + payload string + expectedChecksStatus PullRequestChecksStatus + }{ + { + name: "EXPECTED is treated as Pending", + payload: singleStatusContextWithState("EXPECTED"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + { + name: "PENDING is treated as Pending", + payload: singleStatusContextWithState("PENDING"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + { + name: "SUCCESS is treated as Passing", + payload: singleStatusContextWithState("SUCCESS"), + expectedChecksStatus: PullRequestChecksStatus{Passing: 1, Total: 1}, + }, + { + name: "ERROR is treated as Failing", + payload: singleStatusContextWithState("ERROR"), + expectedChecksStatus: PullRequestChecksStatus{Failing: 1, Total: 1}, + }, + { + name: "FAILURE is treated as Failing", + payload: singleStatusContextWithState("FAILURE"), + expectedChecksStatus: PullRequestChecksStatus{Failing: 1, Total: 1}, + }, + { + name: "Unrecognized States are treated as Pending", + payload: singleStatusContextWithState("AnUnrecognizedStateJustForThisTest"), + expectedChecksStatus: PullRequestChecksStatus{Pending: 1, Total: 1}, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + var pr PullRequest + require.NoError(t, json.Unmarshal([]byte(tt.payload), &pr)) + + require.Equal(t, tt.expectedChecksStatus, pr.ChecksStatus()) + }) + } +} + +func TestChecksStatus_SummarisingCheckRunsAndStatusContexts(t *testing.T) { + t.Parallel() + + // This might look a bit intimidating, but we're just inserting three nodes + // into the rollup, two completed check run nodes and one status context node. + payload := fmt.Sprintf(` { "statusCheckRollup": { "nodes": [{ "commit": { "statusCheckRollup": { "contexts": { "nodes": [ - { "state": "SUCCESS" }, - { "state": "PENDING" }, - { "state": "FAILURE" }, - { "status": "IN_PROGRESS", - "conclusion": null }, - { "status": "COMPLETED", - "conclusion": "SUCCESS" }, - { "status": "COMPLETED", - "conclusion": "FAILURE" }, - { "status": "COMPLETED", - "conclusion": "ACTION_REQUIRED" }, - { "status": "COMPLETED", - "conclusion": "STALE" } + %s, + %s, + %s + ] + } + } + } }] } } + `, + completedCheckRunNode("SUCCESS"), + statusContextNode("PENDING"), + completedCheckRunNode("FAILURE"), + ) + + var pr PullRequest + require.NoError(t, json.Unmarshal([]byte(payload), &pr)) + + expectedChecksStatus := PullRequestChecksStatus{ + Pending: 1, + Failing: 1, + Passing: 1, + Total: 3, + } + require.Equal(t, expectedChecksStatus, pr.ChecksStatus()) +} + +func TestChecksStatus_SummarisingCheckRunAndStatusContextCountsByState(t *testing.T) { + t.Parallel() + + payload := ` + { "statusCheckRollup": { "nodes": [{ "commit": { + "statusCheckRollup": { + "contexts": { + "checkRunCount": 14, + "checkRunCountsByState": [ + { + "state": "ACTION_REQUIRED", + "count": 1 + }, + { + "state": "CANCELLED", + "count": 1 + }, + { + "state": "COMPLETED", + "count": 1 + }, + { + "state": "FAILURE", + "count": 1 + }, + { + "state": "IN_PROGRESS", + "count": 1 + }, + { + "state": "NEUTRAL", + "count": 1 + }, + { + "state": "PENDING", + "count": 1 + }, + { + "state": "QUEUED", + "count": 1 + }, + { + "state": "SKIPPED", + "count": 1 + }, + { + "state": "STALE", + "count": 1 + }, + { + "state": "STARTUP_FAILURE", + "count": 1 + }, + { + "state": "SUCCESS", + "count": 1 + }, + { + "state": "TIMED_OUT", + "count": 1 + }, + { + "state": "WAITING", + "count": 1 + }, + { + "state": "AnUnrecognizedStateJustForThisTest", + "count": 1 + } + ], + "statusContextCount": 6, + "statusContextCountsByState": [ + { + "state": "EXPECTED", + "count": 1 + }, + { + "state": "ERROR", + "count": 1 + }, + { + "state": "FAILURE", + "count": 1 + }, + { + "state": "PENDING", + "count": 1 + }, + { + "state": "SUCCESS", + "count": 1 + }, + { + "state": "AnUnrecognizedStateJustForThisTest", + "count": 1 + } ] } } } }] } } ` - err := json.Unmarshal([]byte(payload), &pr) - assert.NoError(t, err) - checks := pr.ChecksStatus() - assert.Equal(t, 8, checks.Total) - assert.Equal(t, 3, checks.Pending) - assert.Equal(t, 3, checks.Failing) - assert.Equal(t, 2, checks.Passing) + var pr PullRequest + require.NoError(t, json.Unmarshal([]byte(payload), &pr)) + + expectedChecksStatus := PullRequestChecksStatus{ + Pending: 11, + Failing: 6, + Passing: 4, + Total: 20, + } + require.Equal(t, expectedChecksStatus, pr.ChecksStatus()) +} + +// Note that it would be incorrect to provide a status of COMPLETED here +// as the conclusion is always set to null. If you want a COMPLETED status, +// use `singleCompletedCheckRunWithConclusion`. +func singleCheckRunWithStatus(status string) string { + return fmt.Sprintf(` + { "statusCheckRollup": { "nodes": [{ "commit": { + "statusCheckRollup": { + "contexts": { + "nodes": [ + { + "__typename": "CheckRun", + "status": "%s", + "conclusion": null + } + ] + } + } + } }] } } + `, status) +} + +func singleCompletedCheckRunWithConclusion(conclusion string) string { + return fmt.Sprintf(` + { "statusCheckRollup": { "nodes": [{ "commit": { + "statusCheckRollup": { + "contexts": { + "nodes": [ + { + "__typename": "CheckRun", + "status": "COMPLETED", + "conclusion": "%s" + } + ] + } + } + } }] } } + `, conclusion) +} + +func singleStatusContextWithState(state string) string { + return fmt.Sprintf(` + { "statusCheckRollup": { "nodes": [{ "commit": { + "statusCheckRollup": { + "contexts": { + "nodes": [ + { + "__typename": "StatusContext", + "state": "%s" + } + ] + } + } + } }] } } + `, state) +} + +func completedCheckRunNode(conclusion string) string { + return fmt.Sprintf(` + { + "__typename": "CheckRun", + "status": "COMPLETED", + "conclusion": "%s" + }`, conclusion) +} + +func statusContextNode(state string) string { + return fmt.Sprintf(` + { + "__typename": "StatusContext", + "state": "%s" + }`, state) } diff --git a/api/queries_branch_issue_reference.go b/api/queries_branch_issue_reference.go index 5393b484d..16e36831b 100644 --- a/api/queries_branch_issue_reference.go +++ b/api/queries_branch_issue_reference.go @@ -4,63 +4,16 @@ import ( "fmt" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/shurcooL/githubv4" ) type LinkedBranch struct { - ID string BranchName string - RepoUrl string + URL string } -// method to return url of linked branch, adds the branch name to the end of the repo url -func (b *LinkedBranch) Url() string { - return fmt.Sprintf("%s/tree/%s", b.RepoUrl, b.BranchName) -} - -func nameParam(params map[string]interface{}) string { - if params["name"] != "" { - return "name: $name," - } - return "" -} - -func nameArg(params map[string]interface{}) string { - if params["name"] != "" { - return "$name: String, " - } - - return "" -} - -func CreateBranchIssueReference(client *Client, repo *Repository, params map[string]interface{}) (*LinkedBranch, error) { - query := fmt.Sprintf(` - mutation CreateLinkedBranch($issueId: ID!, $oid: GitObjectID!, %[1]s$repositoryId: ID) { - createLinkedBranch(input: { - issueId: $issueId, - %[2]s - oid: $oid, - repositoryId: $repositoryId - }) { - linkedBranch { - id - ref { - name - } - } - } - }`, nameArg(params), nameParam(params)) - - inputParams := map[string]interface{}{ - "repositoryId": repo.ID, - } - for key, val := range params { - switch key { - case "issueId", "name", "oid": - inputParams[key] = val - } - } - - result := struct { +func CreateLinkedBranch(client *Client, host string, repoID, issueID, branchID, branchName string) (string, error) { + var mutation struct { CreateLinkedBranch struct { LinkedBranch struct { ID string @@ -68,91 +21,76 @@ func CreateBranchIssueReference(client *Client, repo *Repository, params map[str Name string } } - } - }{} + } `graphql:"createLinkedBranch(input: $input)"` + } - err := client.GraphQL(repo.RepoHost(), query, inputParams, &result) + input := githubv4.CreateLinkedBranchInput{ + IssueID: githubv4.ID(issueID), + Oid: githubv4.GitObjectID(branchID), + } + if repoID != "" { + repo := githubv4.ID(repoID) + input.RepositoryID = &repo + } + if branchName != "" { + name := githubv4.String(branchName) + input.Name = &name + } + variables := map[string]interface{}{ + "input": input, + } + + err := client.Mutate(host, "CreateLinkedBranch", &mutation, variables) if err != nil { - return nil, err + return "", err } - ref := LinkedBranch{ - ID: result.CreateLinkedBranch.LinkedBranch.ID, - BranchName: result.CreateLinkedBranch.LinkedBranch.Ref.Name, - } - return &ref, nil - + return mutation.CreateLinkedBranch.LinkedBranch.Ref.Name, nil } func ListLinkedBranches(client *Client, repo ghrepo.Interface, issueNumber int) ([]LinkedBranch, error) { - query := ` - query BranchIssueReferenceListLinkedBranches($repositoryName: String!, $repositoryOwner: String!, $issueNumber: Int!) { - repository(name: $repositoryName, owner: $repositoryOwner) { - issue(number: $issueNumber) { - linkedBranches(first: 30) { - edges { - node { - ref { - name - repository { - url - } - } - } - } - } - } - } - } - ` - variables := map[string]interface{}{ - "repositoryName": repo.RepoName(), - "repositoryOwner": repo.RepoOwner(), - "issueNumber": issueNumber, - } - - result := struct { + var query struct { Repository struct { Issue struct { LinkedBranches struct { - Edges []struct { - Node struct { - Ref struct { - Name string - Repository struct { - NameWithOwner string - Url string - } + Nodes []struct { + Ref struct { + Name string + Repository struct { + Url string } } } - } - } - } - }{} - - err := client.GraphQL(repo.RepoHost(), query, variables, &result) - var branchNames []LinkedBranch - if err != nil { - return branchNames, err + } `graphql:"linkedBranches(first: 30)"` + } `graphql:"issue(number: $number)"` + } `graphql:"repository(owner: $owner, name: $name)"` } - for _, edge := range result.Repository.Issue.LinkedBranches.Edges { - branch := LinkedBranch{ - BranchName: edge.Node.Ref.Name, - RepoUrl: edge.Node.Ref.Repository.Url, - } + variables := map[string]interface{}{ + "number": githubv4.Int(issueNumber), + "owner": githubv4.String(repo.RepoOwner()), + "name": githubv4.String(repo.RepoName()), + } + if err := client.Query(repo.RepoHost(), "ListLinkedBranches", &query, variables); err != nil { + return []LinkedBranch{}, err + } + + var branchNames []LinkedBranch + + for _, node := range query.Repository.Issue.LinkedBranches.Nodes { + branch := LinkedBranch{ + BranchName: node.Ref.Name, + URL: fmt.Sprintf("%s/tree/%s", node.Ref.Repository.Url, node.Ref.Name), + } branchNames = append(branchNames, branch) } return branchNames, nil - } -// introspects the schema to see if we expose the LinkedBranch type -func CheckLinkedBranchFeature(client *Client, host string) (err error) { - var featureDetection struct { +func CheckLinkedBranchFeature(client *Client, host string) error { + var query struct { Name struct { Fields []struct { Name string @@ -160,44 +98,21 @@ func CheckLinkedBranchFeature(client *Client, host string) (err error) { } `graphql:"LinkedBranch: __type(name: \"LinkedBranch\")"` } - err = client.Query(host, "LinkedBranch_fields", &featureDetection, nil) - - if err != nil { + if err := client.Query(host, "LinkedBranchFeature", &query, nil); err != nil { return err } - if len(featureDetection.Name.Fields) == 0 { + if len(query.Name.Fields) == 0 { return fmt.Errorf("the `gh issue develop` command is not currently available") } + return nil } -// This fetches the oids for the repo's default branch (`main`, etc) and the name the user might have provided in one shot. -func FindBaseOid(client *Client, repo *Repository, ref string) (string, string, error) { - query := ` - query BranchIssueReferenceFindBaseOid($repositoryName: String!, $repositoryOwner: String!, $ref: String!) { - repository(name: $repositoryName, owner: $repositoryOwner) { - defaultBranchRef { - target { - oid - } - } - ref(qualifiedName: $ref) { - target { - oid - } - } - } - }` - - variables := map[string]interface{}{ - "repositoryName": repo.Name, - "repositoryOwner": repo.RepoOwner(), - "ref": ref, - } - - result := struct { +func FindRepoBranchID(client *Client, repo ghrepo.Interface, ref string) (string, string, error) { + var query struct { Repository struct { + Id string DefaultBranchRef struct { Target struct { Oid string @@ -207,13 +122,24 @@ func FindBaseOid(client *Client, repo *Repository, ref string) (string, string, Target struct { Oid string } - } - } - }{} + } `graphql:"ref(qualifiedName: $ref)"` + } `graphql:"repository(owner: $owner, name: $name)"` + } - err := client.GraphQL(repo.RepoHost(), query, variables, &result) - if err != nil { + variables := map[string]interface{}{ + "ref": githubv4.String(ref), + "owner": githubv4.String(repo.RepoOwner()), + "name": githubv4.String(repo.RepoName()), + } + + if err := client.Query(repo.RepoHost(), "FindRepoBranchID", &query, variables); err != nil { return "", "", err } - return result.Repository.Ref.Target.Oid, result.Repository.DefaultBranchRef.Target.Oid, nil + + branchID := query.Repository.Ref.Target.Oid + if branchID == "" { + branchID = query.Repository.DefaultBranchRef.Target.Oid + } + + return query.Repository.Id, branchID, nil } diff --git a/api/queries_pr.go b/api/queries_pr.go index 87ffd658d..36f3c2eef 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -39,6 +39,8 @@ type PullRequest struct { ClosedAt *time.Time MergedAt *time.Time + AutoMergeRequest *AutoMergeRequest + MergeCommit *Commit PotentialMergeCommit *Commit @@ -95,7 +97,84 @@ type CommitStatusCheckRollup struct { Contexts CheckContexts } +// https://docs.github.com/en/graphql/reference/enums#checkrunstate +type CheckRunState string + +const ( + CheckRunStateActionRequired CheckRunState = "ACTION_REQUIRED" + CheckRunStateCancelled CheckRunState = "CANCELLED" + CheckRunStateCompleted CheckRunState = "COMPLETED" + CheckRunStateFailure CheckRunState = "FAILURE" + CheckRunStateInProgress CheckRunState = "IN_PROGRESS" + CheckRunStateNeutral CheckRunState = "NEUTRAL" + CheckRunStatePending CheckRunState = "PENDING" + CheckRunStateQueued CheckRunState = "QUEUED" + CheckRunStateSkipped CheckRunState = "SKIPPED" + CheckRunStateStale CheckRunState = "STALE" + CheckRunStateStartupFailure CheckRunState = "STARTUP_FAILURE" + CheckRunStateSuccess CheckRunState = "SUCCESS" + CheckRunStateTimedOut CheckRunState = "TIMED_OUT" + CheckRunStateWaiting CheckRunState = "WAITING" +) + +type CheckRunCountByState struct { + State CheckRunState + Count int +} + +// https://docs.github.com/en/graphql/reference/enums#statusstate +type StatusState string + +const ( + StatusStateError StatusState = "ERROR" + StatusStateExpected StatusState = "EXPECTED" + StatusStateFailure StatusState = "FAILURE" + StatusStatePending StatusState = "PENDING" + StatusStateSuccess StatusState = "SUCCESS" +) + +type StatusContextCountByState struct { + State StatusState + Count int +} + +// https://docs.github.com/en/graphql/reference/enums#checkstatusstate +type CheckStatusState string + +const ( + CheckStatusStateCompleted CheckStatusState = "COMPLETED" + CheckStatusStateInProgress CheckStatusState = "IN_PROGRESS" + CheckStatusStatePending CheckStatusState = "PENDING" + CheckStatusStateQueued CheckStatusState = "QUEUED" + CheckStatusStateRequested CheckStatusState = "REQUESTED" + CheckStatusStateWaiting CheckStatusState = "WAITING" +) + +// https://docs.github.com/en/graphql/reference/enums#checkconclusionstate +type CheckConclusionState string + +const ( + CheckConclusionStateActionRequired CheckConclusionState = "ACTION_REQUIRED" + CheckConclusionStateCancelled CheckConclusionState = "CANCELLED" + CheckConclusionStateFailure CheckConclusionState = "FAILURE" + CheckConclusionStateNeutral CheckConclusionState = "NEUTRAL" + CheckConclusionStateSkipped CheckConclusionState = "SKIPPED" + CheckConclusionStateStale CheckConclusionState = "STALE" + CheckConclusionStateStartupFailure CheckConclusionState = "STARTUP_FAILURE" + CheckConclusionStateSuccess CheckConclusionState = "SUCCESS" + CheckConclusionStateTimedOut CheckConclusionState = "TIMED_OUT" +) + type CheckContexts struct { + // These fields are available on newer versions of the GraphQL API + // to support summary counts by state + CheckRunCount int + CheckRunCountsByState []CheckRunCountByState + StatusContextCount int + StatusContextCountsByState []StatusContextCountByState + + // These are available on older versions and provide more details + // required for checks Nodes []CheckContext PageInfo struct { HasNextPage bool @@ -104,31 +183,38 @@ type CheckContexts struct { } type CheckContext struct { - TypeName string `json:"__typename"` - Name string `json:"name"` - IsRequired bool `json:"isRequired"` - CheckSuite struct { - WorkflowRun struct { - Workflow struct { - Name string `json:"name"` - } `json:"workflow"` - } `json:"workflowRun"` - } `json:"checkSuite"` + TypeName string `json:"__typename"` + Name string `json:"name"` + IsRequired bool `json:"isRequired"` + CheckSuite CheckSuite `json:"checkSuite"` // QUEUED IN_PROGRESS COMPLETED WAITING PENDING REQUESTED Status string `json:"status"` // ACTION_REQUIRED TIMED_OUT CANCELLED FAILURE SUCCESS NEUTRAL SKIPPED STARTUP_FAILURE STALE - Conclusion string `json:"conclusion"` - StartedAt time.Time `json:"startedAt"` - CompletedAt time.Time `json:"completedAt"` - DetailsURL string `json:"detailsUrl"` + Conclusion CheckConclusionState `json:"conclusion"` + StartedAt time.Time `json:"startedAt"` + CompletedAt time.Time `json:"completedAt"` + DetailsURL string `json:"detailsUrl"` /* StatusContext fields */ - - Context string `json:"context"` + Context string `json:"context"` + Description string `json:"description"` // EXPECTED ERROR FAILURE PENDING SUCCESS - State string `json:"state"` - TargetURL string `json:"targetUrl"` - CreatedAt time.Time `json:"createdAt"` + State StatusState `json:"state"` + TargetURL string `json:"targetUrl"` + CreatedAt time.Time `json:"createdAt"` +} + +type CheckSuite struct { + WorkflowRun WorkflowRun `json:"workflowRun"` +} + +type WorkflowRun struct { + Event string `json:"event"` + Workflow Workflow `json:"workflow"` +} + +type Workflow struct { + Name string `json:"name"` } type PRRepository struct { @@ -136,6 +222,16 @@ type PRRepository struct { Name string `json:"name"` } +type AutoMergeRequest struct { + AuthorEmail *string `json:"authorEmail"` + CommitBody *string `json:"commitBody"` + CommitHeadline *string `json:"commitHeadline"` + // MERGE, REBASE, SQUASH + MergeMethod string `json:"mergeMethod"` + EnabledAt time.Time `json:"enabledAt"` + EnabledBy Author `json:"enabledBy"` +} + // Commit loads just the commit SHA and nothing else type Commit struct { OID string `json:"oid"` @@ -249,33 +345,136 @@ type PullRequestChecksStatus struct { Total int } -func (pr *PullRequest) ChecksStatus() (summary PullRequestChecksStatus) { +func (pr *PullRequest) ChecksStatus() PullRequestChecksStatus { + var summary PullRequestChecksStatus + if len(pr.StatusCheckRollup.Nodes) == 0 { - return + return summary } - commit := pr.StatusCheckRollup.Nodes[0].Commit - for _, c := range commit.StatusCheckRollup.Contexts.Nodes { - state := c.State // StatusContext - if state == "" { - // CheckRun - if c.Status == "COMPLETED" { - state = c.Conclusion - } else { - state = c.Status + + contexts := pr.StatusCheckRollup.Nodes[0].Commit.StatusCheckRollup.Contexts + + // If this commit has counts by state then we can summarise check status from those + if len(contexts.CheckRunCountsByState) != 0 && len(contexts.StatusContextCountsByState) != 0 { + summary.Total = contexts.CheckRunCount + contexts.StatusContextCount + for _, countByState := range contexts.CheckRunCountsByState { + switch parseCheckStatusFromCheckRunState(countByState.State) { + case passing: + summary.Passing += countByState.Count + case failing: + summary.Failing += countByState.Count + default: + summary.Pending += countByState.Count } } - switch state { - case "SUCCESS", "NEUTRAL", "SKIPPED": - summary.Passing++ - case "ERROR", "FAILURE", "CANCELLED", "TIMED_OUT", "ACTION_REQUIRED": - summary.Failing++ - default: // "EXPECTED", "REQUESTED", "WAITING", "QUEUED", "PENDING", "IN_PROGRESS", "STALE" - summary.Pending++ + + for _, countByState := range contexts.StatusContextCountsByState { + switch parseCheckStatusFromStatusState(countByState.State) { + case passing: + summary.Passing += countByState.Count + case failing: + summary.Failing += countByState.Count + default: + summary.Pending += countByState.Count + } + } + + return summary + } + + // If we don't have the counts by state, then we'll need to summarise by looking at the more detailed contexts + for _, c := range contexts.Nodes { + // Nodes are a discriminated union of CheckRun or StatusContext and we can match on + // the TypeName to narrow the type. + if c.TypeName == "CheckRun" { + // https://docs.github.com/en/graphql/reference/enums#checkstatusstate + // If the status is completed then we can check the conclusion field + if c.Status == "COMPLETED" { + switch parseCheckStatusFromCheckConclusionState(c.Conclusion) { + case passing: + summary.Passing++ + case failing: + summary.Failing++ + default: + summary.Pending++ + } + // otherwise we're in some form of pending state: + // "COMPLETED", "IN_PROGRESS", "PENDING", "QUEUED", "REQUESTED", "WAITING" or otherwise unknown + } else { + summary.Pending++ + } + + } else { // c.TypeName == StatusContext + switch parseCheckStatusFromStatusState(c.State) { + case passing: + summary.Passing++ + case failing: + summary.Failing++ + default: + summary.Pending++ + } } summary.Total++ } - return + return summary +} + +type checkStatus int + +const ( + passing checkStatus = iota + failing + pending +) + +func parseCheckStatusFromStatusState(state StatusState) checkStatus { + switch state { + case StatusStateSuccess: + return passing + case StatusStateFailure, StatusStateError: + return failing + case StatusStateExpected, StatusStatePending: + return pending + // Currently, we treat anything unknown as pending, which includes any future unknown + // states we might get back from the API. It might be interesting to do some work to add an additional + // unknown state. + default: + return pending + } +} + +func parseCheckStatusFromCheckRunState(state CheckRunState) checkStatus { + switch state { + case CheckRunStateNeutral, CheckRunStateSkipped, CheckRunStateSuccess: + return passing + case CheckRunStateActionRequired, CheckRunStateCancelled, CheckRunStateFailure, CheckRunStateTimedOut: + return failing + case CheckRunStateCompleted, CheckRunStateInProgress, CheckRunStatePending, CheckRunStateQueued, + CheckRunStateStale, CheckRunStateStartupFailure, CheckRunStateWaiting: + return pending + // Currently, we treat anything unknown as pending, which includes any future unknown + // states we might get back from the API. It might be interesting to do some work to add an additional + // unknown state. + default: + return pending + } +} + +func parseCheckStatusFromCheckConclusionState(state CheckConclusionState) checkStatus { + switch state { + case CheckConclusionStateNeutral, CheckConclusionStateSkipped, CheckConclusionStateSuccess: + return passing + case CheckConclusionStateActionRequired, CheckConclusionStateCancelled, CheckConclusionStateFailure, CheckConclusionStateTimedOut: + return failing + case CheckConclusionStateStale, CheckConclusionStateStartupFailure: + return pending + // Currently, we treat anything unknown as pending, which includes any future unknown + // states we might get back from the API. It might be interesting to do some work to add an additional + // unknown state. + default: + return pending + } } func (pr *PullRequest) DisplayableReviews() PullRequestReviews { diff --git a/api/queries_projects_v2.go b/api/queries_projects_v2.go index d706ae45e..a602653af 100644 --- a/api/queries_projects_v2.go +++ b/api/queries_projects_v2.go @@ -255,7 +255,7 @@ func CurrentUserProjectsV2(client *Client, hostname string) ([]ProjectV2, error) // When querying ProjectsV2 fields we generally dont want to show the user // scope errors and field does not exist errors. ProjectsV2IgnorableError // checks against known error strings to see if an error can be safely ignored. -// Due to the fact that the GQLClient can return multiple types of errors +// Due to the fact that the GraphQLClient can return multiple types of errors // this uses brittle string comparison to check against the known error strings. func ProjectsV2IgnorableError(err error) bool { msg := err.Error() diff --git a/api/queries_repo.go b/api/queries_repo.go index 3b7e2e773..ba94edef1 100644 --- a/api/queries_repo.go +++ b/api/queries_repo.go @@ -16,8 +16,7 @@ import ( "golang.org/x/sync/errgroup" "github.com/cli/cli/v2/internal/ghrepo" - "github.com/cli/go-gh/pkg/api" - ghAPI "github.com/cli/go-gh/pkg/api" + ghAPI "github.com/cli/go-gh/v2/pkg/api" "github.com/shurcooL/githubv4" ) @@ -264,8 +263,8 @@ func FetchRepository(client *Client, repo ghrepo.Interface, fields []string) (*R // guaranteed to happen when an authentication token with insufficient permissions is being used. if result.Repository == nil { return nil, GraphQLError{ - GQLError: ghAPI.GQLError{ - Errors: []ghAPI.GQLErrorItem{{ + GraphQLError: &ghAPI.GraphQLError{ + Errors: []ghAPI.GraphQLErrorItem{{ Type: "NOT_FOUND", Message: fmt.Sprintf("Could not resolve to a Repository with the name '%s/%s'.", repo.RepoOwner(), repo.RepoName()), }}, @@ -317,8 +316,8 @@ func GitHubRepo(client *Client, repo ghrepo.Interface) (*Repository, error) { // guaranteed to happen when an authentication token with insufficient permissions is being used. if result.Repository == nil { return nil, GraphQLError{ - GQLError: ghAPI.GQLError{ - Errors: []ghAPI.GQLErrorItem{{ + GraphQLError: &ghAPI.GraphQLError{ + Errors: []ghAPI.GraphQLErrorItem{{ Type: "NOT_FOUND", Message: fmt.Sprintf("Could not resolve to a Repository with the name '%s/%s'.", repo.RepoOwner(), repo.RepoName()), }}, @@ -1372,7 +1371,6 @@ func GetRepoIDs(client *Client, host string, repositories []ghrepo.Interface) ([ return result, nil } -// RenameRepo renames the repository on GitHub and returns the renamed repository func RepoExists(client *Client, repo ghrepo.Interface) (bool, error) { path := fmt.Sprintf("%srepos/%s/%s", ghinstance.RESTPrefix(repo.RepoHost()), repo.RepoOwner(), repo.RepoName()) @@ -1387,6 +1385,6 @@ func RepoExists(client *Client, repo ghrepo.Interface) (bool, error) { case 404: return false, nil default: - return false, api.HandleHTTPError(resp) + return false, ghAPI.HandleHTTPError(resp) } } diff --git a/api/query_builder.go b/api/query_builder.go index 3aa7464c4..b74f9519b 100644 --- a/api/query_builder.go +++ b/api/query_builder.go @@ -132,7 +132,42 @@ var prCommits = shortenQuery(` } `) -func StatusCheckRollupGraphQL(after string) string { +var autoMergeRequest = shortenQuery(` + autoMergeRequest { + authorEmail, + commitBody, + commitHeadline, + mergeMethod, + enabledAt, + enabledBy{login,...on User{id,name}} + } +`) + +func StatusCheckRollupGraphQLWithCountByState() string { + return shortenQuery(` + statusCheckRollup: commits(last: 1) { + nodes { + commit { + statusCheckRollup { + contexts { + checkRunCount, + checkRunCountsByState { + state, + count + }, + statusContextCount, + statusContextCountsByState { + state, + count + } + } + } + } + } + }`) +} + +func StatusCheckRollupGraphQLWithoutCountByState(after string) string { var afterClause string if after != "" { afterClause = ",after:" + after @@ -149,7 +184,8 @@ func StatusCheckRollupGraphQL(after string) string { context, state, targetUrl, - createdAt + createdAt, + description }, ...on CheckRun { name, @@ -187,11 +223,12 @@ func RequiredStatusCheckRollupGraphQL(prID, after string) string { state, targetUrl, createdAt, + description, isRequired(pullRequestId: %[2]s) }, ...on CheckRun { name, - checkSuite{workflowRun{workflow{name}}}, + checkSuite{workflowRun{event,workflow{name}}}, status, conclusion, startedAt, @@ -231,6 +268,7 @@ var IssueFields = []string{ var PullRequestFields = append(IssueFields, "additions", + "autoMergeRequest", "baseRefName", "changedFiles", "commits", @@ -285,6 +323,8 @@ func IssueGraphQL(fields []string) string { q = append(q, `mergeCommit{oid}`) case "potentialMergeCommit": q = append(q, `potentialMergeCommit{oid}`) + case "autoMergeRequest": + q = append(q, autoMergeRequest) case "comments": q = append(q, issueComments) case "lastComment": // pseudo-field @@ -306,7 +346,9 @@ func IssueGraphQL(fields []string) string { case "requiresStrictStatusChecks": // pseudo-field q = append(q, `baseRef{branchProtectionRule{requiresStrictStatusChecks}}`) case "statusCheckRollup": - q = append(q, StatusCheckRollupGraphQL("")) + q = append(q, StatusCheckRollupGraphQLWithoutCountByState("")) + case "statusCheckRollupWithCountByState": // pseudo-field + q = append(q, StatusCheckRollupGraphQLWithCountByState()) default: q = append(q, field) } @@ -372,6 +414,7 @@ var RepositoryFields = []string{ "isInOrganization", "isMirror", "isPrivate", + "visibility", "isTemplate", "isUserConfigurationRepository", "licenseInfo", diff --git a/api/sanitize_ascii.go b/api/sanitize_ascii.go index aeedaccbf..75cb3062b 100644 --- a/api/sanitize_ascii.go +++ b/api/sanitize_ascii.go @@ -47,9 +47,9 @@ type sanitizer struct { addEscape bool } -// Transform uses a sliding window alogorithm to detect C0 and C1 +// Transform uses a sliding window algorithm to detect C0 and C1 // ASCII control sequences as they are read and replaces them -// with equivelent inert characters. Characters that are not part +// with equivalent inert characters. Characters that are not part // of a control sequence are not modified. func (t *sanitizer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { lSrc := len(src) diff --git a/build/windows/gh.wixproj b/build/windows/gh.wixproj index aa72d4da3..6c1e971d0 100644 --- a/build/windows/gh.wixproj +++ b/build/windows/gh.wixproj @@ -30,9 +30,5 @@ - - - - diff --git a/cmd/gen-docs/main.go b/cmd/gen-docs/main.go index 7c5078df9..aa5c013ff 100644 --- a/cmd/gen-docs/main.go +++ b/cmd/gen-docs/main.go @@ -2,13 +2,17 @@ package main import ( "fmt" + "io" "os" "path/filepath" "strings" + "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/docs" + "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmd/root" "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/extensions" "github.com/cli/cli/v2/pkg/iostreams" "github.com/spf13/pflag" ) @@ -41,9 +45,13 @@ func run(args []string) error { } ios, _, _, _ := iostreams.Test() - rootCmd := root.NewCmdRoot(&cmdutil.Factory{ + rootCmd, _ := root.NewCmdRoot(&cmdutil.Factory{ IOStreams: ios, Browser: &browser{}, + Config: func() (config.Config, error) { + return config.NewFromString(""), nil + }, + ExtensionManager: &em{}, }, "", "") rootCmd.InitDefaultHelpCmd() @@ -79,8 +87,42 @@ func linkHandler(name string) string { return fmt.Sprintf("./%s", strings.TrimSuffix(name, ".md")) } +// Implements browser.Browser interface. type browser struct{} -func (b *browser) Browse(url string) error { +func (b *browser) Browse(_ string) error { return nil } + +// Implements extensions.ExtensionManager interface. +type em struct{} + +func (e *em) List() []extensions.Extension { + return nil +} + +func (e *em) Install(_ ghrepo.Interface, _ string) error { + return nil +} + +func (e *em) InstallLocal(_ string) error { + return nil +} + +func (e *em) Upgrade(_ string, _ bool) error { + return nil +} + +func (e *em) Remove(_ string) error { + return nil +} + +func (e *em) Dispatch(_ []string, _ io.Reader, _, _ io.Writer) (bool, error) { + return false, nil +} + +func (e *em) Create(_ string, _ extensions.ExtTemplateType) error { + return nil +} + +func (e *em) EnableDryRunMode() {} diff --git a/cmd/gh/main.go b/cmd/gh/main.go index eb0ad0672..15af90721 100644 --- a/cmd/gh/main.go +++ b/cmd/gh/main.go @@ -14,16 +14,10 @@ import ( surveyCore "github.com/AlecAivazis/survey/v2/core" "github.com/AlecAivazis/survey/v2/terminal" - "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" - "github.com/cli/cli/v2/git" "github.com/cli/cli/v2/internal/build" "github.com/cli/cli/v2/internal/config" - "github.com/cli/cli/v2/internal/ghrepo" - "github.com/cli/cli/v2/internal/run" - "github.com/cli/cli/v2/internal/text" "github.com/cli/cli/v2/internal/update" - "github.com/cli/cli/v2/pkg/cmd/alias/expand" "github.com/cli/cli/v2/pkg/cmd/factory" "github.com/cli/cli/v2/pkg/cmd/root" "github.com/cli/cli/v2/pkg/cmdutil" @@ -80,9 +74,6 @@ func mainRun() exitCode { surveyCore.TemplateFuncsWithColor["color"] = func(style string) string { switch style { case "white": - if cmdFactory.IOStreams.ColorSupport256() { - return fmt.Sprintf("\x1b[%d;5;%dm", 38, 242) - } return ansi.ColorCode("default") default: return ansi.ColorCode(style) @@ -96,11 +87,9 @@ func mainRun() exitCode { cobra.MousetrapHelpText = "" } - rootCmd := root.NewCmdRoot(cmdFactory, buildVersion, buildDate) - - cfg, err := cmdFactory.Config() + rootCmd, err := root.NewCmdRoot(cmdFactory, buildVersion, buildDate) if err != nil { - fmt.Fprintf(stderr, "failed to read configuration: %s\n", err) + fmt.Fprintf(stderr, "failed to create root command: %s\n", err) return exitError } @@ -109,110 +98,10 @@ func mainRun() exitCode { expandedArgs = os.Args[1:] } - // translate `gh help ` to `gh --help` for extensions - if len(expandedArgs) == 2 && expandedArgs[0] == "help" && !hasCommand(rootCmd, expandedArgs[1:]) { - expandedArgs = []string{expandedArgs[1], "--help"} - } - - if !hasCommand(rootCmd, expandedArgs) { - originalArgs := expandedArgs - isShell := false - - argsForExpansion := append([]string{"gh"}, expandedArgs...) - expandedArgs, isShell, err = expand.ExpandAlias(cfg, argsForExpansion, nil) - if err != nil { - fmt.Fprintf(stderr, "failed to process aliases: %s\n", err) - return exitError - } - - if hasDebug { - fmt.Fprintf(stderr, "%v -> %v\n", originalArgs, expandedArgs) - } - - if isShell { - exe, err := safeexec.LookPath(expandedArgs[0]) - if err != nil { - fmt.Fprintf(stderr, "failed to run external command: %s", err) - return exitError - } - - externalCmd := exec.Command(exe, expandedArgs[1:]...) - externalCmd.Stderr = os.Stderr - externalCmd.Stdout = os.Stdout - externalCmd.Stdin = os.Stdin - preparedCmd := run.PrepareCmd(externalCmd) - - err = preparedCmd.Run() - if err != nil { - var execError *exec.ExitError - if errors.As(err, &execError) { - return exitCode(execError.ExitCode()) - } - fmt.Fprintf(stderr, "failed to run external command: %s\n", err) - return exitError - } - - return exitOK - } else if len(expandedArgs) > 0 && !hasCommand(rootCmd, expandedArgs) { - extensionManager := cmdFactory.ExtensionManager - if found, err := extensionManager.Dispatch(expandedArgs, os.Stdin, os.Stdout, os.Stderr); err != nil { - var execError *exec.ExitError - if errors.As(err, &execError) { - return exitCode(execError.ExitCode()) - } - fmt.Fprintf(stderr, "failed to run extension: %s\n", err) - return exitError - } else if found { - return exitOK - } - } - } - - // provide completions for aliases and extensions - rootCmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - var results []string - aliases := cfg.Aliases() - for aliasName, aliasValue := range aliases.All() { - if strings.HasPrefix(aliasName, toComplete) { - var s string - if strings.HasPrefix(aliasValue, "!") { - s = fmt.Sprintf("%s\tShell alias", aliasName) - } else { - aliasValue = text.Truncate(80, aliasValue) - s = fmt.Sprintf("%s\tAlias for %s", aliasName, aliasValue) - } - results = append(results, s) - } - } - for _, ext := range cmdFactory.ExtensionManager.List() { - if strings.HasPrefix(ext.Name(), toComplete) { - var s string - if ext.IsLocal() { - s = fmt.Sprintf("%s\tLocal extension gh-%s", ext.Name(), ext.Name()) - } else { - path := ext.URL() - if u, err := git.ParseURL(ext.URL()); err == nil { - if r, err := ghrepo.FromURL(u); err == nil { - path = ghrepo.FullName(r) - } - } - s = fmt.Sprintf("%s\tExtension %s", ext.Name(), path) - } - results = append(results, s) - } - } - return results, cobra.ShellCompDirectiveNoFileComp - } - - authError := errors.New("authError") - rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { - // require that the user is authenticated before running most commands - if cmdutil.IsAuthCheckEnabled(cmd) && !cmdutil.CheckAuth(cfg) { - fmt.Fprint(stderr, authHelp()) - return authError - } - - return nil + // translate `gh help ` to `gh --help` for extensions. + if len(expandedArgs) >= 2 && expandedArgs[0] == "help" && isExtensionCommand(rootCmd, expandedArgs[1:]) { + expandedArgs = expandedArgs[1:] + expandedArgs = append(expandedArgs, "--help") } rootCmd.SetArgs(expandedArgs) @@ -220,6 +109,8 @@ func mainRun() exitCode { if cmd, err := rootCmd.ExecuteContextC(ctx); err != nil { var pagerPipeError *iostreams.ErrClosedPagerPipe var noResultsError cmdutil.NoResultsError + var extError *root.ExternalCommandExitError + var authError *root.AuthError if err == cmdutil.SilentError { return exitError } else if cmdutil.IsUserCancellation(err) { @@ -228,7 +119,7 @@ func mainRun() exitCode { fmt.Fprint(stderr, "\n") } return exitCancel - } else if errors.Is(err, authError) { + } else if errors.As(err, &authError) { return exitAuth } else if errors.As(err, &pagerPipeError) { // ignore the error raised when piping to a closed pager @@ -239,6 +130,9 @@ func mainRun() exitCode { } // no results is not a command failure return exitOK + } else if errors.As(err, &extError) { + // pass on exit codes from extensions and shell aliases + return exitCode(extError.ExitCode()) } printError(stderr, err, cmd, hasDebug) @@ -287,10 +181,10 @@ func mainRun() exitCode { return exitOK } -// hasCommand returns true if args resolve to a built-in command -func hasCommand(rootCmd *cobra.Command, args []string) bool { - c, _, err := rootCmd.Traverse(args) - return err == nil && c != rootCmd +// isExtensionCommand returns true if args resolve to an extension command. +func isExtensionCommand(rootCmd *cobra.Command, args []string) bool { + c, _, err := rootCmd.Find(args) + return err == nil && c != nil && c.GroupID == "extension" } func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) { @@ -315,27 +209,6 @@ func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) { } } -func authHelp() string { - if os.Getenv("GITHUB_ACTIONS") == "true" { - return heredoc.Doc(` - gh: To use GitHub CLI in a GitHub Actions workflow, set the GH_TOKEN environment variable. Example: - env: - GH_TOKEN: ${{ github.token }} - `) - } - - if os.Getenv("CI") != "" { - return heredoc.Doc(` - gh: To use GitHub CLI in automation, set the GH_TOKEN environment variable. - `) - } - - return heredoc.Doc(` - To get started with GitHub CLI, please run: gh auth login - Alternatively, populate the GH_TOKEN environment variable with a GitHub API authentication token. - `) -} - func shouldCheckForUpdate() bool { if os.Getenv("GH_NO_UPDATE_NOTIFIER") != "" { return false diff --git a/docs/releasing.md b/docs/releasing.md index e762d845e..d9b01ea41 100644 --- a/docs/releasing.md +++ b/docs/releasing.md @@ -1,36 +1,40 @@ # Releasing -Our build system automatically compiles and attaches cross-platform binaries to any git tag named `vX.Y.Z`. The changelog is [generated from git commit log](https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes). +To initiate a new production deployment: +```sh +script/release vX.Y.Z +``` +See `script/release --help` for more information. -Users who run official builds of `gh` on their machines will get notified about the new version within a 24 hour period. +> **Note:** +> Every production release will request an approval by the select few people before it can proceed. -To test out the build system, publish a prerelease tag with a name such as `vX.Y.Z-pre.0` or `vX.Y.Z-rc.1`. Note that such a release will still be public, but it will be marked as a "prerelease", meaning that it will never show up as a "latest" release nor trigger upgrade notifications for users. +What this does is: +- Builds Linux binaries on Ubuntu; +- Builds and signs Windows binaries on Windows; +- Builds, signs, and notarizes macOS binaries on macOS; +- Uploads all release artifacts to a new GitHub Release; +- A new git tag `vX.Y.Z` is created in the remote repository; +- The changelog is [generated from the list of merged pull requests](https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes); +- Updates cli.github.com with the contents of the new release; +- Updates our Homebrew formula in the homebrew-core repo. + +To test out the build system while avoiding creating an actual release: +```sh +script/release --staging vX.Y.Z --branch patch-1 -p macos +``` +The build artifacts will be available via `gh run download -n macos`. ## General guidelines * Features to be released should be reviewed and approved at least one day prior to the release. * Feature releases should bump up the minor version number. +* Breaking releases should bump up the major version number. These should generally be rare. -## Tagging a new release - -1. `git tag v1.2.3 && git push origin v1.2.3` -2. Wait several minutes for builds to run: -3. Verify release is displayed and has correct assets: -4. Scan generated release notes and optionally add a human touch by grouping items under topic sections -5. Verify the marketing site was updated: -6. (Optional) Delete any pre-releases related to this release - -A successful build will result in changes across several repositories: -* -* -* - -If the build fails, there is not a clean way to re-run it. The easiest way would be to start over by deleting the partial release on GitHub and re-publishing the tag. Note that this might be disruptive to users or tooling that were already notified about an upgrade. If a functional release and its binaries are already out there, it might be better to try to manually fix up only the specific workflow tasks that failed. Use your best judgement depending on the failure type. - -## Release locally for debugging +## Test the build system locally A local release can be created for testing without creating anything official on the release page. 1. Make sure GoReleaser is installed: `brew install goreleaser` -2. `goreleaser --skip-validate --skip-publish --rm-dist` +2. `script/release --local` 3. Find the built products under `dist/`. diff --git a/docs/triage.md b/docs/triage.md index 8a329de7c..da69c892e 100644 --- a/docs/triage.md +++ b/docs/triage.md @@ -16,7 +16,10 @@ To be considered triaged, `enhancement` issues require at least one of the follo - `needs-investigation`: work that requires a mystery be solved by the core team before it can move forward - `needs-user-input`: we need more information from our users before the task can move forward -To be considered triaged, `bug` issues require a severity label: one of `p1`, `p2`, or `p3` +To be considered triaged, `bug` issues require a severity label: one of `p1`, `p2`, or `p3`, which are defined as follows: + - `p1`: Affects a large population and inhibits work + - `p2`: Affects more than a few users but doesn't prevent core functions + - `p3`: Affects a small number of users or is largely cosmetic ## Expectations for community pull requests diff --git a/git/client.go b/git/client.go index a7d59899e..a029c8ba6 100644 --- a/git/client.go +++ b/git/client.go @@ -36,6 +36,19 @@ type Client struct { mu sync.Mutex } +func (c *Client) Copy() *Client { + return &Client{ + GhPath: c.GhPath, + RepoDir: c.RepoDir, + GitPath: c.GitPath, + Stderr: c.Stderr, + Stdin: c.Stdin, + Stdout: c.Stdout, + + commandContext: c.commandContext, + } +} + func (c *Client) Command(ctx context.Context, args ...string) (*Command, error) { if c.RepoDir != "" { args = append([]string{"-C", c.RepoDir}, args...) @@ -408,6 +421,44 @@ func (c *Client) revParse(ctx context.Context, args ...string) ([]byte, error) { return cmd.Output() } +func (c *Client) IsLocalGitRepo(ctx context.Context) (bool, error) { + _, err := c.GitDir(ctx) + if err != nil { + var execError errWithExitCode + if errors.As(err, &execError) && execError.ExitCode() == 128 { + return false, nil + } + return false, err + } + return true, nil +} + +func (c *Client) UnsetRemoteResolution(ctx context.Context, name string) error { + args := []string{"config", "--unset", fmt.Sprintf("remote.%s.gh-resolved", name)} + cmd, err := c.Command(ctx, args...) + if err != nil { + return err + } + _, err = cmd.Output() + if err != nil { + return err + } + return nil +} + +func (c *Client) SetRemoteBranches(ctx context.Context, remote string, refspec string) error { + args := []string{"remote", "set-branches", remote, refspec} + cmd, err := c.Command(ctx, args...) + if err != nil { + return err + } + _, err = cmd.Output() + if err != nil { + return err + } + return nil +} + // Below are commands that make network calls and need authentication credentials supplied from gh. func (c *Client) Fetch(ctx context.Context, remote string, refspec string, mods ...CommandModifier) error { @@ -513,31 +564,6 @@ func (c *Client) AddRemote(ctx context.Context, name, urlStr string, trackingBra return remote, nil } -func (c *Client) IsLocalGitRepo(ctx context.Context) (bool, error) { - _, err := c.GitDir(ctx) - if err != nil { - var execError errWithExitCode - if errors.As(err, &execError) && execError.ExitCode() == 128 { - return false, nil - } - return false, err - } - return true, nil -} - -func (c *Client) UnsetRemoteResolution(ctx context.Context, name string) error { - args := []string{"config", "--unset", fmt.Sprintf("remote.%s.gh-resolved", name)} - cmd, err := c.Command(ctx, args...) - if err != nil { - return err - } - _, err = cmd.Output() - if err != nil { - return err - } - return nil -} - func resolveGitPath() (string, error) { path, err := safeexec.LookPath("git") if err != nil { diff --git a/git/client_test.go b/git/client_test.go index 68cfc2739..5e96bb1b7 100644 --- a/git/client_test.go +++ b/git/client_test.go @@ -303,7 +303,7 @@ func TestClientCurrentBranch(t *testing.T) { wantBranch: "branch\u00A0with\u00A0non\u00A0breaking\u00A0space", }, { - name: "detatched head", + name: "detached head", cmdExitStatus: 1, wantCmdArgs: `path/to/git symbolic-ref --quiet HEAD`, wantErrorMsg: "failed to run git: not on any branch", @@ -339,7 +339,7 @@ func TestClientShowRefs(t *testing.T) { wantErrorMsg string }{ { - name: "show refs with one vaid ref and one invalid ref", + name: "show refs with one valid ref and one invalid ref", cmdExitStatus: 128, cmdStdout: "9ea76237a557015e73446d33268569a114c0649c refs/heads/valid", cmdStderr: "fatal: 'refs/heads/invalid' - not a valid ref", @@ -834,6 +834,84 @@ func TestClientPathFromRoot(t *testing.T) { } } +func TestClientUnsetRemoteResolution(t *testing.T) { + tests := []struct { + name string + cmdExitStatus int + cmdStdout string + cmdStderr string + wantCmdArgs string + wantErrorMsg string + }{ + { + name: "unset remote resolution", + wantCmdArgs: `path/to/git config --unset remote.origin.gh-resolved`, + }, + { + name: "git error", + cmdExitStatus: 1, + cmdStderr: "git error message", + wantCmdArgs: `path/to/git config --unset remote.origin.gh-resolved`, + wantErrorMsg: "failed to run git: git error message", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd, cmdCtx := createCommandContext(t, tt.cmdExitStatus, tt.cmdStdout, tt.cmdStderr) + client := Client{ + GitPath: "path/to/git", + commandContext: cmdCtx, + } + err := client.UnsetRemoteResolution(context.Background(), "origin") + assert.Equal(t, tt.wantCmdArgs, strings.Join(cmd.Args[3:], " ")) + if tt.wantErrorMsg == "" { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, tt.wantErrorMsg) + } + }) + } +} + +func TestClientSetRemoteBranches(t *testing.T) { + tests := []struct { + name string + cmdExitStatus int + cmdStdout string + cmdStderr string + wantCmdArgs string + wantErrorMsg string + }{ + { + name: "set remote branches", + wantCmdArgs: `path/to/git remote set-branches origin trunk`, + }, + { + name: "git error", + cmdExitStatus: 1, + cmdStderr: "git error message", + wantCmdArgs: `path/to/git remote set-branches origin trunk`, + wantErrorMsg: "failed to run git: git error message", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd, cmdCtx := createCommandContext(t, tt.cmdExitStatus, tt.cmdStdout, tt.cmdStderr) + client := Client{ + GitPath: "path/to/git", + commandContext: cmdCtx, + } + err := client.SetRemoteBranches(context.Background(), "origin", "trunk") + assert.Equal(t, tt.wantCmdArgs, strings.Join(cmd.Args[3:], " ")) + if tt.wantErrorMsg == "" { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, tt.wantErrorMsg) + } + }) + } +} + func TestClientFetch(t *testing.T) { tests := []struct { name string diff --git a/go.mod b/go.mod index be436c04b..2c31b40a9 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,13 @@ module github.com/cli/cli/v2 go 1.19 require ( - github.com/AlecAivazis/survey/v2 v2.3.6 + github.com/AlecAivazis/survey/v2 v2.3.7 github.com/MakeNowJust/heredoc v1.0.0 github.com/briandowns/spinner v1.18.1 - github.com/cenkalti/backoff/v4 v4.2.0 + github.com/cenkalti/backoff/v4 v4.2.1 github.com/charmbracelet/glamour v0.5.1-0.20220727184942-e70ff2d969da github.com/charmbracelet/lipgloss v0.5.0 - github.com/cli/go-gh v1.2.1 + github.com/cli/go-gh/v2 v2.0.1 github.com/cli/oauth v1.0.1 github.com/cli/safeexec v1.0.1 github.com/cpuguy83/go-md2man/v2 v2.0.2 @@ -21,27 +21,28 @@ require ( github.com/gorilla/websocket v1.4.2 github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-version v1.3.0 - github.com/henvic/httpretty v0.0.6 + github.com/henvic/httpretty v0.1.2 github.com/joho/godotenv v1.5.1 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/mattn/go-colorable v0.1.13 - github.com/mattn/go-isatty v0.0.18 + github.com/mattn/go-isatty v0.0.19 github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d github.com/muhammadmuzzammil1998/jsonc v0.0.0-20201229145248-615b0916ca38 github.com/opentracing/opentracing-go v1.1.0 github.com/rivo/tview v0.0.0-20221029100920-c4a7e501810d - github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00 + github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9 github.com/sourcegraph/jsonrpc2 v0.1.0 github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 - github.com/stretchr/testify v1.7.5 - github.com/zalando/go-keyring v0.2.2 + github.com/stretchr/testify v1.8.4 + github.com/zalando/go-keyring v0.2.3 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 golang.org/x/sync v0.1.0 - golang.org/x/term v0.6.0 - golang.org/x/text v0.8.0 - google.golang.org/grpc v1.49.0 - google.golang.org/protobuf v1.27.1 + golang.org/x/term v0.7.0 + golang.org/x/text v0.9.0 + google.golang.org/grpc v1.53.0 + google.golang.org/protobuf v1.28.1 + gopkg.in/h2non/gock.v1 v1.1.2 gopkg.in/yaml.v3 v3.0.1 ) @@ -49,9 +50,9 @@ require ( github.com/alecthomas/chroma v0.10.0 // indirect github.com/alessio/shellescape v1.4.1 // indirect github.com/aymerick/douceur v0.2.0 // indirect - github.com/cli/browser v1.1.0 // indirect - github.com/cli/shurcooL-graphql v0.0.2 // indirect - github.com/danieljoos/wincred v1.1.2 // indirect + github.com/cli/browser v1.2.0 // indirect + github.com/cli/shurcooL-graphql v0.0.3 // indirect + github.com/danieljoos/wincred v1.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dlclark/regexp2 v1.4.0 // indirect github.com/fatih/color v1.7.0 // indirect @@ -59,10 +60,12 @@ require ( github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/gorilla/css v1.0.0 // indirect + github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect - github.com/itchyny/gojq v0.12.8 // indirect - github.com/itchyny/timefmt-go v0.1.3 // indirect + github.com/itchyny/gojq v0.12.13 // indirect + github.com/itchyny/timefmt-go v0.1.5 // indirect + github.com/kr/text v0.2.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect github.com/microcosm-cc/bluemonday v1.0.20 // indirect @@ -70,20 +73,17 @@ require ( github.com/muesli/termenv v0.12.0 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rivo/uniseg v0.4.2 // indirect + github.com/rivo/uniseg v0.4.4 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect - github.com/stretchr/objx v0.4.0 // indirect + github.com/stretchr/objx v0.5.0 // indirect github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e // indirect github.com/yuin/goldmark v1.4.13 // indirect github.com/yuin/goldmark-emoji v1.0.1 // indirect - golang.org/x/net v0.8.0 // indirect - golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a // indirect - golang.org/x/sys v0.6.0 // indirect - google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect + golang.org/x/net v0.9.0 // indirect + golang.org/x/sys v0.8.0 // indirect + google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) replace golang.org/x/crypto => github.com/cli/crypto v0.0.0-20210929142629-6be313f59b03 - -replace github.com/henvic/httpretty v0.0.6 => github.com/mislav/httpretty v0.1.1-0.20230202151216-d31343e0d884 diff --git a/go.sum b/go.sum index 86db0a40a..f36c934c0 100644 --- a/go.sum +++ b/go.sum @@ -1,40 +1,5 @@ -cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= -cloud.google.com/go v0.44.1/go.mod h1:iSa0KzasP4Uvy3f1mN/7PiObzGgflwredwwASm/v6AU= -cloud.google.com/go v0.44.2/go.mod h1:60680Gw3Yr4ikxnPRS/oxxkBccT6SA1yMk63TGekxKY= -cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc= -cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg0= -cloud.google.com/go v0.50.0/go.mod h1:r9sluTvynVuxRIOHXQEHMFffphuXHOMZMycpNR5e6To= -cloud.google.com/go v0.52.0/go.mod h1:pXajvRH/6o3+F9jDHZWQ5PbGhn+o8w9qiu/CffaVdO4= -cloud.google.com/go v0.53.0/go.mod h1:fp/UouUEsRkN6ryDKNW/Upv/JBKnv6WDthjR6+vze6M= -cloud.google.com/go v0.54.0/go.mod h1:1rq2OEkV3YMf6n/9ZvGWI3GWw0VoqH/1x2nd8Is/bPc= -cloud.google.com/go v0.56.0/go.mod h1:jr7tqZxxKOVYizybht9+26Z/gUq7tiRzu+ACVAMbKVk= -cloud.google.com/go v0.57.0/go.mod h1:oXiQ6Rzq3RAkkY7N6t3TcE6jE+CIBBbA36lwQ1JyzZs= -cloud.google.com/go v0.62.0/go.mod h1:jmCYTdRCQuc1PHIIJ/maLInMho30T/Y0M4hTdTShOYc= -cloud.google.com/go v0.65.0/go.mod h1:O5N8zS7uWy9vkA9vayVHs65eM1ubvY4h553ofrNHObY= -cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o= -cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE= -cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvftPBK2Dvzc= -cloud.google.com/go/bigquery v1.5.0/go.mod h1:snEHRnqQbz117VIFhE8bmtwIDY80NLUZUMb4Nv6dBIg= -cloud.google.com/go/bigquery v1.7.0/go.mod h1://okPTzCYNXSlb24MZs83e2Do+h+VXtc4gLoIoXIAPc= -cloud.google.com/go/bigquery v1.8.0/go.mod h1:J5hqkt3O0uAFnINi6JXValWIb1v0goeZM77hZzJN/fQ= -cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= -cloud.google.com/go/datastore v1.1.0/go.mod h1:umbIZjpQpHh4hmRpGhH4tLFup+FVzqBi1b3c64qFpCk= -cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= -cloud.google.com/go/pubsub v1.1.0/go.mod h1:EwwdRX2sKPjnvnqCa270oGRyludottCI76h+R3AArQw= -cloud.google.com/go/pubsub v1.2.0/go.mod h1:jhfEVHT8odbXTkndysNHCcx0awwzvfOlguIAii9o8iA= -cloud.google.com/go/pubsub v1.3.1/go.mod h1:i+ucay31+CNRpDW4Lu78I4xXG+O1r/MAHgjpRVR+TSU= -cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= -cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos= -cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk= -cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= -cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= -dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/AlecAivazis/survey/v2 v2.3.6 h1:NvTuVHISgTHEHeBFqt6BHOe4Ny/NwGZr7w+F8S9ziyw= -github.com/AlecAivazis/survey/v2 v2.3.6/go.mod h1:4AuI9b7RjAR+G7v9+C4YSlX/YL3K3cWNXgWXOhllqvI= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ= +github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= @@ -47,48 +12,39 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= github.com/briandowns/spinner v1.18.1 h1:yhQmQtM1zsqFsouh09Bk/jCjd50pC3EOGsh28gLVvwY= github.com/briandowns/spinner v1.18.1/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU= -github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4= -github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= -github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= +github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/charmbracelet/glamour v0.5.1-0.20220727184942-e70ff2d969da h1:FGz53GWQRiKQ/5xUsoCCkewSQIC7u81Scaxx2nUy3nM= github.com/charmbracelet/glamour v0.5.1-0.20220727184942-e70ff2d969da/go.mod h1:HXz79SMFnF9arKxqeoHWxmo1BhplAH7wehlRhKQIL94= github.com/charmbracelet/lipgloss v0.5.0 h1:lulQHuVeodSgDez+3rGiuxlPVXSnhth442DATR2/8t8= github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs= -github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/cli/browser v1.0.0/go.mod h1:IEWkHYbLjkhtjwwWlwTHW2lGxeS5gezEQBMLTwDHf5Q= -github.com/cli/browser v1.1.0 h1:xOZBfkfY9L9vMBgqb1YwRirGu6QFaQ5dP/vXt5ENSOY= -github.com/cli/browser v1.1.0/go.mod h1:HKMQAt9t12kov91Mn7RfZxyJQQgWgyS/3SZswlZ5iTI= +github.com/cli/browser v1.2.0 h1:yvU7e9qf97kZqGFX6n2zJPHsmSObY9ske+iCvKelvXg= +github.com/cli/browser v1.2.0/go.mod h1:xFFnXLVcAyW9ni0cuo6NnrbCP75JxJ0RO7VtCBiH/oI= github.com/cli/crypto v0.0.0-20210929142629-6be313f59b03 h1:3f4uHLfWx4/WlnMPXGai03eoWAI+oGHJwr+5OXfxCr8= github.com/cli/crypto v0.0.0-20210929142629-6be313f59b03/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -github.com/cli/go-gh v1.2.1 h1:xFrjejSsgPiwXFP6VYynKWwxLQcNJy3Twbu82ZDlR/o= -github.com/cli/go-gh v1.2.1/go.mod h1:Jxk8X+TCO4Ui/GarwY9tByWm/8zp4jJktzVZNlTW5VM= +github.com/cli/go-gh/v2 v2.0.1 h1:W4L7C5xT9CwkcqTsUzBhFhRKGpek9oRtdDLku2Hku+E= +github.com/cli/go-gh/v2 v2.0.1/go.mod h1:zWab1jRnJ0Ug8qRjsZHFk/Oq51ZWuhSxRL2FDUDgQWk= github.com/cli/oauth v1.0.1 h1:pXnTFl/qUegXHK531Dv0LNjW4mLx626eS42gnzfXJPA= github.com/cli/oauth v1.0.1/go.mod h1:qd/FX8ZBD6n1sVNQO3aIdRxeu5LGw9WhKnYhIIoC2A4= github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q= github.com/cli/safeexec v1.0.1 h1:e/C79PbXF4yYTN/wauC4tviMxEV13BwljGj0N9j+N00= github.com/cli/safeexec v1.0.1/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q= -github.com/cli/shurcooL-graphql v0.0.2 h1:rwP5/qQQ2fM0TzkUTwtt6E2LbIYf6R+39cUXTa04NYk= -github.com/cli/shurcooL-graphql v0.0.2/go.mod h1:tlrLmw/n5Q/+4qSvosT+9/W5zc8ZMjnJeYBxSdb4nWA= -github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cli/shurcooL-graphql v0.0.3 h1:CtpPxyGDs136/+ZeyAfUKYmcQBjDlq5aqnrDCW5Ghh8= +github.com/cli/shurcooL-graphql v0.0.3/go.mod h1:tlrLmw/n5Q/+4qSvosT+9/W5zc8ZMjnJeYBxSdb4nWA= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= -github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0= -github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0= +github.com/danieljoos/wincred v1.2.0 h1:ozqKHaLK0W/ii4KVbbvluM91W2H3Sh0BncbUNPS7jLE= +github.com/danieljoos/wincred v1.2.0/go.mod h1:FzQLLMKBFdvu+osBrnFODiv32YGwCfx0SkRa/eYHgec= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E= github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= -github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= -github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= @@ -97,100 +53,46 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= github.com/gdamore/tcell/v2 v2.5.4 h1:TGU4tSjD3sCL788vFNeJnTdzpNKIw1H5dgLnJRQVv/k= github.com/gdamore/tcell/v2 v2.5.4/go.mod h1:dZgRy5v4iMobMEcWNYBtREnDZAT9DYmfqIkrgEMxLyw= -github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= -github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= -github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= -github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= -github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= -github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= -github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= -github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= -github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= -github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= -github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= -github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= -github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= -github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= -github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= -github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= -github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= -github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= -github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= -github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= -github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= +github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw= github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= -github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= -github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/henvic/httpretty v0.1.2 h1:EQo556sO0xeXAjP10eB+BZARMuvkdGqtfeS4Ntjvkiw= +github.com/henvic/httpretty v0.1.2/go.mod h1:ViEsly7wgdugYtymX54pYp6Vv2wqZmNHayJ6q8tlKCc= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= -github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/itchyny/gojq v0.12.8 h1:Zxcwq8w4IeR8JJYEtoG2MWJZUv0RGY6QqJcO1cqV8+A= -github.com/itchyny/gojq v0.12.8/go.mod h1:gE2kZ9fVRU0+JAksaTzjIlgnCa2akU+a1V0WXgJQN5c= -github.com/itchyny/timefmt-go v0.1.3 h1:7M3LGVDsqcd0VZH2U+x393obrzZisp7C0uEe921iRkU= -github.com/itchyny/timefmt-go v0.1.3/go.mod h1:0osSSCQSASBJMsIZnhAaF1C2fCBTJZXrnj37mG8/c+A= +github.com/itchyny/gojq v0.12.13 h1:IxyYlHYIlspQHHTE0f3cJF0NKDMfajxViuhBLnHd/QU= +github.com/itchyny/gojq v0.12.13/go.mod h1:JzwzAqenfhrPUuwbmEz3nu3JQmFLlQTQMUcOdnu/Sf4= +github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE= +github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= -github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= -github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= @@ -199,8 +101,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= -github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= @@ -213,8 +115,6 @@ github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyex github.com/microcosm-cc/bluemonday v1.0.19/go.mod h1:QNzV2UbLK2/53oIIwTOyLUSABMkjZ4tqiyC1g/DyqxE= github.com/microcosm-cc/bluemonday v1.0.20 h1:flpzsq4KU3QIYAYGV/szUat7H+GPOXR0B2JU5A1Wp8Y= github.com/microcosm-cc/bluemonday v1.0.20/go.mod h1:yfBmMi8mxvaZut3Yytv+jTXRY8mxyjJ0/kQBTElld50= -github.com/mislav/httpretty v0.1.1-0.20230202151216-d31343e0d884 h1:JQp1j1IWuMQZc2tyDQ9KmksjQbw5MhUOzWzZZn7WyU0= -github.com/mislav/httpretty v0.1.1-0.20230202151216-d31343e0d884/go.mod h1:ViEsly7wgdugYtymX54pYp6Vv2wqZmNHayJ6q8tlKCc= github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ= github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= @@ -224,24 +124,24 @@ github.com/muesli/termenv v0.12.0 h1:KuQRUE3PgxRFWhq4gHvZtPSLCGDqM5q/cYr1pZ39ytc github.com/muesli/termenv v0.12.0/go.mod h1:WCCv32tusQ/EEZ5S8oUIIrC/nIuBcxCVqlN4Xfkv+7A= github.com/muhammadmuzzammil1998/jsonc v0.0.0-20201229145248-615b0916ca38 h1:0FrBxrkJ0hVembTb/e4EU5Ml6vLcOusAqymmYISg5Uo= github.com/muhammadmuzzammil1998/jsonc v0.0.0-20201229145248-615b0916ca38/go.mod h1:saF2fIVw4banK0H4+/EuqfFLpRnoy5S+ECwTOCcRcSU= +github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4= +github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rivo/tview v0.0.0-20221029100920-c4a7e501810d h1:jKIUJdMcIVGOSHi6LSqJqw9RqblyblE2ZrHvFbWR3S0= github.com/rivo/tview v0.0.0-20221029100920-c4a7e501810d/go.mod h1:YX2wUZOcJGOIycErz2s9KvDaP0jnWwRCirQMPLPpQ+Y= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.2 h1:YwD0ulJSJytLpiaWua0sBDusfsCZohxjxzVTYjwxfV8= -github.com/rivo/uniseg v0.4.2/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= -github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= +github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00 h1:fiFvD4lT0aWjuuAb64LlZ/67v87m+Kc9Qsu5cMFNK0w= -github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo= +github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9 h1:nCBaIs5/R0HFP5+aPW/SzFUF8z0oKuCXmuDmHWaxzjY= +github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo= github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 h1:B1PEwpArrNp4dkQrfxh/abbBAOZBVp0ds+fBEOUOqOc= github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg= github.com/sourcegraph/jsonrpc2 v0.1.0 h1:ohJHjZ+PcaLxDUjqk2NC3tIGsVa5bXThe1ZheSXOjuk= @@ -251,19 +151,17 @@ github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUq github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q= -github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e h1:BuzhfgfWQbX0dWzYzT1zsORLnHRv3bcRcsaUk0VmXA8= github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e/go.mod h1:/Tnicc6m/lsJE0irFMA0LfIwTBo4QP7A8IfyIv4zZKI= -github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg= @@ -271,198 +169,55 @@ github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark-emoji v1.0.1 h1:ctuWEyzGBwiucEqxzwe0SOYDXPAucOrE9NQC18Wa1os= github.com/yuin/goldmark-emoji v1.0.1/go.mod h1:2w1E6FEWLcDQkoTE+7HU6QF1F6SLlNGjRIBbIZQFqkQ= -github.com/zalando/go-keyring v0.2.2 h1:f0xmpYiSrHtSNAVgwip93Cg8tuF45HJM6rHq/A5RI/4= -github.com/zalando/go-keyring v0.2.2/go.mod h1:sI3evg9Wvpw3+n4SqplGSJUMwtDeROfD4nsFz4z9PG0= -go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= -go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= -go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= -golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek= -golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY= -golang.org/x/exp v0.0.0-20191129062945-2f5052295587/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= -golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= -golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= -golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= -golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= -golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= -golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= -golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs= -golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= -golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= -golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= -golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= -golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +github.com/zalando/go-keyring v0.2.3 h1:v9CUu9phlABObO4LPWycf+zwMG7nlbb3t/B5wa97yms= +github.com/zalando/go-keyring v0.2.3/go.mod h1:HL4k+OXQfJUWaMnqyuSOc0drfGPX2b51Du6K+MRgZMk= golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220923203811-8be639271d50/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= -golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= -golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= -golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a h1:qfl7ob3DIEs3Ml9oLuPwY2N04gymzAW04WsUQHIClgM= -golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +golang.org/x/oauth2 v0.4.0 h1:NF0gk8LVPg1Ml7SSbGyySuoxdsXitj7TvgvuRxIMc/M= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200501052902-10377860bb8e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw= -golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= -golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ= +golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= -golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191113191852-77e3bb0ad9e7/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191115202509-3a792d9c32b2/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191216173652-a0e659d51361/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20191227053925-7b8e75db28f4/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200117161641-43d50277825c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200122220014-bf1340f18c4a/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200204074204-1cc6d1ef6c74/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200227222343-706bc42d1f0d/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= -golang.org/x/tools v0.0.0-20200312045724-11d5b4c81c7d/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= -golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= -golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= -golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= -golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= @@ -470,104 +225,20 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= -google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= -google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= -google.golang.org/api v0.9.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= -google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= -google.golang.org/api v0.14.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= -google.golang.org/api v0.15.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= -google.golang.org/api v0.17.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= -google.golang.org/api v0.18.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= -google.golang.org/api v0.19.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= -google.golang.org/api v0.20.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= -google.golang.org/api v0.22.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= -google.golang.org/api v0.24.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE= -google.golang.org/api v0.28.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE= -google.golang.org/api v0.29.0/go.mod h1:Lcubydp8VUV7KeIHD9z2Bys/sm/vGKnG1UHuDBSrHWM= -google.golang.org/api v0.30.0/go.mod h1:QGmEvQ87FHZNiUVJkT14jQNYJ4ZJjdRF23ZXz5138Fc= -google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= -google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= -google.golang.org/appengine v1.6.6 h1:lMO5rYAqUxkmaj76jAkRUvt5JZgFymx/+Q5Mzfivuhc= -google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= -google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= -google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20191115194625-c23dd37a84c9/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20191230161307-f3c370f40bfb/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20200115191322-ca5a22157cba/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90/go.mod h1:GmwEX6Z4W5gMy59cAlVYjN9JhxgbQH6Gn+gFDQe2lzA= -google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200228133532-8c2c7df3a383/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200305110556-506484158171/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200312145019-da6875a35672/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200331122359-1ee6d9798940/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200515170657-fc4c6c6a6587/go.mod h1:YsZOwe1myG/8QRHRsmBRE1LrgQY60beZKjly0O1fX9U= -google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= -google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA= -google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 h1:PDIOdWxZ8eRizhKa1AAvY53xsvLB1cWorMjslvY3VA8= -google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= -google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= -google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= -google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= -google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= -google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= -google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= -google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= -google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= -google.golang.org/grpc v1.49.0 h1:WTLtQzmQori5FUH25Pq4WT22oCsv8USpQ+F6rqtsmxw= -google.golang.org/grpc v1.49.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI= -google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= -google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= -google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= -google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= -google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= -google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= +google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f h1:BWUVssLB0HVOSY78gIdvk1dTVYtT1y8SBWtPYuTJ/6w= +google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM= +google.golang.org/grpc v1.53.0 h1:LAv2ds7cmFV/XTS3XG1NneeENYrXGmorPxsBbptIjNc= +google.golang.org/grpc v1.53.0/go.mod h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= -google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= +google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= -honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= -honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= -rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= -rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= -rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= diff --git a/internal/browser/browser.go b/internal/browser/browser.go index e3c225b0f..0f231ddc8 100644 --- a/internal/browser/browser.go +++ b/internal/browser/browser.go @@ -3,7 +3,7 @@ package browser import ( "io" - ghBrowser "github.com/cli/go-gh/pkg/browser" + ghBrowser "github.com/cli/go-gh/v2/pkg/browser" ) type Browser interface { @@ -12,5 +12,5 @@ type Browser interface { func New(launcher string, stdout, stderr io.Writer) Browser { b := ghBrowser.New(launcher, stdout, stderr) - return &b + return b } diff --git a/internal/build/build.go b/internal/build/build.go index 8c3e78a7e..267315ad2 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -1,6 +1,7 @@ package build import ( + "os" "runtime/debug" ) @@ -16,4 +17,11 @@ func init() { Version = info.Main.Version } } + + // Signal the tcell library to skip its expensive `init` block. This saves 30-40ms in startup + // time for the gh process. The downside is that some Unicode glyphs from user-generated + // content might cause mis-alignment in tcell-enabled views. + // + // https://github.com/gdamore/tcell/commit/2f889d79bd61b1fd2f43372529975a65b792a7ae + _ = os.Setenv("TCELL_MINIMIZE", "1") } diff --git a/internal/codespaces/api/api.go b/internal/codespaces/api/api.go index 0f7c71aa9..d66aff065 100644 --- a/internal/codespaces/api/api.go +++ b/internal/codespaces/api/api.go @@ -122,17 +122,24 @@ func (a *API) GetUser(ctx context.Context) (*User, error) { var response User if err := json.Unmarshal(b, &response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } return &response, nil } +// RepositoryOwner represents owner of a repository +type RepositoryOwner struct { + Type string `json:"type"` + Login string `json:"login"` +} + // Repository represents a GitHub repository. type Repository struct { - ID int `json:"id"` - FullName string `json:"full_name"` - DefaultBranch string `json:"default_branch"` + ID int `json:"id"` + FullName string `json:"full_name"` + DefaultBranch string `json:"default_branch"` + Owner RepositoryOwner `json:"owner"` } // GetRepository returns the repository associated with the given owner and name. @@ -160,7 +167,7 @@ func (a *API) GetRepository(ctx context.Context, nwo string) (*Repository, error var response Repository if err := json.Unmarshal(b, &response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } return &response, nil @@ -185,6 +192,15 @@ type Codespace struct { PendingOperationDisabledReason string `json:"pending_operation_disabled_reason"` IdleTimeoutNotice string `json:"idle_timeout_notice"` WebURL string `json:"web_url"` + DevContainerPath string `json:"devcontainer_path"` + Prebuild bool `json:"prebuild"` + Location string `json:"location"` + IdleTimeoutMinutes int `json:"idle_timeout_minutes"` + RetentionPeriodMinutes int `json:"retention_period_minutes"` + RetentionExpiresAt string `json:"retention_expires_at"` + RecentFolders []string `json:"recent_folders"` + BillableOwner User `json:"billable_owner"` + EnvironmentId string `json:"environment_id"` } type CodespaceGitStatus struct { @@ -223,8 +239,8 @@ type CodespaceConnection struct { HostPublicKeys []string `json:"hostPublicKeys"` } -// CodespaceFields is the list of exportable fields for a codespace. -var CodespaceFields = []string{ +// ListCodespaceFields is the list of exportable fields for a codespace when using the `gh cs list` command. +var ListCodespaceFields = []string{ "displayName", "name", "owner", @@ -237,6 +253,30 @@ var CodespaceFields = []string{ "vscsTarget", } +// ViewCodespaceFields is the list of exportable fields for a codespace when using the `gh cs view` command. +var ViewCodespaceFields = []string{ + "name", + "displayName", + "state", + "owner", + "billableOwner", + "location", + "repository", + "gitStatus", + "devcontainerPath", + "machineName", + "machineDisplayName", + "prebuild", + "createdAt", + "lastUsedAt", + "idleTimeoutMinutes", + "retentionPeriodDays", + "retentionExpiresAt", + "recentFolders", + "vscsTarget", + "environmentId", +} + func (c *Codespace) ExportData(fields []string) map[string]interface{} { v := reflect.ValueOf(c).Elem() data := map[string]interface{}{} @@ -249,11 +289,17 @@ func (c *Codespace) ExportData(fields []string) map[string]interface{} { data[f] = c.Repository.FullName case "machineName": data[f] = c.Machine.Name + case "machineDisplayName": + data[f] = c.Machine.DisplayName + case "retentionPeriodDays": + data[f] = c.RetentionPeriodMinutes / 1440 case "gitStatus": data[f] = map[string]interface{}{ "ref": c.GitStatus.Ref, "hasUnpushedChanges": c.GitStatus.HasUnpushedChanges, "hasUncommittedChanges": c.GitStatus.HasUncommittedChanges, + "ahead": c.GitStatus.Ahead, + "behind": c.GitStatus.Behind, } case "vscsTarget": if c.VSCSTarget != "" && c.VSCSTarget != VSCSTargetProduction { @@ -336,7 +382,7 @@ func (a *API) ListCodespaces(ctx context.Context, opts ListCodespacesOptions) (c dec := json.NewDecoder(resp.Body) if err := dec.Decode(&response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } nextURL := findNextPage(resp.Header.Get("Link")) @@ -398,7 +444,7 @@ func (a *API) GetOrgMemberCodespace(ctx context.Context, orgName string, userNam dec := json.NewDecoder(resp.Body) if err := dec.Decode(&response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } for _, cs := range response.Codespaces { @@ -455,7 +501,7 @@ func (a *API) GetCodespace(ctx context.Context, codespaceName string, includeCon var response Codespace if err := json.Unmarshal(b, &response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } return &response, nil @@ -563,7 +609,7 @@ func (a *API) GetCodespacesMachines(ctx context.Context, repoID int, branch, loc Machines []*Machine `json:"machines"` } if err := json.Unmarshal(b, &response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } return response.Machines, nil @@ -636,7 +682,7 @@ func (a *API) GetCodespaceRepoSuggestions(ctx context.Context, partialSearch str Items []*Repository `json:"items"` } if err := json.Unmarshal(b, &response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } repoNames := make([]string, len(response.Items)) @@ -683,7 +729,7 @@ func (a *API) GetCodespaceBillableOwner(ctx context.Context, nwo string) (*User, } } if err := json.Unmarshal(b, &response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } // While this response contains further helpful information ahead of codespace creation, @@ -815,7 +861,7 @@ func (a *API) startCreate(ctx context.Context, params *CreateCodespaceParams) (* var response Codespace if err := json.Unmarshal(b, &response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } return &response, errProvisioningInProgress // RPC finished before result of creation known @@ -831,7 +877,7 @@ func (a *API) startCreate(ctx context.Context, params *CreateCodespaceParams) (* return nil, fmt.Errorf("error reading response body: %w", err) } if err := json.Unmarshal(b, &ue); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } if ue.AllowPermissionsURL != "" { @@ -853,7 +899,7 @@ func (a *API) startCreate(ctx context.Context, params *CreateCodespaceParams) (* var response Codespace if err := json.Unmarshal(b, &response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } return &response, nil @@ -934,7 +980,7 @@ func (a *API) ListDevContainers(ctx context.Context, repoID int, branch string, dec := json.NewDecoder(resp.Body) if err := dec.Decode(&response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } nextURL := findNextPage(resp.Header.Get("Link")) @@ -1007,7 +1053,7 @@ func (a *API) EditCodespace(ctx context.Context, codespaceName string, params *E var response Codespace if err := json.Unmarshal(b, &response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } return &response, nil @@ -1055,7 +1101,7 @@ func (a *API) GetCodespaceRepositoryContents(ctx context.Context, codespace *Cod var response getCodespaceRepositoryContentsResponse if err := json.Unmarshal(b, &response); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + return nil, fmt.Errorf("error unmarshalling response: %w", err) } decoded, err := base64.StdEncoding.DecodeString(response.Content) diff --git a/internal/codespaces/api/api_test.go b/internal/codespaces/api/api_test.go index e8fab74f1..5e9316fc1 100644 --- a/internal/codespaces/api/api_test.go +++ b/internal/codespaces/api/api_test.go @@ -21,7 +21,7 @@ func generateCodespaceList(start int, end int) []*Codespace { return codespacesList } -func createFakeListEndpointServer(t *testing.T, initalTotal int, finalTotal int) *httptest.Server { +func createFakeListEndpointServer(t *testing.T, initialTotal int, finalTotal int) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/user/codespaces" { t.Fatal("Incorrect path") @@ -48,7 +48,7 @@ func createFakeListEndpointServer(t *testing.T, initalTotal int, finalTotal int) switch page { case 1: response.Codespaces = generateCodespaceList(0, per_page) - response.TotalCount = initalTotal + response.TotalCount = initialTotal w.Header().Set("Link", fmt.Sprintf(`; rel="last", ; rel="next"`, r.Host, per_page)) case 2: response.Codespaces = generateCodespaceList(per_page, per_page*2) diff --git a/internal/codespaces/codespaces.go b/internal/codespaces/codespaces.go index 13b27c3da..9db12f01e 100644 --- a/internal/codespaces/codespaces.go +++ b/internal/codespaces/codespaces.go @@ -88,9 +88,14 @@ func ConnectToLiveshare(ctx context.Context, progress progressIndicator, session }) } -// ListenTCP starts a localhost tcp listener and returns the listener and bound port -func ListenTCP(port int) (*net.TCPListener, int, error) { - addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%d", port)) +// ListenTCP starts a localhost tcp listener on 127.0.0.1 (unless allInterfaces is true) and returns the listener and bound port +func ListenTCP(port int, allInterfaces bool) (*net.TCPListener, int, error) { + host := "127.0.0.1" + if allInterfaces { + host = "" + } + + addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", host, port)) if err != nil { return nil, 0, fmt.Errorf("failed to build tcp address: %w", err) } diff --git a/internal/codespaces/rpc/codespace/codespace_host_service.v1.pb.go b/internal/codespaces/rpc/codespace/codespace_host_service.v1.pb.go index 6da7f9e39..aa3601f96 100644 --- a/internal/codespaces/rpc/codespace/codespace_host_service.v1.pb.go +++ b/internal/codespaces/rpc/codespace/codespace_host_service.v1.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.28.1 -// protoc v3.21.12 +// protoc v3.12.4 // source: codespace/codespace_host_service.v1.proto package codespace diff --git a/internal/codespaces/rpc/codespace/codespace_host_service.v1_grpc.pb.go b/internal/codespaces/rpc/codespace/codespace_host_service.v1_grpc.pb.go index e876578ad..c8bf17a8f 100644 --- a/internal/codespaces/rpc/codespace/codespace_host_service.v1_grpc.pb.go +++ b/internal/codespaces/rpc/codespace/codespace_host_service.v1_grpc.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.2.0 -// - protoc v3.21.12 +// - protoc v3.12.4 // source: codespace/codespace_host_service.v1.proto package codespace diff --git a/internal/codespaces/rpc/generate.sh b/internal/codespaces/rpc/generate.sh index 4ba2f898a..2314b6b57 100755 --- a/internal/codespaces/rpc/generate.sh +++ b/internal/codespaces/rpc/generate.sh @@ -20,11 +20,11 @@ function generate { local contract="$dir/$proto" - protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative "$contract" + protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative "$contract" --experimental_allow_proto3_optional echo "Generated protocol buffers for $contract" - services=$(cat "$contract" | grep -Eo "service .+ {" | awk '{print $2 "Server"}') - moq -out $contract.mock.go $dir $services + services=$(grep -Eo "service .+ {" <$contract | awk '{print $2 "Server"}') + moq -out "$contract.mock.go" "$dir" "$services" echo "Generated mock protocols for $contract" } diff --git a/internal/codespaces/rpc/invoker.go b/internal/codespaces/rpc/invoker.go index bb2e25a55..39ae5ab19 100644 --- a/internal/codespaces/rpc/invoker.go +++ b/internal/codespaces/rpc/invoker.go @@ -239,6 +239,7 @@ func (i *invoker) StartSSHServerWithOptions(ctx context.Context, options StartSS } func listenTCP() (*net.TCPListener, error) { + // We will end up using this same address to connect, so specify the IP also or the connect will fail addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0") if err != nil { return nil, fmt.Errorf("failed to build tcp address: %w", err) diff --git a/internal/codespaces/rpc/jupyter/jupyter_server_host_service.v1.pb.go b/internal/codespaces/rpc/jupyter/jupyter_server_host_service.v1.pb.go index b8f400d3c..ba1987b87 100644 --- a/internal/codespaces/rpc/jupyter/jupyter_server_host_service.v1.pb.go +++ b/internal/codespaces/rpc/jupyter/jupyter_server_host_service.v1.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.28.1 -// protoc v3.21.12 +// protoc v3.12.4 // source: jupyter/jupyter_server_host_service.v1.proto package jupyter diff --git a/internal/codespaces/rpc/jupyter/jupyter_server_host_service.v1_grpc.pb.go b/internal/codespaces/rpc/jupyter/jupyter_server_host_service.v1_grpc.pb.go index 31802020d..0473d5a8f 100644 --- a/internal/codespaces/rpc/jupyter/jupyter_server_host_service.v1_grpc.pb.go +++ b/internal/codespaces/rpc/jupyter/jupyter_server_host_service.v1_grpc.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.2.0 -// - protoc v3.21.12 +// - protoc v3.12.4 // source: jupyter/jupyter_server_host_service.v1.proto package jupyter diff --git a/internal/codespaces/rpc/ssh/ssh_server_host_service.v1.pb.go b/internal/codespaces/rpc/ssh/ssh_server_host_service.v1.pb.go index 3dd22f583..d36dd7b56 100644 --- a/internal/codespaces/rpc/ssh/ssh_server_host_service.v1.pb.go +++ b/internal/codespaces/rpc/ssh/ssh_server_host_service.v1.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.28.1 -// protoc v3.21.12 +// protoc v3.12.4 // source: ssh/ssh_server_host_service.v1.proto package ssh diff --git a/internal/codespaces/rpc/ssh/ssh_server_host_service.v1_grpc.pb.go b/internal/codespaces/rpc/ssh/ssh_server_host_service.v1_grpc.pb.go index a111656e8..995eb6ce5 100644 --- a/internal/codespaces/rpc/ssh/ssh_server_host_service.v1_grpc.pb.go +++ b/internal/codespaces/rpc/ssh/ssh_server_host_service.v1_grpc.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.2.0 -// - protoc v3.21.12 +// - protoc v3.12.4 // source: ssh/ssh_server_host_service.v1.proto package ssh diff --git a/internal/codespaces/ssh.go b/internal/codespaces/ssh.go index 9a146c4f6..a623d36f9 100644 --- a/internal/codespaces/ssh.go +++ b/internal/codespaces/ssh.go @@ -18,13 +18,15 @@ type printer interface { // Shell runs an interactive secure shell over an existing // port-forwarding session. It runs until the shell is terminated // (including by cancellation of the context). -func Shell(ctx context.Context, p printer, sshArgs []string, port int, destination string, usingCustomPort bool) error { +func Shell( + ctx context.Context, p printer, sshArgs []string, port int, destination string, printConnDetails bool, +) error { cmd, connArgs, err := newSSHCommand(ctx, port, destination, sshArgs) if err != nil { return fmt.Errorf("failed to create ssh command: %w", err) } - if usingCustomPort { + if printConnDetails { p.Printf("Connection Details: ssh %s %s", destination, connArgs) } diff --git a/internal/codespaces/states.go b/internal/codespaces/states.go index 9874d1a62..3a2872365 100644 --- a/internal/codespaces/states.go +++ b/internal/codespaces/states.go @@ -52,7 +52,7 @@ func PollPostCreateStates(ctx context.Context, progress progressIndicator, apiCl }() // Ensure local port is listening before client (getPostCreateOutput) connects. - listen, localPort, err := ListenTCP(0) + listen, localPort, err := ListenTCP(0, false) if err != nil { return err } diff --git a/internal/config/config.go b/internal/config/config.go index 890dcec9f..bf14a1aa0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,9 +4,9 @@ import ( "os" "path/filepath" - ghAuth "github.com/cli/go-gh/pkg/auth" - ghConfig "github.com/cli/go-gh/pkg/config" - "github.com/zalando/go-keyring" + "github.com/cli/cli/v2/internal/keyring" + ghAuth "github.com/cli/go-gh/v2/pkg/auth" + ghConfig "github.com/cli/go-gh/v2/pkg/config" ) const ( @@ -124,7 +124,7 @@ type AuthConfig struct { // Token will retrieve the auth token for the given hostname, // searching environment variables, plain text config, and -// lastly encypted storage. +// lastly encrypted storage. func (c *AuthConfig) Token(hostname string) (string, string) { if c.tokenOverride != nil { return c.tokenOverride(hostname) diff --git a/internal/config/stub.go b/internal/config/stub.go index f8e56ee4e..5e37a8ccb 100644 --- a/internal/config/stub.go +++ b/internal/config/stub.go @@ -6,7 +6,7 @@ import ( "path/filepath" "testing" - ghConfig "github.com/cli/go-gh/pkg/config" + ghConfig "github.com/cli/go-gh/v2/pkg/config" ) func NewBlankConfig() *ConfigMock { diff --git a/internal/featuredetection/feature_detection.go b/internal/featuredetection/feature_detection.go index 6c81ac546..983784804 100644 --- a/internal/featuredetection/feature_detection.go +++ b/internal/featuredetection/feature_detection.go @@ -22,30 +22,25 @@ var allIssueFeatures = IssueFeatures{ } type PullRequestFeatures struct { - ReviewDecision bool - StatusCheckRollup bool - BranchProtectionRule bool - MergeQueue bool + MergeQueue bool + // CheckRunAndStatusContextCounts indicates whether the API supports + // the checkRunCount, checkRunCountsByState, statusContextCount and stausContextCountsByState + // fields on the StatusCheckRollupContextConnection + CheckRunAndStatusContextCounts bool } var allPullRequestFeatures = PullRequestFeatures{ - ReviewDecision: true, - StatusCheckRollup: true, - BranchProtectionRule: true, - MergeQueue: true, + MergeQueue: true, + CheckRunAndStatusContextCounts: true, } type RepositoryFeatures struct { - IssueTemplateMutation bool - IssueTemplateQuery bool PullRequestTemplateQuery bool VisibilityField bool AutoMerge bool } var allRepositoryFeatures = RepositoryFeatures{ - IssueTemplateMutation: true, - IssueTemplateQuery: true, PullRequestTemplateQuery: true, VisibilityField: true, AutoMerge: true, @@ -103,32 +98,40 @@ func (d *detector) PullRequestFeatures() (PullRequestFeatures, error) { // return allPullRequestFeatures, nil // } - features := PullRequestFeatures{ - ReviewDecision: true, - StatusCheckRollup: true, - BranchProtectionRule: true, - } - - var featureDetection struct { + var pullRequestFeatureDetection struct { PullRequest struct { Fields []struct { Name string } `graphql:"fields(includeDeprecated: true)"` } `graphql:"PullRequest: __type(name: \"PullRequest\")"` + StatusCheckRollupContextConnection struct { + Fields []struct { + Name string + } `graphql:"fields(includeDeprecated: true)"` + } `graphql:"StatusCheckRollupContextConnection: __type(name: \"StatusCheckRollupContextConnection\")"` } gql := api.NewClientFromHTTP(d.httpClient) - err := gql.Query(d.host, "PullRequest_fields", &featureDetection, nil) - if err != nil { - return features, err + if err := gql.Query(d.host, "PullRequest_fields", &pullRequestFeatureDetection, nil); err != nil { + return PullRequestFeatures{}, err } - for _, field := range featureDetection.PullRequest.Fields { + features := PullRequestFeatures{} + + for _, field := range pullRequestFeatureDetection.PullRequest.Fields { if field.Name == "isInMergeQueue" { features.MergeQueue = true } } + for _, field := range pullRequestFeatureDetection.StatusCheckRollupContextConnection.Fields { + // We only check for checkRunCount here but it, checkRunCountsByState, statusContextCount and statusContextCountsByState + // were all introduced in the same version of the API. + if field.Name == "checkRunCount" { + features.CheckRunAndStatusContextCounts = true + } + } + return features, nil } @@ -137,10 +140,7 @@ func (d *detector) RepositoryFeatures() (RepositoryFeatures, error) { return allRepositoryFeatures, nil } - features := RepositoryFeatures{ - IssueTemplateQuery: true, - IssueTemplateMutation: true, - } + features := RepositoryFeatures{} var featureDetection struct { Repository struct { diff --git a/internal/featuredetection/feature_detection_test.go b/internal/featuredetection/feature_detection_test.go index 2f2fb5b05..dda6fe6a5 100644 --- a/internal/featuredetection/feature_detection_test.go +++ b/internal/featuredetection/feature_detection_test.go @@ -82,21 +82,32 @@ func TestPullRequestFeatures(t *testing.T) { wantErr bool }{ { - name: "github.com", + name: "github.com with merge queue and status check counts by state", hostname: "github.com", queryResponse: map[string]string{ `query PullRequest_fields\b`: heredoc.Doc(` - { "data": { "PullRequest": { "fields": [ - {"name": "isInMergeQueue"}, - {"name": "isMergeQueueEnabled"} - ] } } } - `), + { + "data": { + "PullRequest": { + "fields": [ + {"name": "isInMergeQueue"}, + {"name": "isMergeQueueEnabled"} + ] + }, + "StatusCheckRollupContextConnection": { + "fields": [ + {"name": "checkRunCount"}, + {"name": "checkRunCountsByState"}, + {"name": "statusContextCount"}, + {"name": "statusContextCountsByState"} + ] + } + } + }`), }, wantFeatures: PullRequestFeatures{ - ReviewDecision: true, - StatusCheckRollup: true, - BranchProtectionRule: true, - MergeQueue: true, + MergeQueue: true, + CheckRunAndStatusContextCounts: true, }, wantErr: false, }, @@ -105,34 +116,77 @@ func TestPullRequestFeatures(t *testing.T) { hostname: "github.com", queryResponse: map[string]string{ `query PullRequest_fields\b`: heredoc.Doc(` - { "data": { "PullRequest": { "fields": [ - ] } } } - `), + { + "data": { + "PullRequest": { + "fields": [] + }, + "StatusCheckRollupContextConnection": { + "fields": [ + {"name": "checkRunCount"}, + {"name": "checkRunCountsByState"}, + {"name": "statusContextCount"}, + {"name": "statusContextCountsByState"} + ] + } + } + }`), }, wantFeatures: PullRequestFeatures{ - ReviewDecision: true, - StatusCheckRollup: true, - BranchProtectionRule: true, - MergeQueue: false, + MergeQueue: false, + CheckRunAndStatusContextCounts: true, }, wantErr: false, }, { - name: "GHE", + name: "GHE with merge queue and status check counts by state", hostname: "git.my.org", queryResponse: map[string]string{ `query PullRequest_fields\b`: heredoc.Doc(` - { "data": { "PullRequest": { "fields": [ - {"name": "isInMergeQueue"}, - {"name": "isMergeQueueEnabled"} - ] } } } - `), + { + "data": { + "PullRequest": { + "fields": [ + {"name": "isInMergeQueue"}, + {"name": "isMergeQueueEnabled"} + ] + }, + "StatusCheckRollupContextConnection": { + "fields": [ + {"name": "checkRunCount"}, + {"name": "checkRunCountsByState"}, + {"name": "statusContextCount"}, + {"name": "statusContextCountsByState"} + ] + } + } + }`), }, wantFeatures: PullRequestFeatures{ - ReviewDecision: true, - StatusCheckRollup: true, - BranchProtectionRule: true, - MergeQueue: true, + MergeQueue: true, + CheckRunAndStatusContextCounts: true, + }, + wantErr: false, + }, + { + name: "GHE without merge queue and status check counts by state", + hostname: "git.my.org", + queryResponse: map[string]string{ + `query PullRequest_fields\b`: heredoc.Doc(` + { + "data": { + "PullRequest": { + "fields": [] + }, + "StatusCheckRollupContextConnection": { + "fields": [] + } + } + }`), + }, + wantFeatures: PullRequestFeatures{ + MergeQueue: false, + CheckRunAndStatusContextCounts: false, }, wantErr: false, }, @@ -169,8 +223,6 @@ func TestRepositoryFeatures(t *testing.T) { name: "github.com", hostname: "github.com", wantFeatures: RepositoryFeatures{ - IssueTemplateMutation: true, - IssueTemplateQuery: true, PullRequestTemplateQuery: true, VisibilityField: true, AutoMerge: true, @@ -184,8 +236,6 @@ func TestRepositoryFeatures(t *testing.T) { `query Repository_fields\b`: `{"data": {}}`, }, wantFeatures: RepositoryFeatures{ - IssueTemplateMutation: true, - IssueTemplateQuery: true, PullRequestTemplateQuery: false, }, wantErr: false, @@ -201,8 +251,6 @@ func TestRepositoryFeatures(t *testing.T) { `), }, wantFeatures: RepositoryFeatures{ - IssueTemplateMutation: true, - IssueTemplateQuery: true, PullRequestTemplateQuery: true, }, wantErr: false, @@ -218,9 +266,7 @@ func TestRepositoryFeatures(t *testing.T) { `), }, wantFeatures: RepositoryFeatures{ - IssueTemplateMutation: true, - IssueTemplateQuery: true, - VisibilityField: true, + VisibilityField: true, }, wantErr: false, }, @@ -235,9 +281,7 @@ func TestRepositoryFeatures(t *testing.T) { `), }, wantFeatures: RepositoryFeatures{ - IssueTemplateMutation: true, - IssueTemplateQuery: true, - AutoMerge: true, + AutoMerge: true, }, wantErr: false, }, diff --git a/internal/ghinstance/host.go b/internal/ghinstance/host.go index 1fde25e23..729d47f31 100644 --- a/internal/ghinstance/host.go +++ b/internal/ghinstance/host.go @@ -6,37 +6,56 @@ import ( "strings" ) +// DefaultHostname is the domain name of the default GitHub instance. const defaultHostname = "github.com" -// localhost is the domain name of a local GitHub instance +// Localhost is the domain name of a local GitHub instance. const localhost = "github.localhost" -// Default returns the host name of the default GitHub instance +// TenancyHost is the domain name of a tenancy GitHub instance. +const tenancyHost = "ghe.com" + +// Default returns the host name of the default GitHub instance. func Default() string { return defaultHostname } -// IsEnterprise reports whether a non-normalized host name looks like a GHE instance +// IsEnterprise reports whether a non-normalized host name looks like a GHE instance. func IsEnterprise(h string) bool { normalizedHostName := NormalizeHostname(h) return normalizedHostName != defaultHostname && normalizedHostName != localhost } +// IsTenancy reports whether a non-normalized host name looks like a tenancy instance. +func IsTenancy(h string) bool { + normalizedHostName := NormalizeHostname(h) + return strings.HasSuffix(normalizedHostName, "."+tenancyHost) +} + +// TenantName extracts the tenant name from tenancy host name and +// reports whether it found the tenant name. +func TenantName(h string) (string, bool) { + normalizedHostName := NormalizeHostname(h) + return cutSuffix(normalizedHostName, "."+tenancyHost) +} + func isGarage(h string) bool { return strings.EqualFold(h, "garage.github.com") } -// NormalizeHostname returns the canonical host name of a GitHub instance +// NormalizeHostname returns the canonical host name of a GitHub instance. func NormalizeHostname(h string) string { hostname := strings.ToLower(h) if strings.HasSuffix(hostname, "."+defaultHostname) { return defaultHostname } - if strings.HasSuffix(hostname, "."+localhost) { return localhost } - + if before, found := cutSuffix(hostname, "."+tenancyHost); found { + idx := strings.LastIndex(before, ".") + return fmt.Sprintf("%s.%s", before[idx+1:], tenancyHost) + } return hostname } @@ -78,11 +97,9 @@ func RESTPrefix(hostname string) string { func GistPrefix(hostname string) string { prefix := "https://" - if strings.EqualFold(hostname, localhost) { prefix = "http://" } - return prefix + GistHost(hostname) } @@ -105,3 +122,11 @@ func HostPrefix(hostname string) string { } return fmt.Sprintf("https://%s/", hostname) } + +// Backport strings.CutSuffix from Go 1.20. +func cutSuffix(s, suffix string) (string, bool) { + if !strings.HasSuffix(s, suffix) { + return s, false + } + return s[:len(s)-len(suffix)], true +} diff --git a/internal/ghinstance/host_test.go b/internal/ghinstance/host_test.go index 5fa6eb4f3..673fad24c 100644 --- a/internal/ghinstance/host_test.go +++ b/internal/ghinstance/host_test.go @@ -49,6 +49,87 @@ func TestIsEnterprise(t *testing.T) { } } +func TestIsTenancy(t *testing.T) { + tests := []struct { + host string + want bool + }{ + { + host: "github.com", + want: false, + }, + { + host: "github.localhost", + want: false, + }, + { + host: "garage.github.com", + want: false, + }, + { + host: "ghe.com", + want: false, + }, + { + host: "tenant.ghe.com", + want: true, + }, + { + host: "api.tenant.ghe.com", + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.host, func(t *testing.T) { + if got := IsTenancy(tt.host); got != tt.want { + t.Errorf("IsTenancy() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestTenantName(t *testing.T) { + tests := []struct { + host string + wantTenant string + wantFound bool + }{ + { + host: "github.com", + wantTenant: "github.com", + }, + { + host: "github.localhost", + wantTenant: "github.localhost", + }, + { + host: "garage.github.com", + wantTenant: "github.com", + }, + { + host: "ghe.com", + wantTenant: "ghe.com", + }, + { + host: "tenant.ghe.com", + wantTenant: "tenant", + wantFound: true, + }, + { + host: "api.tenant.ghe.com", + wantTenant: "tenant", + wantFound: true, + }, + } + for _, tt := range tests { + t.Run(tt.host, func(t *testing.T) { + if tenant, found := TenantName(tt.host); tenant != tt.wantTenant || found != tt.wantFound { + t.Errorf("TenantName(%v) = %v %v, want %v %v", tt.host, tenant, found, tt.wantTenant, tt.wantFound) + } + }) + } +} + func TestNormalizeHostname(t *testing.T) { tests := []struct { host string @@ -90,6 +171,18 @@ func TestNormalizeHostname(t *testing.T) { host: "git.my.org", want: "git.my.org", }, + { + host: "ghe.com", + want: "ghe.com", + }, + { + host: "tenant.ghe.com", + want: "tenant.ghe.com", + }, + { + host: "api.tenant.ghe.com", + want: "tenant.ghe.com", + }, } for _, tt := range tests { t.Run(tt.host, func(t *testing.T) { @@ -139,6 +232,7 @@ func TestHostnameValidator(t *testing.T) { }) } } + func TestGraphQLEndpoint(t *testing.T) { tests := []struct { host string diff --git a/internal/ghrepo/repo.go b/internal/ghrepo/repo.go index 5151f65d4..4f0328e99 100644 --- a/internal/ghrepo/repo.go +++ b/internal/ghrepo/repo.go @@ -6,8 +6,8 @@ import ( "strings" "github.com/cli/cli/v2/internal/ghinstance" - ghAuth "github.com/cli/go-gh/pkg/auth" - "github.com/cli/go-gh/pkg/repository" + ghAuth "github.com/cli/go-gh/v2/pkg/auth" + "github.com/cli/go-gh/v2/pkg/repository" ) // Interface describes an object that represents a GitHub repository @@ -54,7 +54,7 @@ func FromFullNameWithHost(nwo, fallbackHost string) (Interface, error) { if err != nil { return nil, err } - return NewWithHost(repo.Owner(), repo.Name(), repo.Host()), nil + return NewWithHost(repo.Owner, repo.Name, repo.Host), nil } // FromURL extracts the GitHub repository information from a git remote URL @@ -92,12 +92,13 @@ func GenerateRepoURL(repo Interface, p string, args ...interface{}) string { return baseURL } -// TODO there is a parallel implementation for non-isolated commands func FormatRemoteURL(repo Interface, protocol string) string { if protocol == "ssh" { + if tenant, found := ghinstance.TenantName(repo.RepoHost()); found { + return fmt.Sprintf("%s@%s:%s/%s.git", tenant, repo.RepoHost(), repo.RepoOwner(), repo.RepoName()) + } return fmt.Sprintf("git@%s:%s/%s.git", repo.RepoHost(), repo.RepoOwner(), repo.RepoName()) } - return fmt.Sprintf("%s%s/%s.git", ghinstance.HostPrefix(repo.RepoHost()), repo.RepoOwner(), repo.RepoName()) } diff --git a/internal/ghrepo/repo_test.go b/internal/ghrepo/repo_test.go index 1530f8f24..7ea6b0598 100644 --- a/internal/ghrepo/repo_test.go +++ b/internal/ghrepo/repo_test.go @@ -220,3 +220,59 @@ func TestFromFullName(t *testing.T) { }) } } + +func TestFormatRemoteURL(t *testing.T) { + tests := []struct { + name string + repoHost string + repoOwner string + repoName string + protocol string + want string + }{ + { + name: "https protocol", + repoHost: "github.com", + repoOwner: "owner", + repoName: "name", + protocol: "https", + want: "https://github.com/owner/name.git", + }, + { + name: "https protocol local host", + repoHost: "github.localhost", + repoOwner: "owner", + repoName: "name", + protocol: "https", + want: "http://github.localhost/owner/name.git", + }, + { + name: "ssh protocol", + repoHost: "github.com", + repoOwner: "owner", + repoName: "name", + protocol: "ssh", + want: "git@github.com:owner/name.git", + }, + { + name: "ssh protocol tenancy host", + repoHost: "tenant.ghe.com", + repoOwner: "owner", + repoName: "name", + protocol: "ssh", + want: "tenant@tenant.ghe.com:owner/name.git", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := ghRepo{ + hostname: tt.repoHost, + owner: tt.repoOwner, + name: tt.repoName, + } + if url := FormatRemoteURL(r, tt.protocol); url != tt.want { + t.Errorf("expected url %q, got %q", tt.want, url) + } + }) + } +} diff --git a/internal/keyring/keyring.go b/internal/keyring/keyring.go new file mode 100644 index 000000000..afb5025e6 --- /dev/null +++ b/internal/keyring/keyring.go @@ -0,0 +1,68 @@ +// Package keyring is a simple wrapper that adds timeouts to the zalando/go-keyring package. +package keyring + +import ( + "time" + + "github.com/zalando/go-keyring" +) + +type TimeoutError struct { + message string +} + +func (e *TimeoutError) Error() string { + return e.message +} + +// Set secret in keyring for user. +func Set(service, user, secret string) error { + ch := make(chan error, 1) + go func() { + defer close(ch) + ch <- keyring.Set(service, user, secret) + }() + select { + case err := <-ch: + return err + case <-time.After(3 * time.Second): + return &TimeoutError{"timeout while trying to set secret in keyring"} + } +} + +// Get secret from keyring given service and user name. +func Get(service, user string) (string, error) { + ch := make(chan struct { + val string + err error + }, 1) + go func() { + defer close(ch) + val, err := keyring.Get(service, user) + ch <- struct { + val string + err error + }{val, err} + }() + select { + case res := <-ch: + return res.val, res.err + case <-time.After(3 * time.Second): + return "", &TimeoutError{"timeout while trying to get secret from keyring"} + } +} + +// Delete secret from keyring. +func Delete(service, user string) error { + ch := make(chan error, 1) + go func() { + defer close(ch) + ch <- keyring.Delete(service, user) + }() + select { + case err := <-ch: + return err + case <-time.After(3 * time.Second): + return &TimeoutError{"timeout while trying to delete secret from keyring"} + } +} diff --git a/internal/prompter/prompter.go b/internal/prompter/prompter.go index a9cf77992..cd90fcd2d 100644 --- a/internal/prompter/prompter.go +++ b/internal/prompter/prompter.go @@ -7,13 +7,14 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/cli/cli/v2/internal/ghinstance" + "github.com/cli/cli/v2/internal/text" "github.com/cli/cli/v2/pkg/surveyext" ) //go:generate moq -rm -out prompter_mock.go . Prompter type Prompter interface { Select(string, string, []string) (int, error) - MultiSelect(string, string, []string) ([]string, error) + MultiSelect(string, []string, []string) ([]int, error) Input(string, string) (string, error) InputHostname() (string, error) Password(string) (string, error) @@ -49,11 +50,24 @@ type surveyPrompter struct { stderr io.Writer } +// LatinMatchingFilter returns whether the value matches the input filter. +// The strings are compared normalized in case. +// The filter's diactritics are kept as-is, but the value's are normalized, +// so that a missing diactritic in the filter still returns a result. +func LatinMatchingFilter(filter, value string, index int) bool { + filter = strings.ToLower(filter) + value = strings.ToLower(value) + + // include this option if it matches. + return strings.Contains(value, filter) || strings.Contains(text.RemoveDiacritics(value), filter) +} + func (p *surveyPrompter) Select(message, defaultValue string, options []string) (result int, err error) { q := &survey.Select{ Message: message, Options: options, PageSize: 20, + Filter: LatinMatchingFilter, } if defaultValue != "" { @@ -72,15 +86,25 @@ func (p *surveyPrompter) Select(message, defaultValue string, options []string) return } -func (p *surveyPrompter) MultiSelect(message, defaultValue string, options []string) (result []string, err error) { +func (p *surveyPrompter) MultiSelect(message string, defaultValues, options []string) (result []int, err error) { q := &survey.MultiSelect{ Message: message, Options: options, PageSize: 20, + Filter: LatinMatchingFilter, } - if defaultValue != "" { - q.Default = defaultValue + if len(defaultValues) > 0 { + // TODO I don't actually know that this is needed, just being extra cautious + validatedDefault := []string{} + for _, x := range defaultValues { + for _, y := range options { + if x == y { + validatedDefault = append(validatedDefault, x) + } + } + } + q.Default = validatedDefault } err = p.ask(q, &result) diff --git a/internal/prompter/prompter_mock.go b/internal/prompter/prompter_mock.go index 859832450..9a59a7bd1 100644 --- a/internal/prompter/prompter_mock.go +++ b/internal/prompter/prompter_mock.go @@ -35,7 +35,7 @@ var _ Prompter = &PrompterMock{} // MarkdownEditorFunc: func(s1 string, s2 string, b bool) (string, error) { // panic("mock out the MarkdownEditor method") // }, -// MultiSelectFunc: func(s1 string, s2 string, strings []string) ([]string, error) { +// MultiSelectFunc: func(s string, strings1 []string, strings2 []string) ([]int, error) { // panic("mock out the MultiSelect method") // }, // PasswordFunc: func(s string) (string, error) { @@ -70,7 +70,7 @@ type PrompterMock struct { MarkdownEditorFunc func(s1 string, s2 string, b bool) (string, error) // MultiSelectFunc mocks the MultiSelect method. - MultiSelectFunc func(s1 string, s2 string, strings []string) ([]string, error) + MultiSelectFunc func(s string, strings1 []string, strings2 []string) ([]int, error) // PasswordFunc mocks the Password method. PasswordFunc func(s string) (string, error) @@ -116,12 +116,12 @@ type PrompterMock struct { } // MultiSelect holds details about calls to the MultiSelect method. MultiSelect []struct { - // S1 is the s1 argument value. - S1 string - // S2 is the s2 argument value. - S2 string - // Strings is the strings argument value. - Strings []string + // S is the s argument value. + S string + // Strings1 is the strings1 argument value. + Strings1 []string + // Strings2 is the strings2 argument value. + Strings2 []string } // Password holds details about calls to the Password method. Password []struct { @@ -348,23 +348,23 @@ func (mock *PrompterMock) MarkdownEditorCalls() []struct { } // MultiSelect calls MultiSelectFunc. -func (mock *PrompterMock) MultiSelect(s1 string, s2 string, strings []string) ([]string, error) { +func (mock *PrompterMock) MultiSelect(s string, strings1 []string, strings2 []string) ([]int, error) { if mock.MultiSelectFunc == nil { panic("PrompterMock.MultiSelectFunc: method is nil but Prompter.MultiSelect was just called") } callInfo := struct { - S1 string - S2 string - Strings []string + S string + Strings1 []string + Strings2 []string }{ - S1: s1, - S2: s2, - Strings: strings, + S: s, + Strings1: strings1, + Strings2: strings2, } mock.lockMultiSelect.Lock() mock.calls.MultiSelect = append(mock.calls.MultiSelect, callInfo) mock.lockMultiSelect.Unlock() - return mock.MultiSelectFunc(s1, s2, strings) + return mock.MultiSelectFunc(s, strings1, strings2) } // MultiSelectCalls gets all the calls that were made to MultiSelect. @@ -372,14 +372,14 @@ func (mock *PrompterMock) MultiSelect(s1 string, s2 string, strings []string) ([ // // len(mockedPrompter.MultiSelectCalls()) func (mock *PrompterMock) MultiSelectCalls() []struct { - S1 string - S2 string - Strings []string + S string + Strings1 []string + Strings2 []string } { var calls []struct { - S1 string - S2 string - Strings []string + S string + Strings1 []string + Strings2 []string } mock.lockMultiSelect.RLock() calls = mock.calls.MultiSelect diff --git a/internal/prompter/prompter_test.go b/internal/prompter/prompter_test.go new file mode 100644 index 000000000..98d78786b --- /dev/null +++ b/internal/prompter/prompter_test.go @@ -0,0 +1,78 @@ +package prompter + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFilterDiacritics(t *testing.T) { + tests := []struct { + name string + filter string + value string + want bool + }{ + { + name: "exact match no diacritics", + filter: "Mikelis", + value: "Mikelis", + want: true, + }, + { + name: "exact match no diacritics", + filter: "Mikelis", + value: "Mikelis", + want: true, + }, + { + name: "exact match diacritics", + filter: "Miķelis", + value: "Miķelis", + want: true, + }, + { + name: "partial match diacritics", + filter: "Miķe", + value: "Miķelis", + want: true, + }, + { + name: "exact match diacritics in value", + filter: "Mikelis", + value: "Miķelis", + want: true, + }, + { + name: "partial match diacritics in filter", + filter: "Miķe", + value: "Miķelis", + want: true, + }, + { + name: "no match when removing diacritics in filter", + filter: "Mielis", + value: "Mikelis", + want: false, + }, + { + name: "no match when removing diacritics in value", + filter: "Mikelis", + value: "Mielis", + want: false, + }, + { + name: "no match diacritics in filter", + filter: "Miķelis", + value: "Mikelis", + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, LatinMatchingFilter(tt.filter, tt.value, 0), tt.want) + }) + } + +} diff --git a/internal/prompter/test.go b/internal/prompter/test.go index a2d0b83ee..c376e9c83 100644 --- a/internal/prompter/test.go +++ b/internal/prompter/test.go @@ -50,7 +50,7 @@ type ConfirmStub struct { type MultiSelectStub struct { Prompt string ExpectedOpts []string - Fn func(string, string, []string) ([]string, error) + Fn func(string, []string, []string) ([]int, error) } type InputHostnameStub struct { @@ -84,8 +84,6 @@ type MockPrompter struct { ConfirmDeletionStubs []ConfirmDeletionStub } -// TODO thread safety - func NewMockPrompter(t *testing.T) *MockPrompter { m := &MockPrompter{ t: t, @@ -120,17 +118,17 @@ func NewMockPrompter(t *testing.T) *MockPrompter { return s.Fn(p, d, opts) } - m.MultiSelectFunc = func(p, d string, opts []string) ([]string, error) { + m.MultiSelectFunc = func(p string, d, opts []string) ([]int, error) { var s MultiSelectStub - if len(m.SelectStubs) > 0 { + if len(m.MultiSelectStubs) > 0 { s = m.MultiSelectStubs[0] - m.SelectStubs = m.SelectStubs[1:len(m.SelectStubs)] + m.MultiSelectStubs = m.MultiSelectStubs[1:len(m.MultiSelectStubs)] } else { - return []string{}, NoSuchPromptErr(p) + return []int{}, NoSuchPromptErr(p) } if s.Prompt != p { - return []string{}, NoSuchPromptErr(p) + return []int{}, NoSuchPromptErr(p) } AssertOptions(m.t, s.ExpectedOpts, opts) @@ -240,7 +238,7 @@ func (m *MockPrompter) RegisterSelect(prompt string, opts []string, stub func(_, Fn: stub}) } -func (m *MockPrompter) RegisterMultiSelect(prompt string, opts []string, stub func(_, _ string, _ []string) ([]string, error)) { +func (m *MockPrompter) RegisterMultiSelect(prompt string, d, opts []string, stub func(_ string, _, _ []string) ([]int, error)) { m.MultiSelectStubs = append(m.MultiSelectStubs, MultiSelectStub{ Prompt: prompt, ExpectedOpts: opts, diff --git a/internal/tableprinter/table_printer.go b/internal/tableprinter/table_printer.go index 059203a6e..1cd7c7d6e 100644 --- a/internal/tableprinter/table_printer.go +++ b/internal/tableprinter/table_printer.go @@ -6,7 +6,7 @@ import ( "github.com/cli/cli/v2/internal/text" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/go-gh/pkg/tableprinter" + "github.com/cli/go-gh/v2/pkg/tableprinter" ) type TablePrinter struct { diff --git a/internal/text/text.go b/internal/text/text.go index 0671e1e74..034d2f6e6 100644 --- a/internal/text/text.go +++ b/internal/text/text.go @@ -6,10 +6,14 @@ import ( "regexp" "strings" "time" + "unicode" - "github.com/cli/go-gh/pkg/text" + "github.com/cli/go-gh/v2/pkg/text" "golang.org/x/text/cases" "golang.org/x/text/language" + "golang.org/x/text/runes" + "golang.org/x/text/transform" + "golang.org/x/text/unicode/norm" ) var whitespaceRE = regexp.MustCompile(`\s+`) @@ -72,3 +76,19 @@ func DisplayURL(urlStr string) string { } return u.Hostname() + u.Path } + +// RemoveDiacritics returns the input value without "diacritics", or accent marks +func RemoveDiacritics(value string) string { + // Mn = "Mark, nonspacing" unicode character category + removeMnTransfomer := runes.Remove(runes.In(unicode.Mn)) + + // 1/ Decompose the text into characters and diacritical marks, + // 2/ Remove the diacriticals marks + // 3/ Recompose the text + t := transform.Chain(norm.NFD, removeMnTransfomer, norm.NFC) + normalized, _, err := transform.String(t, value) + if err != nil { + return value + } + return normalized +} diff --git a/internal/text/text_test.go b/internal/text/text_test.go index 98f16da5d..794d2327b 100644 --- a/internal/text/text_test.go +++ b/internal/text/text_test.go @@ -54,3 +54,57 @@ func TestFuzzyAgoAbbr(t *testing.T) { assert.Equal(t, expected, fuzzy) } } + +func TestRemoveDiacritics(t *testing.T) { + tests := [][]string{ + // no diacritics + {"e", "e"}, + {"و", "و"}, + {"И", "И"}, + {"ж", "ж"}, + {"私", "私"}, + {"万", "万"}, + + // diacritics test sets + {"à", "a"}, + {"é", "e"}, + {"è", "e"}, + {"ô", "o"}, + {"ᾳ", "α"}, + {"εͅ", "ε"}, + {"ῃ", "η"}, + {"ιͅ", "ι"}, + + {"ؤ", "و"}, + + {"ā", "a"}, + {"č", "c"}, + {"ģ", "g"}, + {"ķ", "k"}, + {"ņ", "n"}, + {"š", "s"}, + {"ž", "z"}, + + {"ŵ", "w"}, + {"ŷ", "y"}, + {"ä", "a"}, + {"ÿ", "y"}, + {"á", "a"}, + {"ẁ", "w"}, + {"ỳ", "y"}, + {"ō", "o"}, + + // full words + {"Miķelis", "Mikelis"}, + {"François", "Francois"}, + {"žluťoučký", "zlutoucky"}, + {"învățătorița", "invatatorita"}, + {"Kękę przy łóżku", "Keke przy łozku"}, + } + + for _, tt := range tests { + t.Run(RemoveDiacritics(tt[0]), func(t *testing.T) { + assert.Equal(t, tt[1], RemoveDiacritics(tt[0])) + }) + } +} diff --git a/pkg/cmd/actions/actions.go b/pkg/cmd/actions/actions.go index d24df6631..fcf33f447 100644 --- a/pkg/cmd/actions/actions.go +++ b/pkg/cmd/actions/actions.go @@ -42,7 +42,7 @@ func actionsExplainer(cs *iostreams.ColorScheme) string { To see more help, run 'gh help run ' %s - gh workflow list: List all the workflow files in your repository + gh workflow list: List workflow files in your repository gh workflow view: View details for a workflow file gh workflow enable: Enable a workflow file gh workflow disable: Disable a workflow file diff --git a/pkg/cmd/alias/alias.go b/pkg/cmd/alias/alias.go index 46d7e2bc8..713e7d1db 100644 --- a/pkg/cmd/alias/alias.go +++ b/pkg/cmd/alias/alias.go @@ -3,6 +3,7 @@ package alias import ( "github.com/MakeNowJust/heredoc" deleteCmd "github.com/cli/cli/v2/pkg/cmd/alias/delete" + importCmd "github.com/cli/cli/v2/pkg/cmd/alias/imports" listCmd "github.com/cli/cli/v2/pkg/cmd/alias/list" setCmd "github.com/cli/cli/v2/pkg/cmd/alias/set" "github.com/cli/cli/v2/pkg/cmdutil" @@ -23,6 +24,7 @@ func NewCmdAlias(f *cmdutil.Factory) *cobra.Command { cmdutil.DisableAuthCheck(cmd) cmd.AddCommand(deleteCmd.NewCmdDelete(f, nil)) + cmd.AddCommand(importCmd.NewCmdImport(f, nil)) cmd.AddCommand(listCmd.NewCmdList(f, nil)) cmd.AddCommand(setCmd.NewCmdSet(f, nil)) diff --git a/pkg/cmd/alias/expand/expand.go b/pkg/cmd/alias/expand/expand.go deleted file mode 100644 index d0e2627fb..000000000 --- a/pkg/cmd/alias/expand/expand.go +++ /dev/null @@ -1,90 +0,0 @@ -package expand - -import ( - "errors" - "fmt" - "os/exec" - "regexp" - "runtime" - "strings" - - "github.com/cli/cli/v2/internal/config" - "github.com/cli/cli/v2/pkg/findsh" - "github.com/google/shlex" -) - -// ExpandAlias processes argv to see if it should be rewritten according to a user's aliases. The -// second return value indicates whether the alias should be executed in a new shell process instead -// of running gh itself. -func ExpandAlias(cfg config.Config, args []string, findShFunc func() (string, error)) (expanded []string, isShell bool, err error) { - if len(args) < 2 { - // the command is lacking a subcommand - return - } - expanded = args[1:] - - aliases := cfg.Aliases() - - expansion, getErr := aliases.Get(args[1]) - if getErr != nil { - return - } - - if strings.HasPrefix(expansion, "!") { - isShell = true - if findShFunc == nil { - findShFunc = findSh - } - shPath, shErr := findShFunc() - if shErr != nil { - err = shErr - return - } - - expanded = []string{shPath, "-c", expansion[1:]} - - if len(args[2:]) > 0 { - expanded = append(expanded, "--") - expanded = append(expanded, args[2:]...) - } - - return - } - - extraArgs := []string{} - for i, a := range args[2:] { - if !strings.Contains(expansion, "$") { - extraArgs = append(extraArgs, a) - } else { - expansion = strings.ReplaceAll(expansion, fmt.Sprintf("$%d", i+1), a) - } - } - lingeringRE := regexp.MustCompile(`\$\d`) - if lingeringRE.MatchString(expansion) { - err = fmt.Errorf("not enough arguments for alias: %s", expansion) - return - } - - var newArgs []string - newArgs, err = shlex.Split(expansion) - if err != nil { - return - } - - expanded = append(newArgs, extraArgs...) - return -} - -func findSh() (string, error) { - shPath, err := findsh.Find() - if err != nil { - if errors.Is(err, exec.ErrNotFound) { - if runtime.GOOS == "windows" { - return "", errors.New("unable to locate sh to execute the shell alias with. The sh.exe interpreter is typically distributed with Git for Windows.") - } - return "", errors.New("unable to locate sh to execute shell alias with") - } - return "", err - } - return shPath, nil -} diff --git a/pkg/cmd/alias/expand/expand_test.go b/pkg/cmd/alias/expand/expand_test.go deleted file mode 100644 index 33af4b073..000000000 --- a/pkg/cmd/alias/expand/expand_test.go +++ /dev/null @@ -1,185 +0,0 @@ -package expand - -import ( - "errors" - "reflect" - "testing" - - "github.com/MakeNowJust/heredoc" - "github.com/cli/cli/v2/internal/config" -) - -func TestExpandAlias(t *testing.T) { - findShFunc := func() (string, error) { - return "/usr/bin/sh", nil - } - - cfg := config.NewFromString(heredoc.Doc(` - aliases: - co: pr checkout - il: issue list --author="$1" --label="$2" - ia: issue list --author="$1" --assignee="$1" - `)) - - type args struct { - config config.Config - argv []string - } - tests := []struct { - name string - args args - wantExpanded []string - wantIsShell bool - wantErr error - }{ - { - name: "no arguments", - args: args{ - config: cfg, - argv: []string{}, - }, - wantExpanded: []string(nil), - wantIsShell: false, - wantErr: nil, - }, - { - name: "too few arguments", - args: args{ - config: cfg, - argv: []string{"gh"}, - }, - wantExpanded: []string(nil), - wantIsShell: false, - wantErr: nil, - }, - { - name: "no expansion", - args: args{ - config: cfg, - argv: []string{"gh", "pr", "status"}, - }, - wantExpanded: []string{"pr", "status"}, - wantIsShell: false, - wantErr: nil, - }, - { - name: "simple expansion", - args: args{ - config: cfg, - argv: []string{"gh", "co"}, - }, - wantExpanded: []string{"pr", "checkout"}, - wantIsShell: false, - wantErr: nil, - }, - { - name: "adding arguments after expansion", - args: args{ - config: cfg, - argv: []string{"gh", "co", "123"}, - }, - wantExpanded: []string{"pr", "checkout", "123"}, - wantIsShell: false, - wantErr: nil, - }, - { - name: "not enough arguments for expansion", - args: args{ - config: cfg, - argv: []string{"gh", "il"}, - }, - wantExpanded: []string{}, - wantIsShell: false, - wantErr: errors.New(`not enough arguments for alias: issue list --author="$1" --label="$2"`), - }, - { - name: "not enough arguments for expansion 2", - args: args{ - config: cfg, - argv: []string{"gh", "il", "vilmibm"}, - }, - wantExpanded: []string{}, - wantIsShell: false, - wantErr: errors.New(`not enough arguments for alias: issue list --author="vilmibm" --label="$2"`), - }, - { - name: "satisfy expansion arguments", - args: args{ - config: cfg, - argv: []string{"gh", "il", "vilmibm", "help wanted"}, - }, - wantExpanded: []string{"issue", "list", "--author=vilmibm", "--label=help wanted"}, - wantIsShell: false, - wantErr: nil, - }, - { - name: "mixed positional and non-positional arguments", - args: args{ - config: cfg, - argv: []string{"gh", "il", "vilmibm", "epic", "-R", "monalisa/testing"}, - }, - wantExpanded: []string{"issue", "list", "--author=vilmibm", "--label=epic", "-R", "monalisa/testing"}, - wantIsShell: false, - wantErr: nil, - }, - { - name: "dollar in expansion", - args: args{ - config: cfg, - argv: []string{"gh", "ia", "$coolmoney$"}, - }, - wantExpanded: []string{"issue", "list", "--author=$coolmoney$", "--assignee=$coolmoney$"}, - wantIsShell: false, - wantErr: nil, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - gotExpanded, gotIsShell, err := ExpandAlias(tt.args.config, tt.args.argv, findShFunc) - if tt.wantErr != nil { - if err == nil { - t.Fatal("expected error") - } - if tt.wantErr.Error() != err.Error() { - t.Fatalf("expected error %q, got %q", tt.wantErr, err) - } - return - } - if err != nil { - t.Fatalf("got error: %v", err) - } - if !reflect.DeepEqual(gotExpanded, tt.wantExpanded) { - t.Errorf("ExpandAlias() gotExpanded = %v, want %v", gotExpanded, tt.wantExpanded) - } - if gotIsShell != tt.wantIsShell { - t.Errorf("ExpandAlias() gotIsShell = %v, want %v", gotIsShell, tt.wantIsShell) - } - }) - } -} - -// cfg := `--- -// aliases: -// co: pr checkout -// il: issue list --author="$1" --label="$2" -// ia: issue list --author="$1" --assignee="$1" -// ` -// initBlankContext(cfg, "OWNER/REPO", "trunk") -// for _, c := range []struct { -// Args string -// ExpectedArgs []string -// Err string -// }{ -// {"gh co", []string{"pr", "checkout"}, ""}, -// {"gh il", nil, `not enough arguments for alias: issue list --author="$1" --label="$2"`}, -// {"gh il vilmibm", nil, `not enough arguments for alias: issue list --author="vilmibm" --label="$2"`}, -// {"gh co 123", []string{"pr", "checkout", "123"}, ""}, -// {"gh il vilmibm epic", []string{"issue", "list", `--author=vilmibm`, `--label=epic`}, ""}, -// {"gh ia vilmibm", []string{"issue", "list", `--author=vilmibm`, `--assignee=vilmibm`}, ""}, -// {"gh ia $coolmoney$", []string{"issue", "list", `--author=$coolmoney$`, `--assignee=$coolmoney$`}, ""}, -// {"gh pr status", []string{"pr", "status"}, ""}, -// {"gh il vilmibm epic -R vilmibm/testing", []string{"issue", "list", "--author=vilmibm", "--label=epic", "-R", "vilmibm/testing"}, ""}, -// {"gh dne", []string{"dne"}, ""}, -// {"gh", []string{}, ""}, -// {"", []string{}, ""}, -// } { diff --git a/pkg/cmd/alias/imports/import.go b/pkg/cmd/alias/imports/import.go new file mode 100644 index 000000000..ef2176d45 --- /dev/null +++ b/pkg/cmd/alias/imports/import.go @@ -0,0 +1,201 @@ +package imports + +import ( + "fmt" + "sort" + "strings" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/pkg/cmd/alias/shared" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/spf13/cobra" + "gopkg.in/yaml.v3" +) + +type ImportOptions struct { + Config func() (config.Config, error) + IO *iostreams.IOStreams + + Filename string + OverwriteExisting bool + + validAliasName func(string) bool + validAliasExpansion func(string) bool +} + +func NewCmdImport(f *cmdutil.Factory, runF func(*ImportOptions) error) *cobra.Command { + opts := &ImportOptions{ + IO: f.IOStreams, + Config: f.Config, + } + + cmd := &cobra.Command{ + Use: "import [ | -]", + Short: "Import aliases from a YAML file", + Long: heredoc.Doc(` + Import aliases from the contents of a YAML file. + + Aliases should be defined as a map in YAML, where the keys represent aliases and + the values represent the corresponding expansions. An example file should look like + the following: + + bugs: issue list --label=bug + igrep: '!gh issue list --label="$1" | grep "$2"' + features: |- + issue list + --label=enhancement + + Use "-" to read aliases (in YAML format) from standard input. + + The output from the gh command "alias list" can be used to produce a YAML file + containing your aliases, which you can use to import them from one machine to + another. Run "gh help alias list" to learn more. + `), + Example: heredoc.Doc(` + # Import aliases from a file + $ gh alias import aliases.yml + + # Import aliases from standard input + $ gh alias import - + `), + Args: func(cmd *cobra.Command, args []string) error { + if len(args) > 1 { + return cmdutil.FlagErrorf("too many arguments") + } + if len(args) == 0 && opts.IO.IsStdinTTY() { + return cmdutil.FlagErrorf("no filename passed and nothing on STDIN") + } + return nil + }, + RunE: func(cmd *cobra.Command, args []string) error { + opts.Filename = "-" + if len(args) > 0 { + opts.Filename = args[0] + } + + opts.validAliasName = shared.ValidAliasNameFunc(cmd) + opts.validAliasExpansion = shared.ValidAliasExpansionFunc(cmd) + + if runF != nil { + return runF(opts) + } + + return importRun(opts) + }, + } + + cmd.Flags().BoolVar(&opts.OverwriteExisting, "clobber", false, "Overwrite existing aliases of the same name") + + return cmd +} + +func importRun(opts *ImportOptions) error { + cs := opts.IO.ColorScheme() + cfg, err := opts.Config() + if err != nil { + return err + } + + aliasCfg := cfg.Aliases() + + b, err := cmdutil.ReadFile(opts.Filename, opts.IO.In) + if err != nil { + return err + } + + aliasMap := map[string]string{} + if err = yaml.Unmarshal(b, &aliasMap); err != nil { + return err + } + + isTerminal := opts.IO.IsStdoutTTY() + if isTerminal { + if opts.Filename == "-" { + fmt.Fprintf(opts.IO.ErrOut, "- Importing aliases from standard input\n") + } else { + fmt.Fprintf(opts.IO.ErrOut, "- Importing aliases from file %q\n", opts.Filename) + } + } + + var msg strings.Builder + + for _, alias := range getSortedKeys(aliasMap) { + var existingAlias bool + if _, err := aliasCfg.Get(alias); err == nil { + existingAlias = true + } + + if !opts.validAliasName(alias) { + if !existingAlias { + msg.WriteString( + fmt.Sprintf("%s Could not import alias %s: already a gh command or extension\n", + cs.FailureIcon(), + cs.Bold(alias), + ), + ) + continue + } + + if existingAlias && !opts.OverwriteExisting { + msg.WriteString( + fmt.Sprintf("%s Could not import alias %s: name already taken\n", + cs.FailureIcon(), + cs.Bold(alias), + ), + ) + continue + } + } + + expansion := aliasMap[alias] + + if !opts.validAliasExpansion(expansion) { + msg.WriteString( + fmt.Sprintf("%s Could not import alias %s: expansion does not correspond to a gh command, extension, or alias\n", + cs.FailureIcon(), + cs.Bold(alias), + ), + ) + continue + } + + aliasCfg.Add(alias, expansion) + + if existingAlias && opts.OverwriteExisting { + msg.WriteString( + fmt.Sprintf("%s Changed alias %s\n", + cs.WarningIcon(), + cs.Bold(alias), + ), + ) + } else { + msg.WriteString( + fmt.Sprintf("%s Added alias %s\n", + cs.SuccessIcon(), + cs.Bold(alias), + ), + ) + } + } + + if err := cfg.Write(); err != nil { + return err + } + + if isTerminal { + fmt.Fprintln(opts.IO.ErrOut, msg.String()) + } + + return nil +} + +func getSortedKeys(m map[string]string) []string { + keys := []string{} + for key := range m { + keys = append(keys, key) + } + sort.Strings(keys) + return keys +} diff --git a/pkg/cmd/alias/imports/import_test.go b/pkg/cmd/alias/imports/import_test.go new file mode 100644 index 000000000..024f52f02 --- /dev/null +++ b/pkg/cmd/alias/imports/import_test.go @@ -0,0 +1,349 @@ +package imports + +import ( + "bytes" + "fmt" + "io" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/pkg/cmd/alias/shared" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNewCmdImport(t *testing.T) { + tests := []struct { + name string + cli string + tty bool + wants ImportOptions + wantsError string + }{ + { + name: "no filename and stdin tty", + cli: "", + tty: true, + wants: ImportOptions{ + Filename: "", + OverwriteExisting: false, + }, + wantsError: "no filename passed and nothing on STDIN", + }, + { + name: "no filename and stdin is not tty", + cli: "", + tty: false, + wants: ImportOptions{ + Filename: "-", + OverwriteExisting: false, + }, + }, + { + name: "stdin arg", + cli: "-", + wants: ImportOptions{ + Filename: "-", + OverwriteExisting: false, + }, + }, + { + name: "multiple filenames", + cli: "aliases1 aliases2", + wants: ImportOptions{ + Filename: "aliases1 aliases2", + OverwriteExisting: false, + }, + wantsError: "too many arguments", + }, + { + name: "clobber flag", + cli: "aliases --clobber", + wants: ImportOptions{ + Filename: "aliases", + OverwriteExisting: true, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + ios.SetStdinTTY(tt.tty) + f := &cmdutil.Factory{IOStreams: ios} + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts *ImportOptions + cmd := NewCmdImport(f, func(opts *ImportOptions) error { + gotOpts = opts + return nil + }) + cmd.SetArgs(argv) + cmd.SetIn(&bytes.Buffer{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + + _, err = cmd.ExecuteC() + if tt.wantsError != "" { + assert.EqualError(t, err, tt.wantsError) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.Filename, gotOpts.Filename) + assert.Equal(t, tt.wants.OverwriteExisting, gotOpts.OverwriteExisting) + }) + } +} + +func TestImportRun(t *testing.T) { + tmpFile := filepath.Join(t.TempDir(), "aliases.yml") + importFileMsg := fmt.Sprintf(`- Importing aliases from file %q`, tmpFile) + importStdinMsg := "- Importing aliases from standard input" + + tests := []struct { + name string + opts *ImportOptions + stdin string + fileContents string + initConfig string + aliasCommands []*cobra.Command + wantConfig string + wantStderr string + }{ + { + name: "with no existing aliases", + opts: &ImportOptions{ + Filename: tmpFile, + OverwriteExisting: false, + }, + fileContents: heredoc.Doc(` + co: pr checkout + igrep: '!gh issue list --label="$1" | grep "$2"' + `), + wantConfig: heredoc.Doc(` + aliases: + co: pr checkout + igrep: '!gh issue list --label="$1" | grep "$2"' + `), + wantStderr: importFileMsg + "\n✓ Added alias co\n✓ Added alias igrep\n\n", + }, + { + name: "with existing aliases", + opts: &ImportOptions{ + Filename: tmpFile, + OverwriteExisting: false, + }, + fileContents: heredoc.Doc(` + users: |- + api graphql -F name="$1" -f query=' + query ($name: String!) { + user(login: $name) { + name + } + }' + co: pr checkout + `), + initConfig: heredoc.Doc(` + aliases: + igrep: '!gh issue list --label="$1" | grep "$2"' + editor: vim + `), + aliasCommands: []*cobra.Command{ + {Use: "igrep"}, + }, + wantConfig: heredoc.Doc(` + aliases: + igrep: '!gh issue list --label="$1" | grep "$2"' + co: pr checkout + users: |- + api graphql -F name="$1" -f query=' + query ($name: String!) { + user(login: $name) { + name + } + }' + editor: vim + `), + wantStderr: importFileMsg + "\n✓ Added alias co\n✓ Added alias users\n\n", + }, + { + name: "from stdin", + opts: &ImportOptions{ + Filename: "-", + OverwriteExisting: false, + }, + stdin: heredoc.Doc(` + co: pr checkout + features: |- + issue list + --label=enhancement + igrep: '!gh issue list --label="$1" | grep "$2"' + `), + wantConfig: heredoc.Doc(` + aliases: + co: pr checkout + features: |- + issue list + --label=enhancement + igrep: '!gh issue list --label="$1" | grep "$2"' + `), + wantStderr: importStdinMsg + "\n✓ Added alias co\n✓ Added alias features\n✓ Added alias igrep\n\n", + }, + { + name: "already taken aliases", + opts: &ImportOptions{ + Filename: tmpFile, + OverwriteExisting: false, + }, + fileContents: heredoc.Doc(` + co: pr checkout -R cool/repo + igrep: '!gh issue list --label="$1" | grep "$2"' + `), + initConfig: heredoc.Doc(` + aliases: + co: pr checkout + editor: vim + `), + aliasCommands: []*cobra.Command{ + {Use: "co"}, + }, + wantConfig: heredoc.Doc(` + aliases: + co: pr checkout + igrep: '!gh issue list --label="$1" | grep "$2"' + editor: vim + `), + wantStderr: importFileMsg + "\nX Could not import alias co: name already taken\n✓ Added alias igrep\n\n", + }, + { + name: "override aliases", + opts: &ImportOptions{ + Filename: tmpFile, + OverwriteExisting: true, + }, + fileContents: heredoc.Doc(` + co: pr checkout -R cool/repo + igrep: '!gh issue list --label="$1" | grep "$2"' + `), + initConfig: heredoc.Doc(` + aliases: + co: pr checkout + editor: vim + `), + aliasCommands: []*cobra.Command{ + {Use: "co"}, + }, + wantConfig: heredoc.Doc(` + aliases: + co: pr checkout -R cool/repo + igrep: '!gh issue list --label="$1" | grep "$2"' + editor: vim + `), + wantStderr: importFileMsg + "\n! Changed alias co\n✓ Added alias igrep\n\n", + }, + { + name: "alias is a gh command", + opts: &ImportOptions{ + Filename: tmpFile, + OverwriteExisting: false, + }, + fileContents: heredoc.Doc(` + pr: pr checkout + issue: issue list + api: api graphql + `), + wantStderr: strings.Join( + []string{ + importFileMsg, + "X Could not import alias api: already a gh command or extension", + "X Could not import alias issue: already a gh command or extension", + "X Could not import alias pr: already a gh command or extension\n\n", + }, + "\n", + ), + }, + { + name: "invalid expansion", + opts: &ImportOptions{ + Filename: tmpFile, + OverwriteExisting: false, + }, + fileContents: heredoc.Doc(` + alias1: + alias2: ps checkout + `), + wantStderr: strings.Join( + []string{ + importFileMsg, + "X Could not import alias alias1: expansion does not correspond to a gh command, extension, or alias", + "X Could not import alias alias2: expansion does not correspond to a gh command, extension, or alias\n\n", + }, + "\n", + ), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.fileContents != "" { + err := os.WriteFile(tmpFile, []byte(tt.fileContents), 0600) + require.NoError(t, err) + } + + ios, stdin, _, stderr := iostreams.Test() + ios.SetStdinTTY(true) + ios.SetStdoutTTY(true) + ios.SetStderrTTY(true) + tt.opts.IO = ios + + readConfigs := config.StubWriteConfig(t) + cfg := config.NewFromString(tt.initConfig) + tt.opts.Config = func() (config.Config, error) { + return cfg, nil + } + + // Create fake command structure for testing. + rootCmd := &cobra.Command{} + prCmd := &cobra.Command{Use: "pr"} + prCmd.AddCommand(&cobra.Command{Use: "checkout"}) + prCmd.AddCommand(&cobra.Command{Use: "status"}) + rootCmd.AddCommand(prCmd) + issueCmd := &cobra.Command{Use: "issue"} + issueCmd.AddCommand(&cobra.Command{Use: "list"}) + rootCmd.AddCommand(issueCmd) + apiCmd := &cobra.Command{Use: "api"} + apiCmd.AddCommand(&cobra.Command{Use: "graphql"}) + rootCmd.AddCommand(apiCmd) + for _, cmd := range tt.aliasCommands { + rootCmd.AddCommand(cmd) + } + + tt.opts.validAliasName = shared.ValidAliasNameFunc(rootCmd) + tt.opts.validAliasExpansion = shared.ValidAliasExpansionFunc(rootCmd) + + if tt.stdin != "" { + stdin.WriteString(tt.stdin) + } + + err := importRun(tt.opts) + require.NoError(t, err) + + configOut := bytes.Buffer{} + readConfigs(&configOut, io.Discard) + + assert.Equal(t, tt.wantStderr, stderr.String()) + assert.Equal(t, tt.wantConfig, configOut.String()) + }) + } +} diff --git a/pkg/cmd/alias/set/set.go b/pkg/cmd/alias/set/set.go index 6d09787a4..20c86c473 100644 --- a/pkg/cmd/alias/set/set.go +++ b/pkg/cmd/alias/set/set.go @@ -7,9 +7,9 @@ import ( "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/pkg/cmd/alias/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/google/shlex" "github.com/spf13/cobra" ) @@ -21,7 +21,8 @@ type SetOptions struct { Expansion string IsShell bool - validCommand func(string) bool + validAliasName func(string) bool + validAliasExpansion func(string) bool } func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command { @@ -59,6 +60,9 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command $ gh alias set homework 'issue list --assignee @me' $ gh homework + $ gh alias set 'issue mine' 'issue list --mention @me' + $ gh issue mine + $ gh alias set epicsBy 'issue list --author="$1" --label="epic"' $ gh epicsBy vilmibm #=> gh issue list --author="vilmibm" --label="epic" @@ -70,29 +74,13 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command opts.Name = args[0] opts.Expansion = args[1] - opts.validCommand = func(args string) bool { - split, err := shlex.Split(args) - if err != nil { - return false - } - - rootCmd := cmd.Root() - cmd, _, err := rootCmd.Traverse(split) - if err == nil && cmd != rootCmd { - return true - } - - for _, ext := range f.ExtensionManager.List() { - if ext.Name() == split[0] { - return true - } - } - return false - } + opts.validAliasName = shared.ValidAliasNameFunc(cmd) + opts.validAliasExpansion = shared.ValidAliasExpansionFunc(cmd) if runF != nil { return runF(opts) } + return setRun(opts) }, } @@ -121,18 +109,16 @@ func setRun(opts *SetOptions) error { fmt.Fprintf(opts.IO.ErrOut, "- Adding alias for %s: %s\n", cs.Bold(opts.Name), cs.Bold(expansion)) } - isShell := opts.IsShell - if isShell && !strings.HasPrefix(expansion, "!") { + if opts.IsShell && !strings.HasPrefix(expansion, "!") { expansion = "!" + expansion } - isShell = strings.HasPrefix(expansion, "!") - if opts.validCommand(opts.Name) { - return fmt.Errorf("could not create alias: %q is already a gh command", opts.Name) + if !opts.validAliasName(opts.Name) { + return fmt.Errorf("could not create alias: %q is already a gh command, extension, or alias", opts.Name) } - if !isShell && !opts.validCommand(expansion) { - return fmt.Errorf("could not create alias: %s does not correspond to a gh command", expansion) + if !opts.validAliasExpansion(expansion) { + return fmt.Errorf("could not create alias: %s does not correspond to a gh command, extension, or alias", expansion) } successMsg := fmt.Sprintf("%s Added alias.", cs.SuccessIcon()) diff --git a/pkg/cmd/alias/set/set_test.go b/pkg/cmd/alias/set/set_test.go index 72d6fb413..54fcabf5b 100644 --- a/pkg/cmd/alias/set/set_test.go +++ b/pkg/cmd/alias/set/set_test.go @@ -38,7 +38,7 @@ func runCommand(cfg config.Config, isTTY bool, cli string, in string) (*test.Cmd cmd := NewCmdSet(factory, nil) - // fake command nesting structure needed for validCommand + // Create fake command structure for testing. rootCmd := &cobra.Command{} rootCmd.AddCommand(cmd) prCmd := &cobra.Command{Use: "pr"} @@ -73,7 +73,7 @@ func TestAliasSet_gh_command(t *testing.T) { cfg := config.NewFromString(``) _, err := runCommand(cfg, true, "pr 'pr status'", "") - assert.EqualError(t, err, `could not create alias: "pr" is already a gh command`) + assert.EqualError(t, err, `could not create alias: "pr" is already a gh command, extension, or alias`) } func TestAliasSet_empty_aliases(t *testing.T) { @@ -231,7 +231,7 @@ func TestAliasSet_invalid_command(t *testing.T) { cfg := config.NewFromString(``) _, err := runCommand(cfg, true, "co 'pe checkout'", "") - assert.EqualError(t, err, "could not create alias: pe checkout does not correspond to a gh command") + assert.EqualError(t, err, "could not create alias: pe checkout does not correspond to a gh command, extension, or alias") } func TestShellAlias_flag(t *testing.T) { diff --git a/pkg/cmd/alias/shared/validations.go b/pkg/cmd/alias/shared/validations.go new file mode 100644 index 000000000..9a213ab98 --- /dev/null +++ b/pkg/cmd/alias/shared/validations.go @@ -0,0 +1,51 @@ +package shared + +import ( + "strings" + + "github.com/google/shlex" + "github.com/spf13/cobra" +) + +// ValidAliasNameFunc returns a function that will check if the given string +// is a valid alias name. A name is valid if: +// - it does not shadow an existing command, +// - it is not nested under a command that is runnable, +// - it is not nested under a command that does not exist. +func ValidAliasNameFunc(cmd *cobra.Command) func(string) bool { + return func(args string) bool { + split, err := shlex.Split(args) + if err != nil || len(split) == 0 { + return false + } + + rootCmd := cmd.Root() + foundCmd, foundArgs, _ := rootCmd.Find(split) + if foundCmd != nil && !foundCmd.Runnable() && len(foundArgs) == 1 { + return true + } + + return false + } +} + +// ValidAliasExpansionFunc returns a function that will check if the given string +// is a valid alias expansion. An expansion is valid if: +// - it is a shell expansion, +// - it is a non-shell expansion that corresponds to an existing command, extension, or alias. +func ValidAliasExpansionFunc(cmd *cobra.Command) func(string) bool { + return func(expansion string) bool { + if strings.HasPrefix(expansion, "!") { + return true + } + + split, err := shlex.Split(expansion) + if err != nil || len(split) == 0 { + return false + } + + rootCmd := cmd.Root() + cmd, _, _ = rootCmd.Find(split) + return cmd != rootCmd + } +} diff --git a/pkg/cmd/alias/shared/validations_test.go b/pkg/cmd/alias/shared/validations_test.go new file mode 100644 index 000000000..72270a608 --- /dev/null +++ b/pkg/cmd/alias/shared/validations_test.go @@ -0,0 +1,54 @@ +package shared + +import ( + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" +) + +func TestValidAliasNameFunc(t *testing.T) { + // Create fake command structure for testing. + issueCmd := &cobra.Command{Use: "issue"} + prCmd := &cobra.Command{Use: "pr"} + prCmd.AddCommand(&cobra.Command{Use: "checkout"}) + + cmd := &cobra.Command{} + cmd.AddCommand(prCmd) + cmd.AddCommand(issueCmd) + + f := ValidAliasNameFunc(cmd) + + assert.False(t, f("pr")) + assert.False(t, f("pr checkout")) + assert.False(t, f("issue")) + assert.False(t, f("repo list")) + + assert.True(t, f("ps")) + assert.True(t, f("checkout")) + assert.True(t, f("issue erase")) + assert.True(t, f("pr erase")) + assert.True(t, f("pr checkout branch")) +} + +func TestValidAliasExpansionFunc(t *testing.T) { + // Create fake command structure for testing. + issueCmd := &cobra.Command{Use: "issue"} + prCmd := &cobra.Command{Use: "pr"} + prCmd.AddCommand(&cobra.Command{Use: "checkout"}) + + cmd := &cobra.Command{} + cmd.AddCommand(prCmd) + cmd.AddCommand(issueCmd) + + f := ValidAliasExpansionFunc(cmd) + + assert.False(t, f("ps")) + assert.False(t, f("checkout")) + assert.False(t, f("repo list")) + + assert.True(t, f("!git branch --show-current")) + assert.True(t, f("pr")) + assert.True(t, f("pr checkout")) + assert.True(t, f("issue")) +} diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index 1a6f84d0b..3ac83ad6b 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -3,6 +3,7 @@ package api import ( "bytes" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -23,8 +24,8 @@ import ( "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" "github.com/cli/cli/v2/pkg/jsoncolor" - "github.com/cli/go-gh/pkg/jq" - "github.com/cli/go-gh/pkg/template" + "github.com/cli/go-gh/v2/pkg/jq" + "github.com/cli/go-gh/v2/pkg/template" "github.com/spf13/cobra" ) @@ -321,6 +322,7 @@ func apiRun(opts *ApiOptions) error { return err } + isFirstPage := true hasNextPage := true for hasNextPage { resp, err := httpRequest(httpClient, host, method, requestPath, requestBody, requestHeaders) @@ -328,10 +330,16 @@ func apiRun(opts *ApiOptions) error { return err } - endCursor, err := processResponse(resp, opts, bodyWriter, headersWriter, &tmpl) + if !isGraphQL { + requestPath, hasNextPage = findNextPage(resp) + requestBody = nil // prevent repeating GET parameters + } + + endCursor, err := processResponse(resp, opts, bodyWriter, headersWriter, tmpl, isFirstPage, !hasNextPage) if err != nil { return err } + isFirstPage = false if !opts.Paginate { break @@ -342,9 +350,6 @@ func apiRun(opts *ApiOptions) error { if hasNextPage { params["endCursor"] = endCursor } - } else { - requestPath, hasNextPage = findNextPage(resp) - requestBody = nil // prevent repeating GET parameters } if hasNextPage && opts.ShowResponseHeaders { @@ -355,7 +360,7 @@ func apiRun(opts *ApiOptions) error { return tmpl.Flush() } -func processResponse(resp *http.Response, opts *ApiOptions, bodyWriter, headersWriter io.Writer, template *template.Template) (endCursor string, err error) { +func processResponse(resp *http.Response, opts *ApiOptions, bodyWriter, headersWriter io.Writer, template *template.Template, isFirstPage, isLastPage bool) (endCursor string, err error) { if opts.ShowResponseHeaders { fmt.Fprintln(headersWriter, resp.Proto, resp.Status) printHeaders(headersWriter, resp.Header, opts.IO.ColorEnabled()) @@ -387,7 +392,11 @@ func processResponse(resp *http.Response, opts *ApiOptions, bodyWriter, headersW if opts.FilterOutput != "" && serverError == "" { // TODO: reuse parsed query across pagination invocations - err = jq.Evaluate(responseBody, bodyWriter, opts.FilterOutput) + indent := "" + if opts.IO.IsStdoutTTY() { + indent = " " + } + err = jq.EvaluateFormatted(responseBody, bodyWriter, opts.FilterOutput, indent, opts.IO.ColorEnabled()) if err != nil { return } @@ -399,6 +408,13 @@ func processResponse(resp *http.Response, opts *ApiOptions, bodyWriter, headersW } else if isJSON && opts.IO.ColorEnabled() { err = jsoncolor.Write(bodyWriter, responseBody, " ") } else { + if isJSON && opts.Paginate && !isGraphQLPaginate && !opts.ShowResponseHeaders { + responseBody = &paginatedArrayReader{ + Reader: responseBody, + isFirstPage: isFirstPage, + isLastPage: isLastPage, + } + } _, err = io.Copy(bodyWriter, responseBody) } if err != nil { @@ -454,6 +470,11 @@ func fillPlaceholders(value string, opts *ApiOptions) (string, error) { err = e } case "branch": + if os.Getenv("GH_REPO") != "" { + err = errors.New("unable to determine an appropriate value for the 'branch' placeholder") + return m + } + if branch, e := opts.Branch(); e == nil { return branch } else { diff --git a/pkg/cmd/api/api_test.go b/pkg/cmd/api/api_test.go index 27a47dd92..2d43daea7 100644 --- a/pkg/cmd/api/api_test.go +++ b/pkg/cmd/api/api_test.go @@ -19,7 +19,7 @@ import ( "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/go-gh/pkg/template" + "github.com/cli/go-gh/v2/pkg/template" "github.com/google/shlex" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -378,6 +378,7 @@ func Test_apiRun(t *testing.T) { err error stdout string stderr string + isatty bool }{ { name: "success", @@ -388,6 +389,7 @@ func Test_apiRun(t *testing.T) { err: nil, stdout: `bam!`, stderr: ``, + isatty: false, }, { name: "show response headers", @@ -404,6 +406,7 @@ func Test_apiRun(t *testing.T) { err: nil, stdout: "HTTP/1.1 200 Okey-dokey\nContent-Type: text/plain\r\n\r\nbody", stderr: ``, + isatty: false, }, { name: "success 204", @@ -414,6 +417,7 @@ func Test_apiRun(t *testing.T) { err: nil, stdout: ``, stderr: ``, + isatty: false, }, { name: "REST error", @@ -425,6 +429,7 @@ func Test_apiRun(t *testing.T) { err: cmdutil.SilentError, stdout: `{"message": "THIS IS FINE"}`, stderr: "gh: THIS IS FINE (HTTP 400)\n", + isatty: false, }, { name: "REST string errors", @@ -436,6 +441,7 @@ func Test_apiRun(t *testing.T) { err: cmdutil.SilentError, stdout: `{"errors": ["ALSO", "FINE"]}`, stderr: "gh: ALSO\nFINE\n", + isatty: false, }, { name: "GraphQL error", @@ -450,6 +456,7 @@ func Test_apiRun(t *testing.T) { err: cmdutil.SilentError, stdout: `{"errors": [{"message":"AGAIN"}, {"message":"FINE"}]}`, stderr: "gh: AGAIN\nFINE\n", + isatty: false, }, { name: "failure", @@ -460,6 +467,7 @@ func Test_apiRun(t *testing.T) { err: cmdutil.SilentError, stdout: `gateway timeout`, stderr: "gh: HTTP 502\n", + isatty: false, }, { name: "silent", @@ -473,6 +481,7 @@ func Test_apiRun(t *testing.T) { err: nil, stdout: ``, stderr: ``, + isatty: false, }, { name: "show response headers even when silent", @@ -490,6 +499,7 @@ func Test_apiRun(t *testing.T) { err: nil, stdout: "HTTP/1.1 200 Okey-dokey\nContent-Type: text/plain\r\n\r\n", stderr: ``, + isatty: false, }, { name: "output template", @@ -504,6 +514,7 @@ func Test_apiRun(t *testing.T) { err: nil, stdout: "not a cat", stderr: ``, + isatty: false, }, { name: "output template when REST error", @@ -518,6 +529,7 @@ func Test_apiRun(t *testing.T) { err: cmdutil.SilentError, stdout: `{"message": "THIS IS FINE"}`, stderr: "gh: THIS IS FINE (HTTP 400)\n", + isatty: false, }, { name: "jq filter", @@ -532,6 +544,7 @@ func Test_apiRun(t *testing.T) { err: nil, stdout: "Mona\nHubot\n", stderr: ``, + isatty: false, }, { name: "jq filter when REST error", @@ -546,12 +559,29 @@ func Test_apiRun(t *testing.T) { err: cmdutil.SilentError, stdout: `{"message": "THIS IS FINE"}`, stderr: "gh: THIS IS FINE (HTTP 400)\n", + isatty: false, + }, + { + name: "jq filter outputting JSON to a TTY", + options: ApiOptions{ + FilterOutput: `.`, + }, + httpResponse: &http.Response{ + StatusCode: 200, + Body: io.NopCloser(bytes.NewBufferString(`[{"name":"Mona"},{"name":"Hubot"}]`)), + Header: http.Header{"Content-Type": []string{"application/json"}}, + }, + err: nil, + stdout: "[\n {\n \"name\": \"Mona\"\n },\n {\n \"name\": \"Hubot\"\n }\n]\n", + stderr: ``, + isatty: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ios, _, stdout, stderr := iostreams.Test() + ios.SetStdoutTTY(tt.isatty) tt.options.IO = ios tt.options.Config = func() (config.Config, error) { return config.NewBlankConfig(), nil } @@ -638,6 +668,78 @@ func Test_apiRun_paginationREST(t *testing.T) { assert.Equal(t, "https://api.github.com/repositories/1227/issues?page=3", responses[2].Request.URL.String()) } +func Test_apiRun_arrayPaginationREST(t *testing.T) { + ios, _, stdout, stderr := iostreams.Test() + ios.SetStdoutTTY(false) + + requestCount := 0 + responses := []*http.Response{ + { + StatusCode: 200, + Body: io.NopCloser(bytes.NewBufferString(`[{"item":1},{"item":2}]`)), + Header: http.Header{ + "Content-Type": []string{"application/json"}, + "Link": []string{`; rel="next", ; rel="last"`}, + }, + }, + { + StatusCode: 200, + Body: io.NopCloser(bytes.NewBufferString(`[{"item":3},{"item":4}]`)), + Header: http.Header{ + "Content-Type": []string{"application/json"}, + "Link": []string{`; rel="next", ; rel="last"`}, + }, + }, + { + StatusCode: 200, + Body: io.NopCloser(bytes.NewBufferString(`[{"item":5}]`)), + Header: http.Header{ + "Content-Type": []string{"application/json"}, + "Link": []string{`; rel="next", ; rel="last"`}, + }, + }, + { + StatusCode: 200, + Body: io.NopCloser(bytes.NewBufferString(`[]`)), + Header: http.Header{ + "Content-Type": []string{"application/json"}, + }, + }, + } + + options := ApiOptions{ + IO: ios, + HttpClient: func() (*http.Client, error) { + var tr roundTripper = func(req *http.Request) (*http.Response, error) { + resp := responses[requestCount] + resp.Request = req + requestCount++ + return resp, nil + } + return &http.Client{Transport: tr}, nil + }, + Config: func() (config.Config, error) { + return config.NewBlankConfig(), nil + }, + + RequestMethod: "GET", + RequestMethodPassed: true, + RequestPath: "issues", + Paginate: true, + RawFields: []string{"per_page=50", "page=1"}, + } + + err := apiRun(&options) + assert.NoError(t, err) + + assert.Equal(t, `[{"item":1},{"item":2},{"item":3},{"item":4},{"item":5} ]`, stdout.String(), "stdout") + assert.Equal(t, "", stderr.String(), "stderr") + + assert.Equal(t, "https://api.github.com/issues?page=1&per_page=50", responses[0].Request.URL.String()) + assert.Equal(t, "https://api.github.com/repositories/1227/issues?page=2", responses[1].Request.URL.String()) + assert.Equal(t, "https://api.github.com/repositories/1227/issues?page=3", responses[2].Request.URL.String()) +} + func Test_apiRun_paginationGraphQL(t *testing.T) { ios, _, stdout, stderr := iostreams.Test() @@ -994,10 +1096,11 @@ func Test_fillPlaceholders(t *testing.T) { opts *ApiOptions } tests := []struct { - name string - args args - want string - wantErr bool + name string + args args + repoOverride bool + want string + wantErr bool }{ { name: "no changes", @@ -1134,9 +1237,26 @@ func Test_fillPlaceholders(t *testing.T) { want: "{}{ownership}/{repository}", wantErr: false, }, + { + name: "branch can't be filled when GH_REPO is set", + repoOverride: true, + args: args{ + value: "repos/:owner/:repo/branches/:branch", + opts: &ApiOptions{ + BaseRepo: func() (ghrepo.Interface, error) { + return ghrepo.New("hubot", "robot-uprising"), nil + }, + }, + }, + want: "repos/hubot/robot-uprising/branches/:branch", + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + if tt.repoOverride { + t.Setenv("GH_REPO", "hubot/robot-uprising") + } got, err := fillPlaceholders(tt.args.value, tt.args.opts) if (err != nil) != tt.wantErr { t.Errorf("fillPlaceholders() error = %v, wantErr %v", err, tt.wantErr) @@ -1206,7 +1326,7 @@ func Test_processResponse_template(t *testing.T) { tmpl := template.New(ios.Out, ios.TerminalWidth(), ios.ColorEnabled()) err := tmpl.Parse(opts.Template) require.NoError(t, err) - _, err = processResponse(&resp, &opts, ios.Out, io.Discard, &tmpl) + _, err = processResponse(&resp, &opts, ios.Out, io.Discard, tmpl, true, true) require.NoError(t, err) err = tmpl.Flush() require.NoError(t, err) diff --git a/pkg/cmd/api/http.go b/pkg/cmd/api/http.go index 30a493130..3d6bb240e 100644 --- a/pkg/cmd/api/http.go +++ b/pkg/cmd/api/http.go @@ -50,7 +50,7 @@ func httpRequest(client *http.Client, hostname string, method string, p string, return nil, fmt.Errorf("unrecognized parameters type: %v", params) } - req, err := http.NewRequest(method, requestURL, body) + req, err := http.NewRequest(strings.ToUpper(method), requestURL, body) if err != nil { return nil, err } @@ -103,21 +103,8 @@ func addQuery(path string, params map[string]interface{}) string { } query := url.Values{} - for key, value := range params { - switch v := value.(type) { - case string: - query.Add(key, v) - case []byte: - query.Add(key, string(v)) - case nil: - query.Add(key, "") - case int: - query.Add(key, fmt.Sprintf("%d", v)) - case bool: - query.Add(key, fmt.Sprintf("%v", v)) - default: - panic(fmt.Sprintf("unknown type %v", v)) - } + if err := addQueryParam(query, "", params); err != nil { + panic(err) } sep := "?" @@ -126,3 +113,34 @@ func addQuery(path string, params map[string]interface{}) string { } return path + sep + query.Encode() } + +func addQueryParam(query url.Values, key string, value interface{}) error { + switch v := value.(type) { + case string: + query.Add(key, v) + case []byte: + query.Add(key, string(v)) + case nil: + query.Add(key, "") + case int: + query.Add(key, fmt.Sprintf("%d", v)) + case bool: + query.Add(key, fmt.Sprintf("%v", v)) + case map[string]interface{}: + for subkey, value := range v { + // support for nested subkeys can be added here if that is ever necessary + if err := addQueryParam(query, subkey, value); err != nil { + return err + } + } + case []interface{}: + for _, entry := range v { + if err := addQueryParam(query, key+"[]", entry); err != nil { + return err + } + } + default: + return fmt.Errorf("unknown type %v", v) + } + return nil +} diff --git a/pkg/cmd/api/http_test.go b/pkg/cmd/api/http_test.go index 9f0e7d6c3..f63566490 100644 --- a/pkg/cmd/api/http_test.go +++ b/pkg/cmd/api/http_test.go @@ -129,6 +129,24 @@ func Test_httpRequest(t *testing.T) { headers: "", }, }, + { + name: "lowercase HTTP method", + args: args{ + client: &httpClient, + host: "github.com", + method: "get", + p: "repos/octocat/spoon-knife", + params: nil, + headers: []string{}, + }, + wantErr: false, + want: expects{ + method: "GET", + u: "https://api.github.com/repos/octocat/spoon-knife", + body: "", + headers: "", + }, + }, { name: "GET with leading slash", args: args{ @@ -322,6 +340,14 @@ func Test_addQuery(t *testing.T) { }, want: "?a=hello", }, + { + name: "array", + args: args{ + path: "", + params: map[string]interface{}{"a": []interface{}{"hello", "world"}}, + }, + want: "?a%5B%5D=hello&a%5B%5D=world", + }, { name: "append", args: args{ diff --git a/pkg/cmd/api/pagination.go b/pkg/cmd/api/pagination.go index 65d816480..057a5a7fa 100644 --- a/pkg/cmd/api/pagination.go +++ b/pkg/cmd/api/pagination.go @@ -106,3 +106,43 @@ func addPerPage(p string, perPage int, params map[string]interface{}) string { return fmt.Sprintf("%s%sper_page=%d", p, sep, perPage) } + +// paginatedArrayReader wraps a Reader to omit the opening and/or the closing square bracket of a +// JSON array in order to apply pagination context between multiple API requests. +type paginatedArrayReader struct { + io.Reader + isFirstPage bool + isLastPage bool + + isSubsequentRead bool + cachedByte byte +} + +func (r *paginatedArrayReader) Read(p []byte) (int, error) { + var n int + var err error + if r.cachedByte != 0 && len(p) > 0 { + p[0] = r.cachedByte + n, err = r.Reader.Read(p[1:]) + n += 1 + r.cachedByte = 0 + } else { + n, err = r.Reader.Read(p) + } + if !r.isSubsequentRead && !r.isFirstPage && n > 0 && p[0] == '[' { + if n > 1 && p[1] == ']' { + // empty array case + p[0] = ' ' + } else { + // avoid starting a new array and continue with a comma instead + p[0] = ',' + } + } + if !r.isLastPage && n > 0 && p[n-1] == ']' { + // avoid closing off an array in case we determine we are at EOF + r.cachedByte = p[n-1] + n -= 1 + } + r.isSubsequentRead = true + return n, err +} diff --git a/pkg/cmd/auth/login/login.go b/pkg/cmd/auth/login/login.go index 258853164..84894bf10 100644 --- a/pkg/cmd/auth/login/login.go +++ b/pkg/cmd/auth/login/login.go @@ -14,7 +14,7 @@ import ( "github.com/cli/cli/v2/pkg/cmd/auth/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - ghAuth "github.com/cli/go-gh/pkg/auth" + ghAuth "github.com/cli/go-gh/v2/pkg/auth" "github.com/spf13/cobra" ) @@ -151,6 +151,11 @@ func loginRun(opts *LoginOptions) error { } } + // The go-gh Config object currently does not support case-insensitive lookups for host names, + // so normalize the host name case here before performing any lookups with it or persisting it. + // https://github.com/cli/go-gh/pull/105 + hostname = strings.ToLower(hostname) + if src, writeable := shared.AuthTokenWriteable(authCfg, hostname); !writeable { fmt.Fprintf(opts.IO.ErrOut, "The value of the %s environment variable is being used for authentication.\n", src) fmt.Fprint(opts.IO.ErrOut, "To have GitHub CLI store credentials instead, first clear the value from the environment.\n") diff --git a/pkg/cmd/auth/refresh/refresh.go b/pkg/cmd/auth/refresh/refresh.go index fc068440c..4debd0a45 100644 --- a/pkg/cmd/auth/refresh/refresh.go +++ b/pkg/cmd/auth/refresh/refresh.go @@ -12,6 +12,7 @@ import ( "github.com/cli/cli/v2/pkg/cmd/auth/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/cli/v2/pkg/set" "github.com/spf13/cobra" ) @@ -24,9 +25,11 @@ type RefreshOptions struct { MainExecutable string - Hostname string - Scopes []string - AuthFlow func(*config.AuthConfig, *iostreams.IOStreams, string, []string, bool, bool) error + Hostname string + Scopes []string + RemoveScopes []string + ResetScopes bool + AuthFlow func(*config.AuthConfig, *iostreams.IOStreams, string, []string, bool, bool) error Interactive bool InsecureStorage bool @@ -62,16 +65,25 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra. your gh credentials to have. If no scopes are provided, the command maintains previously added scopes. - The command can only add additional scopes, but not remove previously - added ones. To reset scopes to the default minimum set of scopes, you - will need to create new credentials using the auth login command. + The --remove-scopes flag accepts a comma separated list of scopes you + want to remove from your gh credentials. Scope removal is idempotent. + The minimum set of scopes ("repo", "read:org" and "gist") cannot be removed. + + The --reset-scopes flag resets the scopes for your gh credentials to + the default set of scopes for your auth flow. `), Example: heredoc.Doc(` $ gh auth refresh --scopes write:org,read:public_key - # => open a browser to add write:org and read:public_key scopes for use with gh api + # => open a browser to add write:org and read:public_key scopes $ gh auth refresh # => open a browser to ensure your authentication credentials have the correct minimum scopes + + $ gh auth refresh --remove-scopes delete_repo + # => open a browser to idempotently remove the delete_repo scope + + $ gh auth refresh --reset-scopes + # => open a browser to re-authenticate with the default minimum scopes `), RunE: func(cmd *cobra.Command, args []string) error { opts.Interactive = opts.IO.CanPrompt() @@ -90,6 +102,8 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra. cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The GitHub host to use for authentication") cmd.Flags().StringSliceVarP(&opts.Scopes, "scopes", "s", nil, "Additional authentication scopes for gh to have") + cmd.Flags().StringSliceVarP(&opts.RemoveScopes, "remove-scopes", "r", nil, "Authentication scopes to remove from gh") + cmd.Flags().BoolVar(&opts.ResetScopes, "reset-scopes", false, "Reset authentication scopes to the default minimum set of scopes") // secure storage became the default on 2023/4/04; this flag is left as a no-op for backwards compatibility var secureStorage bool cmd.Flags().BoolVar(&secureStorage, "secure-storage", false, "Save authentication credentials in secure credential store") @@ -143,13 +157,16 @@ func refreshRun(opts *RefreshOptions) error { return cmdutil.SilentError } - var additionalScopes []string - if oldToken, _ := authCfg.Token(hostname); oldToken != "" { - if oldScopes, err := shared.GetScopes(opts.HttpClient, hostname, oldToken); err == nil { - for _, s := range strings.Split(oldScopes, ",") { - s = strings.TrimSpace(s) - if s != "" { - additionalScopes = append(additionalScopes, s) + additionalScopes := set.NewStringSet() + + if !opts.ResetScopes { + if oldToken, _ := authCfg.Token(hostname); oldToken != "" { + if oldScopes, err := shared.GetScopes(opts.HttpClient, hostname, oldToken); err == nil { + for _, s := range strings.Split(oldScopes, ",") { + s = strings.TrimSpace(s) + if s != "" { + additionalScopes.Add(s) + } } } } @@ -165,10 +182,14 @@ func refreshRun(opts *RefreshOptions) error { if err := credentialFlow.Prompt(hostname); err != nil { return err } - additionalScopes = append(additionalScopes, credentialFlow.Scopes()...) + additionalScopes.AddValues(credentialFlow.Scopes()) } - if err := opts.AuthFlow(authCfg, opts.IO, hostname, append(opts.Scopes, additionalScopes...), opts.Interactive, !opts.InsecureStorage); err != nil { + additionalScopes.AddValues(opts.Scopes) + + additionalScopes.RemoveValues(opts.RemoveScopes) + + if err := opts.AuthFlow(authCfg, opts.IO, hostname, additionalScopes.ToSlice(), opts.Interactive, !opts.InsecureStorage); err != nil { return err } diff --git a/pkg/cmd/auth/refresh/refresh_test.go b/pkg/cmd/auth/refresh/refresh_test.go index 8743f1835..c87c2bcb8 100644 --- a/pkg/cmd/auth/refresh/refresh_test.go +++ b/pkg/cmd/auth/refresh/refresh_test.go @@ -16,8 +16,6 @@ import ( "github.com/stretchr/testify/assert" ) -// TODO prompt cfg test - func Test_NewCmdRefresh(t *testing.T) { tests := []struct { name string @@ -99,6 +97,38 @@ func Test_NewCmdRefresh(t *testing.T) { InsecureStorage: true, }, }, + { + name: "reset scopes", + tty: true, + cli: "--reset-scopes", + wants: RefreshOptions{ + ResetScopes: true, + }, + }, + { + name: "remove scope", + tty: true, + cli: "--remove-scopes read:public_key", + wants: RefreshOptions{ + RemoveScopes: []string{"read:public_key"}, + }, + }, + { + name: "remove multiple scopes", + tty: true, + cli: "--remove-scopes workflow,read:public_key", + wants: RefreshOptions{ + RemoveScopes: []string{"workflow", "read:public_key"}, + }, + }, + { + name: "remove scope shorthand", + tty: true, + cli: "-r read:public_key", + wants: RefreshOptions{ + RemoveScopes: []string{"read:public_key"}, + }, + }, } for _, tt := range tests { @@ -185,7 +215,7 @@ func Test_refreshRun(t *testing.T) { }, wantAuthArgs: authArgs{ hostname: "obed.morton", - scopes: nil, + scopes: []string{}, secureStorage: true, }, }, @@ -199,7 +229,7 @@ func Test_refreshRun(t *testing.T) { }, wantAuthArgs: authArgs{ hostname: "github.com", - scopes: nil, + scopes: []string{}, secureStorage: true, }, }, @@ -219,7 +249,7 @@ func Test_refreshRun(t *testing.T) { }, wantAuthArgs: authArgs{ hostname: "github.com", - scopes: nil, + scopes: []string{}, secureStorage: true, }, }, @@ -248,7 +278,7 @@ func Test_refreshRun(t *testing.T) { }, wantAuthArgs: authArgs{ hostname: "github.com", - scopes: []string{"repo:invite", "public_key:read", "delete_repo", "codespace"}, + scopes: []string{"delete_repo", "codespace", "repo:invite", "public_key:read"}, secureStorage: true, }, }, @@ -262,7 +292,7 @@ func Test_refreshRun(t *testing.T) { }, wantAuthArgs: authArgs{ hostname: "obed.morton", - scopes: nil, + scopes: []string{}, secureStorage: true, }, }, @@ -277,7 +307,101 @@ func Test_refreshRun(t *testing.T) { }, wantAuthArgs: authArgs{ hostname: "obed.morton", - scopes: nil, + scopes: []string{}, + }, + }, + { + name: "reset scopes", + cfgHosts: []string{ + "github.com", + }, + oldScopes: "delete_repo, codespace", + opts: &RefreshOptions{ + Hostname: "github.com", + ResetScopes: true, + }, + wantAuthArgs: authArgs{ + hostname: "github.com", + scopes: []string{}, + secureStorage: true, + }, + }, + { + name: "reset scopes and add some scopes", + cfgHosts: []string{ + "github.com", + }, + oldScopes: "repo:invite, delete_repo, codespace", + opts: &RefreshOptions{ + Scopes: []string{"public_key:read", "workflow"}, + ResetScopes: true, + }, + wantAuthArgs: authArgs{ + hostname: "github.com", + scopes: []string{"public_key:read", "workflow"}, + secureStorage: true, + }, + }, + { + name: "remove scopes", + cfgHosts: []string{ + "github.com", + }, + oldScopes: "delete_repo, codespace, repo:invite, public_key:read", + opts: &RefreshOptions{ + Hostname: "github.com", + RemoveScopes: []string{"delete_repo", "repo:invite"}, + }, + wantAuthArgs: authArgs{ + hostname: "github.com", + scopes: []string{"codespace", "public_key:read"}, + secureStorage: true, + }, + }, + { + name: "remove scope but no old scope", + cfgHosts: []string{ + "github.com", + }, + opts: &RefreshOptions{ + Hostname: "github.com", + RemoveScopes: []string{"delete_repo"}, + }, + wantAuthArgs: authArgs{ + hostname: "github.com", + scopes: []string{}, + secureStorage: true, + }, + }, + { + name: "remove and add scopes at the same time", + cfgHosts: []string{ + "github.com", + }, + oldScopes: "repo:invite, delete_repo, codespace", + opts: &RefreshOptions{ + Scopes: []string{"repo:invite", "public_key:read", "workflow"}, + RemoveScopes: []string{"codespace", "repo:invite", "workflow"}, + }, + wantAuthArgs: authArgs{ + hostname: "github.com", + scopes: []string{"delete_repo", "public_key:read"}, + secureStorage: true, + }, + }, + { + name: "remove scopes that don't exist", + cfgHosts: []string{ + "github.com", + }, + oldScopes: "repo:invite, delete_repo, codespace", + opts: &RefreshOptions{ + RemoveScopes: []string{"codespace", "repo:invite", "public_key:read"}, + }, + wantAuthArgs: authArgs{ + hostname: "github.com", + scopes: []string{"delete_repo"}, + secureStorage: true, }, }, } diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index 8070fe9ae..77a5f125c 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -66,7 +66,7 @@ func statusRun(opts *StatusOptions) error { // TODO check tty stderr := opts.IO.ErrOut - + stdout := opts.IO.Out cs := opts.IO.ColorScheme() statusInfo := map[string][]string{} @@ -166,13 +166,22 @@ func statusRun(opts *StatusOptions) error { if !ok { continue } - if prevEntry { + if prevEntry && failed { fmt.Fprint(stderr, "\n") + } else if prevEntry && !failed { + fmt.Fprint(stdout, "\n") } prevEntry = true - fmt.Fprintf(stderr, "%s\n", cs.Bold(hostname)) - for _, line := range lines { - fmt.Fprintf(stderr, " %s\n", line) + if failed { + fmt.Fprintf(stderr, "%s\n", cs.Bold(hostname)) + for _, line := range lines { + fmt.Fprintf(stderr, " %s\n", line) + } + } else { + fmt.Fprintf(stdout, "%s\n", cs.Bold(hostname)) + for _, line := range lines { + fmt.Fprintf(stdout, " %s\n", line) + } } } diff --git a/pkg/cmd/auth/status/status_test.go b/pkg/cmd/auth/status/status_test.go index e169d1b3f..e18604202 100644 --- a/pkg/cmd/auth/status/status_test.go +++ b/pkg/cmd/auth/status/status_test.go @@ -76,12 +76,13 @@ func Test_statusRun(t *testing.T) { readConfigs := config.StubWriteConfig(t) tests := []struct { - name string - opts *StatusOptions - httpStubs func(*httpmock.Registry) - cfgStubs func(*config.ConfigMock) - wantErr string - wantOut string + name string + opts *StatusOptions + httpStubs func(*httpmock.Registry) + cfgStubs func(*config.ConfigMock) + wantErr string + wantOut string + wantErrOut string }{ { name: "hostname set", @@ -126,7 +127,7 @@ func Test_statusRun(t *testing.T) { httpmock.StringResponse(`{"data":{"viewer":{"login":"tess"}}}`)) }, wantErr: "SilentError", - wantOut: heredoc.Doc(` + wantErrOut: heredoc.Doc(` joel.miller X joel.miller: the token in GH_CONFIG_DIR/hosts.yml is missing required scope 'read:org' - To request missing scopes, run: gh auth refresh -h joel.miller @@ -156,7 +157,7 @@ func Test_statusRun(t *testing.T) { httpmock.StringResponse(`{"data":{"viewer":{"login":"tess"}}}`)) }, wantErr: "SilentError", - wantOut: heredoc.Doc(` + wantErrOut: heredoc.Doc(` joel.miller X joel.miller: authentication failed - The joel.miller token in GH_CONFIG_DIR/hosts.yml is no longer valid. @@ -298,9 +299,9 @@ func Test_statusRun(t *testing.T) { cfgStubs: func(c *config.ConfigMock) { c.Set("github.com", "oauth_token", "abc123") }, - httpStubs: func(reg *httpmock.Registry) {}, - wantErr: "SilentError", - wantOut: "Hostname \"github.example.com\" not found among authenticated GitHub hosts\n", + httpStubs: func(reg *httpmock.Registry) {}, + wantErr: "SilentError", + wantErrOut: "Hostname \"github.example.com\" not found among authenticated GitHub hosts\n", }, } @@ -310,13 +311,12 @@ func Test_statusRun(t *testing.T) { tt.opts = &StatusOptions{} } - ios, _, _, stderr := iostreams.Test() + ios, _, stdout, stderr := iostreams.Test() ios.SetStdinTTY(true) ios.SetStderrTTY(true) ios.SetStdoutTTY(true) tt.opts.IO = ios - cfg := config.NewFromString("") if tt.cfgStubs != nil { tt.cfgStubs(cfg) @@ -340,8 +340,10 @@ func Test_statusRun(t *testing.T) { } else { assert.NoError(t, err) } + output := strings.ReplaceAll(stdout.String(), config.ConfigDir()+string(filepath.Separator), "GH_CONFIG_DIR/") + errorOutput := strings.ReplaceAll(stderr.String(), config.ConfigDir()+string(filepath.Separator), "GH_CONFIG_DIR/") - output := strings.ReplaceAll(stderr.String(), config.ConfigDir()+string(filepath.Separator), "GH_CONFIG_DIR/") + assert.Equal(t, tt.wantErrOut, errorOutput) assert.Equal(t, tt.wantOut, output) mainBuf := bytes.Buffer{} diff --git a/pkg/cmd/browse/browse.go b/pkg/cmd/browse/browse.go index ae78c5155..fd9397f79 100644 --- a/pkg/cmd/browse/browse.go +++ b/pkg/cmd/browse/browse.go @@ -163,14 +163,12 @@ func runBrowse(opts *BrowseOptions) error { return fmt.Errorf("unable to determine base repository: %w", err) } - if opts.Commit != "" { - if opts.Commit == emptyCommitFlag { - commit, err := opts.GitClient.LastCommit() - if err != nil { - return err - } - opts.Commit = commit.Sha + if opts.Commit != "" && opts.Commit == emptyCommitFlag { + commit, err := opts.GitClient.LastCommit() + if err != nil { + return err } + opts.Commit = commit.Sha } section, err := parseSection(baseRepo, opts) diff --git a/pkg/cmd/browse/browse_test.go b/pkg/cmd/browse/browse_test.go index 9da60dcb1..67b920fdb 100644 --- a/pkg/cmd/browse/browse_test.go +++ b/pkg/cmd/browse/browse_test.go @@ -370,7 +370,7 @@ func Test_runBrowse(t *testing.T) { opts: BrowseOptions{ SelectorArg: "chocolate-pecan-pie.txt", }, - baseRepo: ghrepo.New("andrewhsu", "recipies"), + baseRepo: ghrepo.New("andrewhsu", "recipes"), defaultBranch: "", wantsErr: true, }, diff --git a/pkg/cmd/codespace/codespace_selector.go b/pkg/cmd/codespace/codespace_selector.go index 69b9ed06b..a51e42b6f 100644 --- a/pkg/cmd/codespace/codespace_selector.go +++ b/pkg/cmd/codespace/codespace_selector.go @@ -15,6 +15,7 @@ type CodespaceSelector struct { repoName string codespaceName string + repoOwner string } var errNoFilteredCodespaces = errors.New("you have no codespaces meeting the filter criteria") @@ -25,8 +26,10 @@ func AddCodespaceSelector(cmd *cobra.Command, api apiClient) *CodespaceSelector cmd.PersistentFlags().StringVarP(&cs.codespaceName, "codespace", "c", "", "Name of the codespace") cmd.PersistentFlags().StringVarP(&cs.repoName, "repo", "R", "", "Filter codespace selection by repository name (user/repo)") + cmd.PersistentFlags().StringVar(&cs.repoOwner, "repo-owner", "", "Filter codespace selection by repository owner (username or org)") cmd.MarkFlagsMutuallyExclusive("codespace", "repo") + cmd.MarkFlagsMutuallyExclusive("codespace", "repo-owner") return cs } @@ -102,6 +105,10 @@ func (cs *CodespaceSelector) fetchCodespaces(ctx context.Context) (codespaces [] codespaces = filteredCodespaces } + if cs.repoOwner != "" { + codespaces = filterCodespacesByRepoOwner(codespaces, cs.repoOwner) + } + if len(codespaces) == 0 { return nil, errNoFilteredCodespaces } diff --git a/pkg/cmd/codespace/codespace_selector_test.go b/pkg/cmd/codespace/codespace_selector_test.go index 30ba3c588..7b34ebd7b 100644 --- a/pkg/cmd/codespace/codespace_selector_test.go +++ b/pkg/cmd/codespace/codespace_selector_test.go @@ -48,68 +48,101 @@ func TestSelectNameWithCodespaceName(t *testing.T) { func TestFetchCodespaces(t *testing.T) { var ( - repoA1 = &api.Codespace{Name: "1", Repository: api.Repository{FullName: "mock/A"}} - repoA2 = &api.Codespace{Name: "2", Repository: api.Repository{FullName: "mock/A"}} + octocatOwner = api.RepositoryOwner{Login: "octocat"} + cliOwner = api.RepositoryOwner{Login: "cli"} + octocatA = &api.Codespace{ + Name: "1", + Repository: api.Repository{FullName: "octocat/A", Owner: octocatOwner}, + } - repoB1 = &api.Codespace{Name: "1", Repository: api.Repository{FullName: "mock/B"}} + octocatA2 = &api.Codespace{ + Name: "2", + Repository: api.Repository{FullName: "octocat/A", Owner: octocatOwner}, + } + + cliA = &api.Codespace{ + Name: "3", + Repository: api.Repository{FullName: "cli/A", Owner: cliOwner}, + } + + octocatB = &api.Codespace{ + Name: "4", + Repository: api.Repository{FullName: "octocat/B", Owner: octocatOwner}, + } ) tests := []struct { tName string apiCodespaces []*api.Codespace + codespaceName string repoName string + repoOwner string wantCodespaces []*api.Codespace wantErr error }{ // Empty case { - "empty", nil, "", nil, errNoCodespaces, + tName: "empty", + apiCodespaces: nil, + wantCodespaces: nil, + wantErr: errNoCodespaces, }, // Tests with no filtering { - "no filtering, single codespace", - []*api.Codespace{repoA1}, - "", - []*api.Codespace{repoA1}, - nil, + tName: "no filtering, single codespaces", + apiCodespaces: []*api.Codespace{octocatA}, + wantCodespaces: []*api.Codespace{octocatA}, + wantErr: nil, }, { - "no filtering, multiple codespaces", - []*api.Codespace{repoA1, repoA2, repoB1}, - "", - []*api.Codespace{repoA1, repoA2, repoB1}, - nil, + tName: "no filtering, multiple codespace", + apiCodespaces: []*api.Codespace{octocatA, cliA, octocatB}, + wantCodespaces: []*api.Codespace{octocatA, cliA, octocatB}, }, // Test repo filtering { - "repo filtering, single codespace", - []*api.Codespace{repoA1}, - "mock/A", - []*api.Codespace{repoA1}, - nil, + tName: "repo name filtering, single codespace", + apiCodespaces: []*api.Codespace{octocatA}, + repoName: "octocat/A", + wantCodespaces: []*api.Codespace{octocatA}, + wantErr: nil, }, { - "repo filtering, multiple codespaces", - []*api.Codespace{repoA1, repoA2, repoB1}, - "mock/A", - []*api.Codespace{repoA1, repoA2}, - nil, + tName: "repo name filtering, multiple codespace", + apiCodespaces: []*api.Codespace{octocatA, octocatA2, cliA, octocatB}, + repoName: "octocat/A", + wantCodespaces: []*api.Codespace{octocatA, octocatA2}, + wantErr: nil, }, { - "repo filtering, multiple codespaces 2", - []*api.Codespace{repoA1, repoA2, repoB1}, - "mock/B", - []*api.Codespace{repoB1}, - nil, + tName: "repo name filtering, multiple codespace 2", + apiCodespaces: []*api.Codespace{octocatA, cliA, octocatB}, + repoName: "octocat/B", + wantCodespaces: []*api.Codespace{octocatB}, + wantErr: nil, }, { - "repo filtering, no matches", - []*api.Codespace{repoA1, repoA2, repoB1}, - "mock/C", - nil, - errNoFilteredCodespaces, + tName: "repo name filtering, no matches", + apiCodespaces: []*api.Codespace{octocatA, cliA, octocatB}, + repoName: "Unknown/unknown", + wantCodespaces: nil, + wantErr: errNoFilteredCodespaces, + }, + { + tName: "repo filtering, match with repo owner", + apiCodespaces: []*api.Codespace{octocatA, octocatA2, cliA, octocatB}, + repoOwner: "octocat", + wantCodespaces: []*api.Codespace{octocatA, octocatA2, octocatB}, + wantErr: nil, + }, + { + tName: "repo filtering, no match with repo owner", + apiCodespaces: []*api.Codespace{octocatA, cliA, octocatB}, + repoOwner: "unknown", + wantCodespaces: []*api.Codespace{}, + wantErr: errNoFilteredCodespaces, }, } @@ -121,7 +154,11 @@ func TestFetchCodespaces(t *testing.T) { }, } - cs := &CodespaceSelector{api: api, repoName: tt.repoName} + cs := &CodespaceSelector{ + api: api, + repoName: tt.repoName, + repoOwner: tt.repoOwner, + } codespaces, err := cs.fetchCodespaces(context.Background()) diff --git a/pkg/cmd/codespace/common.go b/pkg/cmd/codespace/common.go index 8ccc52a4f..f6f6fb937 100644 --- a/pkg/cmd/codespace/common.go +++ b/pkg/cmd/codespace/common.go @@ -10,6 +10,7 @@ import ( "log" "os" "sort" + "strings" "github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2/terminal" @@ -266,3 +267,14 @@ func addDeprecatedRepoShorthand(cmd *cobra.Command, target *string) error { return nil } + +// filterCodespacesByRepoOwner filters a list of codespaces by the owner of the repository. +func filterCodespacesByRepoOwner(codespaces []*api.Codespace, repoOwner string) []*api.Codespace { + filtered := make([]*api.Codespace, 0, len(codespaces)) + for _, c := range codespaces { + if strings.EqualFold(c.Repository.Owner.Login, repoOwner) { + filtered = append(filtered, c) + } + } + return filtered +} diff --git a/pkg/cmd/codespace/delete.go b/pkg/cmd/codespace/delete.go index d67c271ad..e54bf09fb 100644 --- a/pkg/cmd/codespace/delete.go +++ b/pkg/cmd/codespace/delete.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "strings" + "sync/atomic" "time" "github.com/AlecAivazis/survey/v2" @@ -23,6 +24,7 @@ type deleteOptions struct { keepDays uint16 orgName string userName string + repoOwner string isInteractive bool now func() time.Time @@ -60,10 +62,12 @@ func newDeleteCmd(app *App) *cobra.Command { // After the admin subcommand is added (see https://github.com/cli/cli/pull/6944#issuecomment-1419553639) we can revisit this. opts.codespaceName = selector.codespaceName opts.repoFilter = selector.repoName + opts.repoOwner = selector.repoOwner if opts.deleteAll && opts.repoFilter != "" { return cmdutil.FlagErrorf("both `--all` and `--repo` is not supported") } + if opts.orgName != "" && opts.codespaceName != "" && opts.userName == "" { return cmdutil.FlagErrorf("using `--org` with `--codespace` requires `--user`") } @@ -99,6 +103,9 @@ func (a *App) Delete(ctx context.Context, opts deleteOptions) (err error) { userName = currentUser.Login } codespaces, fetchErr = a.apiClient.ListCodespaces(ctx, api.ListCodespacesOptions{OrgName: opts.orgName, UserName: userName}) + if opts.repoOwner != "" { + codespaces = filterCodespacesByRepoOwner(codespaces, opts.repoOwner) + } return }) if err != nil { @@ -139,6 +146,7 @@ func (a *App) Delete(ctx context.Context, opts deleteOptions) (err error) { if opts.repoFilter != "" && !strings.EqualFold(c.Repository.FullName, opts.repoFilter) { continue } + if opts.keepDays > 0 { t, err := time.Parse(time.RFC3339, c.LastUsedAt) if err != nil { @@ -169,7 +177,8 @@ func (a *App) Delete(ctx context.Context, opts deleteOptions) (err error) { progressLabel = "Deleting codespaces" } - return a.RunWithProgress(progressLabel, func() error { + var deletedCodespaces uint32 + err = a.RunWithProgress(progressLabel, func() error { var g errgroup.Group for _, c := range codespacesToDelete { codespaceName := c.Name @@ -178,15 +187,23 @@ func (a *App) Delete(ctx context.Context, opts deleteOptions) (err error) { a.errLogger.Printf("error deleting codespace %q: %v\n", codespaceName, err) return err } + atomic.AddUint32(&deletedCodespaces, 1) return nil }) } if err := g.Wait(); err != nil { - return errors.New("some codespaces failed to delete") + return fmt.Errorf("%d codespace(s) failed to delete", len(codespacesToDelete)-int(deletedCodespaces)) } return nil }) + + if a.io.IsStdoutTTY() && deletedCodespaces > 0 { + successMsg := fmt.Sprintf("%d codespace(s) deleted successfully\n", deletedCodespaces) + fmt.Fprint(a.io.ErrOut, successMsg) + } + + return err } func confirmDeletion(p prompter, apiCodespace *api.Codespace, isInteractive bool) (bool, error) { diff --git a/pkg/cmd/codespace/delete_test.go b/pkg/cmd/codespace/delete_test.go index ca6fe989e..4541af802 100644 --- a/pkg/cmd/codespace/delete_test.go +++ b/pkg/cmd/codespace/delete_test.go @@ -26,7 +26,7 @@ func TestDelete(t *testing.T) { codespaces []*api.Codespace confirms map[string]bool deleteErr error - wantErr bool + wantErr string wantDeleted []string wantStdout string wantStderr string @@ -42,7 +42,7 @@ func TestDelete(t *testing.T) { }, }, wantDeleted: []string{"hubot-robawt-abc"}, - wantStdout: "", + wantStderr: "1 codespace(s) deleted successfully\n", }, { name: "by repo", @@ -70,7 +70,7 @@ func TestDelete(t *testing.T) { }, }, wantDeleted: []string{"monalisa-spoonknife-123", "monalisa-spoonknife-c4f3"}, - wantStdout: "", + wantStderr: "2 codespace(s) deleted successfully\n", }, { name: "unused", @@ -93,7 +93,7 @@ func TestDelete(t *testing.T) { }, }, wantDeleted: []string{"hubot-robawt-abc", "monalisa-spoonknife-c4f3"}, - wantStdout: "", + wantStderr: "2 codespace(s) deleted successfully\n", }, { name: "deletion failed", @@ -109,12 +109,13 @@ func TestDelete(t *testing.T) { }, }, deleteErr: errors.New("aborted by test"), - wantErr: true, + wantErr: "2 codespace(s) failed to delete", wantDeleted: []string{"hubot-robawt-abc", "monalisa-spoonknife-123"}, wantStderr: heredoc.Doc(` error deleting codespace "hubot-robawt-abc": aborted by test error deleting codespace "monalisa-spoonknife-123": aborted by test `), + wantStdout: "", }, { name: "with confirm", @@ -149,7 +150,7 @@ func TestDelete(t *testing.T) { "Codespace hubot-robawt-abc has unsaved changes. OK to delete?": true, }, wantDeleted: []string{"hubot-robawt-abc", "monalisa-spoonknife-c4f3"}, - wantStdout: "", + wantStderr: "2 codespace(s) deleted successfully\n", }, { name: "deletion for org codespace by admin succeeds", @@ -174,7 +175,7 @@ func TestDelete(t *testing.T) { }, }, wantDeleted: []string{"monalisa-spoonknife-123"}, - wantStdout: "", + wantStderr: "1 codespace(s) deleted successfully\n", }, { name: "deletion for org codespace by admin fails for codespace not found", @@ -200,7 +201,8 @@ func TestDelete(t *testing.T) { }, wantDeleted: []string{}, wantStdout: "", - wantErr: true, + wantErr: "error fetching codespace information: " + + "codespace not found for user johnDoe with name monalisa-spoonknife-123", }, { name: "deletion for org codespace succeeds without username", @@ -215,6 +217,45 @@ func TestDelete(t *testing.T) { }, }, wantDeleted: []string{"monalisa-spoonknife-123"}, + wantStderr: "1 codespace(s) deleted successfully\n", + }, + { + name: "by repo owner", + opts: deleteOptions{ + deleteAll: true, + repoOwner: "octocat", + }, + codespaces: []*api.Codespace{ + { + Name: "octocat-spoonknife-123", + Repository: api.Repository{ + FullName: "octocat/Spoon-Knife", + Owner: api.RepositoryOwner{ + Login: "octocat", + }, + }, + }, + { + Name: "cli-robawt-abc", + Repository: api.Repository{ + FullName: "cli/ROBAWT", + Owner: api.RepositoryOwner{ + Login: "cli", + }, + }, + }, + { + Name: "octocat-spoonknife-c4f3", + Repository: api.Repository{ + FullName: "octocat/Spoon-Knife", + Owner: api.RepositoryOwner{ + Login: "octocat", + }, + }, + }, + }, + wantDeleted: []string{"octocat-spoonknife-123", "octocat-spoonknife-c4f3"}, + wantStderr: "2 codespace(s) deleted successfully\n", wantStdout: "", }, } @@ -268,8 +309,8 @@ func TestDelete(t *testing.T) { ios.SetStdoutTTY(true) app := NewApp(ios, nil, apiMock, nil, nil) err := app.Delete(context.Background(), opts) - if (err != nil) != tt.wantErr { - t.Errorf("delete() error = %v, wantErr %v", err, tt.wantErr) + if (err != nil) && tt.wantErr != err.Error() { + t.Errorf("delete() error = %v, wantErr = %v", err, tt.wantErr) } for _, listArgs := range apiMock.ListCodespacesCalls() { if listArgs.Opts.OrgName != "" && listArgs.Opts.UserName == "" { diff --git a/pkg/cmd/codespace/jupyter.go b/pkg/cmd/codespace/jupyter.go index db55c434d..91c798eda 100644 --- a/pkg/cmd/codespace/jupyter.go +++ b/pkg/cmd/codespace/jupyter.go @@ -67,7 +67,7 @@ func (a *App) Jupyter(ctx context.Context, selector *CodespaceSelector) (err err } // Pass 0 to pick a random port - listen, _, err := codespaces.ListenTCP(0) + listen, _, err := codespaces.ListenTCP(0, false) if err != nil { return err } diff --git a/pkg/cmd/codespace/list.go b/pkg/cmd/codespace/list.go index 6b1cd1cc0..bba34ff45 100644 --- a/pkg/cmd/codespace/list.go +++ b/pkg/cmd/codespace/list.go @@ -70,7 +70,7 @@ func newListCmd(app *App) *cobra.Command { listCmd.Flags().StringVarP(&opts.orgName, "org", "o", "", "The `login` handle of the organization to list codespaces for (admin-only)") listCmd.Flags().StringVarP(&opts.userName, "user", "u", "", "The `username` to list codespaces for (used with --org)") - cmdutil.AddJSONFlags(listCmd, &exporter, api.CodespaceFields) + cmdutil.AddJSONFlags(listCmd, &exporter, api.ListCodespaceFields) listCmd.Flags().BoolVarP(&opts.useWeb, "web", "w", false, "List codespaces in the web browser, cannot be used with --user or --org") diff --git a/pkg/cmd/codespace/logs.go b/pkg/cmd/codespace/logs.go index b0dc80459..3ec950849 100644 --- a/pkg/cmd/codespace/logs.go +++ b/pkg/cmd/codespace/logs.go @@ -49,7 +49,7 @@ func (a *App) Logs(ctx context.Context, selector *CodespaceSelector, follow bool defer safeClose(session, &err) // Ensure local port is listening before client (getPostCreateOutput) connects. - listen, localPort, err := codespaces.ListenTCP(0) + listen, localPort, err := codespaces.ListenTCP(0, false) if err != nil { return err } diff --git a/pkg/cmd/codespace/ports.go b/pkg/cmd/codespace/ports.go index fcf50744b..cbe48b7b3 100644 --- a/pkg/cmd/codespace/ports.go +++ b/pkg/cmd/codespace/ports.go @@ -214,7 +214,7 @@ func getDevContainer(ctx context.Context, apiClient apiClient, codespace *api.Co var container devContainer if err := json.Unmarshal(convertedJSON, &container); err != nil { - ch <- devContainerResult{nil, fmt.Errorf("error unmarshaling: %w", err)} + ch <- devContainerResult{nil, fmt.Errorf("error unmarshalling: %w", err)} return } @@ -379,7 +379,7 @@ func (a *App) ForwardPorts(ctx context.Context, selector *CodespaceSelector, por for _, pair := range portPairs { pair := pair group.Go(func() error { - listen, _, err := codespaces.ListenTCP(pair.local) + listen, _, err := codespaces.ListenTCP(pair.local, true) if err != nil { return err } diff --git a/pkg/cmd/codespace/ports_test.go b/pkg/cmd/codespace/ports_test.go index 9979345b2..bb7554238 100644 --- a/pkg/cmd/codespace/ports_test.go +++ b/pkg/cmd/codespace/ports_test.go @@ -225,7 +225,7 @@ func TestPendingOperationDisallowsListPorts(t *testing.T) { } } -func TestPendingOperationDisallowsUpdatePortVisability(t *testing.T) { +func TestPendingOperationDisallowsUpdatePortVisibility(t *testing.T) { app := testingPortsApp() selector := &CodespaceSelector{api: app.apiClient, codespaceName: "disabledCodespace"} diff --git a/pkg/cmd/codespace/root.go b/pkg/cmd/codespace/root.go index 8439430aa..28a80edae 100644 --- a/pkg/cmd/codespace/root.go +++ b/pkg/cmd/codespace/root.go @@ -16,6 +16,7 @@ func NewRootCmd(app *App) *cobra.Command { root.AddCommand(newDeleteCmd(app)) root.AddCommand(newJupyterCmd(app)) root.AddCommand(newListCmd(app)) + root.AddCommand(newViewCmd(app)) root.AddCommand(newLogsCmd(app)) root.AddCommand(newPortsCmd(app)) root.AddCommand(newSSHCmd(app)) diff --git a/pkg/cmd/codespace/ssh.go b/pkg/cmd/codespace/ssh.go index 37a4ee88c..40eebcc63 100644 --- a/pkg/cmd/codespace/ssh.go +++ b/pkg/cmd/codespace/ssh.go @@ -36,14 +36,15 @@ const automaticPrivateKeyName = "codespaces.auto" var errKeyFileNotFound = errors.New("SSH key file does not exist") type sshOptions struct { - selector *CodespaceSelector - profile string - serverPort int - debug bool - debugFile string - stdio bool - config bool - scpArgs []string // scp arguments, for 'cs cp' (nil for 'cs ssh') + selector *CodespaceSelector + profile string + serverPort int + printConnDetails bool + debug bool + debugFile string + stdio bool + config bool + scpArgs []string // scp arguments, for 'cs cp' (nil for 'cs ssh') } func newSSHCmd(app *App) *cobra.Command { @@ -56,8 +57,14 @@ func newSSHCmd(app *App) *cobra.Command { The 'ssh' command is used to SSH into a codespace. In its simplest form, you can run 'gh cs ssh', select a codespace interactively, and connect. - By default, the 'ssh' command will create a public/private ssh key pair to - authenticate with the codespace inside the ~/.ssh directory. + The 'ssh' command will automatically create a public/private ssh key pair in the + ~/.ssh directory if you do not have an existing valid key pair. When selecting the + key pair to use, the preferred order is: + + 1. Key specified by -i in + 2. Automatic key, if it already exists + 3. First valid key pair in ssh config (according to ssh -G) + 4. Automatic key, newly created The 'ssh' command also supports deeper integration with OpenSSH using a '--config' option that generates per-codespace ssh configuration in OpenSSH format. @@ -111,6 +118,9 @@ func newSSHCmd(app *App) *cobra.Command { return nil }, RunE: func(cmd *cobra.Command, args []string) error { + if cmd.Flag("server-port").Changed { + opts.printConnDetails = true + } if opts.config { return app.printOpenSSHConfig(cmd.Context(), opts) } else { @@ -200,12 +210,11 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e } localSSHServerPort := opts.serverPort - usingCustomPort := localSSHServerPort != 0 // suppress log of command line in Shell // Ensure local port is listening before client (Shell) connects. // Unless the user specifies a server port, localSSHServerPort is 0 // and thus the client will pick a random port. - listen, localSSHServerPort, err := codespaces.ListenTCP(localSSHServerPort) + listen, localSSHServerPort, err := codespaces.ListenTCP(localSSHServerPort, false) if err != nil { return err } @@ -229,7 +238,9 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e // args is the correct variable to use here, we just use scpArgs as the check for which command to run err = codespaces.Copy(ctx, args, localSSHServerPort, connectDestination) } else { - err = codespaces.Shell(ctx, a.errLogger, args, localSSHServerPort, connectDestination, usingCustomPort) + err = codespaces.Shell( + ctx, a.errLogger, args, localSSHServerPort, connectDestination, opts.printConnDetails, + ) } shellClosed <- err }() diff --git a/pkg/cmd/codespace/view.go b/pkg/cmd/codespace/view.go new file mode 100644 index 000000000..7fe4ae313 --- /dev/null +++ b/pkg/cmd/codespace/view.go @@ -0,0 +1,133 @@ +package codespace + +import ( + "context" + "fmt" + "os" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/codespaces/api" + "github.com/cli/cli/v2/internal/tableprinter" + "github.com/cli/cli/v2/internal/text" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/spf13/cobra" +) + +const ( + minutesInDay = 1440 +) + +type viewOptions struct { + selector *CodespaceSelector + exporter cmdutil.Exporter +} + +func newViewCmd(app *App) *cobra.Command { + opts := &viewOptions{} + + viewCmd := &cobra.Command{ + Use: "view", + Short: "View details about a codespace", + Example: heredoc.Doc(` + # select a codespace from a list of all codespaces you own + $ gh cs view + + # view the details of a specific codespace + $ gh cs view -c codespace-name-12345 + + # view the list of all available fields for a codespace + $ gh cs view --json + + # view specific fields for a codespace + $ gh cs view --json displayName,machineDisplayName,state + `), + Args: noArgsConstraint, + RunE: func(cmd *cobra.Command, args []string) error { + return app.ViewCodespace(cmd.Context(), opts) + }, + } + opts.selector = AddCodespaceSelector(viewCmd, app.apiClient) + cmdutil.AddJSONFlags(viewCmd, &opts.exporter, api.ViewCodespaceFields) + + return viewCmd +} + +func (a *App) ViewCodespace(ctx context.Context, opts *viewOptions) error { + // If we are in a codespace and a codespace name wasn't provided, show the details for the codespace we are connected to + if (os.Getenv("CODESPACES") == "true") && opts.selector.codespaceName == "" { + codespaceName := os.Getenv("CODESPACE_NAME") + opts.selector.codespaceName = codespaceName + } + + selectedCodespace, err := opts.selector.Select(ctx) + if err != nil { + return err + } + + if err := a.io.StartPager(); err != nil { + a.errLogger.Printf("error starting pager: %v", err) + } + defer a.io.StopPager() + + if opts.exporter != nil { + return opts.exporter.Write(a.io, selectedCodespace) + } + + tp := tableprinter.New(a.io) + c := codespace{selectedCodespace} + formattedName := formatNameForVSCSTarget(c.Name, c.VSCSTarget) + + // Create an array of fields to display in the table with their values + fields := []struct { + name string + value string + }{ + {"Name", formattedName}, + {"State", c.State}, + {"Repository", c.Repository.FullName}, + {"Git Status", formatGitStatus(c)}, + {"Devcontainer Path", c.DevContainerPath}, + {"Machine Display Name", c.Machine.DisplayName}, + {"Idle Timeout", fmt.Sprintf("%d minutes", c.IdleTimeoutMinutes)}, + {"Created At", c.CreatedAt}, + {"Retention Period", formatRetentionPeriodDays(c)}, + } + + for _, field := range fields { + // Don't display the field if it is empty and we are printing to a TTY + if !a.io.IsStdoutTTY() || field.value != "" { + tp.AddField(field.name) + tp.AddField(field.value) + tp.EndRow() + } + } + + err = tp.Render() + if err != nil { + return err + } + + return nil +} + +func formatGitStatus(codespace codespace) string { + branchWithGitStatus := codespace.branchWithGitStatus() + + // Format the commits ahead/behind with proper pluralization + commitsAhead := text.Pluralize(codespace.GitStatus.Ahead, "commit") + commitsBehind := text.Pluralize(codespace.GitStatus.Behind, "commit") + + return fmt.Sprintf("%s - %s ahead, %s behind", branchWithGitStatus, commitsAhead, commitsBehind) +} + +func formatRetentionPeriodDays(codespace codespace) string { + days := codespace.RetentionPeriodMinutes / minutesInDay + // Don't display the retention period if it is 0 days + if days == 0 { + return "" + } else if days == 1 { + return "1 day" + } + + return fmt.Sprintf("%d days", days) +} diff --git a/pkg/cmd/codespace/view_test.go b/pkg/cmd/codespace/view_test.go new file mode 100644 index 000000000..bba487232 --- /dev/null +++ b/pkg/cmd/codespace/view_test.go @@ -0,0 +1,131 @@ +package codespace + +import ( + "context" + "fmt" + "testing" + + "github.com/cli/cli/v2/internal/codespaces/api" + "github.com/cli/cli/v2/pkg/iostreams" +) + +func Test_NewCmdView(t *testing.T) { + tests := []struct { + tName string + codespaceName string + opts *viewOptions + cliArgs []string + wantErr bool + wantStdout string + errMsg string + }{ + { + tName: "selector throws because no terminal found", + opts: &viewOptions{}, + wantErr: true, + errMsg: "choosing codespace: error getting answers: no terminal", + }, + { + tName: "command fails because provided codespace doesn't exist", + codespaceName: "i-dont-exist", + opts: &viewOptions{}, + wantErr: true, + errMsg: "getting full codespace details: codespace not found", + }, + { + tName: "command succeeds because codespace exists (no details)", + codespaceName: "monalisa-cli-cli-abcdef", + opts: &viewOptions{}, + wantErr: false, + wantStdout: "Name\tmonalisa-cli-cli-abcdef\nState\t\nRepository\t\nGit Status\t - 0 commits ahead, 0 commits behind\nDevcontainer Path\t\nMachine Display Name\t\nIdle Timeout\t0 minutes\nCreated At\t\nRetention Period\t\n", + }, + { + tName: "command succeeds because codespace exists (with details)", + codespaceName: "monalisa-cli-cli-hijklm", + opts: &viewOptions{}, + wantErr: false, + wantStdout: "Name\tmonalisa-cli-cli-hijklm\nState\tAvailable\nRepository\tcli/cli\nGit Status\tmain* - 1 commit ahead, 2 commits behind\nDevcontainer Path\t.devcontainer/devcontainer.json\nMachine Display Name\tTest Display Name\nIdle Timeout\t30 minutes\nCreated At\t\nRetention Period\t1 day\n", + }, + } + + for _, tt := range tests { + t.Run(tt.tName, func(t *testing.T) { + ios, _, stdout, _ := iostreams.Test() + a := &App{ + apiClient: testViewApiMock(), + io: ios, + } + selector := &CodespaceSelector{api: a.apiClient, codespaceName: tt.codespaceName} + tt.opts.selector = selector + + var err error + if tt.cliArgs == nil { + if tt.opts.selector == nil { + t.Fatalf("selector must be set in opts if cliArgs are not provided") + } + + err = a.ViewCodespace(context.Background(), tt.opts) + } else { + cmd := newViewCmd(a) + cmd.SilenceUsage = true + cmd.SilenceErrors = true + cmd.SetOut(ios.ErrOut) + cmd.SetErr(ios.ErrOut) + cmd.SetArgs(tt.cliArgs) + _, err = cmd.ExecuteC() + } + + if tt.wantErr { + if err == nil { + t.Error("Edit() expected error, got nil") + } else if err.Error() != tt.errMsg { + t.Errorf("Edit() error = %q, want %q", err, tt.errMsg) + } + } else if err != nil { + t.Errorf("Edit() expected no error, got %v", err) + } + + if out := stdout.String(); out != tt.wantStdout { + t.Errorf("stdout = %q, want %q", out, tt.wantStdout) + } + }) + } +} + +func testViewApiMock() *apiClientMock { + codespaceWithNoDetails := &api.Codespace{ + Name: "monalisa-cli-cli-abcdef", + } + codespaceWithDetails := &api.Codespace{ + Name: "monalisa-cli-cli-hijklm", + GitStatus: api.CodespaceGitStatus{ + Ahead: 1, + Behind: 2, + Ref: "main", + HasUnpushedChanges: true, + HasUncommittedChanges: true, + }, + IdleTimeoutMinutes: 30, + RetentionPeriodMinutes: 1440, + State: "Available", + Repository: api.Repository{FullName: "cli/cli"}, + DevContainerPath: ".devcontainer/devcontainer.json", + Machine: api.CodespaceMachine{ + DisplayName: "Test Display Name", + }, + } + return &apiClientMock{ + GetCodespaceFunc: func(_ context.Context, name string, _ bool) (*api.Codespace, error) { + if name == codespaceWithDetails.Name { + return codespaceWithDetails, nil + } else if name == codespaceWithNoDetails.Name { + return codespaceWithNoDetails, nil + } + + return nil, fmt.Errorf("codespace not found") + }, + ListCodespacesFunc: func(ctx context.Context, opts api.ListCodespacesOptions) ([]*api.Codespace, error) { + return []*api.Codespace{codespaceWithNoDetails, codespaceWithDetails}, nil + }, + } +} diff --git a/pkg/cmd/extension/browse/browse_test.go b/pkg/cmd/extension/browse/browse_test.go index 5ad117356..3c4ae87ca 100644 --- a/pkg/cmd/extension/browse/browse_test.go +++ b/pkg/cmd/extension/browse/browse_test.go @@ -84,43 +84,43 @@ func Test_getExtensionRepos(t *testing.T) { reg.Register( httpmock.QueryMatcher("GET", "search/repositories", values), - httpmock.JSONResponse(search.RepositoriesResult{ - IncompleteResults: false, - Items: []search.Repository{ - { - FullName: "vilmibm/gh-screensaver", - Name: "gh-screensaver", - Description: "terminal animations", - Owner: search.User{ - Login: "vilmibm", + httpmock.JSONResponse(map[string]interface{}{ + "incomplete_results": false, + "total_count": 4, + "items": []interface{}{ + map[string]interface{}{ + "name": "gh-screensaver", + "full_name": "vilmibm/gh-screensaver", + "description": "terminal animations", + "owner": map[string]interface{}{ + "login": "vilmibm", }, }, - { - FullName: "cli/gh-cool", - Name: "gh-cool", - Description: "it's just cool ok", - Owner: search.User{ - Login: "cli", + map[string]interface{}{ + "name": "gh-cool", + "full_name": "cli/gh-cool", + "description": "it's just cool ok", + "owner": map[string]interface{}{ + "login": "cli", }, }, - { - FullName: "samcoe/gh-triage", - Name: "gh-triage", - Description: "helps with triage", - Owner: search.User{ - Login: "samcoe", + map[string]interface{}{ + "name": "gh-triage", + "full_name": "samcoe/gh-triage", + "description": "helps with triage", + "owner": map[string]interface{}{ + "login": "samcoe", }, }, - { - FullName: "github/gh-gei", - Name: "gh-gei", - Description: "something something enterprise", - Owner: search.User{ - Login: "github", + map[string]interface{}{ + "name": "gh-gei", + "full_name": "github/gh-gei", + "description": "something something enterprise", + "owner": map[string]interface{}{ + "login": "github", }, }, }, - Total: 4, }), ) diff --git a/pkg/cmd/extension/command.go b/pkg/cmd/extension/command.go index 0f2add8aa..6629e599d 100644 --- a/pkg/cmd/extension/command.go +++ b/pkg/cmd/extension/command.go @@ -643,10 +643,8 @@ func checkValidExtension(rootCmd *cobra.Command, m extensions.ExtensionManager, } commandName := strings.TrimPrefix(extName, "gh-") - if c, _, err := rootCmd.Traverse([]string{commandName}); err != nil { - return nil, err - } else if c != rootCmd { - return nil, fmt.Errorf("%q matches the name of a built-in command", commandName) + if c, _, _ := rootCmd.Find([]string{commandName}); c != rootCmd && c.GroupID != "extension" { + return nil, fmt.Errorf("%q matches the name of a built-in command or alias", commandName) } for _, ext := range m.List() { diff --git a/pkg/cmd/extension/command_test.go b/pkg/cmd/extension/command_test.go index ebc4716de..fd693d7e5 100644 --- a/pkg/cmd/extension/command_test.go +++ b/pkg/cmd/extension/command_test.go @@ -19,7 +19,6 @@ import ( "github.com/cli/cli/v2/pkg/extensions" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/search" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" ) @@ -74,7 +73,7 @@ func TestNewCmdExtension(t *testing.T) { } reg.Register( httpmock.QueryMatcher("GET", "search/repositories", values), - httpmock.JSONResponse(searchResults()), + httpmock.JSONResponse(searchResults(4)), ) }, isTTY: true, @@ -111,7 +110,7 @@ func TestNewCmdExtension(t *testing.T) { } reg.Register( httpmock.QueryMatcher("GET", "search/repositories", values), - httpmock.JSONResponse(searchResults()), + httpmock.JSONResponse(searchResults(4)), ) }, wantStdout: "installed\tvilmibm/gh-screensaver\tterminal animations\n\tcli/gh-cool\tit's just cool ok\n\tsamcoe/gh-triage\thelps with triage\ninstalled\tgithub/gh-gei\tsomething something enterprise\n", @@ -145,9 +144,7 @@ func TestNewCmdExtension(t *testing.T) { "per_page": []string{"30"}, "q": []string{"screen topic:gh-extension"}, } - results := searchResults() - results.Total = 1 - results.Items = []search.Repository{results.Items[0]} + results := searchResults(1) reg.Register( httpmock.QueryMatcher("GET", "search/repositories", values), httpmock.JSONResponse(results), @@ -175,9 +172,7 @@ func TestNewCmdExtension(t *testing.T) { "per_page": []string{"1"}, "q": []string{"topic:gh-extension"}, } - results := searchResults() - results.Total = 1 - results.Items = []search.Repository{results.Items[0]} + results := searchResults(1) reg.Register( httpmock.QueryMatcher("GET", "search/repositories", values), httpmock.JSONResponse(results), @@ -203,9 +198,7 @@ func TestNewCmdExtension(t *testing.T) { "per_page": []string{"30"}, "q": []string{"license:GPLv3 topic:gh-extension user:jillvalentine"}, } - results := searchResults() - results.Total = 1 - results.Items = []search.Repository{results.Items[0]} + results := searchResults(1) reg.Register( httpmock.QueryMatcher("GET", "search/repositories", values), httpmock.JSONResponse(results), @@ -974,7 +967,7 @@ func Test_checkValidExtension(t *testing.T) { manager: m, extName: "gh-auth", }, - wantError: "\"auth\" matches the name of a built-in command", + wantError: "\"auth\" matches the name of a built-in command or alias", }, { name: "clashes with an installed extension", @@ -998,43 +991,48 @@ func Test_checkValidExtension(t *testing.T) { } } -func searchResults() search.RepositoriesResult { - return search.RepositoriesResult{ - IncompleteResults: false, - Items: []search.Repository{ - { - FullName: "vilmibm/gh-screensaver", - Name: "gh-screensaver", - Description: "terminal animations", - Owner: search.User{ - Login: "vilmibm", +func searchResults(numResults int) interface{} { + result := map[string]interface{}{ + "incomplete_results": false, + "total_count": 4, + "items": []interface{}{ + map[string]interface{}{ + "name": "gh-screensaver", + "full_name": "vilmibm/gh-screensaver", + "description": "terminal animations", + "owner": map[string]interface{}{ + "login": "vilmibm", }, }, - { - FullName: "cli/gh-cool", - Name: "gh-cool", - Description: "it's just cool ok", - Owner: search.User{ - Login: "cli", + map[string]interface{}{ + "name": "gh-cool", + "full_name": "cli/gh-cool", + "description": "it's just cool ok", + "owner": map[string]interface{}{ + "login": "cli", }, }, - { - FullName: "samcoe/gh-triage", - Name: "gh-triage", - Description: "helps with triage", - Owner: search.User{ - Login: "samcoe", + map[string]interface{}{ + "name": "gh-triage", + "full_name": "samcoe/gh-triage", + "description": "helps with triage", + "owner": map[string]interface{}{ + "login": "samcoe", }, }, - { - FullName: "github/gh-gei", - Name: "gh-gei", - Description: "something something enterprise", - Owner: search.User{ - Login: "github", + map[string]interface{}{ + "name": "gh-gei", + "full_name": "github/gh-gei", + "description": "something something enterprise", + "owner": map[string]interface{}{ + "login": "github", }, }, }, - Total: 4, } + if len(result["items"].([]interface{})) > numResults { + fewerItems := result["items"].([]interface{})[0:numResults] + result["items"] = fewerItems + } + return result } diff --git a/pkg/cmd/extension/ext_tmpls/goBinMain.go.txt b/pkg/cmd/extension/ext_tmpls/goBinMain.go.txt index c9d2bdd43..4f65f68d5 100644 --- a/pkg/cmd/extension/ext_tmpls/goBinMain.go.txt +++ b/pkg/cmd/extension/ext_tmpls/goBinMain.go.txt @@ -3,12 +3,12 @@ package main import ( "fmt" - "github.com/cli/go-gh" + "github.com/cli/go-gh/v2/pkg/api" ) func main() { fmt.Println("hi world, this is the %s extension!") - client, err := gh.RESTClient(nil) + client, err := api.DefaultRESTClient() if err != nil { fmt.Println(err) return diff --git a/pkg/cmd/extension/extension.go b/pkg/cmd/extension/extension.go index 9d1d61c21..188687634 100644 --- a/pkg/cmd/extension/extension.go +++ b/pkg/cmd/extension/extension.go @@ -1,8 +1,16 @@ package extension import ( + "bytes" + "fmt" + "net/http" + "os" "path/filepath" "strings" + "sync" + + "github.com/cli/cli/v2/internal/ghrepo" + "gopkg.in/yaml.v3" ) const manifestName = "manifest.yml" @@ -12,16 +20,22 @@ type ExtensionKind int const ( GitKind ExtensionKind = iota BinaryKind + LocalKind ) type Extension struct { - path string + path string + kind ExtensionKind + gitClient gitClient + httpClient *http.Client + + mu sync.RWMutex + + // These fields get resolved dynamically: url string - isLocal bool - isPinned bool + isPinned *bool currentVersion string latestVersion string - kind ExtensionKind } func (e *Extension) Name() string { @@ -32,32 +46,157 @@ func (e *Extension) Path() string { return e.path } -func (e *Extension) URL() string { - return e.url -} - func (e *Extension) IsLocal() bool { - return e.isLocal -} - -func (e *Extension) CurrentVersion() string { - return e.currentVersion -} - -func (e *Extension) IsPinned() bool { - return e.isPinned -} - -func (e *Extension) UpdateAvailable() bool { - if e.isLocal || - e.currentVersion == "" || - e.latestVersion == "" || - e.currentVersion == e.latestVersion { - return false - } - return true + return e.kind == LocalKind } func (e *Extension) IsBinary() bool { return e.kind == BinaryKind } + +func (e *Extension) URL() string { + e.mu.RLock() + if e.url != "" { + defer e.mu.RUnlock() + return e.url + } + e.mu.RUnlock() + + var url string + switch e.kind { + case LocalKind: + case BinaryKind: + if manifest, err := e.loadManifest(); err == nil { + repo := ghrepo.NewWithHost(manifest.Owner, manifest.Name, manifest.Host) + url = ghrepo.GenerateRepoURL(repo, "") + } + case GitKind: + if remoteURL, err := e.gitClient.Config("remote.origin.url"); err == nil { + url = strings.TrimSpace(string(remoteURL)) + } + } + + e.mu.Lock() + e.url = url + e.mu.Unlock() + + return e.url +} + +func (e *Extension) CurrentVersion() string { + e.mu.RLock() + if e.currentVersion != "" { + defer e.mu.RUnlock() + return e.currentVersion + } + e.mu.RUnlock() + + var currentVersion string + switch e.kind { + case LocalKind: + case BinaryKind: + if manifest, err := e.loadManifest(); err == nil { + currentVersion = manifest.Tag + } + case GitKind: + if sha, err := e.gitClient.CommandOutput([]string{"rev-parse", "HEAD"}); err == nil { + currentVersion = string(bytes.TrimSpace(sha)) + } + } + + e.mu.Lock() + e.currentVersion = currentVersion + e.mu.Unlock() + + return e.currentVersion +} + +func (e *Extension) LatestVersion() string { + e.mu.RLock() + if e.latestVersion != "" { + defer e.mu.RUnlock() + return e.latestVersion + } + e.mu.RUnlock() + + var latestVersion string + switch e.kind { + case LocalKind: + case BinaryKind: + repo, err := ghrepo.FromFullName(e.URL()) + if err != nil { + return "" + } + release, err := fetchLatestRelease(e.httpClient, repo) + if err != nil { + return "" + } + latestVersion = release.Tag + case GitKind: + if lsRemote, err := e.gitClient.CommandOutput([]string{"ls-remote", "origin", "HEAD"}); err == nil { + latestVersion = string(bytes.SplitN(lsRemote, []byte("\t"), 2)[0]) + } + } + + e.mu.Lock() + e.latestVersion = latestVersion + e.mu.Unlock() + + return e.latestVersion +} + +func (e *Extension) IsPinned() bool { + e.mu.RLock() + if e.isPinned != nil { + defer e.mu.RUnlock() + return *e.isPinned + } + e.mu.RUnlock() + + var isPinned bool + switch e.kind { + case LocalKind: + case BinaryKind: + if manifest, err := e.loadManifest(); err == nil { + isPinned = manifest.IsPinned + } + case GitKind: + pinPath := filepath.Join(e.Path(), fmt.Sprintf(".pin-%s", e.CurrentVersion())) + if _, err := os.Stat(pinPath); err == nil { + isPinned = true + } else { + isPinned = false + } + } + + e.mu.Lock() + e.isPinned = &isPinned + e.mu.Unlock() + + return *e.isPinned +} + +func (e *Extension) UpdateAvailable() bool { + if e.IsLocal() || + e.CurrentVersion() == "" || + e.LatestVersion() == "" || + e.CurrentVersion() == e.LatestVersion() { + return false + } + return true +} + +func (e *Extension) loadManifest() (binManifest, error) { + var bm binManifest + dir, _ := filepath.Split(e.Path()) + manifestPath := filepath.Join(dir, manifestName) + manifest, err := os.ReadFile(manifestPath) + if err != nil { + return bm, fmt.Errorf("could not open %s for reading: %w", manifestPath, err) + } + err = yaml.Unmarshal(manifest, &bm) + if err != nil { + return bm, fmt.Errorf("could not parse %s: %w", manifestPath, err) + } + return bm, nil +} diff --git a/pkg/cmd/extension/extension_test.go b/pkg/cmd/extension/extension_test.go index 0a50cbfe8..9b56e0bee 100644 --- a/pkg/cmd/extension/extension_test.go +++ b/pkg/cmd/extension/extension_test.go @@ -8,7 +8,7 @@ import ( func TestUpdateAvailable_IsLocal(t *testing.T) { e := &Extension{ - isLocal: true, + kind: LocalKind, } assert.False(t, e.UpdateAvailable()) @@ -16,7 +16,7 @@ func TestUpdateAvailable_IsLocal(t *testing.T) { func TestUpdateAvailable_NoCurrentVersion(t *testing.T) { e := &Extension{ - isLocal: false, + kind: LocalKind, } assert.False(t, e.UpdateAvailable()) @@ -24,7 +24,7 @@ func TestUpdateAvailable_NoCurrentVersion(t *testing.T) { func TestUpdateAvailable_NoLatestVersion(t *testing.T) { e := &Extension{ - isLocal: false, + kind: BinaryKind, currentVersion: "1.0.0", } @@ -33,7 +33,7 @@ func TestUpdateAvailable_NoLatestVersion(t *testing.T) { func TestUpdateAvailable_CurrentVersionIsLatestVersion(t *testing.T) { e := &Extension{ - isLocal: false, + kind: BinaryKind, currentVersion: "1.0.0", latestVersion: "1.0.0", } @@ -43,7 +43,7 @@ func TestUpdateAvailable_CurrentVersionIsLatestVersion(t *testing.T) { func TestUpdateAvailable(t *testing.T) { e := &Extension{ - isLocal: false, + kind: BinaryKind, currentVersion: "1.0.0", latestVersion: "1.1.0", } diff --git a/pkg/cmd/extension/git.go b/pkg/cmd/extension/git.go index e85ff7371..58ef0ca12 100644 --- a/pkg/cmd/extension/git.go +++ b/pkg/cmd/extension/git.go @@ -46,16 +46,9 @@ func (g *gitExecuter) Fetch(remote string, refspec string) error { } func (g *gitExecuter) ForRepo(repoDir string) gitClient { - return &gitExecuter{ - client: &git.Client{ - GhPath: g.client.GhPath, - RepoDir: repoDir, - GitPath: g.client.GitPath, - Stderr: g.client.Stderr, - Stdin: g.client.Stdin, - Stdout: g.client.Stdout, - }, - } + gc := g.client.Copy() + gc.RepoDir = repoDir + return &gitExecuter{client: gc} } func (g *gitExecuter) Pull(remote, branch string) error { diff --git a/pkg/cmd/extension/manager.go b/pkg/cmd/extension/manager.go index 7a76e6a47..b19175880 100644 --- a/pkg/cmd/extension/manager.go +++ b/pkg/cmd/extension/manager.go @@ -1,12 +1,10 @@ package extension import ( - "bytes" _ "embed" "errors" "fmt" "io" - "io/fs" "net/http" "os" "os/exec" @@ -83,7 +81,7 @@ func (m *Manager) Dispatch(args []string, stdin io.Reader, stdout, stderr io.Wri forwardArgs := args[1:] exts, _ := m.list(false) - var ext Extension + var ext *Extension for _, e := range exts { if e.Name() == extName { ext = e @@ -121,39 +119,53 @@ func (m *Manager) Dispatch(args []string, stdin io.Reader, stdout, stderr io.Wri func (m *Manager) List() []extensions.Extension { exts, _ := m.list(false) r := make([]extensions.Extension, len(exts)) - for i, v := range exts { - val := v - r[i] = &val + for i, ext := range exts { + r[i] = ext } return r } -func (m *Manager) list(includeMetadata bool) ([]Extension, error) { +func (m *Manager) list(includeMetadata bool) ([]*Extension, error) { dir := m.installDir() entries, err := os.ReadDir(dir) if err != nil { return nil, err } - var results []Extension + results := make([]*Extension, 0, len(entries)) for _, f := range entries { if !strings.HasPrefix(f.Name(), "gh-") { continue } - var ext Extension - var err error if f.IsDir() { - ext, err = m.parseExtensionDir(f) - if err != nil { - return nil, err + if _, err := os.Stat(filepath.Join(dir, f.Name(), manifestName)); err == nil { + results = append(results, &Extension{ + path: filepath.Join(dir, f.Name(), f.Name()), + kind: BinaryKind, + httpClient: m.client, + }) + } else { + results = append(results, &Extension{ + path: filepath.Join(dir, f.Name(), f.Name()), + kind: GitKind, + gitClient: m.gitClient.ForRepo(filepath.Join(dir, f.Name())), + }) } - results = append(results, ext) + } else if isSymlink(f.Type()) { + results = append(results, &Extension{ + path: filepath.Join(dir, f.Name(), f.Name()), + kind: LocalKind, + }) } else { - ext, err = m.parseExtensionFile(f) + // the contents of a regular file point to a local extension on disk + p, err := readPathFromFile(filepath.Join(dir, f.Name())) if err != nil { return nil, err } - results = append(results, ext) + results = append(results, &Extension{ + path: filepath.Join(p, f.Name()), + kind: LocalKind, + }) } } @@ -164,145 +176,16 @@ func (m *Manager) list(includeMetadata bool) ([]Extension, error) { return results, nil } -func (m *Manager) parseExtensionFile(fi fs.DirEntry) (Extension, error) { - ext := Extension{isLocal: true} - id := m.installDir() - exePath := filepath.Join(id, fi.Name(), fi.Name()) - if !isSymlink(fi.Type()) { - // if this is a regular file, its contents is the local directory of the extension - p, err := readPathFromFile(filepath.Join(id, fi.Name())) - if err != nil { - return ext, err - } - exePath = filepath.Join(p, fi.Name()) - } - ext.path = exePath - return ext, nil -} - -func (m *Manager) parseExtensionDir(fi fs.DirEntry) (Extension, error) { - id := m.installDir() - if _, err := os.Stat(filepath.Join(id, fi.Name(), manifestName)); err == nil { - return m.parseBinaryExtensionDir(fi) - } - - return m.parseGitExtensionDir(fi) -} - -func (m *Manager) parseBinaryExtensionDir(fi fs.DirEntry) (Extension, error) { - id := m.installDir() - exePath := filepath.Join(id, fi.Name(), fi.Name()) - ext := Extension{path: exePath, kind: BinaryKind} - manifestPath := filepath.Join(id, fi.Name(), manifestName) - manifest, err := os.ReadFile(manifestPath) - if err != nil { - return ext, fmt.Errorf("could not open %s for reading: %w", manifestPath, err) - } - var bm binManifest - err = yaml.Unmarshal(manifest, &bm) - if err != nil { - return ext, fmt.Errorf("could not parse %s: %w", manifestPath, err) - } - repo := ghrepo.NewWithHost(bm.Owner, bm.Name, bm.Host) - remoteURL := ghrepo.GenerateRepoURL(repo, "") - ext.url = remoteURL - ext.currentVersion = bm.Tag - ext.isPinned = bm.IsPinned - return ext, nil -} - -func (m *Manager) parseGitExtensionDir(fi fs.DirEntry) (Extension, error) { - id := m.installDir() - exePath := filepath.Join(id, fi.Name(), fi.Name()) - remoteUrl := m.getRemoteUrl(fi.Name()) - currentVersion := m.getCurrentVersion(fi.Name()) - - var isPinned bool - pinPath := filepath.Join(id, fi.Name(), fmt.Sprintf(".pin-%s", currentVersion)) - if _, err := os.Stat(pinPath); err == nil { - isPinned = true - } - - return Extension{ - path: exePath, - url: remoteUrl, - isLocal: false, - currentVersion: currentVersion, - kind: GitKind, - isPinned: isPinned, - }, nil -} - -// getCurrentVersion determines the current version for non-local git extensions. -func (m *Manager) getCurrentVersion(extension string) string { - dir := filepath.Join(m.installDir(), extension) - scopedClient := m.gitClient.ForRepo(dir) - localSha, err := scopedClient.CommandOutput([]string{"rev-parse", "HEAD"}) - if err != nil { - return "" - } - return string(bytes.TrimSpace(localSha)) -} - -// getRemoteUrl determines the remote URL for non-local git extensions. -func (m *Manager) getRemoteUrl(extension string) string { - dir := filepath.Join(m.installDir(), extension) - scopedClient := m.gitClient.ForRepo(dir) - url, err := scopedClient.Config("remote.origin.url") - if err != nil { - return "" - } - return strings.TrimSpace(string(url)) -} - -func (m *Manager) populateLatestVersions(exts []Extension) { - size := len(exts) - type result struct { - index int - version string - } - ch := make(chan result, size) +func (m *Manager) populateLatestVersions(exts []*Extension) { var wg sync.WaitGroup - wg.Add(size) - for idx, ext := range exts { - go func(i int, e Extension) { + for _, ext := range exts { + wg.Add(1) + go func(e *Extension) { defer wg.Done() - version, _ := m.getLatestVersion(e) - ch <- result{index: i, version: version} - }(idx, ext) + e.LatestVersion() + }(ext) } wg.Wait() - close(ch) - for r := range ch { - ext := &exts[r.index] - ext.latestVersion = r.version - } -} - -func (m *Manager) getLatestVersion(ext Extension) (string, error) { - if ext.isLocal { - return "", localExtensionUpgradeError - } - if ext.IsBinary() { - repo, err := ghrepo.FromFullName(ext.url) - if err != nil { - return "", err - } - r, err := fetchLatestRelease(m.client, repo) - if err != nil { - return "", err - } - return r.Tag, nil - } else { - extDir := filepath.Dir(ext.path) - scopedClient := m.gitClient.ForRepo(extDir) - lsRemote, err := scopedClient.CommandOutput([]string{"ls-remote", "origin", "HEAD"}) - if err != nil { - return "", err - } - remoteSha := bytes.SplitN(lsRemote, []byte("\t"), 2)[0] - return string(remoteSha), nil - } } func (m *Manager) InstallLocal(dir string) error { @@ -521,22 +404,23 @@ func (m *Manager) Upgrade(name string, force bool) error { if f.Name() != name { continue } - var err error - // For single extensions manually retrieve latest version since we forgo - // doing it during list. - f.latestVersion, err = m.getLatestVersion(f) - if err != nil { - return err + if f.IsLocal() { + return localExtensionUpgradeError } - return m.upgradeExtensions([]Extension{f}, force) + // For single extensions manually retrieve latest version since we forgo doing it during list. + if latestVersion := f.LatestVersion(); latestVersion == "" { + return fmt.Errorf("unable to retrieve latest version for extension %q", name) + } + return m.upgradeExtensions([]*Extension{f}, force) } return fmt.Errorf("no extension matched %q", name) } -func (m *Manager) upgradeExtensions(exts []Extension, force bool) error { +func (m *Manager) upgradeExtensions(exts []*Extension, force bool) error { var failed bool for _, f := range exts { fmt.Fprintf(m.io.Out, "[%s]: ", f.Name()) + currentVersion := displayExtensionVersion(f, f.CurrentVersion()) err := m.upgradeExtension(f, force) if err != nil { if !errors.Is(err, localExtensionUpgradeError) && @@ -547,8 +431,7 @@ func (m *Manager) upgradeExtensions(exts []Extension, force bool) error { fmt.Fprintf(m.io.Out, "%s\n", err) continue } - currentVersion := displayExtensionVersion(&f, f.currentVersion) - latestVersion := displayExtensionVersion(&f, f.latestVersion) + latestVersion := displayExtensionVersion(f, f.LatestVersion()) if m.dryRunMode { fmt.Fprintf(m.io.Out, "would have upgraded from %s to %s\n", currentVersion, latestVersion) } else { @@ -561,8 +444,8 @@ func (m *Manager) upgradeExtensions(exts []Extension, force bool) error { return nil } -func (m *Manager) upgradeExtension(ext Extension, force bool) error { - if ext.isLocal { +func (m *Manager) upgradeExtension(ext *Extension, force bool) error { + if ext.IsLocal() { return localExtensionUpgradeError } if !force && ext.IsPinned() { @@ -592,7 +475,7 @@ func (m *Manager) upgradeExtension(ext Extension, force bool) error { return err } -func (m *Manager) upgradeGitExtension(ext Extension, force bool) error { +func (m *Manager) upgradeGitExtension(ext *Extension, force bool) error { if m.dryRunMode { return nil } @@ -611,10 +494,10 @@ func (m *Manager) upgradeGitExtension(ext Extension, force bool) error { return scopedClient.Pull("", "") } -func (m *Manager) upgradeBinExtension(ext Extension) error { - repo, err := ghrepo.FromFullName(ext.url) +func (m *Manager) upgradeBinExtension(ext *Extension) error { + repo, err := ghrepo.FromFullName(ext.URL()) if err != nil { - return fmt.Errorf("failed to parse URL %s: %w", ext.url, err) + return fmt.Errorf("failed to parse URL %s: %w", ext.URL(), err) } return m.installBin(repo, "") } diff --git a/pkg/cmd/extension/manager_test.go b/pkg/cmd/extension/manager_test.go index 4f8b3d75d..6d9b63124 100644 --- a/pkg/cmd/extension/manager_test.go +++ b/pkg/cmd/extension/manager_test.go @@ -80,12 +80,8 @@ func TestManager_List(t *testing.T) { dirOne := filepath.Join(tempDir, "extensions", "gh-hello") dirTwo := filepath.Join(tempDir, "extensions", "gh-two") gc, gcOne, gcTwo := &mockGitClient{}, &mockGitClient{}, &mockGitClient{} - gc.On("ForRepo", dirOne).Return(gcOne).Twice() - gc.On("ForRepo", dirTwo).Return(gcTwo).Twice() - gcOne.On("Config", "remote.origin.url").Return("", nil).Once() - gcTwo.On("Config", "remote.origin.url").Return("", nil).Once() - gcOne.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() - gcTwo.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() + gc.On("ForRepo", dirOne).Return(gcOne).Once() + gc.On("ForRepo", dirTwo).Return(gcTwo).Once() m := newTestManager(tempDir, nil, gc, nil) exts := m.List() @@ -145,9 +141,7 @@ func TestManager_Dispatch(t *testing.T) { assert.NoError(t, stubExtension(extPath)) gc, gcOne := &mockGitClient{}, &mockGitClient{} - gc.On("ForRepo", extDir).Return(gcOne).Twice() - gcOne.On("Config", "remote.origin.url").Return("", nil).Once() - gcOne.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() + gc.On("ForRepo", extDir).Return(gcOne).Once() m := newTestManager(tempDir, nil, gc, nil) @@ -223,9 +217,7 @@ func TestManager_Upgrade_NoMatchingExtension(t *testing.T) { assert.NoError(t, stubExtension(filepath.Join(tempDir, "extensions", "gh-hello", "gh-hello"))) ios, _, stdout, stderr := iostreams.Test() gc, gcOne := &mockGitClient{}, &mockGitClient{} - gc.On("ForRepo", extDir).Return(gcOne).Twice() - gcOne.On("Config", "remote.origin.url").Return("", nil).Once() - gcOne.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() + gc.On("ForRepo", extDir).Return(gcOne).Once() m := newTestManager(tempDir, nil, gc, ios) err := m.Upgrade("invalid", false) assert.EqualError(t, err, `no extension matched "invalid"`) @@ -244,12 +236,8 @@ func TestManager_UpgradeExtensions(t *testing.T) { assert.NoError(t, stubLocalExtension(tempDir, filepath.Join(tempDir, "extensions", "gh-local", "gh-local"))) ios, _, stdout, stderr := iostreams.Test() gc, gcOne, gcTwo := &mockGitClient{}, &mockGitClient{}, &mockGitClient{} - gc.On("ForRepo", dirOne).Return(gcOne).Times(4) - gc.On("ForRepo", dirTwo).Return(gcTwo).Times(4) - gcOne.On("Config", "remote.origin.url").Return("", nil).Once() - gcTwo.On("Config", "remote.origin.url").Return("", nil).Once() - gcOne.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() - gcTwo.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() + gc.On("ForRepo", dirOne).Return(gcOne).Times(3) + gc.On("ForRepo", dirTwo).Return(gcTwo).Times(3) gcOne.On("Remotes").Return(nil, nil).Once() gcTwo.On("Remotes").Return(nil, nil).Once() gcOne.On("Pull", "", "").Return(nil).Once() @@ -286,12 +274,8 @@ func TestManager_UpgradeExtensions_DryRun(t *testing.T) { assert.NoError(t, stubLocalExtension(tempDir, filepath.Join(tempDir, "extensions", "gh-local", "gh-local"))) ios, _, stdout, stderr := iostreams.Test() gc, gcOne, gcTwo := &mockGitClient{}, &mockGitClient{}, &mockGitClient{} - gc.On("ForRepo", dirOne).Return(gcOne).Times(3) - gc.On("ForRepo", dirTwo).Return(gcTwo).Times(3) - gcOne.On("Config", "remote.origin.url").Return("", nil).Once() - gcTwo.On("Config", "remote.origin.url").Return("", nil).Once() - gcOne.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() - gcTwo.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() + gc.On("ForRepo", dirOne).Return(gcOne).Twice() + gc.On("ForRepo", dirTwo).Return(gcTwo).Twice() gcOne.On("Remotes").Return(nil, nil).Once() gcTwo.On("Remotes").Return(nil, nil).Once() m := newTestManager(tempDir, nil, gc, ios) @@ -355,9 +339,7 @@ func TestManager_UpgradeExtension_GitExtension(t *testing.T) { assert.NoError(t, stubExtension(filepath.Join(tempDir, "extensions", "gh-remote", "gh-remote"))) ios, _, stdout, stderr := iostreams.Test() gc, gcOne := &mockGitClient{}, &mockGitClient{} - gc.On("ForRepo", extensionDir).Return(gcOne).Times(4) - gcOne.On("Config", "remote.origin.url").Return("", nil).Once() - gcOne.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() + gc.On("ForRepo", extensionDir).Return(gcOne).Times(3) gcOne.On("Remotes").Return(nil, nil).Once() gcOne.On("Pull", "", "").Return(nil).Once() m := newTestManager(tempDir, nil, gc, ios) @@ -381,9 +363,7 @@ func TestManager_UpgradeExtension_GitExtension_DryRun(t *testing.T) { assert.NoError(t, stubExtension(filepath.Join(tempDir, "extensions", "gh-remote", "gh-remote"))) ios, _, stdout, stderr := iostreams.Test() gc, gcOne := &mockGitClient{}, &mockGitClient{} - gc.On("ForRepo", extDir).Return(gcOne).Times(3) - gcOne.On("Config", "remote.origin.url").Return("", nil).Once() - gcOne.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() + gc.On("ForRepo", extDir).Return(gcOne).Twice() gcOne.On("Remotes").Return(nil, nil).Once() m := newTestManager(tempDir, nil, gc, ios) m.EnableDryRunMode() @@ -407,9 +387,7 @@ func TestManager_UpgradeExtension_GitExtension_Force(t *testing.T) { assert.NoError(t, stubExtension(filepath.Join(tempDir, "extensions", "gh-remote", "gh-remote"))) ios, _, stdout, stderr := iostreams.Test() gc, gcOne := &mockGitClient{}, &mockGitClient{} - gc.On("ForRepo", extensionDir).Return(gcOne).Times(4) - gcOne.On("Config", "remote.origin.url").Return("", nil).Once() - gcOne.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() + gc.On("ForRepo", extensionDir).Return(gcOne).Times(3) gcOne.On("Remotes").Return(nil, nil).Once() gcOne.On("Fetch", "origin", "HEAD").Return(nil).Once() gcOne.On("CommandOutput", []string{"reset", "--hard", "origin/HEAD"}).Return("", nil).Once() @@ -713,7 +691,7 @@ func TestManager_UpgradeExtension_BinaryExtension_Pinned(t *testing.T) { assert.Equal(t, err, pinnedExtensionUpgradeError) } -func TestManager_UpgradeExtenion_GitExtension_Pinned(t *testing.T) { +func TestManager_UpgradeExtension_GitExtension_Pinned(t *testing.T) { tempDir := t.TempDir() extDir := filepath.Join(tempDir, "extensions", "gh-remote") assert.NoError(t, stubPinnedExtension(filepath.Join(extDir, "gh-remote"), "abcd1234")) @@ -721,9 +699,7 @@ func TestManager_UpgradeExtenion_GitExtension_Pinned(t *testing.T) { ios, _, _, _ := iostreams.Test() gc, gcOne := &mockGitClient{}, &mockGitClient{} - gc.On("ForRepo", extDir).Return(gcOne).Twice() - gcOne.On("Config", "remote.origin.url").Return("", nil).Once() - gcOne.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once() + gc.On("ForRepo", extDir).Return(gcOne).Once() m := newTestManager(tempDir, nil, gc, ios) @@ -732,7 +708,8 @@ func TestManager_UpgradeExtenion_GitExtension_Pinned(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 1, len(exts)) ext := exts[0] - ext.isPinned = true + pinnedTrue := true + ext.isPinned = &pinnedTrue ext.latestVersion = "new version" err = m.upgradeExtension(ext, false) diff --git a/pkg/cmd/factory/remote_resolver.go b/pkg/cmd/factory/remote_resolver.go index 08ff297c6..6ef7c5e02 100644 --- a/pkg/cmd/factory/remote_resolver.go +++ b/pkg/cmd/factory/remote_resolver.go @@ -10,7 +10,7 @@ import ( "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghinstance" "github.com/cli/cli/v2/pkg/set" - "github.com/cli/go-gh/pkg/ssh" + "github.com/cli/go-gh/v2/pkg/ssh" ) const ( diff --git a/pkg/cmd/gist/create/create.go b/pkg/cmd/gist/create/create.go index 34476afef..4154acf9c 100644 --- a/pkg/cmd/gist/create/create.go +++ b/pkg/cmd/gist/create/create.go @@ -112,25 +112,18 @@ func createRun(opts *CreateOptions) error { return fmt.Errorf("failed to collect files for posting: %w", err) } + errOut := opts.IO.ErrOut cs := opts.IO.ColorScheme() gistName := guessGistName(files) processMessage := "Creating gist..." - completionMessage := "Created gist" if gistName != "" { if len(files) > 1 { processMessage = "Creating gist with multiple files" } else { processMessage = fmt.Sprintf("Creating gist %s", gistName) } - if opts.Public { - completionMessage = fmt.Sprintf("Created %s gist %s", cs.Red("public"), gistName) - } else { - completionMessage = fmt.Sprintf("Created %s gist %s", cs.Green("secret"), gistName) - } } - - errOut := opts.IO.ErrOut fmt.Fprintf(errOut, "%s %s\n", cs.Gray("-"), processMessage) httpClient, err := opts.HttpClient() @@ -161,6 +154,13 @@ func createRun(opts *CreateOptions) error { return fmt.Errorf("%s Failed to create gist: %w", cs.Red("X"), err) } + completionMessage := fmt.Sprintf("Created %s gist", cs.Green("secret")) + if opts.Public { + completionMessage = fmt.Sprintf("Created %s gist", cs.Red("public")) + } + if gistName != "" { + completionMessage += " " + gistName + } fmt.Fprintf(errOut, "%s %s\n", cs.SuccessIconWithColor(cs.Green), completionMessage) if opts.WebMode { diff --git a/pkg/cmd/gist/create/create_test.go b/pkg/cmd/gist/create/create_test.go index 3fd60fed9..2302b96dd 100644 --- a/pkg/cmd/gist/create/create_test.go +++ b/pkg/cmd/gist/create/create_test.go @@ -275,7 +275,7 @@ func Test_createRun(t *testing.T) { }, stdin: "cool stdin content", wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n", - wantStderr: "- Creating gist...\n✓ Created gist\n", + wantStderr: "- Creating gist...\n✓ Created secret gist\n", wantErr: false, wantParams: map[string]interface{}{ "description": "", diff --git a/pkg/cmd/gist/edit/edit.go b/pkg/cmd/gist/edit/edit.go index 773ea41df..f2f04bd92 100644 --- a/pkg/cmd/gist/edit/edit.go +++ b/pkg/cmd/gist/edit/edit.go @@ -12,29 +12,32 @@ import ( "sort" "strings" - "github.com/AlecAivazis/survey/v2" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/gist/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/cli/cli/v2/pkg/surveyext" "github.com/spf13/cobra" ) +var editNextOptions = []string{"Edit another file", "Submit", "Cancel"} + type EditOptions struct { IO *iostreams.IOStreams HttpClient func() (*http.Client, error) Config func() (config.Config, error) + Prompter prompter.Prompter Edit func(string, string, string, *iostreams.IOStreams) (string, error) - Selector string - EditFilename string - AddFilename string - SourceFile string - Description string + Selector string + EditFilename string + AddFilename string + RemoveFilename string + SourceFile string + Description string } func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command { @@ -42,6 +45,7 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman IO: f.IOStreams, HttpClient: f.HttpClient, Config: f.Config, + Prompter: f.Prompter, Edit: func(editorCmd, filename, defaultContent string, io *iostreams.IOStreams) (string, error) { return surveyext.Edit( editorCmd, @@ -55,16 +59,15 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman Use: "edit { | } []", Short: "Edit one of your gists", Args: func(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return cmdutil.FlagErrorf("cannot edit: gist argument required") - } if len(args) > 2 { return cmdutil.FlagErrorf("too many arguments") } return nil }, RunE: func(c *cobra.Command, args []string) error { - opts.Selector = args[0] + if len(args) > 0 { + opts.Selector = args[0] + } if len(args) > 1 { opts.SourceFile = args[1] } @@ -80,12 +83,42 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman cmd.Flags().StringVarP(&opts.AddFilename, "add", "a", "", "Add a new file to the gist") cmd.Flags().StringVarP(&opts.Description, "desc", "d", "", "New description for the gist") cmd.Flags().StringVarP(&opts.EditFilename, "filename", "f", "", "Select a file to edit") + cmd.Flags().StringVarP(&opts.RemoveFilename, "remove", "r", "", "Remove a file from the gist") + + cmd.MarkFlagsMutuallyExclusive("add", "remove") + cmd.MarkFlagsMutuallyExclusive("remove", "filename") return cmd } func editRun(opts *EditOptions) error { + client, err := opts.HttpClient() + if err != nil { + return err + } + + cfg, err := opts.Config() + if err != nil { + return err + } + + host, _ := cfg.Authentication().DefaultHost() + gistID := opts.Selector + if gistID == "" { + cs := opts.IO.ColorScheme() + if gistID == "" { + gistID, err = shared.PromptGists(opts.Prompter, client, host, cs) + if err != nil { + return err + } + + if gistID == "" { + fmt.Fprintln(opts.IO.Out, "No gists found.") + return nil + } + } + } if strings.Contains(gistID, "/") { id, err := shared.GistIDFromURL(gistID) @@ -95,20 +128,8 @@ func editRun(opts *EditOptions) error { gistID = id } - client, err := opts.HttpClient() - if err != nil { - return err - } - apiClient := api.NewClientFromHTTP(client) - cfg, err := opts.Config() - if err != nil { - return err - } - - host, _ := cfg.Authentication().DefaultHost() - gist, err := shared.GetGist(client, host, gistID) if err != nil { if errors.Is(err, shared.NotFoundErr) { @@ -126,10 +147,25 @@ func editRun(opts *EditOptions) error { return errors.New("you do not own this gist") } + // Transform our gist into the schema that the update endpoint expects + filesToupdate := make(map[string]*gistFileToUpdate, len(gist.Files)) + for filename, file := range gist.Files { + filesToupdate[filename] = &gistFileToUpdate{ + Content: file.Content, + NewFilename: file.Filename, + } + } + + gistToUpdate := gistToUpdate{ + id: gist.ID, + Description: gist.Description, + Files: filesToupdate, + } + shouldUpdate := false if opts.Description != "" { shouldUpdate = true - gist.Description = opts.Description + gistToUpdate.Description = opts.Description } if opts.AddFilename != "" { @@ -167,8 +203,18 @@ func editRun(opts *EditOptions) error { return err } - gist.Files = files - return updateGist(apiClient, host, gist) + gistToUpdate.Files = files + return updateGist(apiClient, host, gistToUpdate) + } + + // Remove a file from the gist + if opts.RemoveFilename != "" { + err := removeFile(gistToUpdate, opts.RemoveFilename) + if err != nil { + return err + } + + return updateGist(apiClient, host, gistToUpdate) } filesToUpdate := map[string]string{} @@ -176,7 +222,7 @@ func editRun(opts *EditOptions) error { for { filename := opts.EditFilename candidates := []string{} - for filename := range gist.Files { + for filename := range gistToUpdate.Files { candidates = append(candidates, filename) } @@ -189,19 +235,15 @@ func editRun(opts *EditOptions) error { if !opts.IO.CanPrompt() { return errors.New("unsure what file to edit; either specify --filename or run interactively") } - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - err = prompt.SurveyAskOne(&survey.Select{ - Message: "Edit which file?", - Options: candidates, - }, &filename) - + result, err := opts.Prompter.Select("Edit which file?", "", candidates) if err != nil { return fmt.Errorf("could not prompt: %w", err) } + filename = candidates[result] } } - gistFile, found := gist.Files[filename] + gistFile, found := gistToUpdate.Files[filename] if !found { return fmt.Errorf("gist has no file %q", filename) } @@ -249,20 +291,11 @@ func editRun(opts *EditOptions) error { } choice := "" - - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - err = prompt.SurveyAskOne(&survey.Select{ - Message: "What next?", - Options: []string{ - "Edit another file", - "Submit", - "Cancel", - }, - }, &choice) - + result, err := opts.Prompter.Select("What next?", "", editNextOptions) if err != nil { return fmt.Errorf("could not prompt: %w", err) } + choice = editNextOptions[result] stop := false @@ -288,28 +321,38 @@ func editRun(opts *EditOptions) error { return nil } - return updateGist(apiClient, host, gist) + return updateGist(apiClient, host, gistToUpdate) } -func updateGist(apiClient *api.Client, hostname string, gist *shared.Gist) error { - body := shared.Gist{ - Description: gist.Description, - Files: gist.Files, - } +// https://docs.github.com/en/rest/gists/gists?apiVersion=2022-11-28#update-a-gist +type gistToUpdate struct { + // The id of the gist to update. Does not get marshalled to JSON. + id string + // The description of the gist + Description string `json:"description"` + // The gist files to be updated, renamed or deleted. The key must match the current + // filename of the file to change. To delete a file, set the value to nil + Files map[string]*gistFileToUpdate `json:"files"` +} - path := "gists/" + gist.ID +type gistFileToUpdate struct { + // The new content of the file + Content string `json:"content"` + // The new name for the file + NewFilename string `json:"filename,omitempty"` +} - requestByte, err := json.Marshal(body) +func updateGist(apiClient *api.Client, hostname string, gist gistToUpdate) error { + requestByte, err := json.Marshal(gist) if err != nil { return err } requestBody := bytes.NewReader(requestByte) - result := shared.Gist{} + path := "gists/" + gist.id err = apiClient.REST(hostname, "POST", path, requestBody, &result) - if err != nil { return err } @@ -317,7 +360,7 @@ func updateGist(apiClient *api.Client, hostname string, gist *shared.Gist) error return nil } -func getFilesToAdd(file string, content []byte) (map[string]*shared.GistFile, error) { +func getFilesToAdd(file string, content []byte) (map[string]*gistFileToUpdate, error) { if shared.IsBinaryContents(content) { return nil, fmt.Errorf("failed to upload %s: binary file not supported", file) } @@ -327,10 +370,19 @@ func getFilesToAdd(file string, content []byte) (map[string]*shared.GistFile, er } filename := filepath.Base(file) - return map[string]*shared.GistFile{ + return map[string]*gistFileToUpdate{ filename: { - Filename: filename, - Content: string(content), + NewFilename: filename, + Content: string(content), }, }, nil } + +func removeFile(gist gistToUpdate, filename string) error { + if _, found := gist.Files[filename]; !found { + return fmt.Errorf("gist has no file %q", filename) + } + + gist.Files[filename] = nil + return nil +} diff --git a/pkg/cmd/gist/edit/edit_test.go b/pkg/cmd/gist/edit/edit_test.go index 33223ddee..b57fa41cc 100644 --- a/pkg/cmd/gist/edit/edit_test.go +++ b/pkg/cmd/gist/edit/edit_test.go @@ -10,11 +10,11 @@ import ( "testing" "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/gist/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -26,19 +26,20 @@ func Test_getFilesToAdd(t *testing.T) { gf, err := getFilesToAdd(filename, []byte("hello")) require.NoError(t, err) - assert.Equal(t, map[string]*shared.GistFile{ + assert.Equal(t, map[string]*gistFileToUpdate{ filename: { - Filename: filename, - Content: "hello", + NewFilename: filename, + Content: "hello", }, }, gf) } func TestNewCmdEdit(t *testing.T) { tests := []struct { - name string - cli string - wants EditOptions + name string + cli string + wants EditOptions + wantsErr bool }{ { name: "no flags", @@ -80,6 +81,24 @@ func TestNewCmdEdit(t *testing.T) { Description: "my new description", }, }, + { + name: "remove", + cli: "123 --remove cool.md", + wants: EditOptions{ + Selector: "123", + RemoveFilename: "cool.md", + }, + }, + { + name: "add and remove are mutually exclusive", + cli: "123 --add cool.md --remove great.md", + wantsErr: true, + }, + { + name: "filename and remove are mutually exclusive", + cli: "123 --filename cool.md --remove great.md", + wantsErr: true, + }, } for _, tt := range tests { @@ -100,11 +119,17 @@ func TestNewCmdEdit(t *testing.T) { cmd.SetErr(&bytes.Buffer{}) _, err = cmd.ExecuteC() - assert.NoError(t, err) + if tt.wantsErr { + require.Error(t, err) + return + } - assert.Equal(t, tt.wants.EditFilename, gotOpts.EditFilename) - assert.Equal(t, tt.wants.AddFilename, gotOpts.AddFilename) - assert.Equal(t, tt.wants.Selector, gotOpts.Selector) + require.NoError(t, err) + + require.Equal(t, tt.wants.EditFilename, gotOpts.EditFilename) + require.Equal(t, tt.wants.AddFilename, gotOpts.AddFilename) + require.Equal(t, tt.wants.Selector, gotOpts.Selector) + require.Equal(t, tt.wants.RemoveFilename, gotOpts.RemoveFilename) }) } } @@ -115,15 +140,15 @@ func Test_editRun(t *testing.T) { require.NoError(t, err) tests := []struct { - name string - opts *EditOptions - gist *shared.Gist - httpStubs func(*httpmock.Registry) - askStubs func(*prompt.AskStubber) - nontty bool - stdin string - wantErr string - wantParams map[string]interface{} + name string + opts *EditOptions + gist *shared.Gist + httpStubs func(*httpmock.Registry) + prompterStubs func(*prompter.MockPrompter) + nontty bool + stdin string + wantErr string + wantParams map[string]interface{} }{ { name: "no such gist", @@ -148,22 +173,27 @@ func Test_editRun(t *testing.T) { }, wantParams: map[string]interface{}{ "description": "", - "updated_at": "0001-01-01T00:00:00Z", - "public": false, "files": map[string]interface{}{ "cicada.txt": map[string]interface{}{ "content": "new file content", "filename": "cicada.txt", - "type": "text/plain", }, }, }, }, { name: "multiple files, submit", - askStubs: func(as *prompt.AskStubber) { - as.StubPrompt("Edit which file?").AnswerWith("unix.md") - as.StubPrompt("What next?").AnswerWith("Submit") + prompterStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Edit which file?", + []string{"cicada.txt", "unix.md"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "unix.md") + }) + pm.RegisterSelect("What next?", + editNextOptions, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "Submit") + }) }, gist: &shared.Gist{ ID: "1234", @@ -172,12 +202,10 @@ func Test_editRun(t *testing.T) { "cicada.txt": { Filename: "cicada.txt", Content: "bwhiizzzbwhuiiizzzz", - Type: "text/plain", }, "unix.md": { Filename: "unix.md", Content: "meow", - Type: "application/markdown", }, }, Owner: &shared.GistOwner{Login: "octocat"}, @@ -188,27 +216,31 @@ func Test_editRun(t *testing.T) { }, wantParams: map[string]interface{}{ "description": "catbug", - "updated_at": "0001-01-01T00:00:00Z", - "public": false, "files": map[string]interface{}{ "cicada.txt": map[string]interface{}{ "content": "bwhiizzzbwhuiiizzzz", "filename": "cicada.txt", - "type": "text/plain", }, "unix.md": map[string]interface{}{ "content": "new file content", "filename": "unix.md", - "type": "application/markdown", }, }, }, }, { name: "multiple files, cancel", - askStubs: func(as *prompt.AskStubber) { - as.StubPrompt("Edit which file?").AnswerWith("unix.md") - as.StubPrompt("What next?").AnswerWith("Cancel") + prompterStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Edit which file?", + []string{"cicada.txt", "unix.md"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "unix.md") + }) + pm.RegisterSelect("What next?", + editNextOptions, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "Cancel") + }) }, wantErr: "CancelError", gist: &shared.Gist{ @@ -300,13 +332,10 @@ func Test_editRun(t *testing.T) { }, wantParams: map[string]interface{}{ "description": "my new description", - "updated_at": "0001-01-01T00:00:00Z", - "public": false, "files": map[string]interface{}{ "sample.txt": map[string]interface{}{ "content": "new file content", "filename": "sample.txt", - "type": "text/plain", }, }, }, @@ -334,8 +363,6 @@ func Test_editRun(t *testing.T) { }, wantParams: map[string]interface{}{ "description": "", - "updated_at": "0001-01-01T00:00:00Z", - "public": false, "files": map[string]interface{}{ "from_source.txt": map[string]interface{}{ "content": "hello", @@ -368,8 +395,6 @@ func Test_editRun(t *testing.T) { stdin: "data from stdin", wantParams: map[string]interface{}{ "description": "", - "updated_at": "0001-01-01T00:00:00Z", - "public": false, "files": map[string]interface{}{ "from_source.txt": map[string]interface{}{ "content": "data from stdin", @@ -378,6 +403,60 @@ func Test_editRun(t *testing.T) { }, }, }, + { + name: "remove file, file does not exist", + gist: &shared.Gist{ + ID: "1234", + Files: map[string]*shared.GistFile{ + "sample.txt": { + Filename: "sample.txt", + Content: "bwhiizzzbwhuiiizzzz", + Type: "text/plain", + }, + }, + Owner: &shared.GistOwner{Login: "octocat"}, + }, + opts: &EditOptions{ + RemoveFilename: "sample2.txt", + }, + wantErr: "gist has no file \"sample2.txt\"", + }, + { + name: "remove file from existing gist", + gist: &shared.Gist{ + ID: "1234", + Files: map[string]*shared.GistFile{ + "sample.txt": { + Filename: "sample.txt", + Content: "bwhiizzzbwhuiiizzzz", + Type: "text/plain", + }, + "sample2.txt": { + Filename: "sample2.txt", + Content: "bwhiizzzbwhuiiizzzz", + Type: "text/plain", + }, + }, + Owner: &shared.GistOwner{Login: "octocat"}, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register(httpmock.REST("POST", "gists/1234"), + httpmock.StatusStringResponse(201, "{}")) + }, + opts: &EditOptions{ + RemoveFilename: "sample2.txt", + }, + wantParams: map[string]interface{}{ + "description": "", + "files": map[string]interface{}{ + "sample.txt": map[string]interface{}{ + "filename": "sample.txt", + "content": "bwhiizzzbwhuiiizzzz", + }, + "sample2.txt": nil, + }, + }, + }, { name: "edit gist using file from source parameter", gist: &shared.Gist{ @@ -400,13 +479,10 @@ func Test_editRun(t *testing.T) { }, wantParams: map[string]interface{}{ "description": "", - "updated_at": "0001-01-01T00:00:00Z", - "public": false, "files": map[string]interface{}{ "sample.txt": map[string]interface{}{ "content": "hello", "filename": "sample.txt", - "type": "text/plain", }, }, }, @@ -434,13 +510,10 @@ func Test_editRun(t *testing.T) { stdin: "data from stdin", wantParams: map[string]interface{}{ "description": "", - "updated_at": "0001-01-01T00:00:00Z", - "public": false, "files": map[string]interface{}{ "sample.txt": map[string]interface{}{ "content": "data from stdin", "filename": "sample.txt", - "type": "text/plain", }, }, }, @@ -486,11 +559,11 @@ func Test_editRun(t *testing.T) { } t.Run(tt.name, func(t *testing.T) { - //nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock - as := prompt.NewAskStubber(t) - if tt.askStubs != nil { - tt.askStubs(as) + pm := prompter.NewMockPrompter(t) + if tt.prompterStubs != nil { + tt.prompterStubs(pm) } + tt.opts.Prompter = pm err := editRun(tt.opts) reg.Verify(t) diff --git a/pkg/cmd/gist/shared/shared.go b/pkg/cmd/gist/shared/shared.go index 52c86caa7..66ba6fe0a 100644 --- a/pkg/cmd/gist/shared/shared.go +++ b/pkg/cmd/gist/shared/shared.go @@ -5,10 +5,14 @@ import ( "fmt" "net/http" "net/url" + "sort" "strings" "time" "github.com/cli/cli/v2/api" + "github.com/cli/cli/v2/internal/prompter" + "github.com/cli/cli/v2/internal/text" + "github.com/cli/cli/v2/pkg/iostreams" "github.com/gabriel-vasile/mimetype" "github.com/shurcooL/githubv4" ) @@ -171,3 +175,48 @@ func IsBinaryContents(contents []byte) bool { } return isBinary } + +func PromptGists(prompter prompter.Prompter, client *http.Client, host string, cs *iostreams.ColorScheme) (gistID string, err error) { + gists, err := ListGists(client, host, 10, "all") + if err != nil { + return "", err + } + + if len(gists) == 0 { + return "", nil + } + + var opts []string + var gistIDs = make([]string, len(gists)) + + for i, gist := range gists { + gistIDs[i] = gist.ID + description := "" + gistName := "" + + if gist.Description != "" { + description = gist.Description + } + + filenames := make([]string, 0, len(gist.Files)) + for fn := range gist.Files { + filenames = append(filenames, fn) + } + sort.Strings(filenames) + gistName = filenames[0] + + gistTime := text.FuzzyAgo(time.Now(), gist.UpdatedAt) + // TODO: support dynamic maxWidth + description = text.Truncate(100, text.RemoveExcessiveWhitespace(description)) + opt := fmt.Sprintf("%s %s %s", cs.Bold(gistName), description, cs.Gray(gistTime)) + opts = append(opts, opt) + } + + result, err := prompter.Select("Select a gist", "", opts) + + if err != nil { + return "", err + } + + return gistIDs[result], nil +} diff --git a/pkg/cmd/gist/shared/shared_test.go b/pkg/cmd/gist/shared/shared_test.go index c55cbea67..4d185ff7d 100644 --- a/pkg/cmd/gist/shared/shared_test.go +++ b/pkg/cmd/gist/shared/shared_test.go @@ -1,8 +1,14 @@ package shared import ( + "fmt" + "net/http" "testing" + "time" + "github.com/cli/cli/v2/internal/prompter" + "github.com/cli/cli/v2/pkg/httpmock" + "github.com/cli/cli/v2/pkg/iostreams" "github.com/stretchr/testify/assert" ) @@ -85,3 +91,104 @@ func TestIsBinaryContents(t *testing.T) { assert.Equal(t, tt.want, IsBinaryContents(tt.fileContent)) } } + +func TestPromptGists(t *testing.T) { + tests := []struct { + name string + prompterStubs func(pm *prompter.MockPrompter) + response string + wantOut string + gist *Gist + wantErr bool + }{ + { + name: "multiple files, select first gist", + prompterStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a gist", + []string{"cool.txt about 6 hours ago", "gistfile0.txt about 6 hours ago"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "cool.txt about 6 hours ago") + }) + }, + response: `{ "data": { "viewer": { "gists": { "nodes": [ + { + "name": "gistid1", + "files": [{ "name": "cool.txt" }], + "description": "", + "updatedAt": "%[1]v", + "isPublic": true + }, + { + "name": "gistid2", + "files": [{ "name": "gistfile0.txt" }], + "description": "", + "updatedAt": "%[1]v", + "isPublic": true + } + ] } } } }`, + wantOut: "gistid1", + }, + { + name: "multiple files, select second gist", + prompterStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a gist", + []string{"cool.txt about 6 hours ago", "gistfile0.txt about 6 hours ago"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "gistfile0.txt about 6 hours ago") + }) + }, + response: `{ "data": { "viewer": { "gists": { "nodes": [ + { + "name": "gistid1", + "files": [{ "name": "cool.txt" }], + "description": "", + "updatedAt": "%[1]v", + "isPublic": true + }, + { + "name": "gistid2", + "files": [{ "name": "gistfile0.txt" }], + "description": "", + "updatedAt": "%[1]v", + "isPublic": true + } + ] } } } }`, + wantOut: "gistid2", + }, + { + name: "no files", + response: `{ "data": { "viewer": { "gists": { "nodes": [] } } } }`, + wantOut: "", + }, + } + + ios, _, _, _ := iostreams.Test() + + for _, tt := range tests { + reg := &httpmock.Registry{} + + const query = `query GistList\b` + sixHours, _ := time.ParseDuration("6h") + sixHoursAgo := time.Now().Add(-sixHours) + reg.Register( + httpmock.GraphQL(query), + httpmock.StringResponse(fmt.Sprintf( + tt.response, + sixHoursAgo.Format(time.RFC3339), + )), + ) + client := &http.Client{Transport: reg} + + t.Run(tt.name, func(t *testing.T) { + mockPrompter := prompter.NewMockPrompter(t) + if tt.prompterStubs != nil { + tt.prompterStubs(mockPrompter) + } + + gistID, err := PromptGists(mockPrompter, client, "github.com", ios.ColorScheme()) + assert.NoError(t, err) + assert.Equal(t, tt.wantOut, gistID) + reg.Verify(t) + }) + } +} diff --git a/pkg/cmd/gist/view/view.go b/pkg/cmd/gist/view/view.go index 545681522..e4f5c84e1 100644 --- a/pkg/cmd/gist/view/view.go +++ b/pkg/cmd/gist/view/view.go @@ -5,17 +5,15 @@ import ( "net/http" "sort" "strings" - "time" - "github.com/AlecAivazis/survey/v2" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghinstance" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/internal/text" "github.com/cli/cli/v2/pkg/cmd/gist/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" "github.com/cli/cli/v2/pkg/markdown" - "github.com/cli/cli/v2/pkg/prompt" "github.com/spf13/cobra" ) @@ -28,6 +26,7 @@ type ViewOptions struct { Config func() (config.Config, error) HttpClient func() (*http.Client, error) Browser browser + Prompter prompter.Prompter Selector string Filename string @@ -42,6 +41,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman Config: f.Config, HttpClient: f.HttpClient, Browser: f.Browser, + Prompter: f.Prompter, } cmd := &cobra.Command{ @@ -89,7 +89,7 @@ func viewRun(opts *ViewOptions) error { cs := opts.IO.ColorScheme() if gistID == "" { - gistID, err = promptGists(client, hostname, cs) + gistID, err = shared.PromptGists(opts.Prompter, client, hostname, cs) if err != nil { return err } @@ -204,55 +204,3 @@ func viewRun(opts *ViewOptions) error { return nil } - -func promptGists(client *http.Client, host string, cs *iostreams.ColorScheme) (gistID string, err error) { - gists, err := shared.ListGists(client, host, 10, "all") - if err != nil { - return "", err - } - - if len(gists) == 0 { - return "", nil - } - - var opts []string - var result int - var gistIDs = make([]string, len(gists)) - - for i, gist := range gists { - gistIDs[i] = gist.ID - description := "" - gistName := "" - - if gist.Description != "" { - description = gist.Description - } - - filenames := make([]string, 0, len(gist.Files)) - for fn := range gist.Files { - filenames = append(filenames, fn) - } - sort.Strings(filenames) - gistName = filenames[0] - - gistTime := text.FuzzyAgo(time.Now(), gist.UpdatedAt) - // TODO: support dynamic maxWidth - description = text.Truncate(100, text.RemoveExcessiveWhitespace(description)) - opt := fmt.Sprintf("%s %s %s", cs.Bold(gistName), description, cs.Gray(gistTime)) - opts = append(opts, opt) - } - - questions := &survey.Select{ - Message: "Select a gist", - Options: opts, - } - - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - err = prompt.SurveyAskOne(questions, &result) - - if err != nil { - return "", err - } - - return gistIDs[result], nil -} diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index 8dbc9a748..c571422c5 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -8,11 +8,11 @@ import ( "time" "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/gist/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -336,6 +336,10 @@ func Test_viewRun(t *testing.T) { httpmock.JSONResponse(tt.gist)) } + if tt.opts == nil { + tt.opts = &ViewOptions{} + } + if tt.mockGistList { sixHours, _ := time.ParseDuration("6h") sixHoursAgo := time.Now().Add(-sixHours) @@ -355,13 +359,11 @@ func Test_viewRun(t *testing.T) { )), ) - //nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock - as := prompt.NewAskStubber(t) - as.StubPrompt("Select a gist").AnswerDefault() - } - - if tt.opts == nil { - tt.opts = &ViewOptions{} + pm := prompter.NewMockPrompter(t) + pm.RegisterSelect("Select a gist", []string{"cool.txt about 6 hours ago"}, func(_, _ string, opts []string) (int, error) { + return 0, nil + }) + tt.opts.Prompter = pm } tt.opts.HttpClient = func() (*http.Client, error) { @@ -389,97 +391,3 @@ func Test_viewRun(t *testing.T) { }) } } - -func Test_promptGists(t *testing.T) { - tests := []struct { - name string - askStubs func(as *prompt.AskStubber) - response string - wantOut string - gist *shared.Gist - wantErr bool - }{ - { - name: "multiple files, select first gist", - askStubs: func(as *prompt.AskStubber) { - as.StubPrompt("Select a gist").AnswerWith("cool.txt about 6 hours ago") - }, - response: `{ "data": { "viewer": { "gists": { "nodes": [ - { - "name": "gistid1", - "files": [{ "name": "cool.txt" }], - "description": "", - "updatedAt": "%[1]v", - "isPublic": true - }, - { - "name": "gistid2", - "files": [{ "name": "gistfile0.txt" }], - "description": "", - "updatedAt": "%[1]v", - "isPublic": true - } - ] } } } }`, - wantOut: "gistid1", - }, - { - name: "multiple files, select second gist", - askStubs: func(as *prompt.AskStubber) { - as.StubPrompt("Select a gist").AnswerWith("gistfile0.txt about 6 hours ago") - }, - response: `{ "data": { "viewer": { "gists": { "nodes": [ - { - "name": "gistid1", - "files": [{ "name": "cool.txt" }], - "description": "", - "updatedAt": "%[1]v", - "isPublic": true - }, - { - "name": "gistid2", - "files": [{ "name": "gistfile0.txt" }], - "description": "", - "updatedAt": "%[1]v", - "isPublic": true - } - ] } } } }`, - wantOut: "gistid2", - }, - { - name: "no files", - response: `{ "data": { "viewer": { "gists": { "nodes": [] } } } }`, - wantOut: "", - }, - } - - ios, _, _, _ := iostreams.Test() - - for _, tt := range tests { - reg := &httpmock.Registry{} - - const query = `query GistList\b` - sixHours, _ := time.ParseDuration("6h") - sixHoursAgo := time.Now().Add(-sixHours) - reg.Register( - httpmock.GraphQL(query), - httpmock.StringResponse(fmt.Sprintf( - tt.response, - sixHoursAgo.Format(time.RFC3339), - )), - ) - client := &http.Client{Transport: reg} - - t.Run(tt.name, func(t *testing.T) { - //nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock - as := prompt.NewAskStubber(t) - if tt.askStubs != nil { - tt.askStubs(as) - } - - gistID, err := promptGists(client, "github.com", ios.ColorScheme()) - assert.NoError(t, err) - assert.Equal(t, tt.wantOut, gistID) - reg.Verify(t) - }) - } -} diff --git a/pkg/cmd/issue/create/create.go b/pkg/cmd/issue/create/create.go index 2414c1807..a4db9d3a1 100644 --- a/pkg/cmd/issue/create/create.go +++ b/pkg/cmd/issue/create/create.go @@ -11,7 +11,6 @@ import ( "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/internal/text" - "github.com/cli/cli/v2/pkg/cmd/pr/shared" prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" @@ -143,7 +142,7 @@ func createRun(opts *CreateOptions) (err error) { milestones = []string{opts.Milestone} } - meReplacer := shared.NewMeReplacer(apiClient, baseRepo.RepoHost()) + meReplacer := prShared.NewMeReplacer(apiClient, baseRepo.RepoHost()) assignees, err := meReplacer.ReplaceSlice(opts.Assignees) if err != nil { return err @@ -167,7 +166,7 @@ func createRun(opts *CreateOptions) (err error) { } } - tpl := shared.NewTemplateManager(httpClient, baseRepo, opts.Prompter, opts.RootDirOverride, !opts.HasRepoOverride, false) + tpl := prShared.NewTemplateManager(httpClient, baseRepo, opts.Prompter, opts.RootDirOverride, !opts.HasRepoOverride, false) if opts.WebMode { var openURL string @@ -222,7 +221,7 @@ func createRun(opts *CreateOptions) (err error) { templateContent := "" if opts.RecoverFile == "" { - var template shared.Template + var template prShared.Template if opts.Template != "" { template, err = tpl.Select(opts.Template) @@ -325,7 +324,7 @@ func createRun(opts *CreateOptions) (err error) { return } -func generatePreviewURL(apiClient *api.Client, baseRepo ghrepo.Interface, tb shared.IssueMetadataState) (string, error) { +func generatePreviewURL(apiClient *api.Client, baseRepo ghrepo.Interface, tb prShared.IssueMetadataState) (string, error) { openURL := ghrepo.GenerateRepoURL(baseRepo, "issues/new") return prShared.WithPrAndIssueQueryParams(apiClient, baseRepo, openURL, tb) } diff --git a/pkg/cmd/issue/develop/develop.go b/pkg/cmd/issue/develop/develop.go index cc64e3e36..1a100c9a1 100644 --- a/pkg/cmd/issue/develop/develop.go +++ b/pkg/cmd/issue/develop/develop.go @@ -9,7 +9,6 @@ import ( "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/context" "github.com/cli/cli/v2/git" - "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/internal/tableprinter" "github.com/cli/cli/v2/pkg/cmd/issue/shared" @@ -21,17 +20,16 @@ import ( type DevelopOptions struct { HttpClient func() (*http.Client, error) GitClient *git.Client - Config func() (config.Config, error) IO *iostreams.IOStreams BaseRepo func() (ghrepo.Interface, error) Remotes func() (context.Remotes, error) - IssueRepoSelector string - IssueSelector string - Name string - BaseBranch string - Checkout bool - List bool + IssueSelector string + Name string + BranchRepo string + BaseBranch string + Checkout bool + List bool } func NewCmdDevelop(f *cmdutil.Factory, runF func(*DevelopOptions) error) *cobra.Command { @@ -39,239 +37,223 @@ func NewCmdDevelop(f *cmdutil.Factory, runF func(*DevelopOptions) error) *cobra. IO: f.IOStreams, HttpClient: f.HttpClient, GitClient: f.GitClient, - Config: f.Config, BaseRepo: f.BaseRepo, Remotes: f.Remotes, } cmd := &cobra.Command{ - Use: "develop [flags] { | }", + Use: "develop { | }", Short: "Manage linked branches for an issue", Example: heredoc.Doc(` - $ gh issue develop --list 123 # list branches for issue 123 - $ gh issue develop --list --issue-repo "github/cli" 123 # list branches for issue 123 in repo "github/cli" - $ gh issue develop --list https://github.com/github/cli/issues/123 # list branches for issue 123 in repo "github/cli" - $ gh issue develop 123 --name "my-branch" --base my-feature # create a branch for issue 123 based on the my-feature branch - $ gh issue develop 123 --checkout # fetch and checkout the branch for issue 123 after creating it - `), + # List branches for issue 123 + $ gh issue develop --list 123 + + # List branches for issue 123 in repo cli/cli + $ gh issue develop --list --repo cli/cli 123 + + # Create a branch for issue 123 based on the my-feature branch + $ gh issue develop 123 --base my-feature + + # Create a branch for issue 123 and checkout it out + $ gh issue develop 123 --checkout + + # Create a branch in repo monalisa/cli for issue 123 in repo cli/cli + $ gh issue develop 123 --repo cli/cli --branch-repo monalisa/cli + `), Args: cmdutil.ExactArgs(1, "issue number or url is required"), + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + // This is all a hack to not break the deprecated issue-repo flag. + // It will be removed in the near future and this hack can be removed at the same time. + flags := cmd.Flags() + if flags.Changed("issue-repo") { + if flags.Changed("repo") { + if flags.Changed("branch-repo") { + return cmdutil.FlagErrorf("specify only `--repo` and `--branch-repo`") + } + branchRepo, _ := flags.GetString("repo") + _ = flags.Set("branch-repo", branchRepo) + } + repo, _ := flags.GetString("issue-repo") + _ = flags.Set("repo", repo) + } + if cmd.Parent() != nil { + return cmd.Parent().PersistentPreRunE(cmd, args) + } + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { + // support `-R, --repo` override + opts.BaseRepo = f.BaseRepo + opts.IssueSelector = args[0] + if err := cmdutil.MutuallyExclusive("specify only one of `--list` or `--branch-repo`", opts.List, opts.BranchRepo != ""); err != nil { + return err + } + if err := cmdutil.MutuallyExclusive("specify only one of `--list` or `--base`", opts.List, opts.BaseBranch != ""); err != nil { + return err + } + if err := cmdutil.MutuallyExclusive("specify only one of `--list` or `--checkout`", opts.List, opts.Checkout); err != nil { + return err + } + if err := cmdutil.MutuallyExclusive("specify only one of `--list` or `--name`", opts.List, opts.Name != ""); err != nil { + return err + } if runF != nil { return runF(opts) } - opts.IssueSelector = args[0] - if opts.List { - return developRunList(opts) - } - return developRunCreate(opts) + return developRun(opts) }, } + fl := cmd.Flags() + fl.StringVar(&opts.BranchRepo, "branch-repo", "", "Name or URL of the repository where you want to create your new branch") fl.StringVarP(&opts.BaseBranch, "base", "b", "", "Name of the base branch you want to make your new branch from") fl.BoolVarP(&opts.Checkout, "checkout", "c", false, "Checkout the branch after creating it") - fl.StringVarP(&opts.IssueRepoSelector, "issue-repo", "i", "", "Name or URL of the issue's repository") fl.BoolVarP(&opts.List, "list", "l", false, "List linked branches for the issue") fl.StringVarP(&opts.Name, "name", "n", "", "Name of the branch to create") + + var issueRepoSelector string + fl.StringVarP(&issueRepoSelector, "issue-repo", "i", "", "Name or URL of the issue's repository") + _ = cmd.Flags().MarkDeprecated("issue-repo", "use `--repo` instead") + return cmd } -func developRunCreate(opts *DevelopOptions) (err error) { +func developRun(opts *DevelopOptions) error { httpClient, err := opts.HttpClient() if err != nil { return err } - apiClient := api.NewClientFromHTTP(httpClient) - baseRepo, err := opts.BaseRepo() - if err != nil { - return err - } opts.IO.StartProgressIndicator() - err = api.CheckLinkedBranchFeature(apiClient, baseRepo.RepoHost()) - if err != nil { - return err - } - - repo, err := api.GitHubRepo(apiClient, baseRepo) - if err != nil { - return err - } - - issueNumber, issueRepo, err := issueMetadata(opts.IssueSelector, opts.IssueRepoSelector, baseRepo) - if err != nil { - return err - } - - // The mutation requires the issue id, not just its number - issue, _, err := shared.IssueFromArgWithFields(httpClient, func() (ghrepo.Interface, error) { return issueRepo, nil }, fmt.Sprint(issueNumber), []string{"id"}) - if err != nil { - return err - } - - // The mutation takes an oid instead of a branch name as it's a more stable reference - oid, default_branch_oid, err := api.FindBaseOid(apiClient, repo, opts.BaseBranch) - if err != nil { - return err - } - - if oid == "" { - oid = default_branch_oid - } - - // get the oid of the branch from the base repo - params := map[string]interface{}{ - "issueId": issue.ID, - "name": opts.Name, - "oid": oid, - "repositoryId": repo.ID, - } - - ref, err := api.CreateBranchIssueReference(apiClient, repo, params) + issue, issueRepo, err := shared.IssueFromArgWithFields(httpClient, opts.BaseRepo, opts.IssueSelector, []string{"id", "number"}) opts.IO.StopProgressIndicator() - if ref != nil { - baseRepo.RepoHost() - fmt.Fprintf(opts.IO.Out, "%s/%s/%s/tree/%s\n", baseRepo.RepoHost(), baseRepo.RepoOwner(), baseRepo.RepoName(), ref.BranchName) - - if opts.Checkout { - return checkoutBranch(opts, baseRepo, ref.BranchName) - } - } if err != nil { return err } - return + + apiClient := api.NewClientFromHTTP(httpClient) + + opts.IO.StartProgressIndicator() + err = api.CheckLinkedBranchFeature(apiClient, issueRepo.RepoHost()) + opts.IO.StopProgressIndicator() + if err != nil { + return err + } + + if opts.List { + return developRunList(opts, apiClient, issueRepo, issue) + } + return developRunCreate(opts, apiClient, issueRepo, issue) } -// If the issue is in the base repo, we can use the issue number directly. Otherwise, we need to use the issue's url or the IssueRepoSelector argument. -// If the repo from the URL doesn't match the IssueRepoSelector argument, we error. -func issueMetadata(issueSelector string, issueRepoSelector string, baseRepo ghrepo.Interface) (issueNumber int, issueFlagRepo ghrepo.Interface, err error) { - var targetRepo ghrepo.Interface - if issueRepoSelector != "" { - issueFlagRepo, err = ghrepo.FromFullNameWithHost(issueRepoSelector, baseRepo.RepoHost()) +func developRunCreate(opts *DevelopOptions, apiClient *api.Client, issueRepo ghrepo.Interface, issue *api.Issue) error { + branchRepo := issueRepo + var repoID string + if opts.BranchRepo != "" { + var err error + branchRepo, err = ghrepo.FromFullName(opts.BranchRepo) if err != nil { - return 0, nil, err + return err } } - if issueFlagRepo != nil { - targetRepo = issueFlagRepo - } - - issueNumber, issueArgRepo, err := shared.IssueNumberAndRepoFromArg(issueSelector) - if err != nil { - return 0, nil, err - } - - if issueArgRepo != nil { - targetRepo = issueArgRepo - - if issueFlagRepo != nil { - differentOwner := (issueFlagRepo.RepoOwner() != issueArgRepo.RepoOwner()) - differentName := (issueFlagRepo.RepoName() != issueArgRepo.RepoName()) - if differentOwner || differentName { - return 0, nil, fmt.Errorf("issue repo in url %s/%s does not match the repo from --issue-repo %s/%s", issueArgRepo.RepoOwner(), issueArgRepo.RepoName(), issueFlagRepo.RepoOwner(), issueFlagRepo.RepoName()) - } - } - } - - if issueFlagRepo == nil && issueArgRepo == nil { - targetRepo = baseRepo - } - - if targetRepo == nil { - return 0, nil, fmt.Errorf("could not determine issue repo") - } - - return issueNumber, targetRepo, nil -} - -func printLinkedBranches(io *iostreams.IOStreams, branches []api.LinkedBranch) { - - cs := io.ColorScheme() - table := tableprinter.New(io) - - for _, branch := range branches { - table.AddField(branch.BranchName, tableprinter.WithColor(cs.ColorFromString("cyan"))) - if io.CanPrompt() { - table.AddField(branch.Url()) - } - table.EndRow() - } - - _ = table.Render() -} - -func developRunList(opts *DevelopOptions) (err error) { - httpClient, err := opts.HttpClient() - if err != nil { - return err - } - - apiClient := api.NewClientFromHTTP(httpClient) - baseRepo, err := opts.BaseRepo() + opts.IO.StartProgressIndicator() + repoID, branchID, err := api.FindRepoBranchID(apiClient, branchRepo, opts.BaseBranch) + opts.IO.StopProgressIndicator() if err != nil { return err } opts.IO.StartProgressIndicator() - - err = api.CheckLinkedBranchFeature(apiClient, baseRepo.RepoHost()) - if err != nil { - return err - } - issueNumber, issueRepo, err := issueMetadata(opts.IssueSelector, opts.IssueRepoSelector, baseRepo) - if err != nil { - return err - } - - branches, err := api.ListLinkedBranches(apiClient, issueRepo, issueNumber) - if err != nil { - return err - } - + branchName, err := api.CreateLinkedBranch(apiClient, branchRepo.RepoHost(), repoID, issue.ID, branchID, opts.Name) opts.IO.StopProgressIndicator() + if err != nil { + return err + } + + fmt.Fprintf(opts.IO.Out, "%s/%s/%s/tree/%s\n", branchRepo.RepoHost(), branchRepo.RepoOwner(), branchRepo.RepoName(), branchName) + + return checkoutBranch(opts, branchRepo, branchName) +} + +func developRunList(opts *DevelopOptions, apiClient *api.Client, issueRepo ghrepo.Interface, issue *api.Issue) error { + opts.IO.StartProgressIndicator() + branches, err := api.ListLinkedBranches(apiClient, issueRepo, issue.Number) + opts.IO.StopProgressIndicator() + if err != nil { + return err + } + if len(branches) == 0 { - return cmdutil.NewNoResultsError(fmt.Sprintf("no linked branches found for %s/%s#%d", issueRepo.RepoOwner(), issueRepo.RepoName(), issueNumber)) + return cmdutil.NewNoResultsError(fmt.Sprintf("no linked branches found for %s/%s#%d", issueRepo.RepoOwner(), issueRepo.RepoName(), issue.Number)) } if opts.IO.IsStdoutTTY() { - fmt.Fprintf(opts.IO.Out, "\nShowing linked branches for %s/%s#%d\n\n", issueRepo.RepoOwner(), issueRepo.RepoName(), issueNumber) + fmt.Fprintf(opts.IO.Out, "\nShowing linked branches for %s/%s#%d\n\n", issueRepo.RepoOwner(), issueRepo.RepoName(), issue.Number) } printLinkedBranches(opts.IO, branches) return nil - } -func checkoutBranch(opts *DevelopOptions, baseRepo ghrepo.Interface, checkoutBranch string) (err error) { +func printLinkedBranches(io *iostreams.IOStreams, branches []api.LinkedBranch) { + cs := io.ColorScheme() + table := tableprinter.New(io) + for _, branch := range branches { + table.AddField(branch.BranchName, tableprinter.WithColor(cs.ColorFromString("cyan"))) + table.AddField(branch.URL) + table.EndRow() + } + _ = table.Render() +} + +func checkoutBranch(opts *DevelopOptions, branchRepo ghrepo.Interface, checkoutBranch string) (err error) { remotes, err := opts.Remotes() if err != nil { - return err - } - - baseRemote, err := remotes.FindByRepo(baseRepo.RepoOwner(), baseRepo.RepoName()) - if err != nil { - return err - } - - if opts.GitClient.HasLocalBranch(ctx.Background(), checkoutBranch) { - if err := opts.GitClient.CheckoutBranch(ctx.Background(), checkoutBranch); err != nil { + // If the user specified the branch to be checked out and no remotes are found + // display an error. Otherwise bail out silently, likely the command was not + // run from inside a git directory. + if opts.Checkout { return err + } else { + return nil + } + } + + baseRemote, err := remotes.FindByRepo(branchRepo.RepoOwner(), branchRepo.RepoName()) + if err != nil { + // If the user specified the branch to be checked out and no remote matches the + // base repo, then display an error. Otherwise bail out silently. + if opts.Checkout { + return err + } else { + return nil + } + } + + gc := opts.GitClient + + if err := gc.Fetch(ctx.Background(), baseRemote.Name, fmt.Sprintf("+refs/heads/%[1]s:refs/remotes/%[2]s/%[1]s", checkoutBranch, baseRemote.Name)); err != nil { + return err + } + + if !opts.Checkout { + return nil + } + + if gc.HasLocalBranch(ctx.Background(), checkoutBranch) { + if err := gc.CheckoutBranch(ctx.Background(), checkoutBranch); err != nil { + return err + } + + if err := gc.Pull(ctx.Background(), baseRemote.Name, checkoutBranch); err != nil { + _, _ = fmt.Fprintf(opts.IO.ErrOut, "%s warning: not possible to fast-forward to: %q\n", opts.IO.ColorScheme().WarningIcon(), checkoutBranch) } } else { - err := opts.GitClient.Fetch(ctx.Background(), "origin", fmt.Sprintf("+refs/heads/%[1]s:refs/remotes/origin/%[1]s", checkoutBranch)) - if err != nil { + if err := gc.CheckoutNewBranch(ctx.Background(), baseRemote.Name, checkoutBranch); err != nil { return err } - - if err := opts.GitClient.CheckoutNewBranch(ctx.Background(), baseRemote.Name, checkoutBranch); err != nil { - return err - } - } - - if err := opts.GitClient.Pull(ctx.Background(), baseRemote.Name, checkoutBranch); err != nil { - _, _ = fmt.Fprintf(opts.IO.ErrOut, "%s warning: not possible to fast-forward to: %q\n", opts.IO.ColorScheme().WarningIcon(), checkoutBranch) } return nil diff --git a/pkg/cmd/issue/develop/develop_test.go b/pkg/cmd/issue/develop/develop_test.go index 1cd7526f6..d4f8bce2d 100644 --- a/pkg/cmd/issue/develop/develop_test.go +++ b/pkg/cmd/issue/develop/develop_test.go @@ -1,541 +1,527 @@ package develop import ( + "bytes" "errors" "net/http" "testing" "github.com/cli/cli/v2/context" "github.com/cli/cli/v2/git" - "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/internal/run" + "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" - "github.com/cli/cli/v2/test" + "github.com/google/shlex" "github.com/stretchr/testify/assert" ) -func Test_developRun(t *testing.T) { - featureEnabledPayload := `{ - "data": { - "LinkedBranch": { - "fields": [ - { - "name": "id" - }, - { - "name": "ref" - } - ] - } - } - }` +func TestNewCmdDevelop(t *testing.T) { + tests := []struct { + name string + input string + output DevelopOptions + wantStdout string + wantStderr string + wantErr bool + errMsg string + }{ + { + name: "no argument", + input: "", + output: DevelopOptions{}, + wantErr: true, + errMsg: "issue number or url is required", + }, + { + name: "issue number", + input: "1", + output: DevelopOptions{ + IssueSelector: "1", + }, + }, + { + name: "issue url", + input: "https://github.com/cli/cli/issues/1", + output: DevelopOptions{ + IssueSelector: "https://github.com/cli/cli/issues/1", + }, + }, + { + name: "branch-repo flag", + input: "1 --branch-repo owner/repo", + output: DevelopOptions{ + IssueSelector: "1", + BranchRepo: "owner/repo", + }, + }, + { + name: "base flag", + input: "1 --base feature", + output: DevelopOptions{ + IssueSelector: "1", + BaseBranch: "feature", + }, + }, + { + name: "checkout flag", + input: "1 --checkout", + output: DevelopOptions{ + IssueSelector: "1", + Checkout: true, + }, + }, + { + name: "list flag", + input: "1 --list", + output: DevelopOptions{ + IssueSelector: "1", + List: true, + }, + }, + { + name: "name flag", + input: "1 --name feature", + output: DevelopOptions{ + IssueSelector: "1", + Name: "feature", + }, + }, + { + name: "issue-repo flag", + input: "1 --issue-repo cli/cli", + output: DevelopOptions{ + IssueSelector: "1", + }, + wantStdout: "Flag --issue-repo has been deprecated, use `--repo` instead\n", + }, + { + name: "list and branch repo flags", + input: "1 --list --branch-repo owner/repo", + wantErr: true, + errMsg: "specify only one of `--list` or `--branch-repo`", + }, + { + name: "list and base flags", + input: "1 --list --base feature", + wantErr: true, + errMsg: "specify only one of `--list` or `--base`", + }, + { + name: "list and checkout flags", + input: "1 --list --checkout", + wantErr: true, + errMsg: "specify only one of `--list` or `--checkout`", + }, + { + name: "list and name flags", + input: "1 --list --name my-branch", + wantErr: true, + errMsg: "specify only one of `--list` or `--name`", + }, + } - featureDisabledPayload := `{ "data": { "LinkedBranch": null } }` + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, stdOut, stdErr := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + argv, err := shlex.Split(tt.input) + assert.NoError(t, err) + var gotOpts *DevelopOptions + cmd := NewCmdDevelop(f, func(opts *DevelopOptions) error { + gotOpts = opts + return nil + }) + cmd.SetArgs(argv) + cmd.SetIn(&bytes.Buffer{}) + cmd.SetOut(stdOut) + cmd.SetErr(stdErr) + + _, err = cmd.ExecuteC() + if tt.wantErr { + assert.EqualError(t, err, tt.errMsg) + return + } + + assert.NoError(t, err) + assert.Equal(t, tt.output.IssueSelector, gotOpts.IssueSelector) + assert.Equal(t, tt.output.Name, gotOpts.Name) + assert.Equal(t, tt.output.BaseBranch, gotOpts.BaseBranch) + assert.Equal(t, tt.output.Checkout, gotOpts.Checkout) + assert.Equal(t, tt.output.List, gotOpts.List) + assert.Equal(t, tt.wantStdout, stdOut.String()) + assert.Equal(t, tt.wantStderr, stdErr.String()) + }) + } +} + +func TestDevelopRun(t *testing.T) { + featureEnabledPayload := `{"data":{"LinkedBranch":{"fields":[{"name":"id"},{"name":"ref"}]}}}` + featureDisabledPayload := `{"data":{"LinkedBranch":null}}` tests := []struct { name string - setup func(*DevelopOptions, *testing.T) func() + opts *DevelopOptions cmdStubs func(*run.CommandStubber) runStubs func(*run.CommandStubber) remotes map[string]string - askStubs func(*prompt.AskStubber) // TODO eventually migrate to PrompterMock httpStubs func(*httpmock.Registry, *testing.T) expectedOut string expectedErrOut string - expectedBrowse string wantErr string tty bool }{ - {name: "list branches for an issue", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.IssueSelector = "42" - opts.List = true - return func() {} + { + name: "returns an error when the feature is not supported by the API", + opts: &DevelopOptions{ + IssueSelector: "42", + List: true, }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), - httpmock.StringResponse(featureEnabledPayload), + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"id":"SOMEID","number":42}}}}`), ) reg.Register( - httpmock.GraphQL(`query BranchIssueReferenceListLinkedBranches\b`), - httpmock.GraphQLQuery(`{ - "data": { - "repository": { - "issue": { - "linkedBranches": { - "edges": [ - { - "node": { - "ref": { - "name": "foo" - } - } - }, - { - "node": { - "ref": { - "name": "bar" - } - } - } - ] - } - } - } - } - } - `, func(query string, inputs map[string]interface{}) { - assert.Equal(t, float64(42), inputs["issueNumber"]) - assert.Equal(t, "OWNER", inputs["repositoryOwner"]) - assert.Equal(t, "REPO", inputs["repositoryName"]) - })) - }, - expectedOut: "foo\nbar\n", - }, - {name: "list branches for an issue in tty", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.IssueSelector = "42" - opts.List = true - return func() {} - }, - tty: true, - httpStubs: func(reg *httpmock.Registry, t *testing.T) { - reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), - httpmock.StringResponse(featureEnabledPayload), - ) - reg.Register( - httpmock.GraphQL(`query BranchIssueReferenceListLinkedBranches\b`), - httpmock.GraphQLQuery(`{ - "data": { - "repository": { - "issue": { - "linkedBranches": { - "edges": [ - { - "node": { - "ref": { - "name": "foo", - "repository": { - "url": "http://github.localhost/OWNER/REPO" - } - } - } - }, - { - "node": { - "ref": { - "name": "bar", - "repository": { - "url": "http://github.localhost/OWNER/OTHER-REPO" - } - } - } - } - ] - } - } - } - } - } - `, func(query string, inputs map[string]interface{}) { - assert.Equal(t, float64(42), inputs["issueNumber"]) - assert.Equal(t, "OWNER", inputs["repositoryOwner"]) - assert.Equal(t, "REPO", inputs["repositoryName"]) - })) - }, - expectedOut: "\nShowing linked branches for OWNER/REPO#42\n\nfoo http://github.localhost/OWNER/REPO/tree/foo\nbar http://github.localhost/OWNER/OTHER-REPO/tree/bar\n", - }, - {name: "list branches for an issue providing an issue url", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.IssueSelector = "https://github.com/cli/test-repo/issues/42" - opts.List = true - return func() {} - }, - httpStubs: func(reg *httpmock.Registry, t *testing.T) { - reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), - httpmock.StringResponse(featureEnabledPayload), - ) - reg.Register( - httpmock.GraphQL(`query BranchIssueReferenceListLinkedBranches\b`), - httpmock.GraphQLQuery(`{ - "data": { - "repository": { - "issue": { - "linkedBranches": { - "edges": [ - { - "node": { - "ref": { - "name": "foo" - } - } - }, - { - "node": { - "ref": { - "name": "bar" - } - } - } - ] - } - } - } - } - } - `, func(query string, inputs map[string]interface{}) { - assert.Equal(t, float64(42), inputs["issueNumber"]) - assert.Equal(t, "cli", inputs["repositoryOwner"]) - assert.Equal(t, "test-repo", inputs["repositoryName"]) - })) - }, - expectedOut: "foo\nbar\n", - }, - {name: "list branches for an issue providing an issue repo", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.IssueSelector = "42" - opts.IssueRepoSelector = "cli/test-repo" - opts.List = true - return func() {} - }, - httpStubs: func(reg *httpmock.Registry, t *testing.T) { - reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), - httpmock.StringResponse(featureEnabledPayload), - ) - reg.Register( - httpmock.GraphQL(`query BranchIssueReferenceListLinkedBranches\b`), - httpmock.GraphQLQuery(`{ - "data": { - "repository": { - "issue": { - "linkedBranches": { - "edges": [ - { - "node": { - "ref": { - "name": "foo" - } - } - }, - { - "node": { - "ref": { - "name": "bar" - } - } - } - ] - } - } - } - } - } - `, func(query string, inputs map[string]interface{}) { - assert.Equal(t, float64(42), inputs["issueNumber"]) - assert.Equal(t, "cli", inputs["repositoryOwner"]) - assert.Equal(t, "test-repo", inputs["repositoryName"]) - })) - }, - expectedOut: "foo\nbar\n", - }, - {name: "list branches for an issue providing an issue url and specifying the same repo works", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.IssueSelector = "https://github.com/cli/test-repo/issues/42" - opts.IssueRepoSelector = "cli/test-repo" - opts.List = true - return func() {} - }, - httpStubs: func(reg *httpmock.Registry, t *testing.T) { - reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), - httpmock.StringResponse(featureEnabledPayload), - ) - reg.Register( - httpmock.GraphQL(`query BranchIssueReferenceListLinkedBranches\b`), - httpmock.GraphQLQuery(`{ - "data": { - "repository": { - "issue": { - "linkedBranches": { - "edges": [ - { - "node": { - "ref": { - "name": "foo" - } - } - }, - { - "node": { - "ref": { - "name": "bar" - } - } - } - ] - } - } - } - } - } - `, func(query string, inputs map[string]interface{}) { - assert.Equal(t, float64(42), inputs["issueNumber"]) - assert.Equal(t, "cli", inputs["repositoryOwner"]) - assert.Equal(t, "test-repo", inputs["repositoryName"]) - })) - }, - expectedOut: "foo\nbar\n", - }, - {name: "list branches for an issue providing an issue url and specifying a different repo returns an error", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.IssueSelector = "https://github.com/cli/test-repo/issues/42" - opts.IssueRepoSelector = "cli/other" - opts.List = true - return func() {} - }, - httpStubs: func(reg *httpmock.Registry, t *testing.T) { - reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), - httpmock.StringResponse(featureEnabledPayload), - ) - }, - wantErr: "issue repo in url cli/test-repo does not match the repo from --issue-repo cli/other", - }, - {name: "returns an error when the feature isn't enabled in the GraphQL API", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.IssueSelector = "https://github.com/cli/test-repo/issues/42" - opts.List = true - return func() {} - }, - httpStubs: func(reg *httpmock.Registry, t *testing.T) { - reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), + httpmock.GraphQL(`query LinkedBranchFeature\b`), httpmock.StringResponse(featureDisabledPayload), ) }, wantErr: "the `gh issue develop` command is not currently available", }, - {name: "develop new branch with a name provided", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.Name = "my-branch" - opts.BaseBranch = "main" - opts.IssueSelector = "123" - return func() {} + { + name: "list branches for an issue", + opts: &DevelopOptions{ + IssueSelector: "42", + List: true, }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { - reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), - httpmock.StringResponse(featureEnabledPayload), - ) - reg.Register( - httpmock.GraphQL(`query RepositoryInfo\b`), - httpmock.StringResponse(` - { "data": { "repository": { - "id": "REPOID", - "hasIssuesEnabled": true - } } }`), - ) reg.Register( httpmock.GraphQL(`query IssueByNumber\b`), - httpmock.StringResponse(`{"data":{"repository":{ "hasIssuesEnabled": true, "issue":{"id": "yar", "number":123, "title":"my issue"} }}}`)) - reg.Register( - httpmock.GraphQL(`query BranchIssueReferenceFindBaseOid\b`), - httpmock.StringResponse(`{"data":{"repository":{"ref":{"target":{"oid":"123"}}}}}`)) - - reg.Register( - httpmock.GraphQL(`(?s)mutation CreateLinkedBranch\b.*issueId: \$issueId,\s+name: \$name,\s+oid: \$oid,`), - httpmock.GraphQLQuery(`{ "data": { "createLinkedBranch": { "linkedBranch": {"id": "2", "ref": {"name": "my-branch"} } } } }`, - func(query string, inputs map[string]interface{}) { - assert.Equal(t, "REPOID", inputs["repositoryId"]) - assert.Equal(t, "my-branch", inputs["name"]) - assert.Equal(t, "yar", inputs["issueId"]) - }), + httpmock.StringResponse(`{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"id":"SOMEID","number":42}}}}`), ) - - }, - expectedOut: "github.com/OWNER/REPO/tree/my-branch\n", - }, - {name: "develop new branch without a name provided omits the param from the mutation", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.Name = "" - opts.BaseBranch = "main" - opts.IssueSelector = "123" - return func() {} - }, - httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), + httpmock.GraphQL(`query LinkedBranchFeature\b`), httpmock.StringResponse(featureEnabledPayload), ) reg.Register( - httpmock.GraphQL(`query RepositoryInfo\b`), - httpmock.StringResponse(` - { "data": { "repository": { - "id": "REPOID", - "hasIssuesEnabled": true - } } }`), - ) + httpmock.GraphQL(`query ListLinkedBranches\b`), + httpmock.GraphQLQuery(` + {"data":{"repository":{"issue":{"linkedBranches":{"nodes":[{"ref":{"name":"foo","repository":{"url":"https://github.com/OWNER/REPO"}}},{"ref":{"name":"bar","repository":{"url":"https://github.com/OWNER/REPO"}}}]}}}}} + `, func(query string, inputs map[string]interface{}) { + assert.Equal(t, float64(42), inputs["number"]) + assert.Equal(t, "OWNER", inputs["owner"]) + assert.Equal(t, "REPO", inputs["name"]) + })) + }, + expectedOut: "foo\thttps://github.com/OWNER/REPO/tree/foo\nbar\thttps://github.com/OWNER/REPO/tree/bar\n", + }, + { + name: "list branches for an issue in tty", + opts: &DevelopOptions{ + IssueSelector: "42", + List: true, + }, + tty: true, + httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( httpmock.GraphQL(`query IssueByNumber\b`), - httpmock.StringResponse(`{"data":{"repository":{ "hasIssuesEnabled": true, "issue":{"id": "yar", "number":123, "title":"my issue"} }}}`)) - reg.Register( - httpmock.GraphQL(`query BranchIssueReferenceFindBaseOid\b`), - httpmock.StringResponse(`{"data":{"repository":{"ref":{"target":{"oid":"123"}}}}}`)) - - reg.Register( - httpmock.GraphQL(`(?s)mutation CreateLinkedBranch\b.*\$oid: GitObjectID!, \$repositoryId:.*issueId: \$issueId,\s+oid: \$oid,`), - httpmock.GraphQLQuery(`{ "data": { "createLinkedBranch": { "linkedBranch": {"id": "2", "ref": {"name": "my-issue-1"} } } } }`, - func(query string, inputs map[string]interface{}) { - assert.Equal(t, "REPOID", inputs["repositoryId"]) - assert.Equal(t, "", inputs["name"]) - assert.Equal(t, "yar", inputs["issueId"]) - }), + httpmock.StringResponse(`{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"id":"SOMEID","number":42}}}}`), ) - - }, - expectedOut: "github.com/OWNER/REPO/tree/my-issue-1\n", - }, - {name: "develop providing an issue url and specifying a different repo returns an error", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.IssueSelector = "https://github.com/cli/test-repo/issues/42" - opts.IssueRepoSelector = "cli/other" - return func() {} - }, - httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), + httpmock.GraphQL(`query LinkedBranchFeature\b`), httpmock.StringResponse(featureEnabledPayload), ) reg.Register( - httpmock.GraphQL(`query RepositoryInfo\b`), - httpmock.StringResponse(` - { "data": { "repository": { - "id": "REPOID", - "hasIssuesEnabled": true - } } }`), - ) + httpmock.GraphQL(`query ListLinkedBranches\b`), + httpmock.GraphQLQuery(` + {"data":{"repository":{"issue":{"linkedBranches":{"nodes":[{"ref":{"name":"foo","repository":{"url":"https://github.com/OWNER/REPO"}}},{"ref":{"name":"bar","repository":{"url":"https://github.com/OWNER/OTHER-REPO"}}}]}}}}} + `, func(query string, inputs map[string]interface{}) { + assert.Equal(t, float64(42), inputs["number"]) + assert.Equal(t, "OWNER", inputs["owner"]) + assert.Equal(t, "REPO", inputs["name"]) + })) }, - wantErr: "issue repo in url cli/test-repo does not match the repo from --issue-repo cli/other", + expectedOut: "\nShowing linked branches for OWNER/REPO#42\n\nfoo https://github.com/OWNER/REPO/tree/foo\nbar https://github.com/OWNER/OTHER-REPO/tree/bar\n", }, - {name: "develop new branch with checkout when the branch exists locally", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.Name = "my-branch" - opts.BaseBranch = "main" - opts.IssueSelector = "123" - opts.Checkout = true - return func() {} + { + name: "list branches for an issue providing an issue url", + opts: &DevelopOptions{ + IssueSelector: "https://github.com/cli/cli/issues/42", + List: true, + }, + httpStubs: func(reg *httpmock.Registry, t *testing.T) { + reg.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"id":"SOMEID","number":42}}}}`), + ) + reg.Register( + httpmock.GraphQL(`query LinkedBranchFeature\b`), + httpmock.StringResponse(featureEnabledPayload), + ) + reg.Register( + httpmock.GraphQL(`query ListLinkedBranches\b`), + httpmock.GraphQLQuery(` + {"data":{"repository":{"issue":{"linkedBranches":{"nodes":[{"ref":{"name":"foo","repository":{"url":"https://github.com/OWNER/REPO"}}},{"ref":{"name":"bar","repository":{"url":"https://github.com/OWNER/OTHER-REPO"}}}]}}}}} + `, func(query string, inputs map[string]interface{}) { + assert.Equal(t, float64(42), inputs["number"]) + assert.Equal(t, "cli", inputs["owner"]) + assert.Equal(t, "cli", inputs["name"]) + })) + }, + expectedOut: "foo\thttps://github.com/OWNER/REPO/tree/foo\nbar\thttps://github.com/OWNER/OTHER-REPO/tree/bar\n", + }, + { + name: "develop new branch", + opts: &DevelopOptions{ + IssueSelector: "123", }, remotes: map[string]string{ "origin": "OWNER/REPO", }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), + httpmock.GraphQL(`query LinkedBranchFeature\b`), httpmock.StringResponse(featureEnabledPayload), ) reg.Register( - httpmock.GraphQL(`query RepositoryInfo\b`), - httpmock.StringResponse(` - { "data": { "repository": { - "id": "REPOID", - "hasIssuesEnabled": true - } } }`), + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"id": "SOMEID","number":123,"title":"my issue"}}}}`), + ) + reg.Register( + httpmock.GraphQL(`query FindRepoBranchID\b`), + httpmock.StringResponse(`{"data":{"repository":{"id":"REPOID","defaultBranchRef":{"target":{"oid":"DEFAULTOID"}},"ref":{"target":{"oid":""}}}}}`), + ) + reg.Register( + httpmock.GraphQL(`mutation CreateLinkedBranch\b`), + httpmock.GraphQLMutation(`{"data":{"createLinkedBranch":{"linkedBranch":{"id":"2","ref":{"name":"my-issue-1"}}}}}`, + func(inputs map[string]interface{}) { + assert.Equal(t, "REPOID", inputs["repositoryId"]) + assert.Equal(t, "SOMEID", inputs["issueId"]) + assert.Equal(t, "DEFAULTOID", inputs["oid"]) + }), + ) + }, + runStubs: func(cs *run.CommandStubber) { + cs.Register(`git fetch origin \+refs/heads/my-issue-1:refs/remotes/origin/my-issue-1`, 0, "") + }, + expectedOut: "github.com/OWNER/REPO/tree/my-issue-1\n", + }, + { + name: "develop new branch in diffferent repo than issue", + opts: &DevelopOptions{ + IssueSelector: "123", + BranchRepo: "OWNER2/REPO", + }, + remotes: map[string]string{ + "origin": "OWNER2/REPO", + }, + httpStubs: func(reg *httpmock.Registry, t *testing.T) { + reg.Register( + httpmock.GraphQL(`query LinkedBranchFeature\b`), + httpmock.StringResponse(featureEnabledPayload), ) reg.Register( httpmock.GraphQL(`query IssueByNumber\b`), - httpmock.StringResponse(`{"data":{"repository":{ "hasIssuesEnabled": true, "issue":{"id": "yar", "number":123, "title":"my issue"} }}}`)) - reg.Register( - httpmock.GraphQL(`query BranchIssueReferenceFindBaseOid\b`), - httpmock.StringResponse(`{"data":{"repository":{"ref":{"target":{"oid":"123"}}}}}`)) - - reg.Register( - httpmock.GraphQL(`mutation CreateLinkedBranch\b`), - httpmock.GraphQLQuery(`{ "data": { "createLinkedBranch": { "linkedBranch": {"id": "2", "ref": {"name": "my-branch"} } } } }`, - func(query string, inputs map[string]interface{}) { - assert.Equal(t, "REPOID", inputs["repositoryId"]) - assert.Equal(t, "my-branch", inputs["name"]) - assert.Equal(t, "yar", inputs["issueId"]) + httpmock.GraphQLQuery(`{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"id": "SOMEID","number":123,"title":"my issue"}}}}`, + func(_ string, inputs map[string]interface{}) { + assert.Equal(t, "OWNER", inputs["owner"]) + assert.Equal(t, "REPO", inputs["repo"]) + assert.Equal(t, float64(123), inputs["number"]) + }), + ) + reg.Register( + httpmock.GraphQL(`query FindRepoBranchID\b`), + httpmock.GraphQLQuery(`{"data":{"repository":{"id":"REPOID","defaultBranchRef":{"target":{"oid":"DEFAULTOID"}},"ref":{"target":{"oid":""}}}}}`, + func(_ string, inputs map[string]interface{}) { + assert.Equal(t, "OWNER2", inputs["owner"]) + assert.Equal(t, "REPO", inputs["name"]) + }), + ) + reg.Register( + httpmock.GraphQL(`mutation CreateLinkedBranch\b`), + httpmock.GraphQLMutation(`{"data":{"createLinkedBranch":{"linkedBranch":{"id":"2","ref":{"name":"my-issue-1"}}}}}`, + func(inputs map[string]interface{}) { + assert.Equal(t, "REPOID", inputs["repositoryId"]) + assert.Equal(t, "SOMEID", inputs["issueId"]) + assert.Equal(t, "DEFAULTOID", inputs["oid"]) }), ) - }, runStubs: func(cs *run.CommandStubber) { + cs.Register(`git fetch origin \+refs/heads/my-issue-1:refs/remotes/origin/my-issue-1`, 0, "") + }, + expectedOut: "github.com/OWNER2/REPO/tree/my-issue-1\n", + }, + { + name: "develop new branch with name and base specified", + opts: &DevelopOptions{ + Name: "my-branch", + BaseBranch: "main", + IssueSelector: "123", + }, + remotes: map[string]string{ + "origin": "OWNER/REPO", + }, + httpStubs: func(reg *httpmock.Registry, t *testing.T) { + reg.Register( + httpmock.GraphQL(`query LinkedBranchFeature\b`), + httpmock.StringResponse(featureEnabledPayload), + ) + reg.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ "hasIssuesEnabled":true,"issue":{"id":"SOMEID","number":123,"title":"my issue"}}}}`), + ) + reg.Register( + httpmock.GraphQL(`query FindRepoBranchID\b`), + httpmock.StringResponse(`{"data":{"repository":{"id":"REPOID","ref":{"target":{"oid":"OID"}}}}}`)) + reg.Register( + httpmock.GraphQL(`mutation CreateLinkedBranch\b`), + httpmock.GraphQLMutation(`{"data":{"createLinkedBranch":{"linkedBranch":{"id":"2","ref":{"name":"my-branch"}}}}}`, + func(inputs map[string]interface{}) { + assert.Equal(t, "REPOID", inputs["repositoryId"]) + assert.Equal(t, "SOMEID", inputs["issueId"]) + assert.Equal(t, "OID", inputs["oid"]) + assert.Equal(t, "my-branch", inputs["name"]) + }), + ) + }, + runStubs: func(cs *run.CommandStubber) { + cs.Register(`git fetch origin \+refs/heads/my-branch:refs/remotes/origin/my-branch`, 0, "") + }, + expectedOut: "github.com/OWNER/REPO/tree/my-branch\n", + }, + { + name: "develop new branch outside of local git repo", + opts: &DevelopOptions{ + IssueSelector: "https://github.com/cli/cli/issues/123", + }, + httpStubs: func(reg *httpmock.Registry, t *testing.T) { + reg.Register( + httpmock.GraphQL(`query LinkedBranchFeature\b`), + httpmock.StringResponse(featureEnabledPayload), + ) + reg.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"id": "SOMEID","number":123,"title":"my issue"}}}}`), + ) + reg.Register( + httpmock.GraphQL(`query FindRepoBranchID\b`), + httpmock.StringResponse(`{"data":{"repository":{"id":"REPOID","defaultBranchRef":{"target":{"oid":"DEFAULTOID"}},"ref":{"target":{"oid":""}}}}}`), + ) + reg.Register( + httpmock.GraphQL(`mutation CreateLinkedBranch\b`), + httpmock.GraphQLMutation(`{"data":{"createLinkedBranch":{"linkedBranch":{"id":"2","ref":{"name":"my-issue-1"}}}}}`, + func(inputs map[string]interface{}) { + assert.Equal(t, "REPOID", inputs["repositoryId"]) + assert.Equal(t, "SOMEID", inputs["issueId"]) + assert.Equal(t, "DEFAULTOID", inputs["oid"]) + }), + ) + }, + expectedOut: "github.com/cli/cli/tree/my-issue-1\n", + }, + { + name: "develop new branch with checkout when local branch exists", + opts: &DevelopOptions{ + Name: "my-branch", + IssueSelector: "123", + Checkout: true, + }, + remotes: map[string]string{ + "origin": "OWNER/REPO", + }, + httpStubs: func(reg *httpmock.Registry, t *testing.T) { + reg.Register( + httpmock.GraphQL(`query LinkedBranchFeature\b`), + httpmock.StringResponse(featureEnabledPayload), + ) + reg.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"id": "SOMEID","number":123,"title":"my issue"}}}}`), + ) + reg.Register( + httpmock.GraphQL(`query FindRepoBranchID\b`), + httpmock.StringResponse(`{"data":{"repository":{"id":"REPOID","ref":{"target":{"oid":"OID"}}}}}`), + ) + reg.Register( + httpmock.GraphQL(`mutation CreateLinkedBranch\b`), + httpmock.GraphQLMutation(`{"data":{"createLinkedBranch":{"linkedBranch":{"id":"2","ref":{"name":"my-branch"}}}}}`, + func(inputs map[string]interface{}) { + assert.Equal(t, "REPOID", inputs["repositoryId"]) + assert.Equal(t, "SOMEID", inputs["issueId"]) + assert.Equal(t, "OID", inputs["oid"]) + assert.Equal(t, "my-branch", inputs["name"]) + }), + ) + }, + runStubs: func(cs *run.CommandStubber) { + cs.Register(`git fetch origin \+refs/heads/my-branch:refs/remotes/origin/my-branch`, 0, "") cs.Register(`git rev-parse --verify refs/heads/my-branch`, 0, "") cs.Register(`git checkout my-branch`, 0, "") cs.Register(`git pull --ff-only origin my-branch`, 0, "") }, expectedOut: "github.com/OWNER/REPO/tree/my-branch\n", }, - {name: "develop new branch with checkout when the branch does not exist locally", - setup: func(opts *DevelopOptions, t *testing.T) func() { - opts.Name = "my-branch" - opts.BaseBranch = "main" - opts.IssueSelector = "123" - opts.Checkout = true - return func() {} + { + name: "develop new branch with checkout when local branch does not exist", + opts: &DevelopOptions{ + Name: "my-branch", + IssueSelector: "123", + Checkout: true, }, remotes: map[string]string{ "origin": "OWNER/REPO", }, httpStubs: func(reg *httpmock.Registry, t *testing.T) { reg.Register( - httpmock.GraphQL(`query LinkedBranch_fields\b`), + httpmock.GraphQL(`query LinkedBranchFeature\b`), httpmock.StringResponse(featureEnabledPayload), ) reg.Register( - httpmock.GraphQL(`query RepositoryInfo\b`), - httpmock.StringResponse(` - { "data": { "repository": { - "id": "REPOID", - "hasIssuesEnabled": true - } } }`), + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"id": "SOMEID","number":123,"title":"my issue"}}}}`), ) reg.Register( - httpmock.GraphQL(`query IssueByNumber\b`), - httpmock.StringResponse(`{"data":{"repository":{ "hasIssuesEnabled": true, "issue":{"id": "yar", "number":123, "title":"my issue"} }}}`)) - reg.Register( - httpmock.GraphQL(`query BranchIssueReferenceFindBaseOid\b`), - httpmock.StringResponse(`{"data":{"repository":{"ref":{"target":{"oid":"123"}}}}}`)) - + httpmock.GraphQL(`query FindRepoBranchID\b`), + httpmock.StringResponse(`{"data":{"repository":{"id":"REPOID","ref":{"target":{"oid":"OID"}}}}}`), + ) reg.Register( httpmock.GraphQL(`mutation CreateLinkedBranch\b`), - httpmock.GraphQLQuery(`{ "data": { "createLinkedBranch": { "linkedBranch": {"id": "2", "ref": {"name": "my-branch"} } } } }`, - func(query string, inputs map[string]interface{}) { + httpmock.GraphQLMutation(`{"data":{"createLinkedBranch":{"linkedBranch":{"id":"2","ref":{"name":"my-branch"}}}}}`, + func(inputs map[string]interface{}) { assert.Equal(t, "REPOID", inputs["repositoryId"]) + assert.Equal(t, "SOMEID", inputs["issueId"]) + assert.Equal(t, "OID", inputs["oid"]) assert.Equal(t, "my-branch", inputs["name"]) - assert.Equal(t, "yar", inputs["issueId"]) }), ) - }, runStubs: func(cs *run.CommandStubber) { - cs.Register(`git rev-parse --verify refs/heads/my-branch`, 1, "") cs.Register(`git fetch origin \+refs/heads/my-branch:refs/remotes/origin/my-branch`, 0, "") + cs.Register(`git rev-parse --verify refs/heads/my-branch`, 1, "") cs.Register(`git checkout -b my-branch --track origin/my-branch`, 0, "") - cs.Register(`git pull --ff-only origin my-branch`, 0, "") }, expectedOut: "github.com/OWNER/REPO/tree/my-branch\n", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + opts := tt.opts + reg := &httpmock.Registry{} defer reg.Verify(t) if tt.httpStubs != nil { tt.httpStubs(reg, t) } - - opts := DevelopOptions{} + opts.HttpClient = func() (*http.Client, error) { + return &http.Client{Transport: reg}, nil + } ios, _, stdout, stderr := iostreams.Test() - ios.SetStdoutTTY(tt.tty) ios.SetStdinTTY(tt.tty) ios.SetStderrTTY(tt.tty) @@ -544,12 +530,6 @@ func Test_developRun(t *testing.T) { opts.BaseRepo = func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil } - opts.HttpClient = func() (*http.Client, error) { - return &http.Client{Transport: reg}, nil - } - opts.Config = func() (config.Config, error) { - return config.NewBlankConfig(), nil - } opts.Remotes = func() (context.Remotes, error) { if len(tt.remotes) == 0 { @@ -580,29 +560,14 @@ func Test_developRun(t *testing.T) { tt.runStubs(cmdStubs) } - cleanSetup := func() {} - if tt.setup != nil { - cleanSetup = tt.setup(&opts, t) - } - defer cleanSetup() - - var err error - if opts.List { - err = developRunList(&opts) - } else { - - err = developRunCreate(&opts) - } - output := &test.CmdOut{ - OutBuf: stdout, - ErrBuf: stderr, - } + err := developRun(opts) if tt.wantErr != "" { assert.EqualError(t, err, tt.wantErr) + return } else { assert.NoError(t, err) - assert.Equal(t, tt.expectedOut, output.String()) - assert.Equal(t, tt.expectedErrOut, output.Stderr()) + assert.Equal(t, tt.expectedOut, stdout.String()) + assert.Equal(t, tt.expectedErrOut, stderr.String()) } }) } diff --git a/pkg/cmd/issue/edit/edit.go b/pkg/cmd/issue/edit/edit.go index abea9eaae..2fe4c62b7 100644 --- a/pkg/cmd/issue/edit/edit.go +++ b/pkg/cmd/issue/edit/edit.go @@ -3,10 +3,13 @@ package edit import ( "fmt" "net/http" + "sort" + "sync" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/text" shared "github.com/cli/cli/v2/pkg/cmd/issue/shared" prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared" "github.com/cli/cli/v2/pkg/cmdutil" @@ -24,8 +27,8 @@ type EditOptions struct { EditFieldsSurvey func(*prShared.Editable, string) error FetchOptions func(*api.Client, ghrepo.Interface, *prShared.Editable) error - SelectorArg string - Interactive bool + SelectorArgs []string + Interactive bool prShared.Editable } @@ -43,12 +46,12 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman var bodyFile string cmd := &cobra.Command{ - Use: "edit { | }", - Short: "Edit an issue", + Use: "edit { | }", + Short: "Edit issues", Long: heredoc.Doc(` - Edit an issue. + Edit one or more issues within the same repository. - Editing an issue's projects requires authorization with the "project" scope. + Editing issues' projects requires authorization with the "project" scope. To authorize, run "gh auth refresh -s project". `), Example: heredoc.Doc(` @@ -58,13 +61,14 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman $ gh issue edit 23 --add-project "Roadmap" --remove-project v1,v2 $ gh issue edit 23 --milestone "Version 1" $ gh issue edit 23 --body-file body.txt + $ gh issue edit 23 34 --add-label "help wanted" `), - Args: cobra.ExactArgs(1), + Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { // support `-R, --repo` override opts.BaseRepo = f.BaseRepo - opts.SelectorArg = args[0] + opts.SelectorArgs = args flags := cmd.Flags() @@ -113,6 +117,10 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman return cmdutil.FlagErrorf("field to edit flag required when not running interactively") } + if opts.Interactive && len(opts.SelectorArgs) > 1 { + return cmdutil.FlagErrorf("multiple issues cannot be edited interactively") + } + if runF != nil { return runF(opts) } @@ -141,41 +149,8 @@ func editRun(opts *EditOptions) error { return err } + // Prompt the user which fields they'd like to edit. editable := opts.Editable - lookupFields := []string{"id", "number", "title", "body", "url"} - if opts.Interactive || editable.Assignees.Edited { - lookupFields = append(lookupFields, "assignees") - } - if opts.Interactive || editable.Labels.Edited { - lookupFields = append(lookupFields, "labels") - } - if opts.Interactive || editable.Projects.Edited { - lookupFields = append(lookupFields, "projectCards") - lookupFields = append(lookupFields, "projectItems") - } - if opts.Interactive || editable.Milestone.Edited { - lookupFields = append(lookupFields, "milestone") - } - - issue, repo, err := shared.IssueFromArgWithFields(httpClient, opts.BaseRepo, opts.SelectorArg, lookupFields) - if err != nil { - return err - } - - editable.Title.Default = issue.Title - editable.Body.Default = issue.Body - editable.Assignees.Default = issue.Assignees.Logins() - editable.Labels.Default = issue.Labels.Names() - editable.Projects.Default = append(issue.ProjectCards.ProjectNames(), issue.ProjectItems.ProjectTitles()...) - projectItems := map[string]string{} - for _, n := range issue.ProjectItems.Nodes { - projectItems[n.Project.ID] = n.ID - } - editable.Projects.ProjectItems = projectItems - if issue.Milestone != nil { - editable.Milestone.Default = issue.Milestone.Title - } - if opts.Interactive { err = opts.FieldsToEditSurvey(&editable) if err != nil { @@ -183,33 +158,122 @@ func editRun(opts *EditOptions) error { } } + lookupFields := []string{"id", "number", "title", "body", "url"} + if editable.Assignees.Edited { + lookupFields = append(lookupFields, "assignees") + } + if editable.Labels.Edited { + lookupFields = append(lookupFields, "labels") + } + if editable.Projects.Edited { + lookupFields = append(lookupFields, "projectCards") + lookupFields = append(lookupFields, "projectItems") + } + if editable.Milestone.Edited { + lookupFields = append(lookupFields, "milestone") + } + + // Get all specified issues and make sure they are within the same repo. + issues, repo, err := shared.IssuesFromArgsWithFields(httpClient, opts.BaseRepo, opts.SelectorArgs, lookupFields) + if err != nil { + return err + } + + // Fetch editable shared fields once for all issues. apiClient := api.NewClientFromHTTP(httpClient) - opts.IO.StartProgressIndicator() + opts.IO.StartProgressIndicatorWithLabel("Fetching repository information") err = opts.FetchOptions(apiClient, repo, &editable) opts.IO.StopProgressIndicator() if err != nil { return err } - if opts.Interactive { - editorCommand, err := opts.DetermineEditor() - if err != nil { - return err - } - err = opts.EditFieldsSurvey(&editable, editorCommand) - if err != nil { - return err - } + // Update all issues in parallel. + editedIssueChan := make(chan string, len(issues)) + failedIssueChan := make(chan string, len(issues)) + g := sync.WaitGroup{} + + // Only show progress if we will not prompt below or the survey will break up the progress indicator. + if !opts.Interactive { + opts.IO.StartProgressIndicatorWithLabel(fmt.Sprintf("Updating %d issues", len(issues))) } - opts.IO.StartProgressIndicator() - err = prShared.UpdateIssue(httpClient, repo, issue.ID, issue.IsPullRequest(), editable) + for _, issue := range issues { + // Copy variables to capture in the go routine below. + editable := editable.Clone() + + editable.Title.Default = issue.Title + editable.Body.Default = issue.Body + editable.Assignees.Default = issue.Assignees.Logins() + editable.Labels.Default = issue.Labels.Names() + editable.Projects.Default = append(issue.ProjectCards.ProjectNames(), issue.ProjectItems.ProjectTitles()...) + projectItems := map[string]string{} + for _, n := range issue.ProjectItems.Nodes { + projectItems[n.Project.ID] = n.ID + } + editable.Projects.ProjectItems = projectItems + if issue.Milestone != nil { + editable.Milestone.Default = issue.Milestone.Title + } + + // Allow interactive prompts for one issue; failed earlier if multiple issues specified. + if opts.Interactive { + editorCommand, err := opts.DetermineEditor() + if err != nil { + return err + } + err = opts.EditFieldsSurvey(&editable, editorCommand) + if err != nil { + return err + } + } + + g.Add(1) + go func(issue *api.Issue) { + defer g.Done() + + err := prShared.UpdateIssue(httpClient, repo, issue.ID, issue.IsPullRequest(), editable) + if err != nil { + failedIssueChan <- fmt.Sprintf("failed to update %s: %s", issue.URL, err) + return + } + + editedIssueChan <- issue.URL + }(issue) + } + + g.Wait() + close(editedIssueChan) + close(failedIssueChan) + + // Does nothing if progress was not started above. opts.IO.StopProgressIndicator() - if err != nil { - return err + + // Print a sorted list of successfully edited issue URLs to stdout. + editedIssueURLs := make([]string, 0, len(issues)) + for editedIssueURL := range editedIssueChan { + editedIssueURLs = append(editedIssueURLs, editedIssueURL) } - fmt.Fprintln(opts.IO.Out, issue.URL) + sort.Strings(editedIssueURLs) + for _, editedIssueURL := range editedIssueURLs { + fmt.Fprintln(opts.IO.Out, editedIssueURL) + } + + // Print a sorted list of failures to stderr. + failedIssueErrors := make([]string, 0, len(issues)) + for failedIssueError := range failedIssueChan { + failedIssueErrors = append(failedIssueErrors, failedIssueError) + } + + sort.Strings(failedIssueErrors) + for _, failedIssueError := range failedIssueErrors { + fmt.Fprintln(opts.IO.ErrOut, failedIssueError) + } + + if len(failedIssueErrors) > 0 { + return fmt.Errorf("failed to update %s", text.Pluralize(len(failedIssueErrors), "issue")) + } return nil } diff --git a/pkg/cmd/issue/edit/edit_test.go b/pkg/cmd/issue/edit/edit_test.go index 34f6b6d89..d73132f35 100644 --- a/pkg/cmd/issue/edit/edit_test.go +++ b/pkg/cmd/issue/edit/edit_test.go @@ -8,6 +8,7 @@ import ( "path/filepath" "testing" + "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/ghrepo" prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared" @@ -41,8 +42,8 @@ func TestNewCmdEdit(t *testing.T) { name: "issue number argument", input: "23", output: EditOptions{ - SelectorArg: "23", - Interactive: true, + SelectorArgs: []string{"23"}, + Interactive: true, }, wantsErr: false, }, @@ -50,7 +51,7 @@ func TestNewCmdEdit(t *testing.T) { name: "title flag", input: "23 --title test", output: EditOptions{ - SelectorArg: "23", + SelectorArgs: []string{"23"}, Editable: prShared.Editable{ Title: prShared.EditableString{ Value: "test", @@ -64,7 +65,7 @@ func TestNewCmdEdit(t *testing.T) { name: "body flag", input: "23 --body test", output: EditOptions{ - SelectorArg: "23", + SelectorArgs: []string{"23"}, Editable: prShared.Editable{ Body: prShared.EditableString{ Value: "test", @@ -79,7 +80,7 @@ func TestNewCmdEdit(t *testing.T) { input: "23 --body-file -", stdin: "this is on standard input", output: EditOptions{ - SelectorArg: "23", + SelectorArgs: []string{"23"}, Editable: prShared.Editable{ Body: prShared.EditableString{ Value: "this is on standard input", @@ -93,7 +94,7 @@ func TestNewCmdEdit(t *testing.T) { name: "body from file", input: fmt.Sprintf("23 --body-file '%s'", tmpFile), output: EditOptions{ - SelectorArg: "23", + SelectorArgs: []string{"23"}, Editable: prShared.Editable{ Body: prShared.EditableString{ Value: "a body from file", @@ -107,7 +108,7 @@ func TestNewCmdEdit(t *testing.T) { name: "add-assignee flag", input: "23 --add-assignee monalisa,hubot", output: EditOptions{ - SelectorArg: "23", + SelectorArgs: []string{"23"}, Editable: prShared.Editable{ Assignees: prShared.EditableSlice{ Add: []string{"monalisa", "hubot"}, @@ -121,7 +122,7 @@ func TestNewCmdEdit(t *testing.T) { name: "remove-assignee flag", input: "23 --remove-assignee monalisa,hubot", output: EditOptions{ - SelectorArg: "23", + SelectorArgs: []string{"23"}, Editable: prShared.Editable{ Assignees: prShared.EditableSlice{ Remove: []string{"monalisa", "hubot"}, @@ -135,7 +136,7 @@ func TestNewCmdEdit(t *testing.T) { name: "add-label flag", input: "23 --add-label feature,TODO,bug", output: EditOptions{ - SelectorArg: "23", + SelectorArgs: []string{"23"}, Editable: prShared.Editable{ Labels: prShared.EditableSlice{ Add: []string{"feature", "TODO", "bug"}, @@ -149,7 +150,7 @@ func TestNewCmdEdit(t *testing.T) { name: "remove-label flag", input: "23 --remove-label feature,TODO,bug", output: EditOptions{ - SelectorArg: "23", + SelectorArgs: []string{"23"}, Editable: prShared.Editable{ Labels: prShared.EditableSlice{ Remove: []string{"feature", "TODO", "bug"}, @@ -163,7 +164,7 @@ func TestNewCmdEdit(t *testing.T) { name: "add-project flag", input: "23 --add-project Cleanup,Roadmap", output: EditOptions{ - SelectorArg: "23", + SelectorArgs: []string{"23"}, Editable: prShared.Editable{ Projects: prShared.EditableProjects{ EditableSlice: prShared.EditableSlice{ @@ -179,7 +180,7 @@ func TestNewCmdEdit(t *testing.T) { name: "remove-project flag", input: "23 --remove-project Cleanup,Roadmap", output: EditOptions{ - SelectorArg: "23", + SelectorArgs: []string{"23"}, Editable: prShared.Editable{ Projects: prShared.EditableProjects{ EditableSlice: prShared.EditableSlice{ @@ -195,7 +196,7 @@ func TestNewCmdEdit(t *testing.T) { name: "milestone flag", input: "23 --milestone GA", output: EditOptions{ - SelectorArg: "23", + SelectorArgs: []string{"23"}, Editable: prShared.Editable{ Milestone: prShared.EditableString{ Value: "GA", @@ -205,6 +206,34 @@ func TestNewCmdEdit(t *testing.T) { }, wantsErr: false, }, + { + name: "add label to multiple issues", + input: "23 34 --add-label bug", + output: EditOptions{ + SelectorArgs: []string{"23", "34"}, + Editable: prShared.Editable{ + Labels: prShared.EditableSlice{ + Add: []string{"bug"}, + Edited: true, + }, + }, + }, + wantsErr: false, + }, + { + name: "interactive multiple issues", + input: "23 34", + output: EditOptions{ + SelectorArgs: []string{"23", "34"}, + Editable: prShared.Editable{ + Labels: prShared.EditableSlice{ + Add: []string{"bug"}, + Edited: true, + }, + }, + }, + wantsErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -243,7 +272,7 @@ func TestNewCmdEdit(t *testing.T) { } assert.NoError(t, err) - assert.Equal(t, tt.output.SelectorArg, gotOpts.SelectorArg) + assert.Equal(t, tt.output.SelectorArgs, gotOpts.SelectorArgs) assert.Equal(t, tt.output.Interactive, gotOpts.Interactive) assert.Equal(t, tt.output.Editable, gotOpts.Editable) }) @@ -257,12 +286,13 @@ func Test_editRun(t *testing.T) { httpStubs func(*testing.T, *httpmock.Registry) stdout string stderr string + wantErr bool }{ { name: "non-interactive", input: &EditOptions{ - SelectorArg: "123", - Interactive: false, + SelectorArgs: []string{"123"}, + Interactive: false, Editable: prShared.Editable{ Title: prShared.EditableString{ Value: "new title", @@ -311,11 +341,176 @@ func Test_editRun(t *testing.T) { }, stdout: "https://github.com/OWNER/REPO/issue/123\n", }, + { + name: "non-interactive multiple issues", + input: &EditOptions{ + SelectorArgs: []string{"456", "123"}, + Interactive: false, + Editable: prShared.Editable{ + Assignees: prShared.EditableSlice{ + Add: []string{"monalisa", "hubot"}, + Remove: []string{"octocat"}, + Edited: true, + }, + Labels: prShared.EditableSlice{ + Add: []string{"feature", "TODO", "bug"}, + Remove: []string{"docs"}, + Edited: true, + }, + Projects: prShared.EditableProjects{ + EditableSlice: prShared.EditableSlice{ + Add: []string{"Cleanup", "CleanupV2"}, + Remove: []string{"Roadmap", "RoadmapV2"}, + Edited: true, + }, + }, + Milestone: prShared.EditableString{ + Value: "GA", + Edited: true, + }, + }, + FetchOptions: prShared.FetchOptions, + }, + httpStubs: func(t *testing.T, reg *httpmock.Registry) { + // Should only be one fetch of metadata. + mockRepoMetadata(t, reg) + // All other queries and mutations should be doubled. + mockIssueNumberGet(t, reg, 123) + mockIssueNumberGet(t, reg, 456) + mockIssueProjectItemsGet(t, reg) + mockIssueProjectItemsGet(t, reg) + mockIssueUpdate(t, reg) + mockIssueUpdate(t, reg) + mockIssueUpdateLabels(t, reg) + mockIssueUpdateLabels(t, reg) + mockProjectV2ItemUpdate(t, reg) + mockProjectV2ItemUpdate(t, reg) + }, + stdout: heredoc.Doc(` + https://github.com/OWNER/REPO/issue/123 + https://github.com/OWNER/REPO/issue/456 + `), + }, + { + name: "non-interactive multiple issues with fetch failures", + input: &EditOptions{ + SelectorArgs: []string{"123", "9999"}, + Interactive: false, + Editable: prShared.Editable{ + Assignees: prShared.EditableSlice{ + Add: []string{"monalisa", "hubot"}, + Remove: []string{"octocat"}, + Edited: true, + }, + Labels: prShared.EditableSlice{ + Add: []string{"feature", "TODO", "bug"}, + Remove: []string{"docs"}, + Edited: true, + }, + Projects: prShared.EditableProjects{ + EditableSlice: prShared.EditableSlice{ + Add: []string{"Cleanup", "CleanupV2"}, + Remove: []string{"Roadmap", "RoadmapV2"}, + Edited: true, + }, + }, + Milestone: prShared.EditableString{ + Value: "GA", + Edited: true, + }, + }, + FetchOptions: prShared.FetchOptions, + }, + httpStubs: func(t *testing.T, reg *httpmock.Registry) { + mockIssueNumberGet(t, reg, 123) + reg.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(` + { "errors": [ + { + "type": "NOT_FOUND", + "message": "Could not resolve to an Issue with the number of 9999." + } + ] }`), + ) + }, + wantErr: true, + }, + { + name: "non-interactive multiple issues with update failures", + input: &EditOptions{ + SelectorArgs: []string{"123", "456"}, + Interactive: false, + Editable: prShared.Editable{ + Assignees: prShared.EditableSlice{ + Add: []string{"monalisa", "hubot"}, + Remove: []string{"octocat"}, + Edited: true, + }, + Milestone: prShared.EditableString{ + Value: "GA", + Edited: true, + }, + }, + FetchOptions: prShared.FetchOptions, + }, + httpStubs: func(t *testing.T, reg *httpmock.Registry) { + // Should only be one fetch of metadata. + reg.Register( + httpmock.GraphQL(`query RepositoryAssignableUsers\b`), + httpmock.StringResponse(` + { "data": { "repository": { "assignableUsers": { + "nodes": [ + { "login": "hubot", "id": "HUBOTID" }, + { "login": "MonaLisa", "id": "MONAID" } + ], + "pageInfo": { "hasNextPage": false } + } } } } + `)) + reg.Register( + httpmock.GraphQL(`query RepositoryMilestoneList\b`), + httpmock.StringResponse(` + { "data": { "repository": { "milestones": { + "nodes": [ + { "title": "GA", "id": "GAID" }, + { "title": "Big One.oh", "id": "BIGONEID" } + ], + "pageInfo": { "hasNextPage": false } + } } } } + `)) + // All other queries should be doubled. + mockIssueNumberGet(t, reg, 123) + mockIssueNumberGet(t, reg, 456) + // Updating 123 should succeed. + reg.Register( + httpmock.GraphQLMutationMatcher(`mutation IssueUpdate\b`, func(m map[string]interface{}) bool { + return m["id"] == "123" + }), + httpmock.GraphQLMutation(` + { "data": { "updateIssue": { "__typename": "" } } }`, + func(inputs map[string]interface{}) {}), + ) + // Updating 456 should fail. + reg.Register( + httpmock.GraphQLMutationMatcher(`mutation IssueUpdate\b`, func(m map[string]interface{}) bool { + return m["id"] == "456" + }), + httpmock.GraphQLMutation(` + { "errors": [ { "message": "test error" } ] }`, + func(inputs map[string]interface{}) {}), + ) + }, + stdout: heredoc.Doc(` + https://github.com/OWNER/REPO/issue/123 + `), + stderr: `failed to update https://github.com/OWNER/REPO/issue/456:.*test error`, + wantErr: true, + }, { name: "interactive", input: &EditOptions{ - SelectorArg: "123", - Interactive: true, + SelectorArgs: []string{"123"}, + Interactive: true, FieldsToEditSurvey: func(eo *prShared.Editable) error { eo.Title.Edited = true eo.Body.Edited = true @@ -351,38 +546,48 @@ func Test_editRun(t *testing.T) { }, } for _, tt := range tests { - ios, _, stdout, stderr := iostreams.Test() - ios.SetStdoutTTY(true) - ios.SetStdinTTY(true) - ios.SetStderrTTY(true) - - reg := &httpmock.Registry{} - defer reg.Verify(t) - tt.httpStubs(t, reg) - - httpClient := func() (*http.Client, error) { return &http.Client{Transport: reg}, nil } - baseRepo := func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil } - - tt.input.IO = ios - tt.input.HttpClient = httpClient - tt.input.BaseRepo = baseRepo - t.Run(tt.name, func(t *testing.T) { + ios, _, stdout, stderr := iostreams.Test() + ios.SetStdoutTTY(true) + ios.SetStdinTTY(true) + ios.SetStderrTTY(true) + + reg := &httpmock.Registry{} + defer reg.Verify(t) + tt.httpStubs(t, reg) + + httpClient := func() (*http.Client, error) { return &http.Client{Transport: reg}, nil } + baseRepo := func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil } + + tt.input.IO = ios + tt.input.HttpClient = httpClient + tt.input.BaseRepo = baseRepo + err := editRun(tt.input) - assert.NoError(t, err) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } assert.Equal(t, tt.stdout, stdout.String()) - assert.Equal(t, tt.stderr, stderr.String()) + // Use regex match since mock errors and service errors will differ. + assert.Regexp(t, tt.stderr, stderr.String()) }) } } func mockIssueGet(_ *testing.T, reg *httpmock.Registry) { + mockIssueNumberGet(nil, reg, 123) +} + +func mockIssueNumberGet(_ *testing.T, reg *httpmock.Registry, number int) { reg.Register( httpmock.GraphQL(`query IssueByNumber\b`), - httpmock.StringResponse(` + httpmock.StringResponse(fmt.Sprintf(` { "data": { "repository": { "hasIssuesEnabled": true, "issue": { - "number": 123, - "url": "https://github.com/OWNER/REPO/issue/123", + "id": "%[1]d", + "number": %[1]d, + "url": "https://github.com/OWNER/REPO/issue/%[1]d", "labels": { "nodes": [ { "id": "DOCSID", "name": "docs" } @@ -393,7 +598,7 @@ func mockIssueGet(_ *testing.T, reg *httpmock.Registry) { { "project": { "name": "Roadmap" } } ], "totalCount": 1 } - } } } }`), + } } } }`, number)), ) } diff --git a/pkg/cmd/issue/list/list.go b/pkg/cmd/issue/list/list.go index 71ed5ed0f..20e77e141 100644 --- a/pkg/cmd/issue/list/list.go +++ b/pkg/cmd/issue/list/list.go @@ -15,7 +15,6 @@ import ( "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/internal/text" issueShared "github.com/cli/cli/v2/pkg/cmd/issue/shared" - "github.com/cli/cli/v2/pkg/cmd/pr/shared" prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" @@ -133,7 +132,7 @@ func listRun(opts *ListOptions) error { } issueState := strings.ToLower(opts.State) - if issueState == "open" && shared.QueryHasStateClause(opts.Search) { + if issueState == "open" && prShared.QueryHasStateClause(opts.Search) { issueState = "" } @@ -228,7 +227,7 @@ func issueList(client *http.Client, repo ghrepo.Interface, filters prShared.Filt } var err error - meReplacer := shared.NewMeReplacer(apiClient, repo.RepoHost()) + meReplacer := prShared.NewMeReplacer(apiClient, repo.RepoHost()) filters.Assignee, err = meReplacer.Replace(filters.Assignee) if err != nil { return nil, err diff --git a/pkg/cmd/issue/shared/lookup.go b/pkg/cmd/issue/shared/lookup.go index ef0652b26..1aa58c951 100644 --- a/pkg/cmd/issue/shared/lookup.go +++ b/pkg/cmd/issue/shared/lookup.go @@ -14,25 +14,20 @@ import ( fd "github.com/cli/cli/v2/internal/featuredetection" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/set" + "golang.org/x/sync/errgroup" ) // IssueFromArgWithFields loads an issue or pull request with the specified fields. If some of the fields // could not be fetched by GraphQL, this returns a non-nil issue and a *PartialLoadError. func IssueFromArgWithFields(httpClient *http.Client, baseRepoFn func() (ghrepo.Interface, error), arg string, fields []string) (*api.Issue, ghrepo.Interface, error) { - issueNumber, baseRepo := issueMetadataFromURL(arg) - - if issueNumber == 0 { - var err error - issueNumber, err = strconv.Atoi(strings.TrimPrefix(arg, "#")) - if err != nil { - return nil, nil, fmt.Errorf("invalid issue format: %q", arg) - } + issueNumber, baseRepo, err := IssueNumberAndRepoFromArg(arg) + if err != nil { + return nil, nil, err } if baseRepo == nil { var err error - baseRepo, err = baseRepoFn() - if err != nil { + if baseRepo, err = baseRepoFn(); err != nil { return nil, nil, err } } @@ -41,6 +36,70 @@ func IssueFromArgWithFields(httpClient *http.Client, baseRepoFn func() (ghrepo.I return issue, baseRepo, err } +// IssuesFromArgWithFields loads 1 or more issues or pull requests with the specified fields. If some of the fields +// could not be fetched by GraphQL, this returns non-nil issues and a *PartialLoadError. +func IssuesFromArgsWithFields(httpClient *http.Client, baseRepoFn func() (ghrepo.Interface, error), args []string, fields []string) ([]*api.Issue, ghrepo.Interface, error) { + var issuesRepo ghrepo.Interface + issueNumbers := make([]int, 0, len(args)) + + for _, arg := range args { + issueNumber, baseRepo, err := IssueNumberAndRepoFromArg(arg) + if err != nil { + return nil, nil, err + } + + issueNumbers = append(issueNumbers, issueNumber) + if baseRepo == nil { + var err error + if baseRepo, err = baseRepoFn(); err != nil { + return nil, nil, err + } + } + + if issuesRepo == nil { + issuesRepo = baseRepo + continue + } + + if !ghrepo.IsSame(issuesRepo, baseRepo) { + return nil, nil, fmt.Errorf( + "multiple issues must be in same repo: found %q, expected %q", + ghrepo.FullName(baseRepo), + ghrepo.FullName(issuesRepo), + ) + } + } + + issuesChan := make(chan *api.Issue, len(args)) + g := errgroup.Group{} + for _, num := range issueNumbers { + issueNumber := num + g.Go(func() error { + issue, err := findIssueOrPR(httpClient, issuesRepo, issueNumber, fields) + if err != nil { + return err + } + + issuesChan <- issue + return nil + }) + } + + err := g.Wait() + close(issuesChan) + + if err != nil { + return nil, nil, err + } + + issues := make([]*api.Issue, 0, len(args)) + for issue := range issuesChan { + issues = append(issues, issue) + } + + return issues, issuesRepo, nil +} + var issueURLRE = regexp.MustCompile(`^/([^/]+)/([^/]+)/issues/(\d+)`) func issueMetadataFromURL(s string) (int, ghrepo.Interface) { diff --git a/pkg/cmd/issue/shared/lookup_test.go b/pkg/cmd/issue/shared/lookup_test.go index 2ce96d11b..694a3fd05 100644 --- a/pkg/cmd/issue/shared/lookup_test.go +++ b/pkg/cmd/issue/shared/lookup_test.go @@ -7,6 +7,7 @@ import ( "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/httpmock" + "github.com/stretchr/testify/assert" ) func TestIssueFromArgWithFields(t *testing.T) { @@ -195,3 +196,106 @@ func TestIssueFromArgWithFields(t *testing.T) { }) } } + +func TestIssuesFromArgsWithFields(t *testing.T) { + type args struct { + baseRepoFn func() (ghrepo.Interface, error) + selectors []string + } + tests := []struct { + name string + args args + httpStub func(*httpmock.Registry) + wantIssues []int + wantRepo string + wantErr bool + wantErrMsg string + }{ + { + name: "multiple repos", + args: args{ + selectors: []string{"1", "https://github.com/OWNER/OTHERREPO/issues/2"}, + baseRepoFn: func() (ghrepo.Interface, error) { + return ghrepo.New("OWNER", "REPO"), nil + }, + }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "hasIssuesEnabled": true, + "issue":{"number":1} + }}}`)) + r.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "hasIssuesEnabled": true, + "issue":{"number":2} + }}}`)) + }, + wantErr: true, + wantErrMsg: "multiple issues must be in same repo", + }, + { + name: "multiple issues", + args: args{ + selectors: []string{"1", "2"}, + baseRepoFn: func() (ghrepo.Interface, error) { + return ghrepo.New("OWNER", "REPO"), nil + }, + }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "hasIssuesEnabled": true, + "issue":{"number":1} + }}}`)) + r.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ + "hasIssuesEnabled": true, + "issue":{"number":2} + }}}`)) + }, + wantIssues: []int{1, 2}, + wantRepo: "https://github.com/OWNER/REPO", + }, + } + for _, tt := range tests { + if !tt.wantErr && len(tt.args.selectors) != len(tt.wantIssues) { + t.Fatal("number of selectors and issues not equal") + } + t.Run(tt.name, func(t *testing.T) { + reg := &httpmock.Registry{} + if tt.httpStub != nil { + tt.httpStub(reg) + } + httpClient := &http.Client{Transport: reg} + issues, repo, err := IssuesFromArgsWithFields(httpClient, tt.args.baseRepoFn, tt.args.selectors, []string{"number"}) + if (err != nil) != tt.wantErr { + t.Errorf("IssuesFromArgsWithFields() error = %v, wantErr %v", err, tt.wantErr) + if issues == nil { + return + } + } + if tt.wantErr { + assert.Error(t, err) + assert.ErrorContains(t, err, tt.wantErrMsg) + return + } + assert.NoError(t, err) + for i := range issues { + assert.Contains(t, tt.wantIssues, issues[i].Number) + } + if repo != nil { + repoURL := ghrepo.GenerateRepoURL(repo, "") + if repoURL != tt.wantRepo { + t.Errorf("want repo %s, got %s", tt.wantRepo, repoURL) + } + } else if tt.wantRepo != "" { + t.Errorf("want repo %sw, got nil", tt.wantRepo) + } + }) + } +} diff --git a/pkg/cmd/issue/view/fixtures/issueView_previewWithMetadata.json b/pkg/cmd/issue/view/fixtures/issueView_previewWithMetadata.json index 9bce5f745..186fa1bab 100644 --- a/pkg/cmd/issue/view/fixtures/issueView_previewWithMetadata.json +++ b/pkg/cmd/issue/view/fixtures/issueView_previewWithMetadata.json @@ -25,19 +25,19 @@ "labels": { "nodes": [ { - "name": "one" + "name": "Closed: Won't Fix" }, { - "name": "two" + "name": "Status: In Progress" }, { - "name": "three" + "name": "Type: Bug" }, { - "name": "four" + "name": "help wanted" }, { - "name": "five" + "name": "Closed: Duplicate" } ], "totalCount": 5 diff --git a/pkg/cmd/issue/view/view.go b/pkg/cmd/issue/view/view.go index b721451fa..83132291f 100644 --- a/pkg/cmd/issue/view/view.go +++ b/pkg/cmd/issue/view/view.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "sort" "strings" "time" @@ -306,6 +307,11 @@ func issueLabelList(issue *api.Issue, cs *iostreams.ColorScheme) string { return "" } + // ignore case sort + sort.SliceStable(issue.Labels.Nodes, func(i, j int) bool { + return strings.ToLower(issue.Labels.Nodes[i].Name) < strings.ToLower(issue.Labels.Nodes[j].Name) + }) + labelNames := make([]string, len(issue.Labels.Nodes)) for i, label := range issue.Labels.Nodes { if cs == nil { diff --git a/pkg/cmd/issue/view/view_test.go b/pkg/cmd/issue/view/view_test.go index ff26f4bc3..2c4e7934e 100644 --- a/pkg/cmd/issue/view/view_test.go +++ b/pkg/cmd/issue/view/view_test.go @@ -125,7 +125,7 @@ func TestIssueView_nontty_Preview(t *testing.T) { `author:\tmarseilles`, `state:\tOPEN`, `comments:\t9`, - `labels:\tone, two, three, four, five`, + `labels:\tClosed: Duplicate, Closed: Won't Fix, help wanted, Status: In Progress, Type: Bug`, `projects:\tProject 1 \(column A\), Project 2 \(column B\), Project 3 \(column C\), Project 4 \(Awaiting triage\)\n`, `milestone:\tuluru\n`, `number:\t123\n`, @@ -196,7 +196,7 @@ func TestIssueView_tty_Preview(t *testing.T) { `Open.*marseilles opened about 9 years ago.*9 comments`, `8 \x{1f615} • 7 \x{1f440} • 6 \x{2764}\x{fe0f} • 5 \x{1f389} • 4 \x{1f604} • 3 \x{1f680} • 2 \x{1f44e} • 1 \x{1f44d}`, `Assignees:.*marseilles, monaco\n`, - `Labels:.*one, two, three, four, five\n`, + `Labels:.*Closed: Duplicate, Closed: Won't Fix, help wanted, Status: In Progress, Type: Bug\n`, `Projects:.*Project 1 \(column A\), Project 2 \(column B\), Project 3 \(column C\), Project 4 \(Awaiting triage\)\n`, `Milestone:.*uluru\n`, `bold story`, diff --git a/pkg/cmd/pr/checks/aggregate.go b/pkg/cmd/pr/checks/aggregate.go index 53f2bab5f..f9f0b73cb 100644 --- a/pkg/cmd/pr/checks/aggregate.go +++ b/pkg/cmd/pr/checks/aggregate.go @@ -15,6 +15,9 @@ type check struct { CompletedAt time.Time `json:"completedAt"` Link string `json:"link"` Bucket string `json:"bucket"` + Event string `json:"event"` + Workflow string `json:"workflow"` + Description string `json:"description"` } type checkCounts struct { @@ -30,10 +33,10 @@ func aggregateChecks(checkContexts []api.CheckContext, requiredChecks bool) (che continue } - state := c.State + state := string(c.State) if state == "" { if c.Status == "COMPLETED" { - state = c.Conclusion + state = string(c.Conclusion) } else { state = c.Status } @@ -55,7 +58,11 @@ func aggregateChecks(checkContexts []api.CheckContext, requiredChecks bool) (che StartedAt: c.StartedAt, CompletedAt: c.CompletedAt, Link: link, + Event: c.CheckSuite.WorkflowRun.Event, + Workflow: c.CheckSuite.WorkflowRun.Workflow.Name, + Description: c.Description, } + switch state { case "SUCCESS": item.Bucket = "pass" @@ -91,7 +98,7 @@ func eliminateDuplicates(checkContexts []api.CheckContext) []api.CheckContext { } mapContexts[ctx.Context] = struct{}{} } else { - key := fmt.Sprintf("%s/%s", ctx.Name, ctx.CheckSuite.WorkflowRun.Workflow.Name) + key := fmt.Sprintf("%s/%s/%s", ctx.Name, ctx.CheckSuite.WorkflowRun.Workflow.Name, ctx.CheckSuite.WorkflowRun.Event) if _, exists := mapChecks[key]; exists { continue } diff --git a/pkg/cmd/pr/checks/checks_test.go b/pkg/cmd/pr/checks/checks_test.go index 186c1d324..a043176aa 100644 --- a/pkg/cmd/pr/checks/checks_test.go +++ b/pkg/cmd/pr/checks/checks_test.go @@ -133,7 +133,7 @@ func Test_checksRun(t *testing.T) { wantErr string }{ { - name: "no commits", + name: "no commits tty", tty: true, httpStubs: func(reg *httpmock.Registry) { reg.Register( @@ -145,7 +145,7 @@ func Test_checksRun(t *testing.T) { wantErr: "no commit found on the pull request", }, { - name: "no checks", + name: "no checks tty", tty: true, httpStubs: func(reg *httpmock.Registry) { reg.Register( @@ -157,7 +157,7 @@ func Test_checksRun(t *testing.T) { wantErr: "no checks reported on the 'trunk' branch", }, { - name: "some failing", + name: "some failing tty", tty: true, httpStubs: func(reg *httpmock.Registry) { reg.Register( @@ -165,11 +165,11 @@ func Test_checksRun(t *testing.T) { httpmock.FileResponse("./fixtures/someFailing.json"), ) }, - wantOut: "Some checks were not successful\n1 failing, 1 successful, 0 skipped, and 1 pending checks\n\nX sad tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n* slow tests 1m26s sweet link\n", + wantOut: "Some checks were not successful\n1 failing, 1 successful, 0 skipped, and 1 pending checks\n\nX sad tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n* slow tests 1m26s sweet link\n", wantErr: "SilentError", }, { - name: "some pending", + name: "some pending tty", tty: true, httpStubs: func(reg *httpmock.Registry) { reg.Register( @@ -177,11 +177,11 @@ func Test_checksRun(t *testing.T) { httpmock.FileResponse("./fixtures/somePending.json"), ) }, - wantOut: "Some checks are still pending\n0 failing, 2 successful, 0 skipped, and 1 pending checks\n\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n* slow tests 1m26s sweet link\n", + wantOut: "Some checks are still pending\n0 failing, 2 successful, 0 skipped, and 1 pending checks\n\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n* slow tests 1m26s sweet link\n", wantErr: "SilentError", }, { - name: "all passing", + name: "all passing tty", tty: true, httpStubs: func(reg *httpmock.Registry) { reg.Register( @@ -189,11 +189,11 @@ func Test_checksRun(t *testing.T) { httpmock.FileResponse("./fixtures/allPassing.json"), ) }, - wantOut: "All checks were successful\n0 failing, 3 successful, 0 skipped, and 0 pending checks\n\n✓ awesome tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n", + wantOut: "All checks were successful\n0 failing, 3 successful, 0 skipped, and 0 pending checks\n\n✓ awesome tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n", wantErr: "", }, { - name: "watch all passing", + name: "watch all passing tty", tty: true, watch: true, httpStubs: func(reg *httpmock.Registry) { @@ -202,11 +202,11 @@ func Test_checksRun(t *testing.T) { httpmock.FileResponse("./fixtures/allPassing.json"), ) }, - wantOut: "\x1b[?1049hAll checks were successful\n0 failing, 3 successful, 0 skipped, and 0 pending checks\n\n✓ awesome tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n\x1b[?1049lAll checks were successful\n0 failing, 3 successful, 0 skipped, and 0 pending checks\n\n✓ awesome tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n", + wantOut: "\x1b[?1049hAll checks were successful\n0 failing, 3 successful, 0 skipped, and 0 pending checks\n\n✓ awesome tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n\x1b[?1049lAll checks were successful\n0 failing, 3 successful, 0 skipped, and 0 pending checks\n\n✓ awesome tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n", wantErr: "", }, { - name: "watch some failing with fail fast", + name: "watch some failing with fail fast tty", tty: true, watch: true, failFast: true, @@ -216,11 +216,11 @@ func Test_checksRun(t *testing.T) { httpmock.FileResponse("./fixtures/someFailing.json"), ) }, - wantOut: "\x1b[?1049h\x1b[0;0H\x1b[JRefreshing checks status every 0 seconds. Press Ctrl+C to quit.\n\nSome checks were not successful\n1 failing, 1 successful, 0 skipped, and 1 pending checks\n\nX sad tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n* slow tests 1m26s sweet link\n\x1b[?1049lSome checks were not successful\n1 failing, 1 successful, 0 skipped, and 1 pending checks\n\nX sad tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n* slow tests 1m26s sweet link\n", + wantOut: "\x1b[?1049h\x1b[0;0H\x1b[JRefreshing checks status every 0 seconds. Press Ctrl+C to quit.\n\nSome checks were not successful\n1 failing, 1 successful, 0 skipped, and 1 pending checks\n\nX sad tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n* slow tests 1m26s sweet link\n\x1b[?1049lSome checks were not successful\n1 failing, 1 successful, 0 skipped, and 1 pending checks\n\nX sad tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n* slow tests 1m26s sweet link\n", wantErr: "SilentError", }, { - name: "with statuses", + name: "with statuses tty", tty: true, httpStubs: func(reg *httpmock.Registry) { reg.Register( @@ -228,9 +228,20 @@ func Test_checksRun(t *testing.T) { httpmock.FileResponse("./fixtures/withStatuses.json"), ) }, - wantOut: "Some checks were not successful\n1 failing, 2 successful, 0 skipped, and 0 pending checks\n\nX a status sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n", + wantOut: "Some checks were not successful\n1 failing, 2 successful, 0 skipped, and 0 pending checks\n\nX a status sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n", wantErr: "SilentError", }, + { + name: "no commits", + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.GraphQL(`query PullRequestStatusChecks\b`), + httpmock.StringResponse(`{"data":{"node":{}}}`), + ) + }, + wantOut: "", + wantErr: "no commit found on the pull request", + }, { name: "no checks", httpStubs: func(reg *httpmock.Registry) { @@ -250,7 +261,7 @@ func Test_checksRun(t *testing.T) { httpmock.FileResponse("./fixtures/someFailing.json"), ) }, - wantOut: "sad tests\tfail\t1m26s\tsweet link\ncool tests\tpass\t1m26s\tsweet link\nslow tests\tpending\t1m26s\tsweet link\n", + wantOut: "sad tests\tfail\t1m26s\tsweet link\t\ncool tests\tpass\t1m26s\tsweet link\t\nslow tests\tpending\t1m26s\tsweet link\t\n", wantErr: "SilentError", }, { @@ -261,7 +272,7 @@ func Test_checksRun(t *testing.T) { httpmock.FileResponse("./fixtures/somePending.json"), ) }, - wantOut: "cool tests\tpass\t1m26s\tsweet link\nrad tests\tpass\t1m26s\tsweet link\nslow tests\tpending\t1m26s\tsweet link\n", + wantOut: "cool tests\tpass\t1m26s\tsweet link\t\nrad tests\tpass\t1m26s\tsweet link\t\nslow tests\tpending\t1m26s\tsweet link\t\n", wantErr: "SilentError", }, { @@ -272,7 +283,7 @@ func Test_checksRun(t *testing.T) { httpmock.FileResponse("./fixtures/allPassing.json"), ) }, - wantOut: "awesome tests\tpass\t1m26s\tsweet link\ncool tests\tpass\t1m26s\tsweet link\nrad tests\tpass\t1m26s\tsweet link\n", + wantOut: "awesome tests\tpass\t1m26s\tsweet link\t\ncool tests\tpass\t1m26s\tsweet link\t\nrad tests\tpass\t1m26s\tsweet link\t\n", wantErr: "", }, { @@ -283,9 +294,21 @@ func Test_checksRun(t *testing.T) { httpmock.FileResponse("./fixtures/withStatuses.json"), ) }, - wantOut: "a status\tfail\t0\tsweet link\ncool tests\tpass\t1m26s\tsweet link\nrad tests\tpass\t1m26s\tsweet link\n", + wantOut: "a status\tfail\t0\tsweet link\t\ncool tests\tpass\t1m26s\tsweet link\t\nrad tests\tpass\t1m26s\tsweet link\t\n", wantErr: "SilentError", }, + { + name: "some skipped tty", + tty: true, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.GraphQL(`query PullRequestStatusChecks\b`), + httpmock.FileResponse("./fixtures/someSkipping.json"), + ) + }, + wantOut: "All checks were successful\n0 failing, 1 successful, 2 skipped, and 0 pending checks\n\n✓ cool tests 1m26s sweet link\n- rad tests 1m26s sweet link\n- skip tests 1m26s sweet link\n", + wantErr: "", + }, { name: "some skipped", httpStubs: func(reg *httpmock.Registry) { @@ -294,34 +317,81 @@ func Test_checksRun(t *testing.T) { httpmock.FileResponse("./fixtures/someSkipping.json"), ) }, - tty: true, - wantOut: "All checks were successful\n0 failing, 1 successful, 2 skipped, and 0 pending checks\n\n✓ cool tests 1m26s sweet link\n- rad tests 1m26s sweet link\n- skip tests 1m26s sweet link\n", + wantOut: "cool tests\tpass\t1m26s\tsweet link\t\nrad tests\tskipping\t1m26s\tsweet link\t\nskip tests\tskipping\t1m26s\tsweet link\t\n", wantErr: "", }, { - name: "only required", + name: "only required tty", + tty: true, + required: true, httpStubs: func(reg *httpmock.Registry) { reg.Register( httpmock.GraphQL(`query PullRequestStatusChecks\b`), httpmock.FileResponse("./fixtures/onlyRequired.json"), ) }, - tty: true, - wantOut: "All checks were successful\n0 failing, 1 successful, 0 skipped, and 0 pending checks\n\n✓ cool tests 1m26s sweet link\n", - wantErr: "", - required: true, + wantOut: "All checks were successful\n0 failing, 1 successful, 0 skipped, and 0 pending checks\n\n✓ cool tests 1m26s sweet link\n", + wantErr: "", }, { - name: "no required checks", + name: "only required", + required: true, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.GraphQL(`query PullRequestStatusChecks\b`), + httpmock.FileResponse("./fixtures/onlyRequired.json"), + ) + }, + wantOut: "cool tests\tpass\t1m26s\tsweet link\t\n", + wantErr: "", + }, + { + name: "only required but no required checks tty", + tty: true, + required: true, httpStubs: func(reg *httpmock.Registry) { reg.Register( httpmock.GraphQL(`query PullRequestStatusChecks\b`), httpmock.FileResponse("./fixtures/someSkipping.json"), ) }, - wantOut: "", - wantErr: "no required checks reported on the 'trunk' branch", + wantOut: "", + wantErr: "no required checks reported on the 'trunk' branch", + }, + { + name: "only required but no required checks", required: true, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.GraphQL(`query PullRequestStatusChecks\b`), + httpmock.FileResponse("./fixtures/someSkipping.json"), + ) + }, + wantOut: "", + wantErr: "no required checks reported on the 'trunk' branch", + }, + { + name: "descriptions tty", + tty: true, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.GraphQL(`query PullRequestStatusChecks\b`), + httpmock.FileResponse("./fixtures/withDescriptions.json"), + ) + }, + wantOut: "All checks were successful\n0 failing, 3 successful, 0 skipped, and 0 pending checks\n\n✓ awesome tests awesome description 1m26s sweet link\n✓ cool tests cool description 1m26s sweet link\n✓ rad tests rad description 1m26s sweet link\n", + wantErr: "", + }, + { + name: "descriptions", + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.GraphQL(`query PullRequestStatusChecks\b`), + httpmock.FileResponse("./fixtures/withDescriptions.json"), + ) + }, + wantOut: "awesome tests\tpass\t1m26s\tsweet link\tawesome description\ncool tests\tpass\t1m26s\tsweet link\tcool description\nrad tests\tpass\t1m26s\tsweet link\trad description\n", + wantErr: "", }, } @@ -610,6 +680,156 @@ func TestEliminateDuplicates(t *testing.T) { }, }, }, + { + name: "unique workflow name", + checkContexts: []api.CheckContext{ + { + TypeName: "CheckRun", + Name: "build (ubuntu-latest)", + Status: "COMPLETED", + Conclusion: "SUCCESS", + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + DetailsURL: "https://github.com/cli/cli/runs/1", + CheckSuite: api.CheckSuite{ + WorkflowRun: api.WorkflowRun{ + Event: "push", + Workflow: api.Workflow{ + Name: "some builds", + }, + }, + }, + }, + { + TypeName: "CheckRun", + Name: "build (ubuntu-latest)", + Status: "COMPLETED", + Conclusion: "SUCCESS", + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + DetailsURL: "https://github.com/cli/cli/runs/2", + CheckSuite: api.CheckSuite{ + WorkflowRun: api.WorkflowRun{ + Event: "push", + Workflow: api.Workflow{ + Name: "some other builds", + }, + }, + }, + }, + }, + want: []api.CheckContext{ + { + TypeName: "CheckRun", + Name: "build (ubuntu-latest)", + Status: "COMPLETED", + Conclusion: "SUCCESS", + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + DetailsURL: "https://github.com/cli/cli/runs/1", + CheckSuite: api.CheckSuite{ + WorkflowRun: api.WorkflowRun{ + Event: "push", + Workflow: api.Workflow{ + Name: "some builds", + }, + }, + }, + }, + { + TypeName: "CheckRun", + Name: "build (ubuntu-latest)", + Status: "COMPLETED", + Conclusion: "SUCCESS", + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + DetailsURL: "https://github.com/cli/cli/runs/2", + CheckSuite: api.CheckSuite{ + WorkflowRun: api.WorkflowRun{ + Event: "push", + Workflow: api.Workflow{ + Name: "some other builds", + }, + }, + }, + }, + }, + }, + { + name: "unique workflow run event", + checkContexts: []api.CheckContext{ + { + TypeName: "CheckRun", + Name: "build (ubuntu-latest)", + Status: "COMPLETED", + Conclusion: "SUCCESS", + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + DetailsURL: "https://github.com/cli/cli/runs/1", + CheckSuite: api.CheckSuite{ + WorkflowRun: api.WorkflowRun{ + Event: "push", + Workflow: api.Workflow{ + Name: "builds", + }, + }, + }, + }, + { + TypeName: "CheckRun", + Name: "build (ubuntu-latest)", + Status: "COMPLETED", + Conclusion: "SUCCESS", + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + DetailsURL: "https://github.com/cli/cli/runs/2", + CheckSuite: api.CheckSuite{ + WorkflowRun: api.WorkflowRun{ + Event: "pull_request", + Workflow: api.Workflow{ + Name: "builds", + }, + }, + }, + }, + }, + want: []api.CheckContext{ + { + TypeName: "CheckRun", + Name: "build (ubuntu-latest)", + Status: "COMPLETED", + Conclusion: "SUCCESS", + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + DetailsURL: "https://github.com/cli/cli/runs/1", + CheckSuite: api.CheckSuite{ + WorkflowRun: api.WorkflowRun{ + Event: "push", + Workflow: api.Workflow{ + Name: "builds", + }, + }, + }, + }, + { + TypeName: "CheckRun", + Name: "build (ubuntu-latest)", + Status: "COMPLETED", + Conclusion: "SUCCESS", + StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC), + DetailsURL: "https://github.com/cli/cli/runs/2", + CheckSuite: api.CheckSuite{ + WorkflowRun: api.WorkflowRun{ + Event: "pull_request", + Workflow: api.Workflow{ + Name: "builds", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { diff --git a/pkg/cmd/pr/checks/fixtures/withDescriptions.json b/pkg/cmd/pr/checks/fixtures/withDescriptions.json new file mode 100644 index 000000000..fd5bc9463 --- /dev/null +++ b/pkg/cmd/pr/checks/fixtures/withDescriptions.json @@ -0,0 +1,48 @@ +{ + "data": { + "node": { + "statusCheckRollup": { + "nodes": [ + { + "commit": { + "oid": "abc", + "statusCheckRollup": { + "contexts": { + "nodes": [ + { + "conclusion": "SUCCESS", + "status": "COMPLETED", + "name": "cool tests", + "description": "cool description", + "completedAt": "2020-08-27T19:00:12Z", + "startedAt": "2020-08-27T18:58:46Z", + "detailsUrl": "sweet link" + }, + { + "conclusion": "SUCCESS", + "status": "COMPLETED", + "name": "rad tests", + "description": "rad description", + "completedAt": "2020-08-27T19:00:12Z", + "startedAt": "2020-08-27T18:58:46Z", + "detailsUrl": "sweet link" + }, + { + "conclusion": "SUCCESS", + "status": "COMPLETED", + "name": "awesome tests", + "description": "awesome description", + "completedAt": "2020-08-27T19:00:12Z", + "startedAt": "2020-08-27T18:58:46Z", + "detailsUrl": "sweet link" + } + ] + } + } + } + } + ] + } + } + } +} diff --git a/pkg/cmd/pr/checks/output.go b/pkg/cmd/pr/checks/output.go index bf5ecc839..a1e3f889a 100644 --- a/pkg/cmd/pr/checks/output.go +++ b/pkg/cmd/pr/checks/output.go @@ -4,11 +4,11 @@ import ( "fmt" "sort" + "github.com/cli/cli/v2/internal/tableprinter" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/utils" ) -func addRow(tp utils.TablePrinter, io *iostreams.IOStreams, o check) { +func addRow(tp *tableprinter.TablePrinter, io *iostreams.IOStreams, o check) { cs := io.ColorScheme() elapsed := "" @@ -34,19 +34,29 @@ func addRow(tp utils.TablePrinter, io *iostreams.IOStreams, o check) { } if io.IsStdoutTTY() { - tp.AddField(mark, nil, markColor) - tp.AddField(o.Name, nil, nil) - tp.AddField(elapsed, nil, nil) - tp.AddField(o.Link, nil, nil) - } else { - tp.AddField(o.Name, nil, nil) - tp.AddField(o.Bucket, nil, nil) - if elapsed == "" { - tp.AddField("0", nil, nil) - } else { - tp.AddField(elapsed, nil, nil) + var name string + if o.Workflow != "" { + name += fmt.Sprintf("%s/", o.Workflow) } - tp.AddField(o.Link, nil, nil) + name += o.Name + if o.Event != "" { + name += fmt.Sprintf(" (%s)", o.Event) + } + tp.AddField(mark, tableprinter.WithColor(markColor)) + tp.AddField(name) + tp.AddField(o.Description) + tp.AddField(elapsed) + tp.AddField(o.Link) + } else { + tp.AddField(o.Name) + tp.AddField(o.Bucket) + if elapsed == "" { + tp.AddField("0") + } else { + tp.AddField(elapsed) + } + tp.AddField(o.Link) + tp.AddField(o.Description) } tp.EndRow() @@ -76,8 +86,7 @@ func printSummary(io *iostreams.IOStreams, counts checkCounts) { } func printTable(io *iostreams.IOStreams, checks []check) error { - //nolint:staticcheck // SA1019: utils.NewTablePrinter is deprecated: use internal/tableprinter - tp := utils.NewTablePrinter(io) + tp := tableprinter.New(io) sort.Slice(checks, func(i, j int) bool { b0 := checks[i].Bucket diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index 4fc61f362..4c0c6286b 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "net/url" + "os" "regexp" "strings" "time" @@ -129,6 +130,12 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co opts.TitleProvided = cmd.Flags().Changed("title") opts.RepoOverride, _ = cmd.Flags().GetString("repo") + // Workaround: Due to the way this command is implemented, we need to manually check GH_REPO. + // Commands should use the standard BaseRepoOverride functionality to handle this behavior instead. + if opts.RepoOverride == "" { + opts.RepoOverride = os.Getenv("GH_REPO") + } + noMaintainerEdit, _ := cmd.Flags().GetBool("no-maintainer-edit") opts.MaintainerCanModify = !noMaintainerEdit @@ -318,8 +325,6 @@ func createRun(opts *CreateOptions) (err error) { if template != nil { templateContent = string(template.Body()) - } else { - templateContent = string(tpl.LegacyBody()) } } @@ -731,24 +736,51 @@ func handlePush(opts CreateOptions, ctx CreateContext) error { // missing: // 1. the head repo was just created by auto-forking; // 2. an existing fork was discovered by querying the API. - // // In either case, we want to add the head repo as a new git remote so we - // can push to it. + // can push to it. We will try to add the head repo as the "origin" remote + // and fallback to the "fork" remote if it is unavailable. Also, if the + // base repo is the "origin" remote we will rename it "upstream". if headRemote == nil && ctx.IsPushEnabled { cfg, err := opts.Config() if err != nil { return err } + + remotes, err := opts.Remotes() + if err != nil { + return err + } + cloneProtocol, _ := cfg.GetOrDefault(headRepo.RepoHost(), "git_protocol") - headRepoURL := ghrepo.FormatRemoteURL(headRepo, cloneProtocol) - - // TODO: prevent clashes with another remote of a same name gitClient := ctx.GitClient - gitRemote, err := gitClient.AddRemote(context.Background(), "fork", headRepoURL, []string{}) + origin, _ := remotes.FindByName("origin") + upstream, _ := remotes.FindByName("upstream") + remoteName := "origin" + + if origin != nil { + remoteName = "fork" + } + + if origin != nil && upstream == nil && ghrepo.IsSame(origin, ctx.BaseRepo) { + renameCmd, err := gitClient.Command(context.Background(), "remote", "rename", "origin", "upstream") + if err != nil { + return err + } + if _, err = renameCmd.Output(); err != nil { + return fmt.Errorf("error renaming origin remote: %w", err) + } + remoteName = "origin" + fmt.Fprintf(opts.IO.ErrOut, "Changed %s remote to %q\n", ghrepo.FullName(ctx.BaseRepo), "upstream") + } + + gitRemote, err := gitClient.AddRemote(context.Background(), remoteName, headRepoURL, []string{}) if err != nil { return fmt.Errorf("error adding remote: %w", err) } + + fmt.Fprintf(opts.IO.ErrOut, "Added %s as remote %q\n", ghrepo.FullName(headRepo), remoteName) + headRemote = &ghContext.Remote{ Remote: gitRemote, Repo: headRepo, diff --git a/pkg/cmd/pr/create/create_test.go b/pkg/cmd/pr/create/create_test.go index 84b5cf6fa..5b5fe2ee4 100644 --- a/pkg/cmd/pr/create/create_test.go +++ b/pkg/cmd/pr/create/create_test.go @@ -466,8 +466,9 @@ func Test_createRun(t *testing.T) { cmdStubs: func(cs *run.CommandStubber) { cs.Register(`git config --get-regexp.+branch\\\.feature\\\.`, 0, "") cs.Register(`git show-ref --verify -- HEAD refs/remotes/origin/feature`, 0, "") - cs.Register(`git remote add fork https://github.com/monalisa/REPO.git`, 0, "") - cs.Register(`git push --set-upstream fork HEAD:feature`, 0, "") + cs.Register("git remote rename origin upstream", 0, "") + cs.Register(`git remote add origin https://github.com/monalisa/REPO.git`, 0, "") + cs.Register(`git push --set-upstream origin HEAD:feature`, 0, "") }, promptStubs: func(pm *prompter.PrompterMock) { pm.SelectFunc = func(p, _ string, opts []string) (int, error) { @@ -479,7 +480,7 @@ func Test_createRun(t *testing.T) { } }, expectedOut: "https://github.com/OWNER/REPO/pull/12\n", - expectedErrOut: "\nCreating pull request for monalisa:feature into master in OWNER/REPO\n\n", + expectedErrOut: "\nCreating pull request for monalisa:feature into master in OWNER/REPO\n\nChanged OWNER/REPO remote to \"upstream\"\nAdded monalisa/REPO as remote \"origin\"\n", }, { name: "pushed to non base repo", diff --git a/pkg/cmd/pr/merge/merge.go b/pkg/cmd/pr/merge/merge.go index 1a331597c..d49139011 100644 --- a/pkg/cmd/pr/merge/merge.go +++ b/pkg/cmd/pr/merge/merge.go @@ -477,7 +477,7 @@ func (m *mergeContext) infof(format string, args ...interface{}) error { return err } -// Creates a new MergeConext from MergeOptions. +// Creates a new MergeContext from MergeOptions. func NewMergeContext(opts *MergeOptions) (*mergeContext, error) { findOptions := shared.FindOptions{ Selector: opts.SelectorArg, diff --git a/pkg/cmd/pr/shared/editable.go b/pkg/cmd/pr/shared/editable.go index 7564c24cd..0fbe689fc 100644 --- a/pkg/cmd/pr/shared/editable.go +++ b/pkg/cmd/pr/shared/editable.go @@ -7,6 +7,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/set" "github.com/cli/cli/v2/pkg/surveyext" ) @@ -201,6 +202,59 @@ func (e Editable) MilestoneId() (*string, error) { return &m, err } +// Clone creates a mostly-shallow copy of Editable suitable for use in parallel +// go routines. Fields that would be mutated will be copied. +func (e *Editable) Clone() Editable { + return Editable{ + Title: e.Title.clone(), + Body: e.Body.clone(), + Base: e.Base.clone(), + Reviewers: e.Reviewers.clone(), + Assignees: e.Assignees.clone(), + Labels: e.Labels.clone(), + Projects: e.Projects.clone(), + Milestone: e.Milestone.clone(), + // Shallow copy since no mutation. + Metadata: e.Metadata, + } +} + +func (es *EditableString) clone() EditableString { + return EditableString{ + Value: es.Value, + Default: es.Default, + Edited: es.Edited, + // Shallow copies since no mutation. + Options: es.Options, + } +} + +func (es *EditableSlice) clone() EditableSlice { + cpy := EditableSlice{ + Edited: es.Edited, + Allowed: es.Allowed, + // Shallow copies since no mutation. + Options: es.Options, + // Copy mutable string slices. + Add: make([]string, len(es.Add)), + Remove: make([]string, len(es.Remove)), + Value: make([]string, len(es.Value)), + Default: make([]string, len(es.Default)), + } + copy(cpy.Add, es.Add) + copy(cpy.Remove, es.Remove) + copy(cpy.Value, es.Value) + copy(cpy.Default, es.Default) + return cpy +} + +func (ep *EditableProjects) clone() EditableProjects { + return EditableProjects{ + EditableSlice: ep.EditableSlice.clone(), + ProjectItems: ep.ProjectItems, + } +} + func EditFieldsSurvey(editable *Editable, editorCommand string) error { var err error if editable.Title.Edited { @@ -395,6 +449,7 @@ func multiSelectSurvey(message string, defaults, options []string) ([]string, er Message: message, Options: options, Default: defaults, + Filter: prompter.LatinMatchingFilter, } err := survey.AskOne(q, &results) return results, err diff --git a/pkg/cmd/pr/shared/editable_http.go b/pkg/cmd/pr/shared/editable_http.go index 3353cd5f5..fcc30095a 100644 --- a/pkg/cmd/pr/shared/editable_http.go +++ b/pkg/cmd/pr/shared/editable_http.go @@ -35,7 +35,7 @@ func UpdateIssue(httpClient *http.Client, repo ghrepo.Interface, id string, isPR } } - // updateIssue mutation does not support ProjectsV2 so do them in a seperate request. + // updateIssue mutation does not support ProjectsV2 so do them in a separate request. if options.Projects.Edited { wg.Go(func() error { apiClient := api.NewClientFromHTTP(httpClient) diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go index f1886b3fe..4bb79ac8b 100644 --- a/pkg/cmd/pr/shared/finder.go +++ b/pkg/cmd/pr/shared/finder.go @@ -465,7 +465,7 @@ func preloadPrChecks(client *http.Client, repo ghrepo.Interface, pr *api.PullReq %s } } - }`, api.StatusCheckRollupGraphQL("$endCursor")) + }`, api.StatusCheckRollupGraphQLWithoutCountByState("$endCursor")) variables := map[string]interface{}{ "id": pr.ID, diff --git a/pkg/cmd/pr/shared/survey.go b/pkg/cmd/pr/shared/survey.go index 8582373b0..4e1d1a421 100644 --- a/pkg/cmd/pr/shared/survey.go +++ b/pkg/cmd/pr/shared/survey.go @@ -7,6 +7,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/iostreams" "github.com/cli/cli/v2/pkg/prompt" ) @@ -223,6 +224,7 @@ func MetadataSurvey(io *iostreams.IOStreams, baseRepo ghrepo.Interface, fetcher Message: "Reviewers", Options: reviewers, Default: state.Reviewers, + Filter: prompter.LatinMatchingFilter, }, }) } else { @@ -237,6 +239,7 @@ func MetadataSurvey(io *iostreams.IOStreams, baseRepo ghrepo.Interface, fetcher Message: "Assignees", Options: assignees, Default: state.Assignees, + Filter: prompter.LatinMatchingFilter, }, }) } else { @@ -251,6 +254,7 @@ func MetadataSurvey(io *iostreams.IOStreams, baseRepo ghrepo.Interface, fetcher Message: "Labels", Options: labels, Default: state.Labels, + Filter: prompter.LatinMatchingFilter, }, }) } else { @@ -265,6 +269,7 @@ func MetadataSurvey(io *iostreams.IOStreams, baseRepo ghrepo.Interface, fetcher Message: "Projects", Options: projects, Default: state.Projects, + Filter: prompter.LatinMatchingFilter, }, }) } else { @@ -276,6 +281,8 @@ func MetadataSurvey(io *iostreams.IOStreams, baseRepo ghrepo.Interface, fetcher var milestoneDefault string if len(state.Milestones) > 0 { milestoneDefault = state.Milestones[0] + } else { + milestoneDefault = milestones[1] } mqs = append(mqs, &survey.Question{ Name: "milestone", diff --git a/pkg/cmd/pr/status/fixtures/prStatus.json b/pkg/cmd/pr/status/fixtures/prStatus.json index a226663a3..d5cfb265a 100644 --- a/pkg/cmd/pr/status/fixtures/prStatus.json +++ b/pkg/cmd/pr/status/fixtures/prStatus.json @@ -15,7 +15,8 @@ "headRepositoryOwner": { "login": "OWNER" }, - "isCrossRepository": false + "isCrossRepository": false, + "autoMergeRequest": null } } ] @@ -31,7 +32,8 @@ "state": "OPEN", "url": "https://github.com/cli/cli/pull/8", "headRefName": "strawberries", - "isDraft": false + "isDraft": false, + "autoMergeRequest": null } } ] @@ -46,7 +48,17 @@ "state": "OPEN", "url": "https://github.com/cli/cli/pull/9", "headRefName": "apples", - "isDraft": false + "isDraft": false, + "autoMergeRequest": { + "authorEmail": null, + "commitBody": null, + "commitHeadline": null, + "mergeMethod": "SQUASH", + "enabledAt": "2020-08-27T19:00:12Z", + "enabledBy": { + "login": "hubot" + } + } } }, { "node": { "number": 11, @@ -54,7 +66,8 @@ "state": "OPEN", "url": "https://github.com/cli/cli/pull/1", "headRefName": "figs", - "isDraft": true + "isDraft": true, + "autoMergeRequest": null } } ] diff --git a/pkg/cmd/pr/status/fixtures/prStatusChecks.json b/pkg/cmd/pr/status/fixtures/prStatusChecks.json index 91455ee2c..0f5d88902 100644 --- a/pkg/cmd/pr/status/fixtures/prStatusChecks.json +++ b/pkg/cmd/pr/status/fixtures/prStatusChecks.json @@ -26,6 +26,7 @@ "contexts": { "nodes": [ { + "__typeName": "StatusContext", "state": "SUCCESS" } ] @@ -80,6 +81,7 @@ "contexts": { "nodes": [ { + "__typeName": "CheckRun", "status": "IN_PROGRESS", "conclusion": "" } @@ -109,13 +111,16 @@ "contexts": { "nodes": [ { + "__typeName": "CheckRun", "status": "IN_PROGRESS", "conclusion": "" }, { + "__typeName": "StatusContext", "state": "FAILURE" }, { + "__typeName": "CheckRun", "status": "COMPLETED", "conclusion": "NEUTRAL" } @@ -144,6 +149,7 @@ "contexts": { "nodes": [ { + "__typeName": "StatusContext", "state": "SUCCESS" } ] diff --git a/pkg/cmd/pr/status/fixtures/prStatusChecksWithStatesByCount.json b/pkg/cmd/pr/status/fixtures/prStatusChecksWithStatesByCount.json new file mode 100644 index 000000000..452cb4e06 --- /dev/null +++ b/pkg/cmd/pr/status/fixtures/prStatusChecksWithStatesByCount.json @@ -0,0 +1,464 @@ +{ + "data": { + "repository": { + "pullRequests": { + "totalCount": 0, + "edges": [] + } + }, + "viewerCreated": { + "totalCount": 4, + "edges": [ + { + "node": { + "number": 8, + "title": "Strawberries are not actually berries", + "state": "OPEN", + "url": "https://github.com/cli/cli/pull/8", + "headRefName": "strawberries", + "mergeable": "UNKNOWN", + "reviewDecision": "CHANGES_REQUESTED", + "statusCheckRollup": { + "nodes": [ + { + "commit": { + "statusCheckRollup": { + "contexts": { + "checkRunCount": 0, + "checkRunCountsByState": [ + { + "state": "ACTION_REQUIRED", + "count": 0 + }, + { + "state": "CANCELLED", + "count": 0 + }, + { + "state": "COMPLETED", + "count": 0 + }, + { + "state": "FAILURE", + "count": 0 + }, + { + "state": "IN_PROGRESS", + "count": 0 + }, + { + "state": "NEUTRAL", + "count": 0 + }, + { + "state": "PENDING", + "count": 0 + }, + { + "state": "QUEUED", + "count": 0 + }, + { + "state": "SKIPPED", + "count": 0 + }, + { + "state": "STALE", + "count": 0 + }, + { + "state": "STARTUP_FAILURE", + "count": 0 + }, + { + "state": "SUCCESS", + "count": 0 + }, + { + "state": "TIMED_OUT", + "count": 0 + }, + { + "state": "WAITING", + "count": 0 + } + ], + "statusContextCount": 1, + "statusContextCountsByState": [ + { + "state": "EXPECTED", + "count": 0 + }, + { + "state": "ERROR", + "count": 0 + }, + { + "state": "FAILURE", + "count": 0 + }, + { + "state": "PENDING", + "count": 0 + }, + { + "state": "SUCCESS", + "count": 1 + } + ] + } + } + } + } + ] + } + } + }, + { + "node": { + "number": 7, + "title": "Bananas are berries", + "state": "OPEN", + "url": "https://github.com/cli/cli/pull/7", + "headRefName": "banananana", + "reviewDecision": "APPROVED", + "baseRef": { + "branchProtectionRule": { + "requiredApprovingReviewCount": 0 + } + }, + "latestReviews": { + "nodes": [ + { + "author": { + "login": "bob" + }, + "state": "APPROVED" + }, + { + "author": { + "login": "stella" + }, + "state": "CHANGES_REQUESTED" + }, + { + "author": { + "login": "alice" + }, + "state": "APPROVED" + } + ] + }, + "statusCheckRollup": { + "nodes": [ + { + "commit": { + "statusCheckRollup": { + "contexts": { + "checkRunCount": 1, + "checkRunCountsByState": [ + { + "state": "ACTION_REQUIRED", + "count": 0 + }, + { + "state": "CANCELLED", + "count": 0 + }, + { + "state": "COMPLETED", + "count": 0 + }, + { + "state": "FAILURE", + "count": 0 + }, + { + "state": "IN_PROGRESS", + "count": 1 + }, + { + "state": "NEUTRAL", + "count": 0 + }, + { + "state": "PENDING", + "count": 0 + }, + { + "state": "QUEUED", + "count": 0 + }, + { + "state": "SKIPPED", + "count": 0 + }, + { + "state": "STALE", + "count": 0 + }, + { + "state": "STARTUP_FAILURE", + "count": 0 + }, + { + "state": "SUCCESS", + "count": 0 + }, + { + "state": "TIMED_OUT", + "count": 0 + }, + { + "state": "WAITING", + "count": 0 + } + ], + "statusContextCount": 1, + "statusContextCountsByState": [ + { + "state": "EXPECTED", + "count": 0 + }, + { + "state": "ERROR", + "count": 0 + }, + { + "state": "FAILURE", + "count": 0 + }, + { + "state": "PENDING", + "count": 0 + }, + { + "state": "SUCCESS", + "count": 1 + } + ] + } + } + } + } + ] + } + } + }, + { + "node": { + "number": 6, + "title": "Avocado is probably not a berry", + "state": "OPEN", + "url": "https://github.com/cli/cli/pull/6", + "headRefName": "avo", + "mergeable": "MERGEABLE", + "reviewDecision": "REVIEW_REQUIRED", + "statusCheckRollup": { + "nodes": [ + { + "commit": { + "statusCheckRollup": { + "contexts": { + "checkRunCount": 2, + "checkRunCountsByState": [ + { + "state": "ACTION_REQUIRED", + "count": 0 + }, + { + "state": "CANCELLED", + "count": 0 + }, + { + "state": "COMPLETED", + "count": 0 + }, + { + "state": "FAILURE", + "count": 0 + }, + { + "state": "IN_PROGRESS", + "count": 1 + }, + { + "state": "NEUTRAL", + "count": 1 + }, + { + "state": "PENDING", + "count": 0 + }, + { + "state": "QUEUED", + "count": 0 + }, + { + "state": "SKIPPED", + "count": 0 + }, + { + "state": "STALE", + "count": 0 + }, + { + "state": "STARTUP_FAILURE", + "count": 0 + }, + { + "state": "SUCCESS", + "count": 0 + }, + { + "state": "TIMED_OUT", + "count": 0 + }, + { + "state": "WAITING", + "count": 0 + } + ], + "statusContextCount": 1, + "statusContextCountsByState": [ + { + "state": "EXPECTED", + "count": 0 + }, + { + "state": "ERROR", + "count": 0 + }, + { + "state": "FAILURE", + "count": 1 + }, + { + "state": "PENDING", + "count": 0 + }, + { + "state": "SUCCESS", + "count": 0 + } + ] + } + } + } + } + ] + } + } + }, + { + "node": { + "number": 5, + "title": "Why can't berries get along?", + "state": "OPEN", + "url": "https://github.com/cli/cli/pull/5", + "headRefName": "strawberries", + "mergeable": "CONFLICTING", + "statusCheckRollup": { + "nodes": [ + { + "commit": { + "statusCheckRollup": { + "contexts": { + "checkRunCount": 0, + "checkRunCountsByState": [ + { + "state": "ACTION_REQUIRED", + "count": 0 + }, + { + "state": "CANCELLED", + "count": 0 + }, + { + "state": "COMPLETED", + "count": 0 + }, + { + "state": "FAILURE", + "count": 0 + }, + { + "state": "IN_PROGRESS", + "count": 0 + }, + { + "state": "NEUTRAL", + "count": 0 + }, + { + "state": "PENDING", + "count": 0 + }, + { + "state": "QUEUED", + "count": 0 + }, + { + "state": "SKIPPED", + "count": 0 + }, + { + "state": "STALE", + "count": 0 + }, + { + "state": "STARTUP_FAILURE", + "count": 0 + }, + { + "state": "SUCCESS", + "count": 0 + }, + { + "state": "TIMED_OUT", + "count": 0 + }, + { + "state": "WAITING", + "count": 0 + } + ], + "statusContextCount": 1, + "statusContextCountsByState": [ + { + "state": "EXPECTED", + "count": 0 + }, + { + "state": "ERROR", + "count": 0 + }, + { + "state": "FAILURE", + "count": 0 + }, + { + "state": "PENDING", + "count": 0 + }, + { + "state": "SUCCESS", + "count": 1 + } + ] + } + } + } + } + ] + } + } + } + ] + }, + "reviewRequested": { + "totalCount": 0, + "edges": [] + } + } +} diff --git a/pkg/cmd/pr/status/http.go b/pkg/cmd/pr/status/http.go index e0b2145e9..755ccf30b 100644 --- a/pkg/cmd/pr/status/http.go +++ b/pkg/cmd/pr/status/http.go @@ -17,6 +17,8 @@ type requestOptions struct { Username string Fields []string ConflictStatus bool + + CheckRunAndStatusContextCountsSupported bool } type pullRequestsPayload struct { @@ -57,7 +59,7 @@ func pullRequestStatus(httpClient *http.Client, repo ghrepo.Interface, options r fragments = fmt.Sprintf("fragment pr on PullRequest{%s}fragment prWithReviews on PullRequest{...pr}", gr) } else { var err error - fragments, err = pullRequestFragment(repo.RepoHost(), options.ConflictStatus) + fragments, err = pullRequestFragment(repo.RepoHost(), options.ConflictStatus, options.CheckRunAndStatusContextCountsSupported) if err != nil { return nil, err } @@ -146,8 +148,7 @@ func pullRequestStatus(httpClient *http.Client, repo ghrepo.Interface, options r } var resp response - err := apiClient.GraphQL(repo.RepoHost(), query, variables, &resp) - if err != nil { + if err := apiClient.GraphQL(repo.RepoHost(), query, variables, &resp); err != nil { return nil, err } @@ -187,16 +188,23 @@ func pullRequestStatus(httpClient *http.Client, repo ghrepo.Interface, options r return &payload, nil } -func pullRequestFragment(hostname string, conflictStatus bool) (string, error) { +func pullRequestFragment(hostname string, conflictStatus bool, statusCheckRollupWithCountByState bool) (string, error) { fields := []string{ "number", "title", "state", "url", "isDraft", "isCrossRepository", "headRefName", "headRepositoryOwner", "mergeStateStatus", - "statusCheckRollup", "requiresStrictStatusChecks", + "requiresStrictStatusChecks", "autoMergeRequest", } if conflictStatus { fields = append(fields, "mergeable") } + + if statusCheckRollupWithCountByState { + fields = append(fields, "statusCheckRollupWithCountByState") + } else { + fields = append(fields, "statusCheckRollup") + } + reviewFields := []string{"reviewDecision", "latestReviews"} fragments := fmt.Sprintf(` fragment pr on PullRequest {%s} diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index c2d4271d0..bb7bb735d 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -8,11 +8,13 @@ import ( "regexp" "strconv" "strings" + "time" "github.com/cli/cli/v2/api" ghContext "github.com/cli/cli/v2/context" "github.com/cli/cli/v2/git" "github.com/cli/cli/v2/internal/config" + fd "github.com/cli/cli/v2/internal/featuredetection" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/internal/text" "github.com/cli/cli/v2/pkg/cmd/pr/shared" @@ -33,6 +35,8 @@ type StatusOptions struct { HasRepoOverride bool Exporter cmdutil.Exporter ConflictStatus bool + + Detector fd.Detector } func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Command { @@ -105,6 +109,16 @@ func statusRun(opts *StatusOptions) error { options.Fields = opts.Exporter.Fields() } + if opts.Detector == nil { + cachedClient := api.NewCachedHTTPClient(httpClient, time.Hour*24) + opts.Detector = fd.NewDetector(cachedClient, baseRepo.RepoHost()) + } + prFeatures, err := opts.Detector.PullRequestFeatures() + if err != nil { + return err + } + options.CheckRunAndStatusContextCountsSupported = prFeatures.CheckRunAndStatusContextCounts + prPayload, err := pullRequestStatus(httpClient, baseRepo, options) if err != nil { return err @@ -284,6 +298,10 @@ func printPrs(io *iostreams.IOStreams, totalCount int, prs ...api.PullRequest) { } } + if pr.AutoMergeRequest != nil { + fmt.Fprintf(w, " %s", cs.Green("✓ Auto-merge enabled")) + } + } else { fmt.Fprintf(w, " - %s", shared.StateTitleWithColor(cs, pr)) } diff --git a/pkg/cmd/pr/status/status_test.go b/pkg/cmd/pr/status/status_test.go index f94822e47..3df048503 100644 --- a/pkg/cmd/pr/status/status_test.go +++ b/pkg/cmd/pr/status/status_test.go @@ -12,6 +12,7 @@ import ( "github.com/cli/cli/v2/context" "github.com/cli/cli/v2/git" "github.com/cli/cli/v2/internal/config" + fd "github.com/cli/cli/v2/internal/featuredetection" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/internal/run" "github.com/cli/cli/v2/pkg/cmdutil" @@ -22,6 +23,10 @@ import ( ) func runCommand(rt http.RoundTripper, branch string, isTTY bool, cli string) (*test.CmdOut, error) { + return runCommandWithDetector(rt, branch, isTTY, cli, &fd.DisabledDetectorMock{}) +} + +func runCommandWithDetector(rt http.RoundTripper, branch string, isTTY bool, cli string, detector fd.Detector) (*test.CmdOut, error) { ios, _, stdout, stderr := iostreams.Test() ios.SetStdoutTTY(isTTY) ios.SetStdinTTY(isTTY) @@ -55,7 +60,12 @@ func runCommand(rt http.RoundTripper, branch string, isTTY bool, cli string) (*t GitClient: &git.Client{GitPath: "some/path/git"}, } - cmd := NewCmdStatus(factory, nil) + withProvidedDetector := func(opts *StatusOptions) error { + opts.Detector = detector + return statusRun(opts) + } + + cmd := NewCmdStatus(factory, withProvidedDetector) cmd.PersistentFlags().StringP("repo", "R", "", "") argv, err := shlex.Split(cli) @@ -91,7 +101,7 @@ func TestPRStatus(t *testing.T) { expectedPrs := []*regexp.Regexp{ regexp.MustCompile(`#8.*\[strawberries\]`), - regexp.MustCompile(`#9.*\[apples\]`), + regexp.MustCompile(`#9.*\[apples\].*✓ Auto-merge enabled`), regexp.MustCompile(`#10.*\[blueberries\]`), regexp.MustCompile(`#11.*\[figs\]`), } @@ -106,7 +116,8 @@ func TestPRStatus(t *testing.T) { func TestPRStatus_reviewsAndChecks(t *testing.T) { http := initFakeHTTP() defer http.Verify(t) - http.Register(httpmock.GraphQL(`query PullRequestStatus\b`), httpmock.FileResponse("./fixtures/prStatusChecks.json")) + // status,conclusion matches the old StatusContextRollup query + http.Register(httpmock.GraphQL(`status,conclusion`), httpmock.FileResponse("./fixtures/prStatusChecks.json")) output, err := runCommand(http, "blueberries", true, "") if err != nil { @@ -127,6 +138,31 @@ func TestPRStatus_reviewsAndChecks(t *testing.T) { } } +func TestPRStatus_reviewsAndChecksWithStatesByCount(t *testing.T) { + http := initFakeHTTP() + defer http.Verify(t) + // checkRunCount,checkRunCountsByState matches the new StatusContextRollup query + http.Register(httpmock.GraphQL(`checkRunCount,checkRunCountsByState`), httpmock.FileResponse("./fixtures/prStatusChecksWithStatesByCount.json")) + + output, err := runCommandWithDetector(http, "blueberries", true, "", &fd.EnabledDetectorMock{}) + if err != nil { + t.Errorf("error running command `pr status`: %v", err) + } + + expected := []string{ + "✓ Checks passing + Changes requested ! Merge conflict status unknown", + "- Checks pending ✓ 2 Approved", + "× 1/3 checks failing - Review required ✓ No merge conflicts", + "✓ Checks passing × Merge conflicts", + } + + for _, line := range expected { + if !strings.Contains(output.String(), line) { + t.Errorf("output did not contain %q: %q", line, output.String()) + } + } +} + func TestPRStatus_currentBranch_showTheMostRecentPR(t *testing.T) { http := initFakeHTTP() defer http.Verify(t) diff --git a/pkg/cmd/pr/view/fixtures/prViewPreviewWithAllChecksFailing.json b/pkg/cmd/pr/view/fixtures/prViewPreviewWithAllChecksFailing.json index 59c4acd8a..48484f1c4 100644 --- a/pkg/cmd/pr/view/fixtures/prViewPreviewWithAllChecksFailing.json +++ b/pkg/cmd/pr/view/fixtures/prViewPreviewWithAllChecksFailing.json @@ -46,6 +46,7 @@ "contexts": { "nodes": [ { + "__typename": "CheckRun", "conclusion": "FAILURE", "status": "COMPLETED", "name": "cool tests", @@ -54,6 +55,7 @@ "detailsUrl": "sweet link" }, { + "__typename": "CheckRun", "conclusion": "FAILURE", "status": "COMPLETED", "name": "sad tests", diff --git a/pkg/cmd/pr/view/fixtures/prViewPreviewWithAllChecksPassing.json b/pkg/cmd/pr/view/fixtures/prViewPreviewWithAllChecksPassing.json index d55e460b3..87152d692 100644 --- a/pkg/cmd/pr/view/fixtures/prViewPreviewWithAllChecksPassing.json +++ b/pkg/cmd/pr/view/fixtures/prViewPreviewWithAllChecksPassing.json @@ -46,6 +46,7 @@ "contexts": { "nodes": [ { + "__typename": "CheckRun", "conclusion": "SUCCESS", "status": "COMPLETED", "name": "cool tests", @@ -54,6 +55,7 @@ "detailsUrl": "sweet link" }, { + "__typename": "CheckRun", "conclusion": "SUCCESS", "status": "COMPLETED", "name": "rad tests", @@ -62,6 +64,7 @@ "detailsUrl": "sweet link" }, { + "__typename": "CheckRun", "conclusion": "SUCCESS", "status": "COMPLETED", "name": "awesome tests", diff --git a/pkg/cmd/pr/view/fixtures/prViewPreviewWithAutoMergeEnabled.json b/pkg/cmd/pr/view/fixtures/prViewPreviewWithAutoMergeEnabled.json new file mode 100644 index 000000000..000dc440e --- /dev/null +++ b/pkg/cmd/pr/view/fixtures/prViewPreviewWithAutoMergeEnabled.json @@ -0,0 +1,55 @@ +{ + "data": { + "repository": { + "pullRequest": { + "number": 12, + "title": "Blueberries are from a fork", + "state": "OPEN", + "body": "**blueberries taste good**", + "url": "https://github.com/OWNER/REPO/pull/12", + "author": { + "login": "nobody" + }, + "autoMergeRequest": { + "authorEmail": null, + "commitBody": null, + "commitHeadline": null, + "mergeMethod": "SQUASH", + "enabledAt": "2020-08-27T19:00:12Z", + "enabledBy": { + "login": "hubot" + } + }, + "additions": 100, + "deletions": 10, + "reviewRequests": { + "nodes": [], + "totalcount": 0 + }, + "assignees": { + "nodes": [], + "totalcount": 0 + }, + "labels": { + "nodes": [], + "totalcount": 0 + }, + "projectcards": { + "nodes": [], + "totalcount": 0 + }, + "milestone": {}, + "commits": { + "totalCount": 12 + }, + "baseRefName": "master", + "headRefName": "blueberries", + "headRepositoryOwner": { + "login": "hubot" + }, + "isCrossRepository": true, + "isDraft": false + } + } + } +} diff --git a/pkg/cmd/pr/view/fixtures/prViewPreviewWithMetadataByNumber.json b/pkg/cmd/pr/view/fixtures/prViewPreviewWithMetadataByNumber.json index 00f768f17..be4a7713d 100644 --- a/pkg/cmd/pr/view/fixtures/prViewPreviewWithMetadataByNumber.json +++ b/pkg/cmd/pr/view/fixtures/prViewPreviewWithMetadataByNumber.json @@ -37,19 +37,19 @@ "labels": { "nodes": [ { - "name": "one" + "name": "Closed: Won't Fix" }, { - "name": "two" + "name": "Status: In Progress" }, { - "name": "three" + "name": "Type: Bug" }, { - "name": "four" + "name": "help wanted" }, { - "name": "five" + "name": "Closed: Duplicate" } ], "totalcount": 5 diff --git a/pkg/cmd/pr/view/fixtures/prViewPreviewWithSomeChecksFailing.json b/pkg/cmd/pr/view/fixtures/prViewPreviewWithSomeChecksFailing.json index d817a5da0..adb15e7c3 100644 --- a/pkg/cmd/pr/view/fixtures/prViewPreviewWithSomeChecksFailing.json +++ b/pkg/cmd/pr/view/fixtures/prViewPreviewWithSomeChecksFailing.json @@ -46,6 +46,7 @@ "contexts": { "nodes": [ { + "__typename": "CheckRun", "conclusion": "SUCCESS", "status": "COMPLETED", "name": "cool tests", @@ -54,6 +55,7 @@ "detailsUrl": "sweet link" }, { + "__typename": "CheckRun", "conclusion": "FAILURE", "status": "COMPLETED", "name": "sad tests", diff --git a/pkg/cmd/pr/view/fixtures/prViewPreviewWithSomeChecksPending.json b/pkg/cmd/pr/view/fixtures/prViewPreviewWithSomeChecksPending.json index 019186783..4a011e5c1 100644 --- a/pkg/cmd/pr/view/fixtures/prViewPreviewWithSomeChecksPending.json +++ b/pkg/cmd/pr/view/fixtures/prViewPreviewWithSomeChecksPending.json @@ -46,6 +46,7 @@ "contexts": { "nodes": [ { + "__typename": "CheckRun", "conclusion": "", "status": "WAITING", "name": "cool tests", @@ -54,6 +55,7 @@ "detailsUrl": "sweet link" }, { + "__typename": "CheckRun", "conclusion": "SUCCESS", "status": "COMPLETED", "name": "sad tests", diff --git a/pkg/cmd/pr/view/view.go b/pkg/cmd/pr/view/view.go index 10300a23c..4a69de2f4 100644 --- a/pkg/cmd/pr/view/view.go +++ b/pkg/cmd/pr/view/view.go @@ -77,7 +77,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman } var defaultFields = []string{ - "url", "number", "title", "state", "body", "author", + "url", "number", "title", "state", "body", "author", "autoMergeRequest", "isDraft", "maintainerCanModify", "mergeable", "additions", "deletions", "commitsCount", "baseRefName", "headRefName", "headRepositoryOwner", "headRepository", "isCrossRepository", "reviewRequests", "reviews", "assignees", "labels", "projectCards", "milestone", @@ -157,6 +157,15 @@ func printRawPrPreview(io *iostreams.IOStreams, pr *api.PullRequest) error { fmt.Fprintf(out, "url:\t%s\n", pr.URL) fmt.Fprintf(out, "additions:\t%s\n", cs.Green(strconv.Itoa(pr.Additions))) fmt.Fprintf(out, "deletions:\t%s\n", cs.Red(strconv.Itoa(pr.Deletions))) + var autoMerge string + if pr.AutoMergeRequest == nil { + autoMerge = "disabled" + } else { + autoMerge = fmt.Sprintf("enabled\t%s\t%s", + pr.AutoMergeRequest.EnabledBy.Login, + strings.ToLower(pr.AutoMergeRequest.MergeMethod)) + } + fmt.Fprintf(out, "auto-merge:\t%s\n", autoMerge) fmt.Fprintln(out, "--") fmt.Fprintln(out, pr.Body) @@ -223,6 +232,29 @@ func printHumanPrPreview(opts *ViewOptions, pr *api.PullRequest) error { fmt.Fprintln(out, pr.Milestone.Title) } + // Auto-Merge status + autoMerge := pr.AutoMergeRequest + if autoMerge != nil { + var mergeMethod string + switch autoMerge.MergeMethod { + case "MERGE": + mergeMethod = "a merge commit" + case "REBASE": + mergeMethod = "rebase and merge" + case "SQUASH": + mergeMethod = "squash and merge" + default: + mergeMethod = fmt.Sprintf("an unknown merge method (%s)", autoMerge.MergeMethod) + } + fmt.Fprintf(out, + "%s %s by %s, using %s\n", + cs.Bold("Auto-merge:"), + cs.Green("enabled"), + autoMerge.EnabledBy.Login, + mergeMethod, + ) + } + // Body var md string var err error @@ -383,6 +415,11 @@ func prLabelList(pr api.PullRequest, cs *iostreams.ColorScheme) string { return "" } + // ignore case sort + sort.SliceStable(pr.Labels.Nodes, func(i, j int) bool { + return strings.ToLower(pr.Labels.Nodes[i].Name) < strings.ToLower(pr.Labels.Nodes[j].Name) + }) + labelNames := make([]string, 0, len(pr.Labels.Nodes)) for _, label := range pr.Labels.Nodes { labelNames = append(labelNames, cs.HexToRGB(label.Color, label.Name)) diff --git a/pkg/cmd/pr/view/view_test.go b/pkg/cmd/pr/view/view_test.go index 166289050..5e2c4b922 100644 --- a/pkg/cmd/pr/view/view_test.go +++ b/pkg/cmd/pr/view/view_test.go @@ -229,7 +229,7 @@ func TestPRView_Preview_nontty(t *testing.T) { `title:\tBlueberries are from a fork\n`, `reviewers:\t1 \(Requested\)\n`, `assignees:\tmarseilles, monaco\n`, - `labels:\tone, two, three, four, five\n`, + `labels:\tClosed: Duplicate, Closed: Won't Fix, help wanted, Status: In Progress, Type: Bug\n`, `projects:\tProject 1 \(column A\), Project 2 \(column B\), Project 3 \(column C\), Project 4 \(Awaiting triage\)\n`, `milestone:\tuluru\n`, `\*\*blueberries taste good\*\*`, @@ -314,6 +314,25 @@ func TestPRView_Preview_nontty(t *testing.T) { `\*\*blueberries taste good\*\*`, }, }, + "PR with auto-merge enabled": { + branch: "master", + args: "12", + fixtures: map[string]string{ + "PullRequestByNumber": "./fixtures/prViewPreviewWithAutoMergeEnabled.json", + }, + expectedOutputs: []string{ + `title:\tBlueberries are from a fork\n`, + `state:\tOPEN\n`, + `author:\tnobody\n`, + `labels:\t\n`, + `assignees:\t\n`, + `projects:\t\n`, + `milestone:\t\n`, + `additions:\t100\n`, + `deletions:\t10\n`, + `auto-merge:\tenabled\thubot\tsquash\n`, + }, + }, } for name, tc := range tests { @@ -371,7 +390,7 @@ func TestPRView_Preview(t *testing.T) { `.+100.-10`, `Reviewers:.*1 \(.*Requested.*\)\n`, `Assignees:.*marseilles, monaco\n`, - `Labels:.*one, two, three, four, five\n`, + `Labels:.*Closed: Duplicate, Closed: Won't Fix, help wanted, Status: In Progress, Type: Bug\n`, `Projects:.*Project 1 \(column A\), Project 2 \(column B\), Project 3 \(column C\), Project 4 \(Awaiting triage\)\n`, `Milestone:.*uluru\n`, `blueberries taste good`, @@ -504,6 +523,20 @@ func TestPRView_Preview(t *testing.T) { `View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12`, }, }, + "PR with auto-merge enabled": { + branch: "master", + args: "12", + fixtures: map[string]string{ + "PullRequestByNumber": "./fixtures/prViewPreviewWithAutoMergeEnabled.json", + }, + expectedOutputs: []string{ + `Blueberries are from a fork #12\n`, + `Open.*nobody wants to merge 12 commits into master from blueberries . about X years ago`, + `Auto-merge:.*enabled.* by hubot, using squash and merge`, + `blueberries taste good`, + `View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12`, + }, + }, } for name, tc := range tests { diff --git a/pkg/cmd/project/close/close.go b/pkg/cmd/project/close/close.go new file mode 100644 index 000000000..f4016fe50 --- /dev/null +++ b/pkg/cmd/project/close/close.go @@ -0,0 +1,143 @@ +package close + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type closeOpts struct { + number int32 + owner string + reopen bool + projectID string + format string +} + +type closeConfig struct { + io *iostreams.IOStreams + client *queries.Client + opts closeOpts +} + +// the close command relies on the updateProjectV2 mutation +type updateProjectMutation struct { + UpdateProjectV2 struct { + ProjectV2 queries.Project `graphql:"projectV2"` + } `graphql:"updateProjectV2(input:$input)"` +} + +func NewCmdClose(f *cmdutil.Factory, runF func(config closeConfig) error) *cobra.Command { + opts := closeOpts{} + closeCmd := &cobra.Command{ + Short: "Close a project", + Use: "close []", + Example: heredoc.Doc(` + # close project "1" owned by monalisa + gh project close 1 --owner monalisa + + # reopen closed project "1" owned by github + gh project close 1 --owner github --undo + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + config := closeConfig{ + io: f.IOStreams, + client: client, + opts: opts, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runClose(config) + }, + } + + closeCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + closeCmd.Flags().BoolVar(&opts.reopen, "undo", false, "Reopen a closed project") + closeCmd.Flags().StringVar(&opts.format, "format", "", "Output format, must be 'json'") + + return closeCmd +} + +func runClose(config closeConfig) error { + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false) + if err != nil { + return err + } + config.opts.projectID = project.ID + + query, variables := closeArgs(config) + + err = config.client.Mutate("CloseProjectV2", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, *project) + } + + return printResults(config, query.UpdateProjectV2.ProjectV2) +} + +func closeArgs(config closeConfig) (*updateProjectMutation, map[string]interface{}) { + closed := !config.opts.reopen + return &updateProjectMutation{}, map[string]interface{}{ + "input": githubv4.UpdateProjectV2Input{ + ProjectID: githubv4.ID(config.opts.projectID), + Closed: githubv4.NewBoolean(githubv4.Boolean(closed)), + }, + "firstItems": githubv4.Int(0), + "afterItems": (*githubv4.String)(nil), + "firstFields": githubv4.Int(0), + "afterFields": (*githubv4.String)(nil), + } +} + +func printResults(config closeConfig, project queries.Project) error { + if !config.io.IsStdoutTTY() { + return nil + } + + _, err := fmt.Fprintf(config.io.Out, "%s\n", project.URL) + return err +} + +func printJSON(config closeConfig, project queries.Project) error { + b, err := format.JSONProject(project) + if err != nil { + return err + } + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/close/close_test.go b/pkg/cmd/project/close/close_test.go new file mode 100644 index 000000000..a19dcdabd --- /dev/null +++ b/pkg/cmd/project/close/close_test.go @@ -0,0 +1,457 @@ +package close + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdClose(t *testing.T) { + tests := []struct { + name string + cli string + wants closeOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "not-a-number", + cli: "x", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "number", + cli: "123", + wants: closeOpts{ + number: 123, + }, + }, + { + name: "owner", + cli: "--owner monalisa", + wants: closeOpts{ + owner: "monalisa", + }, + }, + { + name: "reopen", + cli: "--undo", + wants: closeOpts{ + reopen: true, + }, + }, + { + name: "json", + cli: "--format json", + wants: closeOpts{ + format: "json", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts closeOpts + cmd := NewCmdClose(f, func(config closeConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunClose_User(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get user project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]string{ + "id": "an ID", + }, + }, + }, + }) + + // close project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","closed":true}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "monalisa", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + + config := closeConfig{ + io: ios, + opts: closeOpts{ + number: 1, + owner: "monalisa", + }, + client: client, + } + + err := runClose(config) + assert.NoError(t, err) + assert.Equal( + t, + "http://a-url.com\n", + stdout.String()) +} + +func TestRunClose_Org(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // get org project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query OrgProject.*", + "variables": map[string]interface{}{ + "login": "github", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]string{ + "id": "an ID", + }, + }, + }, + }) + + // close project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","closed":true}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "monalisa", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := closeConfig{ + io: ios, + opts: closeOpts{ + number: 1, + owner: "github", + }, + client: client, + } + + err := runClose(config) + assert.NoError(t, err) + assert.Equal(t, "", stdout.String()) +} + +func TestRunClose_Me(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // get viewer project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]string{ + "id": "an ID", + }, + }, + }, + }) + + // close project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","closed":true}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "me", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := closeConfig{ + io: ios, + opts: closeOpts{ + number: 1, + owner: "@me", + }, + client: client, + } + + err := runClose(config) + assert.NoError(t, err) + assert.Equal(t, "", stdout.String()) +} + +func TestRunClose_Reopen(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get user project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]string{ + "id": "an ID", + }, + }, + }, + }) + + // close project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","closed":false}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "monalisa", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + + config := closeConfig{ + io: ios, + opts: closeOpts{ + number: 1, + owner: "monalisa", + reopen: true, + }, + client: client, + } + + err := runClose(config) + assert.NoError(t, err) + assert.Equal( + t, + "http://a-url.com\n", + stdout.String()) +} diff --git a/pkg/cmd/project/copy/copy.go b/pkg/cmd/project/copy/copy.go new file mode 100644 index 000000000..d3e07424b --- /dev/null +++ b/pkg/cmd/project/copy/copy.go @@ -0,0 +1,153 @@ +package copy + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type copyOpts struct { + includeDraftIssues bool + number int32 + ownerID string + projectID string + sourceOwner string + targetOwner string + title string + format string +} + +type copyConfig struct { + io *iostreams.IOStreams + client *queries.Client + opts copyOpts +} + +type copyProjectMutation struct { + CopyProjectV2 struct { + ProjectV2 queries.Project `graphql:"projectV2"` + } `graphql:"copyProjectV2(input:$input)"` +} + +func NewCmdCopy(f *cmdutil.Factory, runF func(config copyConfig) error) *cobra.Command { + opts := copyOpts{} + copyCmd := &cobra.Command{ + Short: "Copy a project", + Use: "copy []", + Example: heredoc.Doc(` + # copy project "1" owned by monalisa to github + gh project copy 1 --source-owner monalisa --target-owner github --title "a new project" + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + config := copyConfig{ + io: f.IOStreams, + client: client, + opts: opts, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runCopy(config) + }, + } + + copyCmd.Flags().StringVar(&opts.sourceOwner, "source-owner", "", "Login of the source owner. Use \"@me\" for the current user.") + copyCmd.Flags().StringVar(&opts.targetOwner, "target-owner", "", "Login of the target owner. Use \"@me\" for the current user.") + copyCmd.Flags().StringVar(&opts.title, "title", "", "Title for the new project") + copyCmd.Flags().BoolVar(&opts.includeDraftIssues, "drafts", false, "Include draft issues when copying") + cmdutil.StringEnumFlag(copyCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + _ = copyCmd.MarkFlagRequired("title") + + return copyCmd +} + +func runCopy(config copyConfig) error { + canPrompt := config.io.CanPrompt() + sourceOwner, err := config.client.NewOwner(canPrompt, config.opts.sourceOwner) + if err != nil { + return err + } + + targetOwner, err := config.client.NewOwner(canPrompt, config.opts.targetOwner) + if err != nil { + return err + } + + project, err := config.client.NewProject(canPrompt, sourceOwner, config.opts.number, false) + if err != nil { + return err + } + + config.opts.projectID = project.ID + config.opts.ownerID = targetOwner.ID + + query, variables := copyArgs(config) + + err = config.client.Mutate("CopyProjectV2", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, query.CopyProjectV2.ProjectV2) + } + + return printResults(config, query.CopyProjectV2.ProjectV2) +} + +func copyArgs(config copyConfig) (*copyProjectMutation, map[string]interface{}) { + return ©ProjectMutation{}, map[string]interface{}{ + "input": githubv4.CopyProjectV2Input{ + OwnerID: githubv4.ID(config.opts.ownerID), + ProjectID: githubv4.ID(config.opts.projectID), + Title: githubv4.String(config.opts.title), + IncludeDraftIssues: githubv4.NewBoolean(githubv4.Boolean(config.opts.includeDraftIssues)), + }, + "firstItems": githubv4.Int(0), + "afterItems": (*githubv4.String)(nil), + "firstFields": githubv4.Int(0), + "afterFields": (*githubv4.String)(nil), + } +} + +func printResults(config copyConfig, project queries.Project) error { + if !config.io.IsStdoutTTY() { + return nil + } + _, err := fmt.Fprintf(config.io.Out, "%s\n", project.URL) + return err +} + +func printJSON(config copyConfig, project queries.Project) error { + b, err := format.JSONProject(project) + if err != nil { + return err + } + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/copy/copy_test.go b/pkg/cmd/project/copy/copy_test.go new file mode 100644 index 000000000..d8abf5ec9 --- /dev/null +++ b/pkg/cmd/project/copy/copy_test.go @@ -0,0 +1,462 @@ +package copy + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdCopy(t *testing.T) { + tests := []struct { + name string + cli string + wants copyOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "not-a-number", + cli: "x --title t", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "title", + cli: "--title t", + wants: copyOpts{ + title: "t", + }, + }, + { + name: "number", + cli: "123 --title t", + wants: copyOpts{ + number: 123, + title: "t", + }, + }, + { + name: "source-owner", + cli: "--source-owner monalisa --title t", + wants: copyOpts{ + sourceOwner: "monalisa", + title: "t", + }, + }, + { + name: "target-owner", + cli: "--target-owner monalisa --title t", + wants: copyOpts{ + targetOwner: "monalisa", + title: "t", + }, + }, + { + name: "drafts", + cli: "--drafts --title t", + wants: copyOpts{ + includeDraftIssues: true, + title: "t", + }, + }, + { + name: "json", + cli: "--format json --title t", + wants: copyOpts{ + format: "json", + title: "t", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts copyOpts + cmd := NewCmdCopy(f, func(config copyConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.sourceOwner, gotOpts.sourceOwner) + assert.Equal(t, tt.wants.targetOwner, gotOpts.targetOwner) + assert.Equal(t, tt.wants.title, gotOpts.title) + assert.Equal(t, tt.wants.includeDraftIssues, gotOpts.includeDraftIssues) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunCopy_User(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get user project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]string{ + "id": "an ID", + }, + }, + }, + }) + + // get source user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]string{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + "login": "monalisa", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get target user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]string{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + "login": "monalisa", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // Copy project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CopyProjectV2.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","ownerId":"an ID","title":"a title","includeDraftIssues":false}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "copyProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "monalisa", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(false) + + config := copyConfig{ + io: ios, + opts: copyOpts{ + title: "a title", + sourceOwner: "monalisa", + targetOwner: "monalisa", + number: 1, + }, + client: client, + } + + err := runCopy(config) + assert.NoError(t, err) + assert.Equal(t, "", stdout.String()) +} + +func TestRunCopy_Org(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get org project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query OrgProject.*", + "variables": map[string]interface{}{ + "login": "github", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]string{ + "id": "an ID", + }, + }, + }, + }) + // get source org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]string{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + "login": "github", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // get target source org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]string{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + "login": "github", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // Copy project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CopyProjectV2.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","ownerId":"an ID","title":"a title","includeDraftIssues":false}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "copyProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "monalisa", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(false) + + config := copyConfig{ + io: ios, + opts: copyOpts{ + title: "a title", + sourceOwner: "github", + targetOwner: "github", + number: 1, + }, + client: client, + } + + err := runCopy(config) + assert.NoError(t, err) + assert.Equal(t, "", stdout.String()) +} + +func TestRunCopy_Me(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get viewer project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]string{ + "id": "an ID", + }, + }, + }, + }) + + // get source viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + "login": "me", + }, + }, + }) + + // get target viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + "login": "me", + }, + }, + }) + + // Copy project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CopyProjectV2.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","ownerId":"an ID","title":"a title","includeDraftIssues":false}}}`).Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "copyProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "me", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + + config := copyConfig{ + io: ios, + opts: copyOpts{ + title: "a title", + sourceOwner: "@me", + targetOwner: "@me", + number: 1, + }, + client: client, + } + + err := runCopy(config) + assert.NoError(t, err) + assert.Equal( + t, + "http://a-url.com\n", + stdout.String()) +} diff --git a/pkg/cmd/project/create/create.go b/pkg/cmd/project/create/create.go new file mode 100644 index 000000000..894a6e9ce --- /dev/null +++ b/pkg/cmd/project/create/create.go @@ -0,0 +1,125 @@ +package create + +import ( + "fmt" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type createOpts struct { + title string + owner string + ownerID string + format string +} + +type createConfig struct { + client *queries.Client + opts createOpts + io *iostreams.IOStreams +} + +type createProjectMutation struct { + CreateProjectV2 struct { + ProjectV2 queries.Project `graphql:"projectV2"` + } `graphql:"createProjectV2(input:$input)"` +} + +func NewCmdCreate(f *cmdutil.Factory, runF func(config createConfig) error) *cobra.Command { + opts := createOpts{} + createCmd := &cobra.Command{ + Short: "Create a project", + Use: "create", + Example: heredoc.Doc(` + # create a new project owned by login monalisa + gh project create --owner monalisa --title "a new project" + `), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + config := createConfig{ + client: client, + opts: opts, + io: f.IOStreams, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runCreate(config) + }, + } + + createCmd.Flags().StringVar(&opts.title, "title", "", "Title for the project") + createCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + cmdutil.StringEnumFlag(createCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + _ = createCmd.MarkFlagRequired("title") + + return createCmd +} + +func runCreate(config createConfig) error { + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + config.opts.ownerID = owner.ID + query, variables := createArgs(config) + + err = config.client.Mutate("CreateProjectV2", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, query.CreateProjectV2.ProjectV2) + } + + return printResults(config, query.CreateProjectV2.ProjectV2) +} + +func createArgs(config createConfig) (*createProjectMutation, map[string]interface{}) { + return &createProjectMutation{}, map[string]interface{}{ + "input": githubv4.CreateProjectV2Input{ + OwnerID: githubv4.ID(config.opts.ownerID), + Title: githubv4.String(config.opts.title), + }, + "firstItems": githubv4.Int(0), + "afterItems": (*githubv4.String)(nil), + "firstFields": githubv4.Int(0), + "afterFields": (*githubv4.String)(nil), + } +} + +func printResults(config createConfig, project queries.Project) error { + if !config.io.IsStdoutTTY() { + return nil + } + + _, err := fmt.Fprintf(config.io.Out, "%s\n", project.URL) + return err +} + +func printJSON(config createConfig, project queries.Project) error { + b, err := format.JSONProject(project) + if err != nil { + return err + } + + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/create/create_test.go b/pkg/cmd/project/create/create_test.go new file mode 100644 index 000000000..3ef773cb9 --- /dev/null +++ b/pkg/cmd/project/create/create_test.go @@ -0,0 +1,278 @@ +package create + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdCreate(t *testing.T) { + tests := []struct { + name string + cli string + wants createOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "title", + cli: "--title t", + wants: createOpts{ + title: "t", + }, + }, + { + name: "owner", + cli: "--title t --owner monalisa", + wants: createOpts{ + owner: "monalisa", + title: "t", + }, + }, + { + name: "json", + cli: "--title t --format json", + wants: createOpts{ + format: "json", + title: "t", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts createOpts + cmd := NewCmdCreate(f, func(config createConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.title, gotOpts.title) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunCreate_User(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]string{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + "login": "monalisa", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // create project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"ownerId":"an ID","title":"a title"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "createProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "monalisa", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createConfig{ + opts: createOpts{ + title: "a title", + owner: "monalisa", + }, + client: client, + io: ios, + } + + err := runCreate(config) + assert.NoError(t, err) + assert.Equal( + t, + "http://a-url.com\n", + stdout.String()) +} + +func TestRunCreate_Org(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]string{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + "login": "github", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // create project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"ownerId":"an ID","title":"a title"}}}`).Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "createProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "monalisa", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createConfig{ + opts: createOpts{ + title: "a title", + owner: "github", + }, + client: client, + io: ios, + } + + err := runCreate(config) + assert.NoError(t, err) + assert.Equal( + t, + "http://a-url.com\n", + stdout.String()) +} + +func TestRunCreate_Me(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + "login": "me", + }, + }, + }) + + // create project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"ownerId":"an ID","title":"a title"}}}`).Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "createProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "me", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createConfig{ + opts: createOpts{ + title: "a title", + owner: "@me", + }, + client: client, + io: ios, + } + + err := runCreate(config) + assert.NoError(t, err) + assert.Equal( + t, + "http://a-url.com\n", + stdout.String()) +} diff --git a/pkg/cmd/project/delete/delete.go b/pkg/cmd/project/delete/delete.go new file mode 100644 index 000000000..674c30a97 --- /dev/null +++ b/pkg/cmd/project/delete/delete.go @@ -0,0 +1,136 @@ +package delete + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type deleteOpts struct { + owner string + number int32 + projectID string + format string +} + +type deleteConfig struct { + client *queries.Client + opts deleteOpts + io *iostreams.IOStreams +} + +type deleteProjectMutation struct { + DeleteProject struct { + Project queries.Project `graphql:"projectV2"` + } `graphql:"deleteProjectV2(input:$input)"` +} + +func NewCmdDelete(f *cmdutil.Factory, runF func(config deleteConfig) error) *cobra.Command { + opts := deleteOpts{} + deleteCmd := &cobra.Command{ + Short: "Delete a project", + Use: "delete []", + Example: heredoc.Doc(` + # delete the current user's project "1" + gh project delete 1 --owner "@me" + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + config := deleteConfig{ + client: client, + opts: opts, + io: f.IOStreams, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runDelete(config) + }, + } + + deleteCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + cmdutil.StringEnumFlag(deleteCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + return deleteCmd +} + +func runDelete(config deleteConfig) error { + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false) + if err != nil { + return err + } + config.opts.projectID = project.ID + + query, variables := deleteItemArgs(config) + err = config.client.Mutate("DeleteProject", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, *project) + } + + return printResults(config, query.DeleteProject.Project) + +} + +func deleteItemArgs(config deleteConfig) (*deleteProjectMutation, map[string]interface{}) { + return &deleteProjectMutation{}, map[string]interface{}{ + "input": githubv4.DeleteProjectV2Input{ + ProjectID: githubv4.ID(config.opts.projectID), + }, + "firstItems": githubv4.Int(0), + "afterItems": (*githubv4.String)(nil), + "firstFields": githubv4.Int(0), + "afterFields": (*githubv4.String)(nil), + } +} + +func printResults(config deleteConfig, project queries.Project) error { + if !config.io.IsStdoutTTY() { + return nil + } + + _, err := fmt.Fprintf(config.io.Out, "Deleted project %d\n", project.Number) + return err +} + +func printJSON(config deleteConfig, project queries.Project) error { + b, err := format.JSONProject(project) + if err != nil { + return err + } + + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/delete/delete_test.go b/pkg/cmd/project/delete/delete_test.go new file mode 100644 index 000000000..a27180266 --- /dev/null +++ b/pkg/cmd/project/delete/delete_test.go @@ -0,0 +1,351 @@ +package delete + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdDelete(t *testing.T) { + tests := []struct { + name string + cli string + wants deleteOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "not-a-number", + cli: "x", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "number", + cli: "123", + wants: deleteOpts{ + number: 123, + }, + }, + { + name: "owner", + cli: "--owner monalisa", + wants: deleteOpts{ + owner: "monalisa", + }, + }, + { + name: "json", + cli: "--format json", + wants: deleteOpts{ + format: "json", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts deleteOpts + cmd := NewCmdDelete(f, func(config deleteConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunDelete_User(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // delete project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation DeleteProject.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "deleteProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "project ID", + "number": 1, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := deleteConfig{ + opts: deleteOpts{ + owner: "monalisa", + number: 1, + }, + client: client, + io: ios, + } + + err := runDelete(config) + assert.NoError(t, err) + assert.Equal( + t, + "Deleted project 1\n", + stdout.String()) +} + +func TestRunDelete_Org(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query OrgProject.*", + "variables": map[string]interface{}{ + "login": "github", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // delete project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation DeleteProject.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "deleteProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "project ID", + "number": 1, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := deleteConfig{ + opts: deleteOpts{ + owner: "github", + number: 1, + }, + client: client, + io: ios, + } + + err := runDelete(config) + assert.NoError(t, err) + assert.Equal( + t, + "Deleted project 1\n", + stdout.String()) +} + +func TestRunDelete_Me(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // delete project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation DeleteProject.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "deleteProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "project ID", + "number": 1, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := deleteConfig{ + opts: deleteOpts{ + owner: "@me", + number: 1, + }, + client: client, + io: ios, + } + + err := runDelete(config) + assert.NoError(t, err) + assert.Equal( + t, + "Deleted project 1\n", + stdout.String()) +} diff --git a/pkg/cmd/project/edit/edit.go b/pkg/cmd/project/edit/edit.go new file mode 100644 index 000000000..92fee3284 --- /dev/null +++ b/pkg/cmd/project/edit/edit.go @@ -0,0 +1,165 @@ +package edit + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type editOpts struct { + number int32 + owner string + title string + readme string + visibility string + shortDescription string + projectID string + format string +} + +type editConfig struct { + client *queries.Client + opts editOpts + io *iostreams.IOStreams +} + +type updateProjectMutation struct { + UpdateProjectV2 struct { + ProjectV2 queries.Project `graphql:"projectV2"` + } `graphql:"updateProjectV2(input:$input)"` +} + +const projectVisibilityPublic = "PUBLIC" +const projectVisibilityPrivate = "PRIVATE" + +func NewCmdEdit(f *cmdutil.Factory, runF func(config editConfig) error) *cobra.Command { + opts := editOpts{} + editCmd := &cobra.Command{ + Short: "Edit a project", + Use: "edit []", + Example: heredoc.Doc(` + # edit the title of monalisa's project "1" + gh project edit 1 --owner monalisa --title "New title" + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + config := editConfig{ + client: client, + opts: opts, + io: f.IOStreams, + } + + if config.opts.title == "" && config.opts.shortDescription == "" && config.opts.readme == "" && config.opts.visibility == "" { + return fmt.Errorf("no fields to edit") + } + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runEdit(config) + }, + } + + editCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + cmdutil.StringEnumFlag(editCmd, &opts.visibility, "visibility", "", "", []string{projectVisibilityPublic, projectVisibilityPrivate}, "Change project visibility") + editCmd.Flags().StringVar(&opts.title, "title", "", "New title for the project") + editCmd.Flags().StringVar(&opts.readme, "readme", "", "New readme for the project") + editCmd.Flags().StringVarP(&opts.shortDescription, "description", "d", "", "New description of the project") + cmdutil.StringEnumFlag(editCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + return editCmd +} + +func runEdit(config editConfig) error { + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false) + if err != nil { + return err + } + config.opts.projectID = project.ID + + query, variables := editArgs(config) + err = config.client.Mutate("UpdateProjectV2", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, *project) + } + + return printResults(config, query.UpdateProjectV2.ProjectV2) +} + +func editArgs(config editConfig) (*updateProjectMutation, map[string]interface{}) { + variables := githubv4.UpdateProjectV2Input{ProjectID: githubv4.ID(config.opts.projectID)} + if config.opts.title != "" { + variables.Title = githubv4.NewString(githubv4.String(config.opts.title)) + } + if config.opts.shortDescription != "" { + variables.ShortDescription = githubv4.NewString(githubv4.String(config.opts.shortDescription)) + } + if config.opts.readme != "" { + variables.Readme = githubv4.NewString(githubv4.String(config.opts.readme)) + } + if config.opts.visibility != "" { + if config.opts.visibility == projectVisibilityPublic { + variables.Public = githubv4.NewBoolean(githubv4.Boolean(true)) + } else if config.opts.visibility == projectVisibilityPrivate { + variables.Public = githubv4.NewBoolean(githubv4.Boolean(false)) + } + } + + return &updateProjectMutation{}, map[string]interface{}{ + "input": variables, + "firstItems": githubv4.Int(0), + "afterItems": (*githubv4.String)(nil), + "firstFields": githubv4.Int(0), + "afterFields": (*githubv4.String)(nil), + } +} + +func printResults(config editConfig, project queries.Project) error { + if !config.io.IsStdoutTTY() { + return nil + } + + _, err := fmt.Fprintf(config.io.Out, "%s\n", project.URL) + return err +} + +func printJSON(config editConfig, project queries.Project) error { + b, err := format.JSONProject(project) + if err != nil { + return err + } + + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/edit/edit_test.go b/pkg/cmd/project/edit/edit_test.go new file mode 100644 index 000000000..605c5f5cc --- /dev/null +++ b/pkg/cmd/project/edit/edit_test.go @@ -0,0 +1,512 @@ +package edit + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdEdit(t *testing.T) { + tests := []struct { + name string + cli string + wants editOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "not-a-number", + cli: "x", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "visibility-error", + cli: "--visibility v", + wantsErr: true, + wantsErrMsg: "invalid argument \"v\" for \"--visibility\" flag: valid values are {PUBLIC|PRIVATE}", + }, + { + name: "no-args", + cli: "", + wantsErr: true, + wantsErrMsg: "no fields to edit", + }, + { + name: "title", + cli: "--title t", + wants: editOpts{ + title: "t", + }, + }, + { + name: "number", + cli: "123 --title t", + wants: editOpts{ + number: 123, + title: "t", + }, + }, + { + name: "owner", + cli: "--owner monalisa --title t", + wants: editOpts{ + owner: "monalisa", + title: "t", + }, + }, + { + name: "readme", + cli: "--readme r", + wants: editOpts{ + readme: "r", + }, + }, + { + name: "description", + cli: "--description d", + wants: editOpts{ + shortDescription: "d", + }, + }, + { + name: "visibility", + cli: "--visibility PUBLIC", + wants: editOpts{ + visibility: "PUBLIC", + }, + }, + { + name: "json", + cli: "--format json --title t", + wants: editOpts{ + format: "json", + title: "t", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts editOpts + cmd := NewCmdEdit(f, func(config editConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.visibility, gotOpts.visibility) + assert.Equal(t, tt.wants.title, gotOpts.title) + assert.Equal(t, tt.wants.readme, gotOpts.readme) + assert.Equal(t, tt.wants.shortDescription, gotOpts.shortDescription) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunUpdate_User(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get user project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]string{ + "id": "an ID", + }, + }, + }, + }) + + // edit project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UpdateProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","title":"a new title","shortDescription":"a new description","readme":"a new readme","public":true}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "monalisa", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := editConfig{ + opts: editOpts{ + number: 1, + owner: "monalisa", + title: "a new title", + shortDescription: "a new description", + visibility: "PUBLIC", + readme: "a new readme", + }, + client: client, + io: ios, + } + + err := runEdit(config) + assert.NoError(t, err) + assert.Equal( + t, + "http://a-url.com\n", + stdout.String()) +} + +func TestRunUpdate_Org(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // get org project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query OrgProject.*", + "variables": map[string]interface{}{ + "login": "github", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]string{ + "id": "an ID", + }, + }, + }, + }) + + // edit project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UpdateProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","title":"a new title","shortDescription":"a new description","readme":"a new readme","public":true}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "monalisa", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := editConfig{ + opts: editOpts{ + number: 1, + owner: "github", + title: "a new title", + shortDescription: "a new description", + visibility: "PUBLIC", + readme: "a new readme", + }, + client: client, + io: ios, + } + + err := runEdit(config) + assert.NoError(t, err) + assert.Equal( + t, + "http://a-url.com\n", + stdout.String()) +} + +func TestRunUpdate_Me(t *testing.T) { + defer gock.Off() + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + gock.Observe(gock.DumpRequest) + // get viewer project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]string{ + "id": "an ID", + }, + }, + }, + }) + + // edit project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UpdateProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","title":"a new title","shortDescription":"a new description","readme":"a new readme","public":false}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "me", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := editConfig{ + opts: editOpts{ + number: 1, + owner: "@me", + title: "a new title", + shortDescription: "a new description", + visibility: "PRIVATE", + readme: "a new readme", + }, + client: client, + io: ios, + } + + err := runEdit(config) + assert.NoError(t, err) + assert.Equal( + t, + "http://a-url.com\n", + stdout.String()) +} + +func TestRunUpdate_OmitParams(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get user project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]string{ + "id": "an ID", + }, + }, + }, + }) + + // Update project + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UpdateProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","title":"another title"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "title": "a title", + "url": "http://a-url.com", + "owner": map[string]string{ + "login": "monalisa", + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := editConfig{ + opts: editOpts{ + number: 1, + owner: "monalisa", + title: "another title", + }, + client: client, + io: ios, + } + + err := runEdit(config) + assert.NoError(t, err) + assert.Equal( + t, + "http://a-url.com\n", + stdout.String()) +} diff --git a/pkg/cmd/project/field-create/field_create.go b/pkg/cmd/project/field-create/field_create.go new file mode 100644 index 000000000..d369148ba --- /dev/null +++ b/pkg/cmd/project/field-create/field_create.go @@ -0,0 +1,163 @@ +package fieldcreate + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type createFieldOpts struct { + name string + dataType string + owner string + singleSelectOptions []string + number int32 + projectID string + format string +} + +type createFieldConfig struct { + client *queries.Client + opts createFieldOpts + io *iostreams.IOStreams +} + +type createProjectV2FieldMutation struct { + CreateProjectV2Field struct { + Field queries.ProjectField `graphql:"projectV2Field"` + } `graphql:"createProjectV2Field(input:$input)"` +} + +func NewCmdCreateField(f *cmdutil.Factory, runF func(config createFieldConfig) error) *cobra.Command { + opts := createFieldOpts{} + createFieldCmd := &cobra.Command{ + Short: "Create a field in a project", + Use: "field-create []", + Example: heredoc.Doc(` + # create a field in the current user's project "1" + gh project field-create 1 --owner "@me" --name "new field" --data-type "text" + + # create a field with three options to select from for owner monalisa + gh project field-create 1 --owner monalisa --name "new field" --data-type "SINGLE_SELECT" --single-select-options "one,two,three" + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + config := createFieldConfig{ + client: client, + opts: opts, + io: f.IOStreams, + } + + if config.opts.dataType == "SINGLE_SELECT" && len(config.opts.singleSelectOptions) == 0 { + return fmt.Errorf("passing `--single-select-options` is required for SINGLE_SELECT data type") + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runCreateField(config) + }, + } + + createFieldCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + createFieldCmd.Flags().StringVar(&opts.name, "name", "", "Name of the new field") + cmdutil.StringEnumFlag(createFieldCmd, &opts.dataType, "data-type", "", "", []string{"TEXT", "SINGLE_SELECT", "DATE", "NUMBER"}, "DataType of the new field.") + createFieldCmd.Flags().StringSliceVar(&opts.singleSelectOptions, "single-select-options", []string{}, "Options for SINGLE_SELECT data type") + cmdutil.StringEnumFlag(createFieldCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + _ = createFieldCmd.MarkFlagRequired("name") + _ = createFieldCmd.MarkFlagRequired("data-type") + + return createFieldCmd +} + +func runCreateField(config createFieldConfig) error { + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false) + if err != nil { + return err + } + config.opts.projectID = project.ID + + query, variables := createFieldArgs(config) + + err = config.client.Mutate("CreateField", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, query.CreateProjectV2Field.Field) + } + + return printResults(config, query.CreateProjectV2Field.Field) +} + +func createFieldArgs(config createFieldConfig) (*createProjectV2FieldMutation, map[string]interface{}) { + input := githubv4.CreateProjectV2FieldInput{ + ProjectID: githubv4.ID(config.opts.projectID), + DataType: githubv4.ProjectV2CustomFieldType(config.opts.dataType), + Name: githubv4.String(config.opts.name), + } + + if len(config.opts.singleSelectOptions) != 0 { + opts := make([]githubv4.ProjectV2SingleSelectFieldOptionInput, 0) + for _, opt := range config.opts.singleSelectOptions { + opts = append(opts, githubv4.ProjectV2SingleSelectFieldOptionInput{ + Name: githubv4.String(opt), + Color: githubv4.ProjectV2SingleSelectFieldOptionColor("GRAY"), + }) + } + input.SingleSelectOptions = &opts + } + + return &createProjectV2FieldMutation{}, map[string]interface{}{ + "input": input, + } +} + +func printResults(config createFieldConfig, field queries.ProjectField) error { + if !config.io.IsStdoutTTY() { + return nil + } + + _, err := fmt.Fprintf(config.io.Out, "Created field\n") + return err +} + +func printJSON(config createFieldConfig, field queries.ProjectField) error { + b, err := format.JSONProjectField(field) + if err != nil { + return err + } + + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/field-create/field_create_test.go b/pkg/cmd/project/field-create/field_create_test.go new file mode 100644 index 000000000..31328a04f --- /dev/null +++ b/pkg/cmd/project/field-create/field_create_test.go @@ -0,0 +1,632 @@ +package fieldcreate + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdCreateField(t *testing.T) { + tests := []struct { + name string + cli string + wants createFieldOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "missing-name-and-data-type", + cli: "", + wantsErr: true, + wantsErrMsg: "required flag(s) \"data-type\", \"name\" not set", + }, + { + name: "not-a-number", + cli: "x --name n --data-type TEXT", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "single-select-no-options", + cli: "123 --name n --data-type SINGLE_SELECT", + wantsErr: true, + wantsErrMsg: "passing `--single-select-options` is required for SINGLE_SELECT data type", + }, + { + name: "number", + cli: "123 --name n --data-type TEXT", + wants: createFieldOpts{ + number: 123, + name: "n", + dataType: "TEXT", + singleSelectOptions: []string{}, + }, + }, + { + name: "owner", + cli: "--owner monalisa --name n --data-type TEXT", + wants: createFieldOpts{ + owner: "monalisa", + name: "n", + dataType: "TEXT", + singleSelectOptions: []string{}, + }, + }, + { + name: "single-select-options", + cli: "--name n --data-type TEXT --single-select-options a,b", + wants: createFieldOpts{ + singleSelectOptions: []string{"a", "b"}, + name: "n", + dataType: "TEXT", + }, + }, + { + name: "json", + cli: "--format json --name n --data-type TEXT ", + wants: createFieldOpts{ + format: "json", + name: "n", + dataType: "TEXT", + singleSelectOptions: []string{}, + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts createFieldOpts + cmd := NewCmdCreateField(f, func(config createFieldConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.name, gotOpts.name) + assert.Equal(t, tt.wants.dataType, gotOpts.dataType) + assert.Equal(t, tt.wants.singleSelectOptions, gotOpts.singleSelectOptions) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunCreateField_User(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // create Field + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateField.*","variables":{"input":{"projectId":"an ID","dataType":"TEXT","name":"a name"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "createProjectV2Field": map[string]interface{}{ + "projectV2Field": map[string]interface{}{ + "id": "Field ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createFieldConfig{ + opts: createFieldOpts{ + name: "a name", + owner: "monalisa", + number: 1, + dataType: "TEXT", + }, + client: client, + io: ios, + } + + err := runCreateField(config) + assert.NoError(t, err) + assert.Equal( + t, + "Created field\n", + stdout.String()) +} + +func TestRunCreateField_Org(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query OrgProject.*", + "variables": map[string]interface{}{ + "login": "github", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // create Field + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateField.*","variables":{"input":{"projectId":"an ID","dataType":"TEXT","name":"a name"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "createProjectV2Field": map[string]interface{}{ + "projectV2Field": map[string]interface{}{ + "id": "Field ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createFieldConfig{ + opts: createFieldOpts{ + name: "a name", + owner: "github", + number: 1, + dataType: "TEXT", + }, + client: client, + io: ios, + } + + err := runCreateField(config) + assert.NoError(t, err) + assert.Equal( + t, + "Created field\n", + stdout.String()) +} + +func TestRunCreateField_Me(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // create Field + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateField.*","variables":{"input":{"projectId":"an ID","dataType":"TEXT","name":"a name"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "createProjectV2Field": map[string]interface{}{ + "projectV2Field": map[string]interface{}{ + "id": "Field ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createFieldConfig{ + opts: createFieldOpts{ + owner: "@me", + number: 1, + name: "a name", + dataType: "TEXT", + }, + client: client, + io: ios, + } + + err := runCreateField(config) + assert.NoError(t, err) + assert.Equal( + t, + "Created field\n", + stdout.String()) +} + +func TestRunCreateField_TEXT(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // create Field + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateField.*","variables":{"input":{"projectId":"an ID","dataType":"TEXT","name":"a name"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "createProjectV2Field": map[string]interface{}{ + "projectV2Field": map[string]interface{}{ + "id": "Field ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createFieldConfig{ + opts: createFieldOpts{ + owner: "@me", + number: 1, + name: "a name", + dataType: "TEXT", + }, + client: client, + io: ios, + } + + err := runCreateField(config) + assert.NoError(t, err) + assert.Equal( + t, + "Created field\n", + stdout.String()) +} + +func TestRunCreateField_DATE(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // create Field + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateField.*","variables":{"input":{"projectId":"an ID","dataType":"DATE","name":"a name"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "createProjectV2Field": map[string]interface{}{ + "projectV2Field": map[string]interface{}{ + "id": "Field ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createFieldConfig{ + opts: createFieldOpts{ + owner: "@me", + number: 1, + name: "a name", + dataType: "DATE", + }, + client: client, + io: ios, + } + + err := runCreateField(config) + assert.NoError(t, err) + assert.Equal( + t, + "Created field\n", + stdout.String()) +} + +func TestRunCreateField_NUMBER(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // create Field + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateField.*","variables":{"input":{"projectId":"an ID","dataType":"NUMBER","name":"a name"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "createProjectV2Field": map[string]interface{}{ + "projectV2Field": map[string]interface{}{ + "id": "Field ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createFieldConfig{ + opts: createFieldOpts{ + owner: "@me", + number: 1, + name: "a name", + dataType: "NUMBER", + }, + client: client, + io: ios, + } + + err := runCreateField(config) + assert.NoError(t, err) + assert.Equal( + t, + "Created field\n", + stdout.String()) +} diff --git a/pkg/cmd/project/field-delete/field_delete.go b/pkg/cmd/project/field-delete/field_delete.go new file mode 100644 index 000000000..3d3bb25ce --- /dev/null +++ b/pkg/cmd/project/field-delete/field_delete.go @@ -0,0 +1,105 @@ +package fielddelete + +import ( + "fmt" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type deleteFieldOpts struct { + fieldID string + format string +} + +type deleteFieldConfig struct { + client *queries.Client + opts deleteFieldOpts + io *iostreams.IOStreams +} + +type deleteProjectV2FieldMutation struct { + DeleteProjectV2Field struct { + Field queries.ProjectField `graphql:"projectV2Field"` + } `graphql:"deleteProjectV2Field(input:$input)"` +} + +func NewCmdDeleteField(f *cmdutil.Factory, runF func(config deleteFieldConfig) error) *cobra.Command { + opts := deleteFieldOpts{} + deleteFieldCmd := &cobra.Command{ + Short: "Delete a field in a project", + Use: "field-delete", + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + config := deleteFieldConfig{ + client: client, + opts: opts, + io: f.IOStreams, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runDeleteField(config) + }, + } + + deleteFieldCmd.Flags().StringVar(&opts.fieldID, "id", "", "ID of the field to delete") + cmdutil.StringEnumFlag(deleteFieldCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + _ = deleteFieldCmd.MarkFlagRequired("id") + + return deleteFieldCmd +} + +func runDeleteField(config deleteFieldConfig) error { + query, variables := deleteFieldArgs(config) + + err := config.client.Mutate("DeleteField", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, query.DeleteProjectV2Field.Field) + } + + return printResults(config, query.DeleteProjectV2Field.Field) +} + +func deleteFieldArgs(config deleteFieldConfig) (*deleteProjectV2FieldMutation, map[string]interface{}) { + return &deleteProjectV2FieldMutation{}, map[string]interface{}{ + "input": githubv4.DeleteProjectV2FieldInput{ + FieldID: githubv4.ID(config.opts.fieldID), + }, + } +} + +func printResults(config deleteFieldConfig, field queries.ProjectField) error { + if !config.io.IsStdoutTTY() { + return nil + } + + _, err := fmt.Fprintf(config.io.Out, "Deleted field\n") + return err +} + +func printJSON(config deleteFieldConfig, field queries.ProjectField) error { + b, err := format.JSONProjectField(field) + if err != nil { + return err + } + + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/field-delete/field_delete_test.go b/pkg/cmd/project/field-delete/field_delete_test.go new file mode 100644 index 000000000..61783d44d --- /dev/null +++ b/pkg/cmd/project/field-delete/field_delete_test.go @@ -0,0 +1,117 @@ +package fielddelete + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdDeleteField(t *testing.T) { + tests := []struct { + name string + cli string + wants deleteFieldOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "no id", + cli: "", + wantsErr: true, + wantsErrMsg: "required flag(s) \"id\" not set", + }, + { + name: "id", + cli: "--id 123", + wants: deleteFieldOpts{ + fieldID: "123", + }, + }, + { + name: "json", + cli: "--id 123 --format json", + wants: deleteFieldOpts{ + format: "json", + fieldID: "123", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts deleteFieldOpts + cmd := NewCmdDeleteField(f, func(config deleteFieldConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.fieldID, gotOpts.fieldID) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunDeleteField(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // delete Field + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation DeleteField.*","variables":{"input":{"fieldId":"an ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "deleteProjectV2Field": map[string]interface{}{ + "projectV2Field": map[string]interface{}{ + "id": "Field ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := deleteFieldConfig{ + opts: deleteFieldOpts{ + fieldID: "an ID", + }, + client: client, + io: ios, + } + + err := runDeleteField(config) + assert.NoError(t, err) + assert.Equal( + t, + "Deleted field\n", + stdout.String()) +} diff --git a/pkg/cmd/project/field-list/field_list.go b/pkg/cmd/project/field-list/field_list.go new file mode 100644 index 000000000..152d90fcd --- /dev/null +++ b/pkg/cmd/project/field-list/field_list.go @@ -0,0 +1,132 @@ +package fieldlist + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/tableprinter" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/spf13/cobra" +) + +type listOpts struct { + limit int + owner string + number int32 + format string +} + +type listConfig struct { + io *iostreams.IOStreams + tp *tableprinter.TablePrinter + client *queries.Client + opts listOpts +} + +func NewCmdList(f *cmdutil.Factory, runF func(config listConfig) error) *cobra.Command { + opts := listOpts{} + listCmd := &cobra.Command{ + Short: "List the fields in a project", + Use: "field-list number", + Example: heredoc.Doc(` + # list fields in the current user's project "1" + gh project field-list 1 --owner "@me" + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + t := tableprinter.New(f.IOStreams) + config := listConfig{ + io: f.IOStreams, + tp: t, + client: client, + opts: opts, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runList(config) + }, + } + + listCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + cmdutil.StringEnumFlag(listCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + listCmd.Flags().IntVarP(&opts.limit, "limit", "L", queries.LimitDefault, "Maximum number of fields to fetch") + + return listCmd +} + +func runList(config listConfig) error { + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + // no need to fetch the project if we already have the number + if config.opts.number == 0 { + canPrompt := config.io.CanPrompt() + project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false) + if err != nil { + return err + } + config.opts.number = project.Number + } + + project, err := config.client.ProjectFields(owner, config.opts.number, config.opts.limit) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, project) + } + + return printResults(config, project.Fields.Nodes, owner.Login) +} + +func printResults(config listConfig, fields []queries.ProjectField, login string) error { + if len(fields) == 0 { + return cmdutil.NewNoResultsError(fmt.Sprintf("Project %d for owner %s has no fields", config.opts.number, login)) + } + + config.tp.HeaderRow("Name", "Data type", "ID") + + for _, f := range fields { + config.tp.AddField(f.Name()) + config.tp.AddField(f.Type()) + config.tp.AddField(f.ID(), tableprinter.WithTruncate(nil)) + config.tp.EndRow() + } + + return config.tp.Render() +} + +func printJSON(config listConfig, project *queries.Project) error { + b, err := format.JSONProjectFields(project) + if err != nil { + return err + } + + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/field-list/field_list_test.go b/pkg/cmd/project/field-list/field_list_test.go new file mode 100644 index 000000000..60e61188d --- /dev/null +++ b/pkg/cmd/project/field-list/field_list_test.go @@ -0,0 +1,425 @@ +package fieldlist + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/internal/tableprinter" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdList(t *testing.T) { + tests := []struct { + name string + cli string + wants listOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "not-a-number", + cli: "x", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "number", + cli: "123", + wants: listOpts{ + number: 123, + limit: 30, + }, + }, + { + name: "owner", + cli: "--owner monalisa", + wants: listOpts{ + owner: "monalisa", + limit: 30, + }, + }, + { + name: "json", + cli: "--format json", + wants: listOpts{ + format: "json", + limit: 30, + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts listOpts + cmd := NewCmdList(f, func(config listConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Equal(t, tt.wantsErrMsg, err.Error()) + assert.Error(t, err) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.limit, gotOpts.limit) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunList_User(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // list project fields + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": queries.LimitMax, + "afterItems": nil, + "firstFields": queries.LimitDefault, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "fields": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "__typename": "ProjectV2Field", + "name": "FieldTitle", + "id": "field ID", + }, + { + "__typename": "ProjectV2SingleSelectField", + "name": "Status", + "id": "status ID", + }, + { + "__typename": "ProjectV2IterationField", + "name": "Iterations", + "id": "iteration ID", + }, + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{ + number: 1, + owner: "monalisa", + }, + client: client, + io: ios, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal( + t, + "FieldTitle\tProjectV2Field\tfield ID\nStatus\tProjectV2SingleSelectField\tstatus ID\nIterations\tProjectV2IterationField\titeration ID\n", + stdout.String()) +} + +func TestRunList_Org(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // list project fields + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query OrgProject.*", + "variables": map[string]interface{}{ + "login": "github", + "number": 1, + "firstItems": queries.LimitMax, + "afterItems": nil, + "firstFields": queries.LimitDefault, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "fields": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "__typename": "ProjectV2Field", + "name": "FieldTitle", + "id": "field ID", + }, + { + "__typename": "ProjectV2SingleSelectField", + "name": "Status", + "id": "status ID", + }, + { + "__typename": "ProjectV2IterationField", + "name": "Iterations", + "id": "iteration ID", + }, + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{ + number: 1, + owner: "github", + }, + client: client, + io: ios, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal( + t, + "FieldTitle\tProjectV2Field\tfield ID\nStatus\tProjectV2SingleSelectField\tstatus ID\nIterations\tProjectV2IterationField\titeration ID\n", + stdout.String()) +} + +func TestRunList_Me(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // list project fields + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": queries.LimitMax, + "afterItems": nil, + "firstFields": queries.LimitDefault, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "fields": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "__typename": "ProjectV2Field", + "name": "FieldTitle", + "id": "field ID", + }, + { + "__typename": "ProjectV2SingleSelectField", + "name": "Status", + "id": "status ID", + }, + { + "__typename": "ProjectV2IterationField", + "name": "Iterations", + "id": "iteration ID", + }, + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{ + number: 1, + owner: "@me", + }, + client: client, + io: ios, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal( + t, + "FieldTitle\tProjectV2Field\tfield ID\nStatus\tProjectV2SingleSelectField\tstatus ID\nIterations\tProjectV2IterationField\titeration ID\n", + stdout.String()) +} + +func TestRunList_Empty(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // list project fields + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": queries.LimitMax, + "afterItems": nil, + "firstFields": queries.LimitDefault, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "fields": map[string]interface{}{ + "nodes": nil, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, _, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{ + number: 1, + owner: "@me", + }, + client: client, + io: ios, + } + + err := runList(config) + assert.EqualError( + t, + err, + "Project 1 for owner @me has no fields") +} diff --git a/pkg/cmd/project/item-add/item_add.go b/pkg/cmd/project/item-add/item_add.go new file mode 100644 index 000000000..51c9ecfce --- /dev/null +++ b/pkg/cmd/project/item-add/item_add.go @@ -0,0 +1,144 @@ +package itemadd + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type addItemOpts struct { + owner string + number int32 + itemURL string + projectID string + itemID string + format string +} + +type addItemConfig struct { + client *queries.Client + opts addItemOpts + io *iostreams.IOStreams +} + +type addProjectItemMutation struct { + CreateProjectItem struct { + ProjectV2Item queries.ProjectItem `graphql:"item"` + } `graphql:"addProjectV2ItemById(input:$input)"` +} + +func NewCmdAddItem(f *cmdutil.Factory, runF func(config addItemConfig) error) *cobra.Command { + opts := addItemOpts{} + addItemCmd := &cobra.Command{ + Short: "Add a pull request or an issue to a project", + Use: "item-add []", + Example: heredoc.Doc(` + # add an item to monalisa's project "1" + gh project item-add 1 --owner monalisa --url https://github.com/monalisa/myproject/issues/23 + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + config := addItemConfig{ + client: client, + opts: opts, + io: f.IOStreams, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runAddItem(config) + }, + } + + addItemCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + addItemCmd.Flags().StringVar(&opts.itemURL, "url", "", "URL of the issue or pull request to add to the project") + cmdutil.StringEnumFlag(addItemCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + _ = addItemCmd.MarkFlagRequired("url") + + return addItemCmd +} + +func runAddItem(config addItemConfig) error { + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false) + if err != nil { + return err + } + config.opts.projectID = project.ID + + itemID, err := config.client.IssueOrPullRequestID(config.opts.itemURL) + if err != nil { + return err + } + config.opts.itemID = itemID + + query, variables := addItemArgs(config) + err = config.client.Mutate("AddItem", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, query.CreateProjectItem.ProjectV2Item) + } + + return printResults(config, query.CreateProjectItem.ProjectV2Item) + +} + +func addItemArgs(config addItemConfig) (*addProjectItemMutation, map[string]interface{}) { + return &addProjectItemMutation{}, map[string]interface{}{ + "input": githubv4.AddProjectV2ItemByIdInput{ + ProjectID: githubv4.ID(config.opts.projectID), + ContentID: githubv4.ID(config.opts.itemID), + }, + } +} + +func printResults(config addItemConfig, item queries.ProjectItem) error { + if !config.io.IsStdoutTTY() { + return nil + } + + _, err := fmt.Fprintf(config.io.Out, "Added item\n") + return err +} + +func printJSON(config addItemConfig, item queries.ProjectItem) error { + b, err := format.JSONProjectItem(item) + if err != nil { + return err + } + + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/item-add/item_add_test.go b/pkg/cmd/project/item-add/item_add_test.go new file mode 100644 index 000000000..4f1ebd4f6 --- /dev/null +++ b/pkg/cmd/project/item-add/item_add_test.go @@ -0,0 +1,426 @@ +package itemadd + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdaddItem(t *testing.T) { + tests := []struct { + name string + cli string + wants addItemOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "missing-url", + cli: "", + wantsErr: true, + wantsErrMsg: "required flag(s) \"url\" not set", + }, + { + name: "not-a-number", + cli: "x --url github.com/cli/cli", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "url", + cli: "--url github.com/cli/cli", + wants: addItemOpts{ + itemURL: "github.com/cli/cli", + }, + }, + { + name: "number", + cli: "123 --url github.com/cli/cli", + wants: addItemOpts{ + number: 123, + itemURL: "github.com/cli/cli", + }, + }, + { + name: "owner", + cli: "--owner monalisa --url github.com/cli/cli", + wants: addItemOpts{ + owner: "monalisa", + itemURL: "github.com/cli/cli", + }, + }, + { + name: "json", + cli: "--format json --url github.com/cli/cli", + wants: addItemOpts{ + format: "json", + itemURL: "github.com/cli/cli", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts addItemOpts + cmd := NewCmdAddItem(f, func(config addItemConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.itemURL, gotOpts.itemURL) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunAddItem_User(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // get item ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query GetIssueOrPullRequest.*", + "variables": map[string]interface{}{ + "url": "https://github.com/cli/go-gh/issues/1", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "resource": map[string]interface{}{ + "id": "item ID", + "__typename": "Issue", + }, + }, + }) + + // create item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation AddItem.*","variables":{"input":{"projectId":"an ID","contentId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "addProjectV2ItemById": map[string]interface{}{ + "item": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := addItemConfig{ + opts: addItemOpts{ + owner: "monalisa", + number: 1, + itemURL: "https://github.com/cli/go-gh/issues/1", + }, + client: client, + io: ios, + } + + err := runAddItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Added item\n", + stdout.String()) +} + +func TestRunAddItem_Org(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query OrgProject.*", + "variables": map[string]interface{}{ + "login": "github", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // get item ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query GetIssueOrPullRequest.*", + "variables": map[string]interface{}{ + "url": "https://github.com/cli/go-gh/issues/1", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "resource": map[string]interface{}{ + "id": "item ID", + "__typename": "Issue", + }, + }, + }) + + // create item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation AddItem.*","variables":{"input":{"projectId":"an ID","contentId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "addProjectV2ItemById": map[string]interface{}{ + "item": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := addItemConfig{ + opts: addItemOpts{ + owner: "github", + number: 1, + itemURL: "https://github.com/cli/go-gh/issues/1", + }, + client: client, + io: ios, + } + + err := runAddItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Added item\n", + stdout.String()) +} + +func TestRunAddItem_Me(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // get item ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query GetIssueOrPullRequest.*", + "variables": map[string]interface{}{ + "url": "https://github.com/cli/go-gh/pull/1", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "resource": map[string]interface{}{ + "id": "item ID", + "__typename": "PullRequest", + }, + }, + }) + + // create item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation AddItem.*","variables":{"input":{"projectId":"an ID","contentId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "addProjectV2ItemById": map[string]interface{}{ + "item": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := addItemConfig{ + opts: addItemOpts{ + owner: "@me", + number: 1, + itemURL: "https://github.com/cli/go-gh/pull/1", + }, + client: client, + io: ios, + } + + err := runAddItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Added item\n", + stdout.String()) +} diff --git a/pkg/cmd/project/item-archive/item_archive.go b/pkg/cmd/project/item-archive/item_archive.go new file mode 100644 index 000000000..7ae336242 --- /dev/null +++ b/pkg/cmd/project/item-archive/item_archive.go @@ -0,0 +1,171 @@ +package itemarchive + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type archiveItemOpts struct { + owner string + number int32 + undo bool + itemID string + projectID string + format string +} + +type archiveItemConfig struct { + client *queries.Client + opts archiveItemOpts + io *iostreams.IOStreams +} + +type archiveProjectItemMutation struct { + ArchiveProjectItem struct { + ProjectV2Item queries.ProjectItem `graphql:"item"` + } `graphql:"archiveProjectV2Item(input:$input)"` +} + +type unarchiveProjectItemMutation struct { + UnarchiveProjectItem struct { + ProjectV2Item queries.ProjectItem `graphql:"item"` + } `graphql:"unarchiveProjectV2Item(input:$input)"` +} + +func NewCmdArchiveItem(f *cmdutil.Factory, runF func(config archiveItemConfig) error) *cobra.Command { + opts := archiveItemOpts{} + archiveItemCmd := &cobra.Command{ + Short: "Archive an item in a project", + Use: "item-archive []", + Example: heredoc.Doc(` + # archive an item in the current user's project "1" + gh project item-archive 1 --owner "@me" --id + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + config := archiveItemConfig{ + client: client, + opts: opts, + io: f.IOStreams, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runArchiveItem(config) + }, + } + + archiveItemCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + archiveItemCmd.Flags().StringVar(&opts.itemID, "id", "", "ID of the item to archive") + archiveItemCmd.Flags().BoolVar(&opts.undo, "undo", false, "Unarchive an item") + cmdutil.StringEnumFlag(archiveItemCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + _ = archiveItemCmd.MarkFlagRequired("id") + + return archiveItemCmd +} + +func runArchiveItem(config archiveItemConfig) error { + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false) + if err != nil { + return err + } + config.opts.projectID = project.ID + + if config.opts.undo { + query, variables := unarchiveItemArgs(config, config.opts.itemID) + err = config.client.Mutate("UnarchiveProjectItem", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, query.UnarchiveProjectItem.ProjectV2Item) + } + + return printResults(config, query.UnarchiveProjectItem.ProjectV2Item) + } + query, variables := archiveItemArgs(config) + err = config.client.Mutate("ArchiveProjectItem", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, query.ArchiveProjectItem.ProjectV2Item) + } + + return printResults(config, query.ArchiveProjectItem.ProjectV2Item) +} + +func archiveItemArgs(config archiveItemConfig) (*archiveProjectItemMutation, map[string]interface{}) { + return &archiveProjectItemMutation{}, map[string]interface{}{ + "input": githubv4.ArchiveProjectV2ItemInput{ + ProjectID: githubv4.ID(config.opts.projectID), + ItemID: githubv4.ID(config.opts.itemID), + }, + } +} + +func unarchiveItemArgs(config archiveItemConfig, itemID string) (*unarchiveProjectItemMutation, map[string]interface{}) { + return &unarchiveProjectItemMutation{}, map[string]interface{}{ + "input": githubv4.UnarchiveProjectV2ItemInput{ + ProjectID: githubv4.ID(config.opts.projectID), + ItemID: githubv4.ID(itemID), + }, + } +} + +func printResults(config archiveItemConfig, item queries.ProjectItem) error { + if !config.io.IsStdoutTTY() { + return nil + } + + if config.opts.undo { + _, err := fmt.Fprintf(config.io.Out, "Unarchived item\n") + return err + } + + _, err := fmt.Fprintf(config.io.Out, "Archived item\n") + return err +} + +func printJSON(config archiveItemConfig, item queries.ProjectItem) error { + b, err := format.JSONProjectItem(item) + if err != nil { + return err + } + + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/item-archive/item_archive_test.go b/pkg/cmd/project/item-archive/item_archive_test.go new file mode 100644 index 000000000..85a34af22 --- /dev/null +++ b/pkg/cmd/project/item-archive/item_archive_test.go @@ -0,0 +1,639 @@ +package itemarchive + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdarchiveItem(t *testing.T) { + tests := []struct { + name string + cli string + wants archiveItemOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "missing-id", + cli: "", + wantsErr: true, + wantsErrMsg: "required flag(s) \"id\" not set", + }, + { + name: "not-a-number", + cli: "x --id 123", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "id", + cli: "--id 123", + wants: archiveItemOpts{ + itemID: "123", + }, + }, + { + name: "number", + cli: "456 --id 123", + wants: archiveItemOpts{ + number: 456, + itemID: "123", + }, + }, + { + name: "owner", + cli: "--owner monalisa --id 123", + wants: archiveItemOpts{ + owner: "monalisa", + itemID: "123", + }, + }, + { + name: "undo", + cli: "--undo --id 123", + wants: archiveItemOpts{ + undo: true, + itemID: "123", + }, + }, + { + name: "json", + cli: "--format json --id 123", + wants: archiveItemOpts{ + format: "json", + itemID: "123", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts archiveItemOpts + cmd := NewCmdArchiveItem(f, func(config archiveItemConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.itemID, gotOpts.itemID) + assert.Equal(t, tt.wants.undo, gotOpts.undo) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunArchive_User(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // archive item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation ArchiveProjectItem.*","variables":{"input":{"projectId":"an ID","itemId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "archiveProjectV2Item": map[string]interface{}{ + "item": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := archiveItemConfig{ + opts: archiveItemOpts{ + owner: "monalisa", + number: 1, + itemID: "item ID", + }, + client: client, + io: ios, + } + + err := runArchiveItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Archived item\n", + stdout.String()) +} + +func TestRunArchive_Org(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query OrgProject.*", + "variables": map[string]interface{}{ + "login": "github", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // archive item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation ArchiveProjectItem.*","variables":{"input":{"projectId":"an ID","itemId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "archiveProjectV2Item": map[string]interface{}{ + "item": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := archiveItemConfig{ + opts: archiveItemOpts{ + owner: "github", + number: 1, + itemID: "item ID", + }, + client: client, + io: ios, + } + + err := runArchiveItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Archived item\n", + stdout.String()) +} + +func TestRunArchive_Me(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // archive item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation ArchiveProjectItem.*","variables":{"input":{"projectId":"an ID","itemId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "archiveProjectV2Item": map[string]interface{}{ + "item": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := archiveItemConfig{ + opts: archiveItemOpts{ + owner: "@me", + number: 1, + itemID: "item ID", + }, + client: client, + io: ios, + } + + err := runArchiveItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Archived item\n", + stdout.String()) +} + +func TestRunArchive_User_Undo(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // archive item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UnarchiveProjectItem.*","variables":{"input":{"projectId":"an ID","itemId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "unarchiveProjectV2Item": map[string]interface{}{ + "item": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := archiveItemConfig{ + opts: archiveItemOpts{ + owner: "monalisa", + number: 1, + itemID: "item ID", + undo: true, + }, + client: client, + io: ios, + } + + err := runArchiveItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Unarchived item\n", + stdout.String()) +} + +func TestRunArchive_Org_Undo(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query OrgProject.*", + "variables": map[string]interface{}{ + "login": "github", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // archive item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UnarchiveProjectItem.*","variables":{"input":{"projectId":"an ID","itemId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "unarchiveProjectV2Item": map[string]interface{}{ + "item": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := archiveItemConfig{ + opts: archiveItemOpts{ + owner: "github", + number: 1, + itemID: "item ID", + undo: true, + }, + client: client, + io: ios, + } + + err := runArchiveItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Unarchived item\n", + stdout.String()) +} + +func TestRunArchive_Me_Undo(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // archive item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UnarchiveProjectItem.*","variables":{"input":{"projectId":"an ID","itemId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "unarchiveProjectV2Item": map[string]interface{}{ + "item": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := archiveItemConfig{ + opts: archiveItemOpts{ + owner: "@me", + number: 1, + itemID: "item ID", + undo: true, + }, + client: client, + io: ios, + } + + err := runArchiveItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Unarchived item\n", + stdout.String()) +} diff --git a/pkg/cmd/project/item-create/item_create.go b/pkg/cmd/project/item-create/item_create.go new file mode 100644 index 000000000..6ee32e9f6 --- /dev/null +++ b/pkg/cmd/project/item-create/item_create.go @@ -0,0 +1,140 @@ +package itemcreate + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type createItemOpts struct { + title string + body string + owner string + number int32 + projectID string + format string +} + +type createItemConfig struct { + client *queries.Client + opts createItemOpts + io *iostreams.IOStreams +} + +type createProjectDraftItemMutation struct { + CreateProjectDraftItem struct { + ProjectV2Item queries.ProjectItem `graphql:"projectItem"` + } `graphql:"addProjectV2DraftIssue(input:$input)"` +} + +func NewCmdCreateItem(f *cmdutil.Factory, runF func(config createItemConfig) error) *cobra.Command { + opts := createItemOpts{} + createItemCmd := &cobra.Command{ + Short: "Create a draft issue item in a project", + Use: "item-create []", + Example: heredoc.Doc(` + # create a draft issue in the current user's project "1" + gh project item-create 1 --owner "@me" --title "new item" --body "new item body" + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + config := createItemConfig{ + client: client, + opts: opts, + io: f.IOStreams, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runCreateItem(config) + }, + } + + createItemCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + createItemCmd.Flags().StringVar(&opts.title, "title", "", "Title for the draft issue") + createItemCmd.Flags().StringVar(&opts.body, "body", "", "Body for the draft issue") + cmdutil.StringEnumFlag(createItemCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + _ = createItemCmd.MarkFlagRequired("title") + + return createItemCmd +} + +func runCreateItem(config createItemConfig) error { + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false) + if err != nil { + return err + } + config.opts.projectID = project.ID + + query, variables := createDraftIssueArgs(config) + + err = config.client.Mutate("CreateDraftItem", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, query.CreateProjectDraftItem.ProjectV2Item) + } + + return printResults(config, query.CreateProjectDraftItem.ProjectV2Item) +} + +func createDraftIssueArgs(config createItemConfig) (*createProjectDraftItemMutation, map[string]interface{}) { + return &createProjectDraftItemMutation{}, map[string]interface{}{ + "input": githubv4.AddProjectV2DraftIssueInput{ + Body: githubv4.NewString(githubv4.String(config.opts.body)), + ProjectID: githubv4.ID(config.opts.projectID), + Title: githubv4.String(config.opts.title), + }, + } +} + +func printResults(config createItemConfig, item queries.ProjectItem) error { + if !config.io.IsStdoutTTY() { + return nil + } + + _, err := fmt.Fprintf(config.io.Out, "Created item\n") + return err +} + +func printJSON(config createItemConfig, item queries.ProjectItem) error { + b, err := format.JSONProjectItem(item) + if err != nil { + return err + } + + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/item-create/item_create_test.go b/pkg/cmd/project/item-create/item_create_test.go new file mode 100644 index 000000000..d49144f3c --- /dev/null +++ b/pkg/cmd/project/item-create/item_create_test.go @@ -0,0 +1,374 @@ +package itemcreate + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdCreateItem(t *testing.T) { + tests := []struct { + name string + cli string + wants createItemOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "missing-title", + cli: "", + wantsErr: true, + wantsErrMsg: "required flag(s) \"title\" not set", + }, + { + name: "not-a-number", + cli: "x --title t", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "title", + cli: "--title t", + wants: createItemOpts{ + title: "t", + }, + }, + { + name: "number", + cli: "123 --title t", + wants: createItemOpts{ + number: 123, + title: "t", + }, + }, + { + name: "owner", + cli: "--owner monalisa --title t", + wants: createItemOpts{ + owner: "monalisa", + title: "t", + }, + }, + { + name: "body", + cli: "--body b --title t", + wants: createItemOpts{ + body: "b", + title: "t", + }, + }, + { + name: "json", + cli: "--format json --title t", + wants: createItemOpts{ + format: "json", + title: "t", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts createItemOpts + cmd := NewCmdCreateItem(f, func(config createItemConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.title, gotOpts.title) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunCreateItem_Draft_User(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // create item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateDraftItem.*","variables":{"input":{"projectId":"an ID","title":"a title","body":""}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "addProjectV2DraftIssue": map[string]interface{}{ + "projectItem": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createItemConfig{ + opts: createItemOpts{ + title: "a title", + owner: "monalisa", + number: 1, + }, + client: client, + io: ios, + } + + err := runCreateItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Created item\n", + stdout.String()) +} + +func TestRunCreateItem_Draft_Org(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query OrgProject.*", + "variables": map[string]interface{}{ + "login": "github", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // create item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateDraftItem.*","variables":{"input":{"projectId":"an ID","title":"a title","body":""}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "addProjectV2DraftIssue": map[string]interface{}{ + "projectItem": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createItemConfig{ + opts: createItemOpts{ + title: "a title", + owner: "github", + number: 1, + }, + client: client, + io: ios, + } + + err := runCreateItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Created item\n", + stdout.String()) +} + +func TestRunCreateItem_Draft_Me(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // create item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation CreateDraftItem.*","variables":{"input":{"projectId":"an ID","title":"a title","body":"a body"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "addProjectV2DraftIssue": map[string]interface{}{ + "projectItem": map[string]interface{}{ + "id": "item ID", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := createItemConfig{ + opts: createItemOpts{ + title: "a title", + owner: "@me", + number: 1, + body: "a body", + }, + client: client, + io: ios, + } + + err := runCreateItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Created item\n", + stdout.String()) +} diff --git a/pkg/cmd/project/item-delete/item_delete.go b/pkg/cmd/project/item-delete/item_delete.go new file mode 100644 index 000000000..2717afedc --- /dev/null +++ b/pkg/cmd/project/item-delete/item_delete.go @@ -0,0 +1,131 @@ +package itemdelete + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type deleteItemOpts struct { + owner string + number int32 + itemID string + projectID string + format string +} + +type deleteItemConfig struct { + client *queries.Client + opts deleteItemOpts + io *iostreams.IOStreams +} + +type deleteProjectItemMutation struct { + DeleteProjectItem struct { + DeletedItemId githubv4.ID `graphql:"deletedItemId"` + } `graphql:"deleteProjectV2Item(input:$input)"` +} + +func NewCmdDeleteItem(f *cmdutil.Factory, runF func(config deleteItemConfig) error) *cobra.Command { + opts := deleteItemOpts{} + deleteItemCmd := &cobra.Command{ + Short: "Delete an item from a project by ID", + Use: "item-delete []", + Example: heredoc.Doc(` + # delete an item in the current user's project "1" + gh project item-delete 1 --owner "@me" --id + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + config := deleteItemConfig{ + client: client, + opts: opts, + io: f.IOStreams, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runDeleteItem(config) + }, + } + + deleteItemCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + deleteItemCmd.Flags().StringVar(&opts.itemID, "id", "", "ID of the item to delete") + cmdutil.StringEnumFlag(deleteItemCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + _ = deleteItemCmd.MarkFlagRequired("id") + + return deleteItemCmd +} + +func runDeleteItem(config deleteItemConfig) error { + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false) + if err != nil { + return err + } + config.opts.projectID = project.ID + + query, variables := deleteItemArgs(config) + err = config.client.Mutate("DeleteProjectItem", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, query.DeleteProjectItem.DeletedItemId) + } + + return printResults(config) + +} + +func deleteItemArgs(config deleteItemConfig) (*deleteProjectItemMutation, map[string]interface{}) { + return &deleteProjectItemMutation{}, map[string]interface{}{ + "input": githubv4.DeleteProjectV2ItemInput{ + ProjectID: githubv4.ID(config.opts.projectID), + ItemID: githubv4.ID(config.opts.itemID), + }, + } +} + +func printResults(config deleteItemConfig) error { + if !config.io.IsStdoutTTY() { + return nil + } + + _, err := fmt.Fprintf(config.io.Out, "Deleted item\n") + return err +} + +func printJSON(config deleteItemConfig, ID githubv4.ID) error { + _, err := config.io.Out.Write([]byte(fmt.Sprintf(`{"id": "%s"}`, ID))) + return err +} diff --git a/pkg/cmd/project/item-delete/item_delete_test.go b/pkg/cmd/project/item-delete/item_delete_test.go new file mode 100644 index 000000000..c5a3f01fd --- /dev/null +++ b/pkg/cmd/project/item-delete/item_delete_test.go @@ -0,0 +1,359 @@ +package itemdelete + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdDeleteItem(t *testing.T) { + tests := []struct { + name string + cli string + wants deleteItemOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "missing-id", + cli: "", + wantsErr: true, + wantsErrMsg: "required flag(s) \"id\" not set", + }, + { + name: "not-a-number", + cli: "x --id 123", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "item-id", + cli: "--id 123", + wants: deleteItemOpts{ + itemID: "123", + }, + }, + { + name: "number", + cli: "456 --id 123", + wants: deleteItemOpts{ + number: 456, + itemID: "123", + }, + }, + { + name: "owner", + cli: "--owner monalisa --id 123", + wants: deleteItemOpts{ + owner: "monalisa", + itemID: "123", + }, + }, + { + name: "json", + cli: "--format json --id 123", + wants: deleteItemOpts{ + format: "json", + itemID: "123", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts deleteItemOpts + cmd := NewCmdDeleteItem(f, func(config deleteItemConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.itemID, gotOpts.itemID) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunDelete_User(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // delete item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation DeleteProjectItem.*","variables":{"input":{"projectId":"an ID","itemId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "deleteProjectV2Item": map[string]interface{}{ + "deletedItemId": "item ID", + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := deleteItemConfig{ + opts: deleteItemOpts{ + owner: "monalisa", + number: 1, + itemID: "item ID", + }, + client: client, + io: ios, + } + + err := runDeleteItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Deleted item\n", + stdout.String()) +} + +func TestRunDelete_Org(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query OrgProject.*", + "variables": map[string]interface{}{ + "login": "github", + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // delete item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation DeleteProjectItem.*","variables":{"input":{"projectId":"an ID","itemId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "deleteProjectV2Item": map[string]interface{}{ + "deletedItemId": "item ID", + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := deleteItemConfig{ + opts: deleteItemOpts{ + owner: "github", + number: 1, + itemID: "item ID", + }, + client: client, + io: ios, + } + + err := runDeleteItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Deleted item\n", + stdout.String()) +} + +func TestRunDelete_Me(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // get project ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerProject.*", + "variables": map[string]interface{}{ + "number": 1, + "firstItems": 0, + "afterItems": nil, + "firstFields": 0, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "id": "an ID", + }, + }, + }, + }) + + // delete item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation DeleteProjectItem.*","variables":{"input":{"projectId":"an ID","itemId":"item ID"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "deleteProjectV2Item": map[string]interface{}{ + "deletedItemId": "item ID", + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + config := deleteItemConfig{ + opts: deleteItemOpts{ + owner: "@me", + number: 1, + itemID: "item ID", + }, + client: client, + io: ios, + } + + err := runDeleteItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Deleted item\n", + stdout.String()) +} diff --git a/pkg/cmd/project/item-edit/item_edit.go b/pkg/cmd/project/item-edit/item_edit.go new file mode 100644 index 000000000..3bd479470 --- /dev/null +++ b/pkg/cmd/project/item-edit/item_edit.go @@ -0,0 +1,253 @@ +package itemedit + +import ( + "fmt" + "strings" + "time" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/shurcooL/githubv4" + "github.com/spf13/cobra" +) + +type editItemOpts struct { + // updateDraftIssue + title string + body string + itemID string + // updateItem + fieldID string + projectID string + text string + number float32 + date string + singleSelectOptionID string + iterationID string + // format + format string +} + +type editItemConfig struct { + io *iostreams.IOStreams + client *queries.Client + opts editItemOpts +} + +type EditProjectDraftIssue struct { + UpdateProjectV2DraftIssue struct { + DraftIssue queries.DraftIssue `graphql:"draftIssue"` + } `graphql:"updateProjectV2DraftIssue(input:$input)"` +} + +type UpdateProjectV2FieldValue struct { + Update struct { + Item queries.ProjectItem `graphql:"projectV2Item"` + } `graphql:"updateProjectV2ItemFieldValue(input:$input)"` +} + +func NewCmdEditItem(f *cmdutil.Factory, runF func(config editItemConfig) error) *cobra.Command { + opts := editItemOpts{} + editItemCmd := &cobra.Command{ + Use: "item-edit", + Short: "Edit an item in a project", + Long: heredoc.Doc(` + Edit either a draft issue or a project item. Both usages require the ID of the item to edit. + + For non-draft issues, the ID of the project is also required, and only a single field value can be updated per invocation. + `), + Example: heredoc.Doc(` + # edit an item's text field value + gh project item-edit --id --field-id --project-id --text "new text" + `), + RunE: func(cmd *cobra.Command, args []string) error { + if err := cmdutil.MutuallyExclusive( + "only one of `--text`, `--number`, `--date`, `--single-select-option-id` or `--iteration-id` may be used", + opts.text != "", + opts.number != 0, + opts.date != "", + opts.singleSelectOptionID != "", + opts.iterationID != "", + ); err != nil { + return err + } + + client, err := client.New(f) + if err != nil { + return err + } + + config := editItemConfig{ + io: f.IOStreams, + client: client, + opts: opts, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runEditItem(config) + }, + } + + editItemCmd.Flags().StringVar(&opts.itemID, "id", "", "ID of the item to edit") + cmdutil.StringEnumFlag(editItemCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + editItemCmd.Flags().StringVar(&opts.title, "title", "", "Title of the draft issue item") + editItemCmd.Flags().StringVar(&opts.body, "body", "", "Body of the draft issue item") + + editItemCmd.Flags().StringVar(&opts.fieldID, "field-id", "", "ID of the field to update") + editItemCmd.Flags().StringVar(&opts.projectID, "project-id", "", "ID of the project to which the field belongs to") + editItemCmd.Flags().StringVar(&opts.text, "text", "", "Text value for the field") + editItemCmd.Flags().Float32Var(&opts.number, "number", 0, "Number value for the field") + editItemCmd.Flags().StringVar(&opts.date, "date", "", "Date value for the field (YYYY-MM-DD)") + editItemCmd.Flags().StringVar(&opts.singleSelectOptionID, "single-select-option-id", "", "ID of the single select option value to set on the field") + editItemCmd.Flags().StringVar(&opts.iterationID, "iteration-id", "", "ID of the iteration value to set on the field") + + _ = editItemCmd.MarkFlagRequired("id") + + return editItemCmd +} + +func runEditItem(config editItemConfig) error { + // update draft issue + if config.opts.title != "" || config.opts.body != "" { + if !strings.HasPrefix(config.opts.itemID, "DI_") { + return cmdutil.FlagErrorf("ID must be the ID of the draft issue content which is prefixed with `DI_`") + } + + query, variables := buildEditDraftIssue(config) + + err := config.client.Mutate("EditDraftIssueItem", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printDraftIssueJSON(config, query.UpdateProjectV2DraftIssue.DraftIssue) + } + + return printDraftIssueResults(config, query.UpdateProjectV2DraftIssue.DraftIssue) + } + + // update item values + if config.opts.text != "" || config.opts.number != 0 || config.opts.date != "" || config.opts.singleSelectOptionID != "" || config.opts.iterationID != "" { + if config.opts.fieldID == "" { + return cmdutil.FlagErrorf("field-id must be provided") + } + if config.opts.projectID == "" { + // TODO: offer to fetch interactively + return cmdutil.FlagErrorf("project-id must be provided") + } + + var parsedDate time.Time + if config.opts.date != "" { + date, err := time.Parse("2006-01-02", config.opts.date) + if err != nil { + return err + } + parsedDate = date + } + + query, variables := buildUpdateItem(config, parsedDate) + err := config.client.Mutate("UpdateItemValues", query, variables) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printItemJSON(config, &query.Update.Item) + } + + return printItemResults(config, &query.Update.Item) + } + + if _, err := fmt.Fprintln(config.io.ErrOut, "error: no changes to make"); err != nil { + return err + } + return cmdutil.SilentError +} + +func buildEditDraftIssue(config editItemConfig) (*EditProjectDraftIssue, map[string]interface{}) { + return &EditProjectDraftIssue{}, map[string]interface{}{ + "input": githubv4.UpdateProjectV2DraftIssueInput{ + Body: githubv4.NewString(githubv4.String(config.opts.body)), + DraftIssueID: githubv4.ID(config.opts.itemID), + Title: githubv4.NewString(githubv4.String(config.opts.title)), + }, + } +} + +func buildUpdateItem(config editItemConfig, date time.Time) (*UpdateProjectV2FieldValue, map[string]interface{}) { + var value githubv4.ProjectV2FieldValue + if config.opts.text != "" { + value = githubv4.ProjectV2FieldValue{ + Text: githubv4.NewString(githubv4.String(config.opts.text)), + } + } else if config.opts.number != 0 { + value = githubv4.ProjectV2FieldValue{ + Number: githubv4.NewFloat(githubv4.Float(config.opts.number)), + } + } else if config.opts.date != "" { + value = githubv4.ProjectV2FieldValue{ + Date: githubv4.NewDate(githubv4.Date{Time: date}), + } + } else if config.opts.singleSelectOptionID != "" { + value = githubv4.ProjectV2FieldValue{ + SingleSelectOptionID: githubv4.NewString(githubv4.String(config.opts.singleSelectOptionID)), + } + } else if config.opts.iterationID != "" { + value = githubv4.ProjectV2FieldValue{ + IterationID: githubv4.NewString(githubv4.String(config.opts.iterationID)), + } + } + + return &UpdateProjectV2FieldValue{}, map[string]interface{}{ + "input": githubv4.UpdateProjectV2ItemFieldValueInput{ + ProjectID: githubv4.ID(config.opts.projectID), + ItemID: githubv4.ID(config.opts.itemID), + FieldID: githubv4.ID(config.opts.fieldID), + Value: value, + }, + } +} + +func printDraftIssueResults(config editItemConfig, item queries.DraftIssue) error { + if !config.io.IsStdoutTTY() { + return nil + } + _, err := fmt.Fprintf(config.io.Out, "Edited draft issue %q\n", item.Title) + return err +} + +func printDraftIssueJSON(config editItemConfig, item queries.DraftIssue) error { + b, err := format.JSONProjectDraftIssue(item) + if err != nil { + return err + } + _, err = config.io.Out.Write(b) + return err +} + +func printItemResults(config editItemConfig, item *queries.ProjectItem) error { + if !config.io.IsStdoutTTY() { + return nil + } + _, err := fmt.Fprintf(config.io.Out, "Edited item %q\n", item.Title()) + return err +} + +func printItemJSON(config editItemConfig, item *queries.ProjectItem) error { + b, err := format.JSONProjectItem(*item) + if err != nil { + return err + } + _, err = config.io.Out.Write(b) + return err + +} diff --git a/pkg/cmd/project/item-edit/item_edit_test.go b/pkg/cmd/project/item-edit/item_edit_test.go new file mode 100644 index 000000000..156871f32 --- /dev/null +++ b/pkg/cmd/project/item-edit/item_edit_test.go @@ -0,0 +1,476 @@ +package itemedit + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdeditItem(t *testing.T) { + tests := []struct { + name string + cli string + wants editItemOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "missing-id", + cli: "", + wantsErr: true, + wantsErrMsg: "required flag(s) \"id\" not set", + }, + { + name: "invalid-flags", + cli: "--id 123 --text t --date 2023-01-01", + wantsErr: true, + wantsErrMsg: "only one of `--text`, `--number`, `--date`, `--single-select-option-id` or `--iteration-id` may be used", + }, + { + name: "item-id", + cli: "--id 123", + wants: editItemOpts{ + itemID: "123", + }, + }, + { + name: "number", + cli: "--number 456 --id 123", + wants: editItemOpts{ + number: 456, + itemID: "123", + }, + }, + { + name: "field-id", + cli: "--field-id FIELD_ID --id 123", + wants: editItemOpts{ + fieldID: "FIELD_ID", + itemID: "123", + }, + }, + { + name: "project-id", + cli: "--project-id PROJECT_ID --id 123", + wants: editItemOpts{ + projectID: "PROJECT_ID", + itemID: "123", + }, + }, + { + name: "text", + cli: "--text t --id 123", + wants: editItemOpts{ + text: "t", + itemID: "123", + }, + }, + { + name: "date", + cli: "--date 2023-01-01 --id 123", + wants: editItemOpts{ + date: "2023-01-01", + itemID: "123", + }, + }, + { + name: "single-select-option-id", + cli: "--single-select-option-id OPTION_ID --id 123", + wants: editItemOpts{ + singleSelectOptionID: "OPTION_ID", + itemID: "123", + }, + }, + { + name: "iteration-id", + cli: "--iteration-id ITERATION_ID --id 123", + wants: editItemOpts{ + iterationID: "ITERATION_ID", + itemID: "123", + }, + }, + { + name: "json", + cli: "--format json --id 123", + wants: editItemOpts{ + format: "json", + itemID: "123", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts editItemOpts + cmd := NewCmdEditItem(f, func(config editItemConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.itemID, gotOpts.itemID) + assert.Equal(t, tt.wants.format, gotOpts.format) + assert.Equal(t, tt.wants.title, gotOpts.title) + assert.Equal(t, tt.wants.fieldID, gotOpts.fieldID) + assert.Equal(t, tt.wants.projectID, gotOpts.projectID) + assert.Equal(t, tt.wants.text, gotOpts.text) + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.date, gotOpts.date) + assert.Equal(t, tt.wants.singleSelectOptionID, gotOpts.singleSelectOptionID) + assert.Equal(t, tt.wants.iterationID, gotOpts.iterationID) + }) + } +} + +func TestRunItemEdit_Draft(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // edit item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation EditDraftIssueItem.*","variables":{"input":{"draftIssueId":"DI_item_id","title":"a title","body":"a new body"}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2DraftIssue": map[string]interface{}{ + "draftIssue": map[string]interface{}{ + "title": "a title", + "body": "a new body", + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + + config := editItemConfig{ + io: ios, + opts: editItemOpts{ + title: "a title", + body: "a new body", + itemID: "DI_item_id", + }, + client: client, + } + + err := runEditItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Edited draft issue \"a title\"\n", + stdout.String()) +} + +func TestRunItemEdit_Text(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // edit item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UpdateItemValues.*","variables":{"input":{"projectId":"project_id","itemId":"item_id","fieldId":"field_id","value":{"text":"item text"}}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2ItemFieldValue": map[string]interface{}{ + "projectV2Item": map[string]interface{}{ + "ID": "item_id", + "content": map[string]interface{}{ + "__typename": "Issue", + "body": "body", + "title": "title", + "number": 1, + "repository": map[string]interface{}{ + "nameWithOwner": "my-repo", + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := editItemConfig{ + io: ios, + opts: editItemOpts{ + text: "item text", + itemID: "item_id", + projectID: "project_id", + fieldID: "field_id", + }, + client: client, + } + + err := runEditItem(config) + assert.NoError(t, err) + assert.Equal(t, "", stdout.String()) +} + +func TestRunItemEdit_Number(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // edit item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UpdateItemValues.*","variables":{"input":{"projectId":"project_id","itemId":"item_id","fieldId":"field_id","value":{"number":2}}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2ItemFieldValue": map[string]interface{}{ + "projectV2Item": map[string]interface{}{ + "ID": "item_id", + "content": map[string]interface{}{ + "__typename": "Issue", + "body": "body", + "title": "title", + "number": 1, + "repository": map[string]interface{}{ + "nameWithOwner": "my-repo", + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + + config := editItemConfig{ + io: ios, + opts: editItemOpts{ + number: 2, + itemID: "item_id", + projectID: "project_id", + fieldID: "field_id", + }, + client: client, + } + + err := runEditItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Edited item \"title\"\n", + stdout.String()) +} + +func TestRunItemEdit_Date(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // edit item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UpdateItemValues.*","variables":{"input":{"projectId":"project_id","itemId":"item_id","fieldId":"field_id","value":{"date":"2023-01-01T00:00:00Z"}}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2ItemFieldValue": map[string]interface{}{ + "projectV2Item": map[string]interface{}{ + "ID": "item_id", + "content": map[string]interface{}{ + "__typename": "Issue", + "body": "body", + "title": "title", + "number": 1, + "repository": map[string]interface{}{ + "nameWithOwner": "my-repo", + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := editItemConfig{ + io: ios, + opts: editItemOpts{ + date: "2023-01-01", + itemID: "item_id", + projectID: "project_id", + fieldID: "field_id", + }, + client: client, + } + + err := runEditItem(config) + assert.NoError(t, err) + assert.Equal(t, "", stdout.String()) +} + +func TestRunItemEdit_SingleSelect(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // edit item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UpdateItemValues.*","variables":{"input":{"projectId":"project_id","itemId":"item_id","fieldId":"field_id","value":{"singleSelectOptionId":"option_id"}}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2ItemFieldValue": map[string]interface{}{ + "projectV2Item": map[string]interface{}{ + "ID": "item_id", + "content": map[string]interface{}{ + "__typename": "Issue", + "body": "body", + "title": "title", + "number": 1, + "repository": map[string]interface{}{ + "nameWithOwner": "my-repo", + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := editItemConfig{ + io: ios, + opts: editItemOpts{ + singleSelectOptionID: "option_id", + itemID: "item_id", + projectID: "project_id", + fieldID: "field_id", + }, + client: client, + } + + err := runEditItem(config) + assert.NoError(t, err) + assert.Equal(t, "", stdout.String()) +} + +func TestRunItemEdit_Iteration(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // edit item + gock.New("https://api.github.com"). + Post("/graphql"). + BodyString(`{"query":"mutation UpdateItemValues.*","variables":{"input":{"projectId":"project_id","itemId":"item_id","fieldId":"field_id","value":{"iterationId":"option_id"}}}}`). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "updateProjectV2ItemFieldValue": map[string]interface{}{ + "projectV2Item": map[string]interface{}{ + "ID": "item_id", + "content": map[string]interface{}{ + "__typename": "Issue", + "body": "body", + "title": "title", + "number": 1, + "repository": map[string]interface{}{ + "nameWithOwner": "my-repo", + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdoutTTY(true) + + config := editItemConfig{ + io: ios, + opts: editItemOpts{ + iterationID: "option_id", + itemID: "item_id", + projectID: "project_id", + fieldID: "field_id", + }, + client: client, + } + + err := runEditItem(config) + assert.NoError(t, err) + assert.Equal( + t, + "Edited item \"title\"\n", + stdout.String()) +} + +func TestRunItemEdit_NoChanges(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + client := queries.NewTestClient() + + ios, _, stdout, stderr := iostreams.Test() + ios.SetStdoutTTY(true) + + config := editItemConfig{ + io: ios, + opts: editItemOpts{}, + client: client, + } + + err := runEditItem(config) + assert.Error(t, err, "SilentError") + assert.Equal(t, "", stdout.String()) + assert.Equal(t, "error: no changes to make\n", stderr.String()) +} + +func TestRunItemEdit_InvalidID(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + client := queries.NewTestClient() + config := editItemConfig{ + opts: editItemOpts{ + title: "a title", + body: "a new body", + itemID: "item_id", + }, + client: client, + } + + err := runEditItem(config) + assert.Error(t, err, "ID must be the ID of the draft issue content which is prefixed with `DI_`") +} diff --git a/pkg/cmd/project/item-list/item_list.go b/pkg/cmd/project/item-list/item_list.go new file mode 100644 index 000000000..4acc95dcb --- /dev/null +++ b/pkg/cmd/project/item-list/item_list.go @@ -0,0 +1,136 @@ +package itemlist + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/tableprinter" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/spf13/cobra" +) + +type listOpts struct { + limit int + owner string + number int32 + format string +} + +type listConfig struct { + io *iostreams.IOStreams + tp *tableprinter.TablePrinter + client *queries.Client + opts listOpts +} + +func NewCmdList(f *cmdutil.Factory, runF func(config listConfig) error) *cobra.Command { + opts := listOpts{} + listCmd := &cobra.Command{ + Short: "List the items in a project", + Use: "item-list []", + Example: heredoc.Doc(` + # list the items in the current users's project "1" + gh project item-list 1 --owner "@me" + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + t := tableprinter.New(f.IOStreams) + config := listConfig{ + io: f.IOStreams, + tp: t, + client: client, + opts: opts, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runList(config) + }, + } + + listCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + cmdutil.StringEnumFlag(listCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + listCmd.Flags().IntVarP(&opts.limit, "limit", "L", queries.LimitDefault, "Maximum number of items to fetch") + + return listCmd +} + +func runList(config listConfig) error { + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + // no need to fetch the project if we already have the number + if config.opts.number == 0 { + project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false) + if err != nil { + return err + } + config.opts.number = project.Number + } + + project, err := config.client.ProjectItems(owner, config.opts.number, config.opts.limit) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, project) + } + + return printResults(config, project.Items.Nodes, owner.Login) +} + +func printResults(config listConfig, items []queries.ProjectItem, login string) error { + if len(items) == 0 { + return cmdutil.NewNoResultsError(fmt.Sprintf("Project %d for owner %s has no items", config.opts.number, login)) + } + + config.tp.HeaderRow("Type", "Title", "Number", "Repository", "ID") + + for _, i := range items { + config.tp.AddField(i.Type()) + config.tp.AddField(i.Title()) + if i.Number() == 0 { + config.tp.AddField("") + } else { + config.tp.AddField(strconv.Itoa(i.Number())) + } + config.tp.AddField(i.Repo()) + config.tp.AddField(i.ID(), tableprinter.WithTruncate(nil)) + config.tp.EndRow() + } + + return config.tp.Render() +} + +func printJSON(config listConfig, project *queries.Project) error { + b, err := format.JSONProjectDetailedItems(project) + if err != nil { + return err + } + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/item-list/item_list_test.go b/pkg/cmd/project/item-list/item_list_test.go new file mode 100644 index 000000000..d618451b1 --- /dev/null +++ b/pkg/cmd/project/item-list/item_list_test.go @@ -0,0 +1,402 @@ +package itemlist + +import ( + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + + "github.com/cli/cli/v2/internal/tableprinter" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdList(t *testing.T) { + tests := []struct { + name string + cli string + wants listOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "not-a-number", + cli: "x", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "number", + cli: "123", + wants: listOpts{ + number: 123, + limit: 30, + }, + }, + { + name: "owner", + cli: "--owner monalisa", + wants: listOpts{ + owner: "monalisa", + limit: 30, + }, + }, + { + name: "json", + cli: "--format json", + wants: listOpts{ + format: "json", + limit: 30, + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts listOpts + cmd := NewCmdList(f, func(config listConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.format, gotOpts.format) + assert.Equal(t, tt.wants.limit, gotOpts.limit) + }) + } +} + +func TestRunList_User(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + // list project items + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query UserProjectWithItems.*", + "variables": map[string]interface{}{ + "firstItems": queries.LimitDefault, + "afterItems": nil, + "firstFields": queries.LimitMax, + "afterFields": nil, + "login": "monalisa", + "number": 1, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "items": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "id": "issue ID", + "content": map[string]interface{}{ + "__typename": "Issue", + "title": "an issue", + "number": 1, + "repository": map[string]string{ + "nameWithOwner": "cli/go-gh", + }, + }, + }, + { + "id": "pull request ID", + "content": map[string]interface{}{ + "__typename": "PullRequest", + "title": "a pull request", + "number": 2, + "repository": map[string]string{ + "nameWithOwner": "cli/go-gh", + }, + }, + }, + { + "id": "draft issue ID", + "content": map[string]interface{}{ + "title": "draft issue", + "__typename": "DraftIssue", + }, + }, + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{ + number: 1, + owner: "monalisa", + }, + client: client, + io: ios, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal( + t, + "Issue\tan issue\t1\tcli/go-gh\tissue ID\nPullRequest\ta pull request\t2\tcli/go-gh\tpull request ID\nDraftIssue\tdraft issue\t\t\tdraft issue ID\n", + stdout.String()) +} + +func TestRunList_Org(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + // list project items + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query OrgProjectWithItems.*", + "variables": map[string]interface{}{ + "firstItems": queries.LimitDefault, + "afterItems": nil, + "firstFields": queries.LimitMax, + "afterFields": nil, + "login": "github", + "number": 1, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "items": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "id": "issue ID", + "content": map[string]interface{}{ + "__typename": "Issue", + "title": "an issue", + "number": 1, + "repository": map[string]string{ + "nameWithOwner": "cli/go-gh", + }, + }, + }, + { + "id": "pull request ID", + "content": map[string]interface{}{ + "__typename": "PullRequest", + "title": "a pull request", + "number": 2, + "repository": map[string]string{ + "nameWithOwner": "cli/go-gh", + }, + }, + }, + { + "id": "draft issue ID", + "content": map[string]interface{}{ + "title": "draft issue", + "__typename": "DraftIssue", + }, + }, + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{ + number: 1, + owner: "github", + }, + client: client, + io: ios, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal( + t, + "Issue\tan issue\t1\tcli/go-gh\tissue ID\nPullRequest\ta pull request\t2\tcli/go-gh\tpull request ID\nDraftIssue\tdraft issue\t\t\tdraft issue ID\n", + stdout.String()) +} + +func TestRunList_Me(t *testing.T) { + defer gock.Off() + // gock.Observe(gock.DumpRequest) + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + // list project items + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query ViewerProjectWithItems.*", + "variables": map[string]interface{}{ + "firstItems": queries.LimitDefault, + "afterItems": nil, + "firstFields": queries.LimitMax, + "afterFields": nil, + "number": 1, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "items": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "id": "issue ID", + "content": map[string]interface{}{ + "__typename": "Issue", + "title": "an issue", + "number": 1, + "repository": map[string]string{ + "nameWithOwner": "cli/go-gh", + }, + }, + }, + { + "id": "pull request ID", + "content": map[string]interface{}{ + "__typename": "PullRequest", + "title": "a pull request", + "number": 2, + "repository": map[string]string{ + "nameWithOwner": "cli/go-gh", + }, + }, + }, + { + "id": "draft issue ID", + "content": map[string]interface{}{ + "title": "draft issue", + "__typename": "DraftIssue", + }, + }, + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{ + number: 1, + owner: "@me", + }, + client: client, + io: ios, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal( + t, + "Issue\tan issue\t1\tcli/go-gh\tissue ID\nPullRequest\ta pull request\t2\tcli/go-gh\tpull request ID\nDraftIssue\tdraft issue\t\t\tdraft issue ID\n", + stdout.String()) +} diff --git a/pkg/cmd/project/list/list.go b/pkg/cmd/project/list/list.go new file mode 100644 index 000000000..661cbcd10 --- /dev/null +++ b/pkg/cmd/project/list/list.go @@ -0,0 +1,186 @@ +package list + +import ( + "fmt" + "strconv" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/tableprinter" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/spf13/cobra" +) + +type listOpts struct { + limit int + web bool + owner string + closed bool + format string +} + +type listConfig struct { + tp *tableprinter.TablePrinter + client *queries.Client + opts listOpts + URLOpener func(string) error + io *iostreams.IOStreams +} + +func NewCmdList(f *cmdutil.Factory, runF func(config listConfig) error) *cobra.Command { + opts := listOpts{} + listCmd := &cobra.Command{ + Short: "List the projects for an owner", + Use: "list", + Example: heredoc.Doc(` + # list the current user's projects + gh project list + + # list the projects for org github including closed projects + gh project list --owner github --closed + `), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + URLOpener := func(url string) error { + return f.Browser.Browse(url) + } + t := tableprinter.New(f.IOStreams) + config := listConfig{ + tp: t, + client: client, + opts: opts, + URLOpener: URLOpener, + io: f.IOStreams, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runList(config) + }, + } + + listCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner") + listCmd.Flags().BoolVarP(&opts.closed, "closed", "", false, "Include closed projects") + listCmd.Flags().BoolVarP(&opts.web, "web", "w", false, "Open projects list in the browser") + cmdutil.StringEnumFlag(listCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + listCmd.Flags().IntVarP(&opts.limit, "limit", "L", queries.LimitDefault, "Maximum number of projects to fetch") + + return listCmd +} + +func runList(config listConfig) error { + if config.opts.web { + url, err := buildURL(config) + if err != nil { + return err + } + + if err := config.URLOpener(url); err != nil { + return err + } + return nil + } + + if config.opts.owner == "" { + config.opts.owner = "@me" + } + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + projects, totalCount, err := config.client.Projects(config.opts.owner, owner.Type, config.opts.limit, false) + if err != nil { + return err + } + projects = filterProjects(projects, config) + + if config.opts.format == "json" { + return printJSON(config, projects, totalCount) + } + + return printResults(config, projects, owner.Login) +} + +// TODO: support non-github.com hostnames +func buildURL(config listConfig) (string, error) { + var url string + if config.opts.owner == "@me" || config.opts.owner == "" { + owner, err := config.client.ViewerLoginName() + if err != nil { + return "", err + } + url = fmt.Sprintf("https://github.com/users/%s/projects", owner) + } else { + _, ownerType, err := config.client.OwnerIDAndType(config.opts.owner) + if err != nil { + return "", err + } + + if ownerType == queries.UserOwner { + url = fmt.Sprintf("https://github.com/users/%s/projects", config.opts.owner) + } else { + url = fmt.Sprintf("https://github.com/orgs/%s/projects", config.opts.owner) + } + } + + if config.opts.closed { + return url + "?query=is%3Aclosed", nil + } + return url, nil +} + +func filterProjects(nodes []queries.Project, config listConfig) []queries.Project { + projects := make([]queries.Project, 0, len(nodes)) + for _, p := range nodes { + if !config.opts.closed && p.Closed { + continue + } + projects = append(projects, p) + } + return projects +} + +func printResults(config listConfig, projects []queries.Project, owner string) error { + if len(projects) == 0 { + return cmdutil.NewNoResultsError(fmt.Sprintf("No projects found for %s", owner)) + } + + config.tp.HeaderRow("Number", "Title", "State", "ID") + + for _, p := range projects { + config.tp.AddField(strconv.Itoa(int(p.Number)), tableprinter.WithTruncate(nil)) + config.tp.AddField(p.Title) + var state string + if p.Closed { + state = "closed" + } else { + state = "open" + } + config.tp.AddField(state) + config.tp.AddField(p.ID, tableprinter.WithTruncate(nil)) + config.tp.EndRow() + } + + return config.tp.Render() +} + +func printJSON(config listConfig, projects []queries.Project, totalCount int) error { + b, err := format.JSONProjects(projects, totalCount) + if err != nil { + return err + } + + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/list/list_test.go b/pkg/cmd/project/list/list_test.go new file mode 100644 index 000000000..48fb689ed --- /dev/null +++ b/pkg/cmd/project/list/list_test.go @@ -0,0 +1,737 @@ +package list + +import ( + "bytes" + "os" + "testing" + + "github.com/cli/cli/v2/internal/tableprinter" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdlist(t *testing.T) { + tests := []struct { + name string + cli string + wants listOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "owner", + cli: "--owner monalisa", + wants: listOpts{ + owner: "monalisa", + limit: 30, + }, + }, + { + name: "closed", + cli: "--closed", + wants: listOpts{ + closed: true, + limit: 30, + }, + }, + { + name: "web", + cli: "--web", + wants: listOpts{ + web: true, + limit: 30, + }, + }, + { + name: "json", + cli: "--format json", + wants: listOpts{ + format: "json", + limit: 30, + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts listOpts + cmd := NewCmdList(f, func(config listConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.closed, gotOpts.closed) + assert.Equal(t, tt.wants.web, gotOpts.web) + assert.Equal(t, tt.wants.limit, gotOpts.limit) + assert.Equal(t, tt.wants.format, gotOpts.format) + }) + } +} + +func TestRunList(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "login": "monalisa", + "projectsV2": map[string]interface{}{ + "nodes": []interface{}{ + map[string]interface{}{ + "title": "Project 1", + "shortDescription": "Short description 1", + "url": "url1", + "closed": false, + "ID": "id-1", + "number": 1, + }, + map[string]interface{}{ + "title": "Project 2", + "shortDescription": "", + "url": "url2", + "closed": true, + "ID": "id-2", + "number": 2, + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{ + owner: "monalisa", + }, + client: client, + io: ios, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal( + t, + "1\tProject 1\topen\tid-1\n", + stdout.String()) +} + +func TestRunList_Me(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "login": "monalisa", + "projectsV2": map[string]interface{}{ + "nodes": []interface{}{ + map[string]interface{}{ + "title": "Project 1", + "shortDescription": "Short description 1", + "url": "url1", + "closed": false, + "ID": "id-1", + "number": 1, + }, + map[string]interface{}{ + "title": "Project 2", + "shortDescription": "", + "url": "url2", + "closed": true, + "ID": "id-2", + "number": 2, + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{ + owner: "@me", + }, + client: client, + io: ios, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal( + t, + "1\tProject 1\topen\tid-1\n", + stdout.String()) +} + +func TestRunListViewer(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "login": "monalisa", + "projectsV2": map[string]interface{}{ + "nodes": []interface{}{ + map[string]interface{}{ + "title": "Project 1", + "shortDescription": "Short description 1", + "url": "url1", + "closed": false, + "ID": "id-1", + "number": 1, + }, + map[string]interface{}{ + "title": "Project 2", + "shortDescription": "", + "url": "url2", + "closed": true, + "ID": "id-2", + "number": 2, + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{}, + client: client, + io: ios, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal( + t, + "1\tProject 1\topen\tid-1\n", + stdout.String()) +} + +func TestRunListOrg(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "login": "monalisa", + "projectsV2": map[string]interface{}{ + "nodes": []interface{}{ + map[string]interface{}{ + "title": "Project 1", + "shortDescription": "Short description 1", + "url": "url1", + "closed": false, + "ID": "id-1", + "number": 1, + }, + map[string]interface{}{ + "title": "Project 2", + "shortDescription": "", + "url": "url2", + "closed": true, + "ID": "id-2", + "number": 2, + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{ + owner: "github", + }, + client: client, + io: ios, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal( + t, + "1\tProject 1\topen\tid-1\n", + stdout.String()) +} + +func TestRunListEmpty(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query Viewer.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + "login": "theviewer", + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "login": "monalisa", + "projectsV2": map[string]interface{}{ + "nodes": []interface{}{}, + }, + }, + }, + }) + client := queries.NewTestClient() + + ios, _, _, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{}, + client: client, + io: ios, + } + + err := runList(config) + assert.EqualError( + t, + err, + "No projects found for @me") +} + +func TestRunListWithClosed(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "login": "monalisa", + "projectsV2": map[string]interface{}{ + "nodes": []interface{}{ + map[string]interface{}{ + "title": "Project 1", + "shortDescription": "Short description 1", + "url": "url1", + "closed": false, + "ID": "id-1", + "number": 1, + }, + map[string]interface{}{ + "title": "Project 2", + "shortDescription": "", + "url": "url2", + "closed": true, + "ID": "id-2", + "number": 2, + }, + }, + }, + }, + }, + }) + + client := queries.NewTestClient() + + ios, _, stdout, _ := iostreams.Test() + config := listConfig{ + tp: tableprinter.New(ios), + opts: listOpts{ + owner: "monalisa", + closed: true, + }, + client: client, + io: ios, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal( + t, + "1\tProject 1\topen\tid-1\n2\tProject 2\tclosed\tid-2\n", + stdout.String()) +} + +func TestRunListWeb_User(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + client := queries.NewTestClient() + buf := bytes.Buffer{} + config := listConfig{ + opts: listOpts{ + owner: "monalisa", + web: true, + }, + URLOpener: func(url string) error { + buf.WriteString(url) + return nil + }, + client: client, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal(t, "https://github.com/users/monalisa/projects", buf.String()) +} + +func TestRunListWeb_Org(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + client := queries.NewTestClient() + buf := bytes.Buffer{} + config := listConfig{ + opts: listOpts{ + owner: "github", + web: true, + }, + URLOpener: func(url string) error { + buf.WriteString(url) + return nil + }, + client: client, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal(t, "https://github.com/orgs/github/projects", buf.String()) +} + +func TestRunListWeb_Me(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query Viewer.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + "login": "theviewer", + }, + }, + }) + + client := queries.NewTestClient() + buf := bytes.Buffer{} + config := listConfig{ + opts: listOpts{ + owner: "@me", + web: true, + }, + URLOpener: func(url string) error { + buf.WriteString(url) + return nil + }, + client: client, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal(t, "https://github.com/users/theviewer/projects", buf.String()) +} + +func TestRunListWeb_Empty(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query Viewer.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + "login": "theviewer", + }, + }, + }) + + client := queries.NewTestClient() + buf := bytes.Buffer{} + config := listConfig{ + opts: listOpts{ + web: true, + }, + URLOpener: func(url string) error { + buf.WriteString(url) + return nil + }, + client: client, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal(t, "https://github.com/users/theviewer/projects", buf.String()) +} + +func TestRunListWeb_Closed(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query Viewer.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + "login": "theviewer", + }, + }, + }) + + client := queries.NewTestClient() + buf := bytes.Buffer{} + config := listConfig{ + opts: listOpts{ + web: true, + closed: true, + }, + URLOpener: func(url string) error { + buf.WriteString(url) + return nil + }, + client: client, + } + + err := runList(config) + assert.NoError(t, err) + assert.Equal(t, "https://github.com/users/theviewer/projects?query=is%3Aclosed", buf.String()) +} diff --git a/pkg/cmd/project/project.go b/pkg/cmd/project/project.go new file mode 100644 index 000000000..dc3365696 --- /dev/null +++ b/pkg/cmd/project/project.go @@ -0,0 +1,61 @@ +package project + +import ( + "github.com/MakeNowJust/heredoc" + cmdClose "github.com/cli/cli/v2/pkg/cmd/project/close" + cmdCopy "github.com/cli/cli/v2/pkg/cmd/project/copy" + cmdCreate "github.com/cli/cli/v2/pkg/cmd/project/create" + cmdDelete "github.com/cli/cli/v2/pkg/cmd/project/delete" + cmdEdit "github.com/cli/cli/v2/pkg/cmd/project/edit" + cmdFieldCreate "github.com/cli/cli/v2/pkg/cmd/project/field-create" + cmdFieldDelete "github.com/cli/cli/v2/pkg/cmd/project/field-delete" + cmdFieldList "github.com/cli/cli/v2/pkg/cmd/project/field-list" + cmdItemAdd "github.com/cli/cli/v2/pkg/cmd/project/item-add" + cmdItemArchive "github.com/cli/cli/v2/pkg/cmd/project/item-archive" + cmdItemCreate "github.com/cli/cli/v2/pkg/cmd/project/item-create" + cmdItemDelete "github.com/cli/cli/v2/pkg/cmd/project/item-delete" + cmdItemEdit "github.com/cli/cli/v2/pkg/cmd/project/item-edit" + cmdItemList "github.com/cli/cli/v2/pkg/cmd/project/item-list" + cmdList "github.com/cli/cli/v2/pkg/cmd/project/list" + cmdView "github.com/cli/cli/v2/pkg/cmd/project/view" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/spf13/cobra" +) + +func NewCmdProject(f *cmdutil.Factory) *cobra.Command { + var cmd = &cobra.Command{ + Use: "project ", + Short: "Work with GitHub Projects.", + Long: "Work with GitHub Projects. Note that the token you are using must have 'project' scope, which is not set by default. You can verify your token scope by running 'gh auth status' and add the project scope by running 'gh auth refresh -s project'.", + Example: heredoc.Doc(` + $ gh project create --owner monalisa --title "Roadmap" + $ gh project view 1 --owner cli --web + $ gh project field-list 1 --owner cli + $ gh project item-list 1 --owner cli + `), + GroupID: "core", + } + + cmd.AddCommand(cmdList.NewCmdList(f, nil)) + cmd.AddCommand(cmdCreate.NewCmdCreate(f, nil)) + cmd.AddCommand(cmdCopy.NewCmdCopy(f, nil)) + cmd.AddCommand(cmdClose.NewCmdClose(f, nil)) + cmd.AddCommand(cmdDelete.NewCmdDelete(f, nil)) + cmd.AddCommand(cmdEdit.NewCmdEdit(f, nil)) + cmd.AddCommand(cmdView.NewCmdView(f, nil)) + + // items + cmd.AddCommand(cmdItemList.NewCmdList(f, nil)) + cmd.AddCommand(cmdItemCreate.NewCmdCreateItem(f, nil)) + cmd.AddCommand(cmdItemAdd.NewCmdAddItem(f, nil)) + cmd.AddCommand(cmdItemEdit.NewCmdEditItem(f, nil)) + cmd.AddCommand(cmdItemArchive.NewCmdArchiveItem(f, nil)) + cmd.AddCommand(cmdItemDelete.NewCmdDeleteItem(f, nil)) + + // fields + cmd.AddCommand(cmdFieldList.NewCmdList(f, nil)) + cmd.AddCommand(cmdFieldCreate.NewCmdCreateField(f, nil)) + cmd.AddCommand(cmdFieldDelete.NewCmdDeleteField(f, nil)) + + return cmd +} diff --git a/pkg/cmd/project/shared/client/client.go b/pkg/cmd/project/shared/client/client.go new file mode 100644 index 000000000..1f5835a54 --- /dev/null +++ b/pkg/cmd/project/shared/client/client.go @@ -0,0 +1,22 @@ +package client + +import ( + "os" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" +) + +func New(f *cmdutil.Factory) (*queries.Client, error) { + if f.HttpClient == nil { + // This is for compatibility with tests that exercise Cobra command functionality. + // These tests do not define a `HttpClient` nor do they need to. + return nil, nil + } + + httpClient, err := f.HttpClient() + if err != nil { + return nil, err + } + return queries.NewClient(httpClient, os.Getenv("GH_HOST"), f.IOStreams), nil +} diff --git a/pkg/cmd/project/shared/format/json.go b/pkg/cmd/project/shared/format/json.go new file mode 100644 index 000000000..17509c6c5 --- /dev/null +++ b/pkg/cmd/project/shared/format/json.go @@ -0,0 +1,369 @@ +package format + +import ( + "encoding/json" + "strings" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" +) + +// JSONProject serializes a Project to JSON. +func JSONProject(project queries.Project) ([]byte, error) { + return json.Marshal(projectJSON{ + Number: project.Number, + URL: project.URL, + ShortDescription: project.ShortDescription, + Public: project.Public, + Closed: project.Closed, + Title: project.Title, + ID: project.ID, + Readme: project.Readme, + Items: struct { + TotalCount int `json:"totalCount"` + }{ + TotalCount: project.Items.TotalCount, + }, + Fields: struct { + TotalCount int `json:"totalCount"` + }{ + TotalCount: project.Fields.TotalCount, + }, + Owner: struct { + Type string `json:"type"` + Login string `json:"login"` + }{ + Type: project.OwnerType(), + Login: project.OwnerLogin(), + }, + }) +} + +// JSONProjects serializes a slice of Projects to JSON. +// JSON fields are `totalCount` and `projects`. +func JSONProjects(projects []queries.Project, totalCount int) ([]byte, error) { + var result []projectJSON + for _, p := range projects { + result = append(result, projectJSON{ + Number: p.Number, + URL: p.URL, + ShortDescription: p.ShortDescription, + Public: p.Public, + Closed: p.Closed, + Title: p.Title, + ID: p.ID, + Readme: p.Readme, + Items: struct { + TotalCount int `json:"totalCount"` + }{ + TotalCount: p.Items.TotalCount, + }, + Fields: struct { + TotalCount int `json:"totalCount"` + }{ + TotalCount: p.Fields.TotalCount, + }, + Owner: struct { + Type string `json:"type"` + Login string `json:"login"` + }{ + Type: p.OwnerType(), + Login: p.OwnerLogin(), + }, + }) + } + + return json.Marshal(struct { + Projects []projectJSON `json:"projects"` + TotalCount int `json:"totalCount"` + }{ + Projects: result, + TotalCount: totalCount, + }) +} + +type projectJSON struct { + Number int32 `json:"number"` + URL string `json:"url"` + ShortDescription string `json:"shortDescription"` + Public bool `json:"public"` + Closed bool `json:"closed"` + Title string `json:"title"` + ID string `json:"id"` + Readme string `json:"readme"` + Items struct { + TotalCount int `json:"totalCount"` + } `graphql:"items(first: 100)" json:"items"` + Fields struct { + TotalCount int `json:"totalCount"` + } `graphql:"fields(first:100)" json:"fields"` + Owner struct { + Type string `json:"type"` + Login string `json:"login"` + } `json:"owner"` +} + +// JSONProjectField serializes a ProjectField to JSON. +func JSONProjectField(field queries.ProjectField) ([]byte, error) { + val := projectFieldJSON{ + ID: field.ID(), + Name: field.Name(), + Type: field.Type(), + } + for _, o := range field.Options() { + val.Options = append(val.Options, singleSelectOptionJSON{ + Name: o.Name, + ID: o.ID, + }) + } + + return json.Marshal(val) +} + +// JSONProjectFields serializes a slice of ProjectFields to JSON. +// JSON fields are `totalCount` and `fields`. +func JSONProjectFields(project *queries.Project) ([]byte, error) { + var result []projectFieldJSON + for _, f := range project.Fields.Nodes { + val := projectFieldJSON{ + ID: f.ID(), + Name: f.Name(), + Type: f.Type(), + } + for _, o := range f.Options() { + val.Options = append(val.Options, singleSelectOptionJSON{ + Name: o.Name, + ID: o.ID, + }) + } + + result = append(result, val) + } + + return json.Marshal(struct { + Fields []projectFieldJSON `json:"fields"` + TotalCount int `json:"totalCount"` + }{ + Fields: result, + TotalCount: project.Fields.TotalCount, + }) +} + +type projectFieldJSON struct { + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + Options []singleSelectOptionJSON `json:"options,omitempty"` +} + +type singleSelectOptionJSON struct { + ID string `json:"id"` + Name string `json:"name"` +} + +// JSONProjectItem serializes a ProjectItem to JSON. +func JSONProjectItem(item queries.ProjectItem) ([]byte, error) { + return json.Marshal(projectItemJSON{ + ID: item.ID(), + Title: item.Title(), + Body: item.Body(), + Type: item.Type(), + URL: item.URL(), + }) +} + +type projectItemJSON struct { + ID string `json:"id"` + Title string `json:"title"` + Body string `json:"body"` + Type string `json:"type"` + URL string `json:"url,omitempty"` +} + +// JSONProjectDraftIssue serializes a DraftIssue to JSON. +// This is needed because the field for +// https://docs.github.com/en/graphql/reference/mutations#updateprojectv2draftissue +// is a DraftIssue https://docs.github.com/en/graphql/reference/objects#draftissue +// and not a ProjectV2Item https://docs.github.com/en/graphql/reference/objects#projectv2item +func JSONProjectDraftIssue(item queries.DraftIssue) ([]byte, error) { + + return json.Marshal(draftIssueJSON{ + ID: item.ID, + Title: item.Title, + Body: item.Body, + Type: "DraftIssue", + }) +} + +type draftIssueJSON struct { + ID string `json:"id"` + Title string `json:"title"` + Body string `json:"body"` + Type string `json:"type"` +} + +func projectItemContent(p queries.ProjectItem) any { + switch p.Content.TypeName { + case "DraftIssue": + return struct { + Type string `json:"type"` + Body string `json:"body"` + Title string `json:"title"` + }{ + Type: p.Type(), + Body: p.Body(), + Title: p.Title(), + } + case "Issue": + return struct { + Type string `json:"type"` + Body string `json:"body"` + Title string `json:"title"` + Number int `json:"number"` + Repository string `json:"repository"` + URL string `json:"url"` + }{ + Type: p.Type(), + Body: p.Body(), + Title: p.Title(), + Number: p.Number(), + Repository: p.Repo(), + URL: p.URL(), + } + case "PullRequest": + return struct { + Type string `json:"type"` + Body string `json:"body"` + Title string `json:"title"` + Number int `json:"number"` + Repository string `json:"repository"` + URL string `json:"url"` + }{ + Type: p.Type(), + Body: p.Body(), + Title: p.Title(), + Number: p.Number(), + Repository: p.Repo(), + URL: p.URL(), + } + } + + return nil +} + +func projectFieldValueData(v queries.FieldValueNodes) any { + switch v.Type { + case "ProjectV2ItemFieldDateValue": + return v.ProjectV2ItemFieldDateValue.Date + case "ProjectV2ItemFieldIterationValue": + return struct { + Title string `json:"title"` + StartDate string `json:"startDate"` + Duration int `json:"duration"` + }{ + Title: v.ProjectV2ItemFieldIterationValue.Title, + StartDate: v.ProjectV2ItemFieldIterationValue.StartDate, + Duration: v.ProjectV2ItemFieldIterationValue.Duration, + } + case "ProjectV2ItemFieldNumberValue": + return v.ProjectV2ItemFieldNumberValue.Number + case "ProjectV2ItemFieldSingleSelectValue": + return v.ProjectV2ItemFieldSingleSelectValue.Name + case "ProjectV2ItemFieldTextValue": + return v.ProjectV2ItemFieldTextValue.Text + case "ProjectV2ItemFieldMilestoneValue": + return struct { + Title string `json:"title"` + Description string `json:"description"` + DueOn string `json:"dueOn"` + }{ + Title: v.ProjectV2ItemFieldMilestoneValue.Milestone.Title, + Description: v.ProjectV2ItemFieldMilestoneValue.Milestone.Description, + DueOn: v.ProjectV2ItemFieldMilestoneValue.Milestone.DueOn, + } + case "ProjectV2ItemFieldLabelValue": + names := make([]string, 0) + for _, p := range v.ProjectV2ItemFieldLabelValue.Labels.Nodes { + names = append(names, p.Name) + } + return names + case "ProjectV2ItemFieldPullRequestValue": + urls := make([]string, 0) + for _, p := range v.ProjectV2ItemFieldPullRequestValue.PullRequests.Nodes { + urls = append(urls, p.Url) + } + return urls + case "ProjectV2ItemFieldRepositoryValue": + return v.ProjectV2ItemFieldRepositoryValue.Repository.Url + case "ProjectV2ItemFieldUserValue": + logins := make([]string, 0) + for _, p := range v.ProjectV2ItemFieldUserValue.Users.Nodes { + logins = append(logins, p.Login) + } + return logins + case "ProjectV2ItemFieldReviewerValue": + names := make([]string, 0) + for _, p := range v.ProjectV2ItemFieldReviewerValue.Reviewers.Nodes { + if p.Type == "Team" { + names = append(names, p.Team.Name) + } else if p.Type == "User" { + names = append(names, p.User.Login) + } + } + return names + + } + + return nil +} + +// serialize creates a map from field to field values +func serializeProjectWithItems(project *queries.Project) []map[string]any { + fields := make(map[string]string) + + // make a map of fields by ID + for _, f := range project.Fields.Nodes { + fields[f.ID()] = CamelCase(f.Name()) + } + itemsSlice := make([]map[string]any, 0) + + // for each value, look up the name by ID + // and set the value to the field value + for _, i := range project.Items.Nodes { + o := make(map[string]any) + o["id"] = i.Id + o["content"] = projectItemContent(i) + for _, v := range i.FieldValues.Nodes { + id := v.ID() + value := projectFieldValueData(v) + + o[fields[id]] = value + } + itemsSlice = append(itemsSlice, o) + } + return itemsSlice +} + +// JSONProjectWithItems returns a detailed JSON representation of project items. +// JSON fields are `totalCount` and `items`. +func JSONProjectDetailedItems(project *queries.Project) ([]byte, error) { + items := serializeProjectWithItems(project) + return json.Marshal(struct { + Items []map[string]any `json:"items"` + TotalCount int `json:"totalCount"` + }{ + Items: items, + TotalCount: project.Items.TotalCount, + }) +} + +// CamelCase converts a string to camelCase, which is useful for turning Go field names to JSON keys. +func CamelCase(s string) string { + if len(s) == 0 { + return "" + } + + if len(s) == 1 { + return strings.ToLower(s) + } + return strings.ToLower(s[0:1]) + s[1:] +} diff --git a/pkg/cmd/project/shared/format/json_test.go b/pkg/cmd/project/shared/format/json_test.go new file mode 100644 index 000000000..c05a3e5ff --- /dev/null +++ b/pkg/cmd/project/shared/format/json_test.go @@ -0,0 +1,363 @@ +package format + +import ( + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + + "github.com/stretchr/testify/assert" +) + +func TestJSONProject_User(t *testing.T) { + project := queries.Project{ + ID: "123", + Number: 2, + URL: "a url", + ShortDescription: "short description", + Public: true, + Readme: "readme", + } + + project.Items.TotalCount = 1 + project.Fields.TotalCount = 2 + project.Owner.TypeName = "User" + project.Owner.User.Login = "monalisa" + b, err := JSONProject(project) + assert.NoError(t, err) + + assert.Equal(t, `{"number":2,"url":"a url","shortDescription":"short description","public":true,"closed":false,"title":"","id":"123","readme":"readme","items":{"totalCount":1},"fields":{"totalCount":2},"owner":{"type":"User","login":"monalisa"}}`, string(b)) +} + +func TestJSONProject_Org(t *testing.T) { + project := queries.Project{ + ID: "123", + Number: 2, + URL: "a url", + ShortDescription: "short description", + Public: true, + Readme: "readme", + } + + project.Items.TotalCount = 1 + project.Fields.TotalCount = 2 + project.Owner.TypeName = "Organization" + project.Owner.Organization.Login = "github" + b, err := JSONProject(project) + assert.NoError(t, err) + + assert.Equal(t, `{"number":2,"url":"a url","shortDescription":"short description","public":true,"closed":false,"title":"","id":"123","readme":"readme","items":{"totalCount":1},"fields":{"totalCount":2},"owner":{"type":"Organization","login":"github"}}`, string(b)) +} + +func TestJSONProjects(t *testing.T) { + userProject := queries.Project{ + ID: "123", + Number: 2, + URL: "a url", + ShortDescription: "short description", + Public: true, + Readme: "readme", + } + + userProject.Items.TotalCount = 1 + userProject.Fields.TotalCount = 2 + userProject.Owner.TypeName = "User" + userProject.Owner.User.Login = "monalisa" + + orgProject := queries.Project{ + ID: "123", + Number: 2, + URL: "a url", + ShortDescription: "short description", + Public: true, + Readme: "readme", + } + + orgProject.Items.TotalCount = 1 + orgProject.Fields.TotalCount = 2 + orgProject.Owner.TypeName = "Organization" + orgProject.Owner.Organization.Login = "github" + b, err := JSONProjects([]queries.Project{userProject, orgProject}, 2) + assert.NoError(t, err) + + assert.Equal( + t, + `{"projects":[{"number":2,"url":"a url","shortDescription":"short description","public":true,"closed":false,"title":"","id":"123","readme":"readme","items":{"totalCount":1},"fields":{"totalCount":2},"owner":{"type":"User","login":"monalisa"}},{"number":2,"url":"a url","shortDescription":"short description","public":true,"closed":false,"title":"","id":"123","readme":"readme","items":{"totalCount":1},"fields":{"totalCount":2},"owner":{"type":"Organization","login":"github"}}],"totalCount":2}`, + string(b)) +} + +func TestJSONProjectField_FieldType(t *testing.T) { + field := queries.ProjectField{} + field.TypeName = "ProjectV2Field" + field.Field.ID = "123" + field.Field.Name = "name" + + b, err := JSONProjectField(field) + assert.NoError(t, err) + + assert.Equal(t, `{"id":"123","name":"name","type":"ProjectV2Field"}`, string(b)) +} + +func TestJSONProjectField_SingleSelectType(t *testing.T) { + field := queries.ProjectField{} + field.TypeName = "ProjectV2SingleSelectField" + field.SingleSelectField.ID = "123" + field.SingleSelectField.Name = "name" + field.SingleSelectField.Options = []queries.SingleSelectFieldOptions{ + { + ID: "123", + Name: "name", + }, + { + ID: "456", + Name: "name2", + }, + } + + b, err := JSONProjectField(field) + assert.NoError(t, err) + + assert.Equal(t, `{"id":"123","name":"name","type":"ProjectV2SingleSelectField","options":[{"id":"123","name":"name"},{"id":"456","name":"name2"}]}`, string(b)) +} + +func TestJSONProjectField_ProjectV2IterationField(t *testing.T) { + field := queries.ProjectField{} + field.TypeName = "ProjectV2IterationField" + field.IterationField.ID = "123" + field.IterationField.Name = "name" + + b, err := JSONProjectField(field) + assert.NoError(t, err) + + assert.Equal(t, `{"id":"123","name":"name","type":"ProjectV2IterationField"}`, string(b)) +} + +func TestJSONProjectFields(t *testing.T) { + field := queries.ProjectField{} + field.TypeName = "ProjectV2Field" + field.Field.ID = "123" + field.Field.Name = "name" + + field2 := queries.ProjectField{} + field2.TypeName = "ProjectV2SingleSelectField" + field2.SingleSelectField.ID = "123" + field2.SingleSelectField.Name = "name" + field2.SingleSelectField.Options = []queries.SingleSelectFieldOptions{ + { + ID: "123", + Name: "name", + }, + { + ID: "456", + Name: "name2", + }, + } + + p := &queries.Project{ + Fields: struct { + TotalCount int + Nodes []queries.ProjectField + PageInfo queries.PageInfo + }{ + Nodes: []queries.ProjectField{field, field2}, + TotalCount: 5, + }, + } + b, err := JSONProjectFields(p) + assert.NoError(t, err) + + assert.Equal(t, `{"fields":[{"id":"123","name":"name","type":"ProjectV2Field"},{"id":"123","name":"name","type":"ProjectV2SingleSelectField","options":[{"id":"123","name":"name"},{"id":"456","name":"name2"}]}],"totalCount":5}`, string(b)) +} + +func TestJSONProjectItem_DraftIssue(t *testing.T) { + item := queries.ProjectItem{} + item.Content.TypeName = "DraftIssue" + item.Id = "123" + item.Content.DraftIssue.Title = "title" + item.Content.DraftIssue.Body = "a body" + + b, err := JSONProjectItem(item) + assert.NoError(t, err) + + assert.Equal(t, `{"id":"123","title":"title","body":"a body","type":"DraftIssue"}`, string(b)) +} + +func TestJSONProjectItem_Issue(t *testing.T) { + item := queries.ProjectItem{} + item.Content.TypeName = "Issue" + item.Id = "123" + item.Content.Issue.Title = "title" + item.Content.Issue.Body = "a body" + item.Content.Issue.URL = "a-url" + + b, err := JSONProjectItem(item) + assert.NoError(t, err) + + assert.Equal(t, `{"id":"123","title":"title","body":"a body","type":"Issue","url":"a-url"}`, string(b)) +} + +func TestJSONProjectItem_PullRequest(t *testing.T) { + item := queries.ProjectItem{} + item.Content.TypeName = "PullRequest" + item.Id = "123" + item.Content.PullRequest.Title = "title" + item.Content.PullRequest.Body = "a body" + item.Content.PullRequest.URL = "a-url" + + b, err := JSONProjectItem(item) + assert.NoError(t, err) + + assert.Equal(t, `{"id":"123","title":"title","body":"a body","type":"PullRequest","url":"a-url"}`, string(b)) +} + +func TestJSONProjectDetailedItems(t *testing.T) { + p := &queries.Project{} + p.Items.TotalCount = 5 + p.Items.Nodes = []queries.ProjectItem{ + { + Id: "issueId", + Content: queries.ProjectItemContent{ + TypeName: "Issue", + Issue: queries.Issue{ + Title: "Issue title", + Body: "a body", + Number: 1, + URL: "issue-url", + Repository: struct { + NameWithOwner string + }{ + NameWithOwner: "cli/go-gh", + }, + }, + }, + }, + { + Id: "pullRequestId", + Content: queries.ProjectItemContent{ + TypeName: "PullRequest", + PullRequest: queries.PullRequest{ + Title: "Pull Request title", + Body: "a body", + Number: 2, + URL: "pr-url", + Repository: struct { + NameWithOwner string + }{ + NameWithOwner: "cli/go-gh", + }, + }, + }, + }, + { + Id: "draftIssueId", + Content: queries.ProjectItemContent{ + TypeName: "DraftIssue", + DraftIssue: queries.DraftIssue{ + Title: "Pull Request title", + Body: "a body", + }, + }, + }, + } + + out, err := JSONProjectDetailedItems(p) + assert.NoError(t, err) + assert.Equal( + t, + `{"items":[{"content":{"type":"Issue","body":"a body","title":"Issue title","number":1,"repository":"cli/go-gh","url":"issue-url"},"id":"issueId"},{"content":{"type":"PullRequest","body":"a body","title":"Pull Request title","number":2,"repository":"cli/go-gh","url":"pr-url"},"id":"pullRequestId"},{"content":{"type":"DraftIssue","body":"a body","title":"Pull Request title"},"id":"draftIssueId"}],"totalCount":5}`, + string(out)) +} + +func TestJSONProjectDraftIssue(t *testing.T) { + item := queries.DraftIssue{} + item.ID = "123" + item.Title = "title" + item.Body = "a body" + + b, err := JSONProjectDraftIssue(item) + assert.NoError(t, err) + + assert.Equal(t, `{"id":"123","title":"title","body":"a body","type":"DraftIssue"}`, string(b)) +} + +func TestJSONProjectItem_DraftIssue_ProjectV2ItemFieldIterationValue(t *testing.T) { + iterationField := queries.ProjectField{TypeName: "ProjectV2IterationField"} + iterationField.IterationField.ID = "sprint" + iterationField.IterationField.Name = "Sprint" + + iterationFieldValue := queries.FieldValueNodes{Type: "ProjectV2ItemFieldIterationValue"} + iterationFieldValue.ProjectV2ItemFieldIterationValue.Title = "Iteration Title" + iterationFieldValue.ProjectV2ItemFieldIterationValue.Field = iterationField + + draftIssue := queries.ProjectItem{ + Id: "draftIssueId", + Content: queries.ProjectItemContent{ + TypeName: "DraftIssue", + DraftIssue: queries.DraftIssue{ + Title: "Pull Request title", + Body: "a body", + }, + }, + } + draftIssue.FieldValues.Nodes = []queries.FieldValueNodes{ + iterationFieldValue, + } + p := &queries.Project{} + p.Fields.Nodes = []queries.ProjectField{iterationField} + p.Items.TotalCount = 5 + p.Items.Nodes = []queries.ProjectItem{ + draftIssue, + } + + out, err := JSONProjectDetailedItems(p) + assert.NoError(t, err) + assert.JSONEq( + t, + `{"items":[{"sprint":{"title":"Iteration Title","startDate":"","duration":0},"content":{"type":"DraftIssue","body":"a body","title":"Pull Request title"},"id":"draftIssueId"}],"totalCount":5}`, + string(out)) + +} + +func TestJSONProjectItem_DraftIssue_ProjectV2ItemFieldMilestoneValue(t *testing.T) { + milestoneField := queries.ProjectField{TypeName: "ProjectV2IterationField"} + milestoneField.IterationField.ID = "milestone" + milestoneField.IterationField.Name = "Milestone" + + milestoneFieldValue := queries.FieldValueNodes{Type: "ProjectV2ItemFieldMilestoneValue"} + milestoneFieldValue.ProjectV2ItemFieldMilestoneValue.Milestone.Title = "Milestone Title" + milestoneFieldValue.ProjectV2ItemFieldMilestoneValue.Field = milestoneField + + draftIssue := queries.ProjectItem{ + Id: "draftIssueId", + Content: queries.ProjectItemContent{ + TypeName: "DraftIssue", + DraftIssue: queries.DraftIssue{ + Title: "Pull Request title", + Body: "a body", + }, + }, + } + draftIssue.FieldValues.Nodes = []queries.FieldValueNodes{ + milestoneFieldValue, + } + p := &queries.Project{} + p.Fields.Nodes = []queries.ProjectField{milestoneField} + p.Items.TotalCount = 5 + p.Items.Nodes = []queries.ProjectItem{ + draftIssue, + } + + out, err := JSONProjectDetailedItems(p) + assert.NoError(t, err) + assert.JSONEq( + t, + `{"items":[{"milestone":{"title":"Milestone Title","dueOn":"","description":""},"content":{"type":"DraftIssue","body":"a body","title":"Pull Request title"},"id":"draftIssueId"}],"totalCount":5}`, + string(out)) + +} + +func TestCamelCase(t *testing.T) { + assert.Equal(t, "camelCase", CamelCase("camelCase")) + assert.Equal(t, "camelCase", CamelCase("CamelCase")) + assert.Equal(t, "c", CamelCase("C")) + assert.Equal(t, "", CamelCase("")) +} diff --git a/pkg/cmd/project/shared/queries/queries.go b/pkg/cmd/project/shared/queries/queries.go new file mode 100644 index 000000000..b540b347f --- /dev/null +++ b/pkg/cmd/project/shared/queries/queries.go @@ -0,0 +1,1214 @@ +package queries + +import ( + "errors" + "fmt" + "net/http" + "net/url" + "regexp" + "strings" + "time" + + "github.com/briandowns/spinner" + "github.com/cli/cli/v2/api" + "github.com/cli/cli/v2/internal/prompter" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/cli/v2/pkg/set" + "github.com/shurcooL/githubv4" +) + +func NewClient(httpClient *http.Client, hostname string, ios *iostreams.IOStreams) *Client { + apiClient := &hostScopedClient{ + hostname: hostname, + Client: api.NewClientFromHTTP(httpClient), + } + return &Client{ + apiClient: apiClient, + spinner: ios.IsStdoutTTY() && ios.IsStderrTTY(), + prompter: prompter.New("", ios.In, ios.Out, ios.ErrOut), + } +} + +func NewTestClient() *Client { + apiClient := &hostScopedClient{ + hostname: "github.com", + Client: api.NewClientFromHTTP(http.DefaultClient), + } + return &Client{ + apiClient: apiClient, + spinner: false, + prompter: nil, + } +} + +type iprompter interface { + Select(string, string, []string) (int, error) +} + +type hostScopedClient struct { + *api.Client + hostname string +} + +func (c *hostScopedClient) Query(queryName string, query interface{}, variables map[string]interface{}) error { + return c.Client.Query(c.hostname, queryName, query, variables) +} + +func (c *hostScopedClient) Mutate(queryName string, query interface{}, variables map[string]interface{}) error { + return c.Client.Mutate(c.hostname, queryName, query, variables) +} + +type graphqlClient interface { + Query(queryName string, query interface{}, variables map[string]interface{}) error + Mutate(queryName string, query interface{}, variables map[string]interface{}) error +} + +type Client struct { + apiClient graphqlClient + spinner bool + prompter iprompter +} + +const ( + LimitDefault = 30 + LimitMax = 100 // https://docs.github.com/en/graphql/overview/resource-limitations#node-limit +) + +// doQuery wraps API calls with a visual spinner +func (c *Client) doQuery(name string, query interface{}, variables map[string]interface{}) error { + var sp *spinner.Spinner + if c.spinner { + // https://github.com/briandowns/spinner#available-character-sets + dotStyle := spinner.CharSets[11] + sp = spinner.New(dotStyle, 120*time.Millisecond, spinner.WithColor("fgCyan")) + sp.Start() + } + err := c.apiClient.Query(name, query, variables) + if sp != nil { + sp.Stop() + } + return handleError(err) +} + +// TODO: un-export this since it couples the caller heavily to api.GraphQLClient +func (c *Client) Mutate(operationName string, query interface{}, variables map[string]interface{}) error { + err := c.apiClient.Mutate(operationName, query, variables) + return handleError(err) +} + +// PageInfo is a PageInfo GraphQL object https://docs.github.com/en/graphql/reference/objects#pageinfo. +type PageInfo struct { + EndCursor githubv4.String + HasNextPage bool +} + +// Project is a ProjectV2 GraphQL object https://docs.github.com/en/graphql/reference/objects#projectv2. +type Project struct { + Number int32 + URL string + ShortDescription string + Public bool + Closed bool + Title string + ID string + Readme string + Items struct { + PageInfo PageInfo + TotalCount int + Nodes []ProjectItem + } `graphql:"items(first: $firstItems, after: $afterItems)"` + Fields struct { + TotalCount int + Nodes []ProjectField + PageInfo PageInfo + } `graphql:"fields(first: $firstFields, after: $afterFields)"` + Owner struct { + TypeName string `graphql:"__typename"` + User struct { + Login string + } `graphql:"... on User"` + Organization struct { + Login string + } `graphql:"... on Organization"` + } +} + +func (p Project) OwnerType() string { + return p.Owner.TypeName +} + +func (p Project) OwnerLogin() string { + if p.OwnerType() == "User" { + return p.Owner.User.Login + } + return p.Owner.Organization.Login +} + +// ProjectItem is a ProjectV2Item GraphQL object https://docs.github.com/en/graphql/reference/objects#projectv2item. +type ProjectItem struct { + Content ProjectItemContent + Id string + FieldValues struct { + Nodes []FieldValueNodes + } `graphql:"fieldValues(first: 100)"` // hardcoded to 100 for now on the assumption that this is a reasonable limit +} + +type ProjectItemContent struct { + TypeName string `graphql:"__typename"` + DraftIssue DraftIssue `graphql:"... on DraftIssue"` + PullRequest PullRequest `graphql:"... on PullRequest"` + Issue Issue `graphql:"... on Issue"` +} + +type FieldValueNodes struct { + Type string `graphql:"__typename"` + ProjectV2ItemFieldDateValue struct { + Date string + Field ProjectField + } `graphql:"... on ProjectV2ItemFieldDateValue"` + ProjectV2ItemFieldIterationValue struct { + Title string + StartDate string + Duration int + Field ProjectField + } `graphql:"... on ProjectV2ItemFieldIterationValue"` + ProjectV2ItemFieldLabelValue struct { + Labels struct { + Nodes []struct { + Name string + } + } `graphql:"labels(first: 10)"` // experienced issues with larger limits, 10 seems like enough for now + Field ProjectField + } `graphql:"... on ProjectV2ItemFieldLabelValue"` + ProjectV2ItemFieldNumberValue struct { + Number float32 + Field ProjectField + } `graphql:"... on ProjectV2ItemFieldNumberValue"` + ProjectV2ItemFieldSingleSelectValue struct { + Name string + Field ProjectField + } `graphql:"... on ProjectV2ItemFieldSingleSelectValue"` + ProjectV2ItemFieldTextValue struct { + Text string + Field ProjectField + } `graphql:"... on ProjectV2ItemFieldTextValue"` + ProjectV2ItemFieldMilestoneValue struct { + Milestone struct { + Title string + Description string + DueOn string + } + Field ProjectField + } `graphql:"... on ProjectV2ItemFieldMilestoneValue"` + ProjectV2ItemFieldPullRequestValue struct { + PullRequests struct { + Nodes []struct { + Url string + } + } `graphql:"pullRequests(first:10)"` // experienced issues with larger limits, 10 seems like enough for now + Field ProjectField + } `graphql:"... on ProjectV2ItemFieldPullRequestValue"` + ProjectV2ItemFieldRepositoryValue struct { + Repository struct { + Url string + } + Field ProjectField + } `graphql:"... on ProjectV2ItemFieldRepositoryValue"` + ProjectV2ItemFieldUserValue struct { + Users struct { + Nodes []struct { + Login string + } + } `graphql:"users(first: 10)"` // experienced issues with larger limits, 10 seems like enough for now + Field ProjectField + } `graphql:"... on ProjectV2ItemFieldUserValue"` + ProjectV2ItemFieldReviewerValue struct { + Reviewers struct { + Nodes []struct { + Type string `graphql:"__typename"` + Team struct { + Name string + } `graphql:"... on Team"` + User struct { + Login string + } `graphql:"... on User"` + } + } `graphql:"reviewers(first: 10)"` // experienced issues with larger limits, 10 seems like enough for now + Field ProjectField + } `graphql:"... on ProjectV2ItemFieldReviewerValue"` +} + +func (v FieldValueNodes) ID() string { + switch v.Type { + case "ProjectV2ItemFieldDateValue": + return v.ProjectV2ItemFieldDateValue.Field.ID() + case "ProjectV2ItemFieldIterationValue": + return v.ProjectV2ItemFieldIterationValue.Field.ID() + case "ProjectV2ItemFieldNumberValue": + return v.ProjectV2ItemFieldNumberValue.Field.ID() + case "ProjectV2ItemFieldSingleSelectValue": + return v.ProjectV2ItemFieldSingleSelectValue.Field.ID() + case "ProjectV2ItemFieldTextValue": + return v.ProjectV2ItemFieldTextValue.Field.ID() + case "ProjectV2ItemFieldMilestoneValue": + return v.ProjectV2ItemFieldMilestoneValue.Field.ID() + case "ProjectV2ItemFieldLabelValue": + return v.ProjectV2ItemFieldLabelValue.Field.ID() + case "ProjectV2ItemFieldPullRequestValue": + return v.ProjectV2ItemFieldPullRequestValue.Field.ID() + case "ProjectV2ItemFieldRepositoryValue": + return v.ProjectV2ItemFieldRepositoryValue.Field.ID() + case "ProjectV2ItemFieldUserValue": + return v.ProjectV2ItemFieldUserValue.Field.ID() + case "ProjectV2ItemFieldReviewerValue": + return v.ProjectV2ItemFieldReviewerValue.Field.ID() + } + + return "" +} + +type DraftIssue struct { + ID string + Body string + Title string +} + +type PullRequest struct { + Body string + Title string + Number int + URL string + Repository struct { + NameWithOwner string + } +} + +type Issue struct { + Body string + Title string + Number int + URL string + Repository struct { + NameWithOwner string + } +} + +// Type is the underlying type of the project item. +func (p ProjectItem) Type() string { + return p.Content.TypeName +} + +// Title is the title of the project item. +func (p ProjectItem) Title() string { + switch p.Content.TypeName { + case "Issue": + return p.Content.Issue.Title + case "PullRequest": + return p.Content.PullRequest.Title + case "DraftIssue": + return p.Content.DraftIssue.Title + } + return "" +} + +// Body is the body of the project item. +func (p ProjectItem) Body() string { + switch p.Content.TypeName { + case "Issue": + return p.Content.Issue.Body + case "PullRequest": + return p.Content.PullRequest.Body + case "DraftIssue": + return p.Content.DraftIssue.Body + } + return "" +} + +// Number is the number of the project item. It is only valid for issues and pull requests. +func (p ProjectItem) Number() int { + switch p.Content.TypeName { + case "Issue": + return p.Content.Issue.Number + case "PullRequest": + return p.Content.PullRequest.Number + } + + return 0 +} + +// ID is the id of the ProjectItem. +func (p ProjectItem) ID() string { + return p.Id +} + +// Repo is the repository of the project item. It is only valid for issues and pull requests. +func (p ProjectItem) Repo() string { + switch p.Content.TypeName { + case "Issue": + return p.Content.Issue.Repository.NameWithOwner + case "PullRequest": + return p.Content.PullRequest.Repository.NameWithOwner + } + return "" +} + +// URL is the URL of the project item. Note the draft issues do not have URLs +func (p ProjectItem) URL() string { + switch p.Content.TypeName { + case "Issue": + return p.Content.Issue.URL + case "PullRequest": + return p.Content.PullRequest.URL + } + return "" +} + +// ProjectItems returns the items of a project. If the OwnerType is VIEWER, no login is required. +// If limit is 0, the default limit is used. +func (c *Client) ProjectItems(o *Owner, number int32, limit int) (*Project, error) { + project := &Project{} + if limit == 0 { + limit = LimitDefault + } + + // set first to the min of limit and LimitMax + first := LimitMax + if limit < first { + first = limit + } + + variables := map[string]interface{}{ + "firstItems": githubv4.Int(first), + "afterItems": (*githubv4.String)(nil), + "firstFields": githubv4.Int(LimitMax), + "afterFields": (*githubv4.String)(nil), + "number": githubv4.Int(number), + } + + var query pager[ProjectItem] + var queryName string + switch o.Type { + case UserOwner: + variables["login"] = githubv4.String(o.Login) + query = &userOwnerWithItems{} // must be a pointer to work with graphql queries + queryName = "UserProjectWithItems" + case OrgOwner: + variables["login"] = githubv4.String(o.Login) + query = &orgOwnerWithItems{} // must be a pointer to work with graphql queries + queryName = "OrgProjectWithItems" + case ViewerOwner: + query = &viewerOwnerWithItems{} // must be a pointer to work with graphql queries + queryName = "ViewerProjectWithItems" + } + err := c.doQuery(queryName, query, variables) + if err != nil { + return project, err + } + project = query.Project() + + items, err := paginateAttributes(c, query, variables, queryName, "firstItems", "afterItems", limit, query.Nodes()) + if err != nil { + return project, err + } + + project.Items.Nodes = items + return project, nil +} + +// pager is an interface for paginating over the attributes of a Project. +type pager[N projectAttribute] interface { + HasNextPage() bool + EndCursor() string + Nodes() []N + Project() *Project +} + +// userOwnerWithItems +func (q userOwnerWithItems) HasNextPage() bool { + return q.Owner.Project.Items.PageInfo.HasNextPage +} + +func (q userOwnerWithItems) EndCursor() string { + return string(q.Owner.Project.Items.PageInfo.EndCursor) +} + +func (q userOwnerWithItems) Nodes() []ProjectItem { + return q.Owner.Project.Items.Nodes +} + +func (q userOwnerWithItems) Project() *Project { + return &q.Owner.Project +} + +// orgOwnerWithItems +func (q orgOwnerWithItems) HasNextPage() bool { + return q.Owner.Project.Items.PageInfo.HasNextPage +} + +func (q orgOwnerWithItems) EndCursor() string { + return string(q.Owner.Project.Items.PageInfo.EndCursor) +} + +func (q orgOwnerWithItems) Nodes() []ProjectItem { + return q.Owner.Project.Items.Nodes +} + +func (q orgOwnerWithItems) Project() *Project { + return &q.Owner.Project +} + +// viewerOwnerWithItems +func (q viewerOwnerWithItems) HasNextPage() bool { + return q.Owner.Project.Items.PageInfo.HasNextPage +} + +func (q viewerOwnerWithItems) EndCursor() string { + return string(q.Owner.Project.Items.PageInfo.EndCursor) +} + +func (q viewerOwnerWithItems) Nodes() []ProjectItem { + return q.Owner.Project.Items.Nodes +} + +func (q viewerOwnerWithItems) Project() *Project { + return &q.Owner.Project +} + +// userOwnerWithFields +func (q userOwnerWithFields) HasNextPage() bool { + return q.Owner.Project.Fields.PageInfo.HasNextPage +} + +func (q userOwnerWithFields) EndCursor() string { + return string(q.Owner.Project.Fields.PageInfo.EndCursor) +} + +func (q userOwnerWithFields) Nodes() []ProjectField { + return q.Owner.Project.Fields.Nodes +} + +func (q userOwnerWithFields) Project() *Project { + return &q.Owner.Project +} + +// orgOwnerWithFields +func (q orgOwnerWithFields) HasNextPage() bool { + return q.Owner.Project.Fields.PageInfo.HasNextPage +} + +func (q orgOwnerWithFields) EndCursor() string { + return string(q.Owner.Project.Fields.PageInfo.EndCursor) +} + +func (q orgOwnerWithFields) Nodes() []ProjectField { + return q.Owner.Project.Fields.Nodes +} + +func (q orgOwnerWithFields) Project() *Project { + return &q.Owner.Project +} + +// viewerOwnerWithFields +func (q viewerOwnerWithFields) HasNextPage() bool { + return q.Owner.Project.Fields.PageInfo.HasNextPage +} + +func (q viewerOwnerWithFields) EndCursor() string { + return string(q.Owner.Project.Fields.PageInfo.EndCursor) +} + +func (q viewerOwnerWithFields) Nodes() []ProjectField { + return q.Owner.Project.Fields.Nodes +} + +func (q viewerOwnerWithFields) Project() *Project { + return &q.Owner.Project +} + +type projectAttribute interface { + ProjectItem | ProjectField +} + +// paginateAttributes is for paginating over the attributes of a project, such as items or fields +// +// firstKey and afterKey are the keys in the variables map that are used to set the first and after +// as these are set independently based on the attribute type, such as item or field. +// +// limit is the maximum number of attributes to return, or 0 for no limit. +// +// nodes is the list of attributes that have already been fetched. +// +// the return value is a slice of the newly fetched attributes appended to nodes. +func paginateAttributes[N projectAttribute](c *Client, p pager[N], variables map[string]any, queryName string, firstKey string, afterKey string, limit int, nodes []N) ([]N, error) { + hasNextPage := p.HasNextPage() + cursor := p.EndCursor() + for { + if !hasNextPage || len(nodes) >= limit { + return nodes, nil + } + + if len(nodes)+LimitMax > limit { + first := limit - len(nodes) + variables[firstKey] = githubv4.Int(first) + } + + // set the cursor to the end of the last page + variables[afterKey] = (*githubv4.String)(&cursor) + err := c.doQuery(queryName, p, variables) + if err != nil { + return nodes, err + } + + nodes = append(nodes, p.Nodes()...) + hasNextPage = p.HasNextPage() + cursor = p.EndCursor() + } +} + +// ProjectField is a ProjectV2FieldConfiguration GraphQL object https://docs.github.com/en/graphql/reference/unions#projectv2fieldconfiguration. +type ProjectField struct { + TypeName string `graphql:"__typename"` + Field struct { + ID string + Name string + DataType string + } `graphql:"... on ProjectV2Field"` + IterationField struct { + ID string + Name string + DataType string + } `graphql:"... on ProjectV2IterationField"` + SingleSelectField struct { + ID string + Name string + DataType string + Options []SingleSelectFieldOptions + } `graphql:"... on ProjectV2SingleSelectField"` +} + +// ID is the ID of the project field. +func (p ProjectField) ID() string { + if p.TypeName == "ProjectV2Field" { + return p.Field.ID + } else if p.TypeName == "ProjectV2IterationField" { + return p.IterationField.ID + } else if p.TypeName == "ProjectV2SingleSelectField" { + return p.SingleSelectField.ID + } + return "" +} + +// Name is the name of the project field. +func (p ProjectField) Name() string { + if p.TypeName == "ProjectV2Field" { + return p.Field.Name + } else if p.TypeName == "ProjectV2IterationField" { + return p.IterationField.Name + } else if p.TypeName == "ProjectV2SingleSelectField" { + return p.SingleSelectField.Name + } + return "" +} + +// Type is the typename of the project field. +func (p ProjectField) Type() string { + return p.TypeName +} + +type SingleSelectFieldOptions struct { + ID string + Name string +} + +func (p ProjectField) Options() []SingleSelectFieldOptions { + if p.TypeName == "ProjectV2SingleSelectField" { + var options []SingleSelectFieldOptions + for _, o := range p.SingleSelectField.Options { + options = append(options, SingleSelectFieldOptions{ + ID: o.ID, + Name: o.Name, + }) + } + return options + } + return nil +} + +// ProjectFields returns a project with fields. If the OwnerType is VIEWER, no login is required. +// If limit is 0, the default limit is used. +func (c *Client) ProjectFields(o *Owner, number int32, limit int) (*Project, error) { + project := &Project{} + if limit == 0 { + limit = LimitDefault + } + + // set first to the min of limit and LimitMax + first := LimitMax + if limit < first { + first = limit + } + variables := map[string]interface{}{ + "firstItems": githubv4.Int(LimitMax), + "afterItems": (*githubv4.String)(nil), + "firstFields": githubv4.Int(first), + "afterFields": (*githubv4.String)(nil), + "number": githubv4.Int(number), + } + + var query pager[ProjectField] + var queryName string + switch o.Type { + case UserOwner: + variables["login"] = githubv4.String(o.Login) + query = &userOwnerWithFields{} // must be a pointer to work with graphql queries + queryName = "UserProjectWithFields" + case OrgOwner: + variables["login"] = githubv4.String(o.Login) + query = &orgOwnerWithFields{} // must be a pointer to work with graphql queries + queryName = "OrgProjectWithFields" + case ViewerOwner: + query = &viewerOwnerWithFields{} // must be a pointer to work with graphql queries + queryName = "ViewerProjectWithFields" + } + err := c.doQuery(queryName, query, variables) + if err != nil { + return project, err + } + project = query.Project() + + fields, err := paginateAttributes(c, query, variables, queryName, "firstFields", "afterFields", limit, query.Nodes()) + if err != nil { + return project, err + } + + project.Fields.Nodes = fields + return project, nil +} + +// viewerLogin is used to query the Login of the viewer. +type viewerLogin struct { + Viewer struct { + Login string + Id string + } +} + +type viewerLoginOrgs struct { + Viewer struct { + Login string + ID string + Organizations struct { + PageInfo PageInfo + Nodes []struct { + Login string + ViewerCanCreateProjects bool + ID string + } + } `graphql:"organizations(first: 100, after: $after)"` + } +} + +// userOwner is used to query the project of a user. +type userOwner struct { + Owner struct { + Project Project `graphql:"projectV2(number: $number)"` + Login string + } `graphql:"user(login: $login)"` +} + +// userOwnerWithItems is used to query the project of a user with its items. +type userOwnerWithItems struct { + Owner struct { + Project Project `graphql:"projectV2(number: $number)"` + } `graphql:"user(login: $login)"` +} + +// userOwnerWithFields is used to query the project of a user with its fields. +type userOwnerWithFields struct { + Owner struct { + Project Project `graphql:"projectV2(number: $number)"` + } `graphql:"user(login: $login)"` +} + +// orgOwner is used to query the project of an organization. +type orgOwner struct { + Owner struct { + Project Project `graphql:"projectV2(number: $number)"` + Login string + } `graphql:"organization(login: $login)"` +} + +// orgOwnerWithItems is used to query the project of an organization with its items. +type orgOwnerWithItems struct { + Owner struct { + Project Project `graphql:"projectV2(number: $number)"` + } `graphql:"organization(login: $login)"` +} + +// orgOwnerWithFields is used to query the project of an organization with its fields. +type orgOwnerWithFields struct { + Owner struct { + Project Project `graphql:"projectV2(number: $number)"` + } `graphql:"organization(login: $login)"` +} + +// viewerOwner is used to query the project of the viewer. +type viewerOwner struct { + Owner struct { + Project Project `graphql:"projectV2(number: $number)"` + Login string + } `graphql:"viewer"` +} + +// viewerOwnerWithItems is used to query the project of the viewer with its items. +type viewerOwnerWithItems struct { + Owner struct { + Project Project `graphql:"projectV2(number: $number)"` + } `graphql:"viewer"` +} + +// viewerOwnerWithFields is used to query the project of the viewer with its fields. +type viewerOwnerWithFields struct { + Owner struct { + Project Project `graphql:"projectV2(number: $number)"` + } `graphql:"viewer"` +} + +// OwnerType is the type of the owner of a project, which can be either a user or an organization. Viewer is the current user. +type OwnerType string + +const UserOwner OwnerType = "USER" +const OrgOwner OwnerType = "ORGANIZATION" +const ViewerOwner OwnerType = "VIEWER" + +// ViewerLoginName returns the login name of the viewer. +func (c *Client) ViewerLoginName() (string, error) { + var query viewerLogin + err := c.doQuery("Viewer", &query, map[string]interface{}{}) + if err != nil { + return "", err + } + return query.Viewer.Login, nil +} + +// OwnerIDAndType returns the ID and OwnerType. The special login "@me" or an empty string queries the current user. +func (c *Client) OwnerIDAndType(login string) (string, OwnerType, error) { + if login == "@me" || login == "" { + var query viewerLogin + err := c.doQuery("ViewerOwner", &query, nil) + if err != nil { + return "", "", err + } + return query.Viewer.Id, ViewerOwner, nil + } + + variables := map[string]interface{}{ + "login": githubv4.String(login), + } + var query struct { + User struct { + Login string + Id string + } `graphql:"user(login: $login)"` + Organization struct { + Login string + Id string + } `graphql:"organization(login: $login)"` + } + + err := c.doQuery("UserOrgOwner", &query, variables) + if err != nil { + // Due to the way the queries are structured, we don't know if a login belongs to a user + // or to an org, even though they are unique. To deal with this, we try both - if neither + // is found, we return the error. + var graphErr api.GraphQLError + if errors.As(err, &graphErr) { + if graphErr.Match("NOT_FOUND", "user") && graphErr.Match("NOT_FOUND", "organization") { + return "", "", err + } else if graphErr.Match("NOT_FOUND", "organization") { // org isn't found must be a user + return query.User.Id, UserOwner, nil + } else if graphErr.Match("NOT_FOUND", "user") { // user isn't found must be an org + return query.Organization.Id, OrgOwner, nil + } + } + } + + return "", "", errors.New("unknown owner type") +} + +// issueOrPullRequest is used to query the global id of an issue or pull request by its URL. +type issueOrPullRequest struct { + Resource struct { + Typename string `graphql:"__typename"` + Issue struct { + ID string + } `graphql:"... on Issue"` + PullRequest struct { + ID string + } `graphql:"... on PullRequest"` + } `graphql:"resource(url: $url)"` +} + +// IssueOrPullRequestID returns the ID of the issue or pull request from a URL. +func (c *Client) IssueOrPullRequestID(rawURL string) (string, error) { + uri, err := url.Parse(rawURL) + if err != nil { + return "", err + } + variables := map[string]interface{}{ + "url": githubv4.URI{URL: uri}, + } + var query issueOrPullRequest + err = c.doQuery("GetIssueOrPullRequest", &query, variables) + if err != nil { + return "", err + } + if query.Resource.Typename == "Issue" { + return query.Resource.Issue.ID, nil + } else if query.Resource.Typename == "PullRequest" { + return query.Resource.PullRequest.ID, nil + } + return "", errors.New("resource not found, please check the URL") +} + +// userProjects queries the $first projects of a user. +type userProjects struct { + Owner struct { + Projects struct { + TotalCount int + PageInfo PageInfo + Nodes []Project + } `graphql:"projectsV2(first: $first, after: $after)"` + Login string + } `graphql:"user(login: $login)"` +} + +// orgProjects queries the $first projects of an organization. +type orgProjects struct { + Owner struct { + Projects struct { + TotalCount int + PageInfo PageInfo + Nodes []Project + } `graphql:"projectsV2(first: $first, after: $after)"` + Login string + } `graphql:"organization(login: $login)"` +} + +// viewerProjects queries the $first projects of the viewer. +type viewerProjects struct { + Owner struct { + Projects struct { + TotalCount int + PageInfo PageInfo + Nodes []Project + } `graphql:"projectsV2(first: $first, after: $after)"` + Login string + } `graphql:"viewer"` +} + +type loginTypes struct { + Login string + Type OwnerType + ID string +} + +// userOrgLogins gets all the logins of the viewer and the organizations the viewer is a member of. +func (c *Client) userOrgLogins() ([]loginTypes, error) { + l := make([]loginTypes, 0) + var v viewerLoginOrgs + variables := map[string]interface{}{ + "after": (*githubv4.String)(nil), + } + + err := c.doQuery("ViewerLoginAndOrgs", &v, variables) + if err != nil { + return l, err + } + + // add the user + l = append(l, loginTypes{ + Login: v.Viewer.Login, + Type: ViewerOwner, + ID: v.Viewer.ID, + }) + + // add orgs where the user can create projects + for _, org := range v.Viewer.Organizations.Nodes { + if org.ViewerCanCreateProjects { + l = append(l, loginTypes{ + Login: org.Login, + Type: OrgOwner, + ID: org.ID, + }) + } + } + + // this seem unlikely, but if there are more org logins, paginate the rest + if v.Viewer.Organizations.PageInfo.HasNextPage { + return c.paginateOrgLogins(l, string(v.Viewer.Organizations.PageInfo.EndCursor)) + } + + return l, nil +} + +// paginateOrgLogins after cursor and append them to the list of logins. +func (c *Client) paginateOrgLogins(l []loginTypes, cursor string) ([]loginTypes, error) { + var v viewerLoginOrgs + variables := map[string]interface{}{ + "after": githubv4.String(cursor), + } + + err := c.doQuery("ViewerLoginAndOrgs", &v, variables) + if err != nil { + return l, err + } + + for _, org := range v.Viewer.Organizations.Nodes { + if org.ViewerCanCreateProjects { + l = append(l, loginTypes{ + Login: org.Login, + Type: OrgOwner, + ID: org.ID, + }) + } + } + + if v.Viewer.Organizations.PageInfo.HasNextPage { + return c.paginateOrgLogins(l, string(v.Viewer.Organizations.PageInfo.EndCursor)) + } + + return l, nil +} + +type Owner struct { + Login string + Type OwnerType + ID string +} + +// NewOwner creates a project Owner +// If canPrompt is false, login is required as we cannot prompt for it. +// If login is not empty, it is used to lookup the project owner. +// If login is empty empty, interative mode is used to select an owner. +// from the current viewer and their organizations +func (c *Client) NewOwner(canPrompt bool, login string) (*Owner, error) { + if login != "" { + id, ownerType, err := c.OwnerIDAndType(login) + if err != nil { + return nil, err + } + + return &Owner{ + Login: login, + Type: ownerType, + ID: id, + }, nil + } + + if !canPrompt { + return nil, fmt.Errorf("login is required when not running interactively") + } + + logins, err := c.userOrgLogins() + if err != nil { + return nil, err + } + + options := make([]string, 0, len(logins)) + for _, l := range logins { + options = append(options, l.Login) + } + + answerIndex, err := c.prompter.Select("Which owner would you like to use?", "", options) + if err != nil { + return nil, err + } + + l := logins[answerIndex] + return &Owner{ + Login: l.Login, + Type: l.Type, + ID: l.ID, + }, nil +} + +// NewProject creates a project based on the owner and project number +// if canPrompt is false, number is required as we cannot prompt for it +// if number is 0 it will prompt the user to select a project interactively +// otherwise it will make a request to get the project by number +// set `fields“ to true to get the project's field data +func (c *Client) NewProject(canPrompt bool, o *Owner, number int32, fields bool) (*Project, error) { + if number != 0 { + variables := map[string]interface{}{ + "number": githubv4.Int(number), + "firstItems": githubv4.Int(0), + "afterItems": (*githubv4.String)(nil), + "firstFields": githubv4.Int(0), + "afterFields": (*githubv4.String)(nil), + } + + if fields { + variables["firstFields"] = githubv4.Int(LimitMax) + } + if o.Type == UserOwner { + var query userOwner + variables["login"] = githubv4.String(o.Login) + err := c.doQuery("UserProject", &query, variables) + return &query.Owner.Project, err + } else if o.Type == OrgOwner { + variables["login"] = githubv4.String(o.Login) + var query orgOwner + err := c.doQuery("OrgProject", &query, variables) + return &query.Owner.Project, err + } else if o.Type == ViewerOwner { + var query viewerOwner + err := c.doQuery("ViewerProject", &query, variables) + return &query.Owner.Project, err + } + return nil, errors.New("unknown owner type") + } + + if !canPrompt { + return nil, fmt.Errorf("project number is required when not running interactively") + } + + projects, _, err := c.Projects(o.Login, o.Type, 0, fields) + if err != nil { + return nil, err + } + + if len(projects) == 0 { + return nil, fmt.Errorf("no projects found for %s", o.Login) + } + + options := make([]string, 0, len(projects)) + for _, p := range projects { + title := fmt.Sprintf("%s (#%d)", p.Title, p.Number) + options = append(options, title) + } + + answerIndex, err := c.prompter.Select("Which project would you like to use?", "", options) + if err != nil { + return nil, err + } + + return &projects[answerIndex], nil +} + +// Projects returns all the projects for an Owner. If the OwnerType is VIEWER, no login is required. +// If limit is 0, the default limit is used. +func (c *Client) Projects(login string, t OwnerType, limit int, fields bool) ([]Project, int, error) { + projects := make([]Project, 0) + cursor := (*githubv4.String)(nil) + hasNextPage := false + totalCount := 0 + + if limit == 0 { + limit = LimitDefault + } + + // set first to the min of limit and LimitMax + first := LimitMax + if limit < first { + first = limit + } + + variables := map[string]interface{}{ + "first": githubv4.Int(first), + "after": cursor, + "firstItems": githubv4.Int(0), + "afterItems": (*githubv4.String)(nil), + "firstFields": githubv4.Int(0), + "afterFields": (*githubv4.String)(nil), + } + + if fields { + variables["firstFields"] = githubv4.Int(LimitMax) + } + + if t != ViewerOwner { + variables["login"] = githubv4.String(login) + } + // loop until we get all the projects + for { + // the code below is very repetitive, the only real difference being the type of the query + // and the query variables. I couldn't figure out a way to make this cleaner that was worth + // the cost. + if t == UserOwner { + var query userProjects + if err := c.doQuery("UserProjects", &query, variables); err != nil { + return projects, 0, err + } + projects = append(projects, query.Owner.Projects.Nodes...) + hasNextPage = query.Owner.Projects.PageInfo.HasNextPage + cursor = &query.Owner.Projects.PageInfo.EndCursor + totalCount = query.Owner.Projects.TotalCount + } else if t == OrgOwner { + var query orgProjects + if err := c.doQuery("OrgProjects", &query, variables); err != nil { + return projects, 0, err + } + projects = append(projects, query.Owner.Projects.Nodes...) + hasNextPage = query.Owner.Projects.PageInfo.HasNextPage + cursor = &query.Owner.Projects.PageInfo.EndCursor + totalCount = query.Owner.Projects.TotalCount + } else if t == ViewerOwner { + var query viewerProjects + if err := c.doQuery("ViewerProjects", &query, variables); err != nil { + return projects, 0, err + } + projects = append(projects, query.Owner.Projects.Nodes...) + hasNextPage = query.Owner.Projects.PageInfo.HasNextPage + cursor = &query.Owner.Projects.PageInfo.EndCursor + totalCount = query.Owner.Projects.TotalCount + } + + if !hasNextPage || len(projects) >= limit { + return projects, totalCount, nil + } + + if len(projects)+LimitMax > limit { + first := limit - len(projects) + variables["first"] = githubv4.Int(first) + } + variables["after"] = cursor + } +} + +func handleError(err error) error { + var gerr api.GraphQLError + if errors.As(err, &gerr) { + missing := set.NewStringSet() + for _, e := range gerr.Errors { + if e.Type != "INSUFFICIENT_SCOPES" { + continue + } + missing.AddValues(requiredScopesFromServerMessage(e.Message)) + } + if missing.Len() > 0 { + s := missing.ToSlice() + // TODO: this duplicates parts of generateScopesSuggestion + return fmt.Errorf( + "error: your authentication token is missing required scopes %v\n"+ + "To request it, run: gh auth refresh -s %s", + s, + strings.Join(s, ",")) + } + } + return err +} + +var scopesRE = regexp.MustCompile(`one of the following scopes: \[(.+?)]`) + +func requiredScopesFromServerMessage(msg string) []string { + m := scopesRE.FindStringSubmatch(msg) + if m == nil { + return nil + } + var scopes []string + for _, mm := range strings.Split(m[1], ",") { + scopes = append(scopes, strings.Trim(mm, "' ")) + } + return scopes +} diff --git a/pkg/cmd/project/shared/queries/queries_test.go b/pkg/cmd/project/shared/queries/queries_test.go new file mode 100644 index 000000000..e1cf8c68b --- /dev/null +++ b/pkg/cmd/project/shared/queries/queries_test.go @@ -0,0 +1,430 @@ +package queries + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestProjectItems_DefaultLimit(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // list project items + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query UserProjectWithItems.*", + "variables": map[string]interface{}{ + "firstItems": LimitMax, + "afterItems": nil, + "firstFields": LimitMax, + "afterFields": nil, + "login": "monalisa", + "number": 1, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "items": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "id": "issue ID", + }, + { + "id": "pull request ID", + }, + { + "id": "draft issue ID", + }, + }, + }, + }, + }, + }, + }) + + client := NewTestClient() + + owner := &Owner{ + Type: "USER", + Login: "monalisa", + ID: "user ID", + } + project, err := client.ProjectItems(owner, 1, LimitMax) + assert.NoError(t, err) + assert.Len(t, project.Items.Nodes, 3) +} + +func TestProjectItems_LowerLimit(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // list project items + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query UserProjectWithItems.*", + "variables": map[string]interface{}{ + "firstItems": 2, + "afterItems": nil, + "firstFields": LimitMax, + "afterFields": nil, + "login": "monalisa", + "number": 1, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "items": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "id": "issue ID", + }, + { + "id": "pull request ID", + }, + }, + }, + }, + }, + }, + }) + + client := NewTestClient() + + owner := &Owner{ + Type: "USER", + Login: "monalisa", + ID: "user ID", + } + project, err := client.ProjectItems(owner, 1, 2) + assert.NoError(t, err) + assert.Len(t, project.Items.Nodes, 2) +} + +func TestProjectItems_NoLimit(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // list project items + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query UserProjectWithItems.*", + "variables": map[string]interface{}{ + "firstItems": LimitDefault, + "afterItems": nil, + "firstFields": LimitMax, + "afterFields": nil, + "login": "monalisa", + "number": 1, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "items": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "id": "issue ID", + }, + { + "id": "pull request ID", + }, + { + "id": "draft issue ID", + }, + }, + }, + }, + }, + }, + }) + + client := NewTestClient() + + owner := &Owner{ + Type: "USER", + Login: "monalisa", + ID: "user ID", + } + project, err := client.ProjectItems(owner, 1, 0) + assert.NoError(t, err) + assert.Len(t, project.Items.Nodes, 3) +} + +func TestProjectFields_LowerLimit(t *testing.T) { + + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // list project fields + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": LimitMax, + "afterItems": nil, + "firstFields": 2, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "fields": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "id": "field ID", + }, + { + "id": "status ID", + }, + }, + }, + }, + }, + }, + }) + + client := NewTestClient() + owner := &Owner{ + Type: "USER", + Login: "monalisa", + ID: "user ID", + } + project, err := client.ProjectFields(owner, 1, 2) + assert.NoError(t, err) + assert.Len(t, project.Fields.Nodes, 2) +} + +func TestProjectFields_DefaultLimit(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // list project fields + // list project fields + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": LimitMax, + "afterItems": nil, + "firstFields": LimitMax, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "fields": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "id": "field ID", + }, + { + "id": "status ID", + }, + { + "id": "iteration ID", + }, + }, + }, + }, + }, + }, + }) + + client := NewTestClient() + + owner := &Owner{ + Type: "USER", + Login: "monalisa", + ID: "user ID", + } + project, err := client.ProjectFields(owner, 1, LimitMax) + assert.NoError(t, err) + assert.Len(t, project.Fields.Nodes, 3) +} + +func TestProjectFields_NoLimit(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // list project fields + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query UserProject.*", + "variables": map[string]interface{}{ + "login": "monalisa", + "number": 1, + "firstItems": LimitMax, + "afterItems": nil, + "firstFields": LimitDefault, + "afterFields": nil, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "fields": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "id": "field ID", + }, + { + "id": "status ID", + }, + { + "id": "iteration ID", + }, + }, + }, + }, + }, + }, + }) + + client := NewTestClient() + + owner := &Owner{ + Type: "USER", + Login: "monalisa", + ID: "user ID", + } + project, err := client.ProjectFields(owner, 1, 0) + assert.NoError(t, err) + assert.Len(t, project.Fields.Nodes, 3) +} + +func Test_requiredScopesFromServerMessage(t *testing.T) { + tests := []struct { + name string + msg string + want []string + }{ + { + name: "no scopes", + msg: "SERVER OOPSIE", + want: []string(nil), + }, + { + name: "one scope", + msg: "Your token has not been granted the required scopes to execute this query. The 'dataType' field requires one of the following scopes: ['read:project'], but your token has only been granted the: ['codespace', repo'] scopes. Please modify your token's scopes at: https://github.com/settings/tokens.", + want: []string{"read:project"}, + }, + { + name: "multiple scopes", + msg: "Your token has not been granted the required scopes to execute this query. The 'dataType' field requires one of the following scopes: ['read:project', 'read:discussion', 'codespace'], but your token has only been granted the: [repo'] scopes. Please modify your token's scopes at: https://github.com/settings/tokens.", + want: []string{"read:project", "read:discussion", "codespace"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := requiredScopesFromServerMessage(tt.msg); !reflect.DeepEqual(got, tt.want) { + t.Errorf("requiredScopesFromServerMessage() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNewProject_nonTTY(t *testing.T) { + client := NewTestClient() + _, err := client.NewProject(false, &Owner{}, 0, false) + assert.EqualError(t, err, "project number is required when not running interactively") +} + +func TestNewOwner_nonTTY(t *testing.T) { + client := NewTestClient() + _, err := client.NewOwner(false, "") + assert.EqualError(t, err, "login is required when not running interactively") + +} + +func TestProjectItems_FieldTitle(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + + // list project items + gock.New("https://api.github.com"). + Post("/graphql"). + JSON(map[string]interface{}{ + "query": "query UserProjectWithItems.*", + "variables": map[string]interface{}{ + "firstItems": LimitMax, + "afterItems": nil, + "firstFields": LimitMax, + "afterFields": nil, + "login": "monalisa", + "number": 1, + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "projectV2": map[string]interface{}{ + "items": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "id": "draft issue ID", + "fieldValues": map[string]interface{}{ + "nodes": []map[string]interface{}{ + { + "__typename": "ProjectV2ItemFieldIterationValue", + "title": "Iteration Title 1", + }, + { + "__typename": "ProjectV2ItemFieldMilestoneValue", + "milestone": map[string]interface{}{ + "title": "Milestone Title 1", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }) + + client := NewTestClient() + + owner := &Owner{ + Type: "USER", + Login: "monalisa", + ID: "user ID", + } + project, err := client.ProjectItems(owner, 1, LimitMax) + assert.NoError(t, err) + assert.Len(t, project.Items.Nodes, 1) + assert.Len(t, project.Items.Nodes[0].FieldValues.Nodes, 2) + assert.Equal(t, project.Items.Nodes[0].FieldValues.Nodes[0].ProjectV2ItemFieldIterationValue.Title, "Iteration Title 1") + assert.Equal(t, project.Items.Nodes[0].FieldValues.Nodes[1].ProjectV2ItemFieldMilestoneValue.Milestone.Title, "Milestone Title 1") +} diff --git a/pkg/cmd/project/view/view.go b/pkg/cmd/project/view/view.go new file mode 100644 index 000000000..36eb148e3 --- /dev/null +++ b/pkg/cmd/project/view/view.go @@ -0,0 +1,203 @@ +package view + +import ( + "fmt" + "strconv" + "strings" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/pkg/cmd/project/shared/client" + "github.com/cli/cli/v2/pkg/cmd/project/shared/format" + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/cli/v2/pkg/markdown" + "github.com/spf13/cobra" +) + +type viewOpts struct { + web bool + owner string + number int32 + format string +} + +type viewConfig struct { + client *queries.Client + opts viewOpts + io *iostreams.IOStreams + URLOpener func(string) error +} + +func NewCmdView(f *cmdutil.Factory, runF func(config viewConfig) error) *cobra.Command { + opts := viewOpts{} + viewCmd := &cobra.Command{ + Short: "View a project", + Use: "view []", + Example: heredoc.Doc(` + # view the current user's project "1" + gh project view 1 + + # open user monalisa's project "1" in the browser + gh project view 1 --owner monalisa --web + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := client.New(f) + if err != nil { + return err + } + + URLOpener := func(url string) error { + return f.Browser.Browse(url) + } + + if len(args) == 1 { + num, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return cmdutil.FlagErrorf("invalid number: %v", args[0]) + } + opts.number = int32(num) + } + + config := viewConfig{ + client: client, + opts: opts, + io: f.IOStreams, + URLOpener: URLOpener, + } + + // allow testing of the command without actually running it + if runF != nil { + return runF(config) + } + return runView(config) + }, + } + + viewCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") + viewCmd.Flags().BoolVarP(&opts.web, "web", "w", false, "Open a project in the browser") + cmdutil.StringEnumFlag(viewCmd, &opts.format, "format", "", "", []string{"json"}, "Output format") + + return viewCmd +} + +func runView(config viewConfig) error { + if config.opts.web { + url, err := buildURL(config) + if err != nil { + return err + } + + if err := config.URLOpener(url); err != nil { + return err + } + return nil + } + + canPrompt := config.io.CanPrompt() + owner, err := config.client.NewOwner(canPrompt, config.opts.owner) + if err != nil { + return err + } + + project, err := config.client.NewProject(canPrompt, owner, config.opts.number, true) + if err != nil { + return err + } + + if config.opts.format == "json" { + return printJSON(config, *project) + } + + return printResults(config, project) +} + +// TODO: support non-github.com hostnames +func buildURL(config viewConfig) (string, error) { + var url string + if config.opts.owner == "@me" { + owner, err := config.client.ViewerLoginName() + if err != nil { + return "", err + } + url = fmt.Sprintf("https://github.com/users/%s/projects/%d", owner, config.opts.number) + } else { + _, ownerType, err := config.client.OwnerIDAndType(config.opts.owner) + if err != nil { + return "", err + } + + if ownerType == queries.UserOwner { + url = fmt.Sprintf("https://github.com/users/%s/projects/%d", config.opts.owner, config.opts.number) + } else { + url = fmt.Sprintf("https://github.com/orgs/%s/projects/%d", config.opts.owner, config.opts.number) + } + } + + return url, nil +} + +func printResults(config viewConfig, project *queries.Project) error { + var sb strings.Builder + sb.WriteString("# Title\n") + sb.WriteString(project.Title) + sb.WriteString("\n") + + sb.WriteString("## Description\n") + if project.ShortDescription == "" { + sb.WriteString(" -- ") + } else { + sb.WriteString(project.ShortDescription) + } + sb.WriteString("\n") + + sb.WriteString("## Visibility\n") + if project.Public { + sb.WriteString("Public") + } else { + sb.WriteString("Private") + } + sb.WriteString("\n") + + sb.WriteString("## URL\n") + sb.WriteString(project.URL) + sb.WriteString("\n") + + sb.WriteString("## Item count\n") + sb.WriteString(fmt.Sprintf("%d", project.Items.TotalCount)) + sb.WriteString("\n") + + sb.WriteString("## Readme\n") + if project.Readme == "" { + sb.WriteString(" -- ") + } else { + sb.WriteString(project.Readme) + } + sb.WriteString("\n") + + sb.WriteString("## Field Name (Field Type)\n") + for _, f := range project.Fields.Nodes { + sb.WriteString(fmt.Sprintf("%s (%s)\n\n", f.Name(), f.Type())) + } + + out, err := markdown.Render(sb.String(), + markdown.WithTheme(config.io.TerminalTheme()), + markdown.WithWrap(config.io.TerminalWidth())) + + if err != nil { + return err + } + _, err = fmt.Fprint(config.io.Out, out) + return err +} + +func printJSON(config viewConfig, project queries.Project) error { + b, err := format.JSONProject(project) + if err != nil { + return err + } + + _, err = config.io.Out.Write(b) + return err +} diff --git a/pkg/cmd/project/view/view_test.go b/pkg/cmd/project/view/view_test.go new file mode 100644 index 000000000..5c76c1413 --- /dev/null +++ b/pkg/cmd/project/view/view_test.go @@ -0,0 +1,545 @@ +package view + +import ( + "bytes" + "os" + "testing" + + "github.com/cli/cli/v2/pkg/cmd/project/shared/queries" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" +) + +func TestNewCmdview(t *testing.T) { + tests := []struct { + name string + cli string + wants viewOpts + wantsErr bool + wantsErrMsg string + }{ + { + name: "not-a-number", + cli: "x", + wantsErr: true, + wantsErrMsg: "invalid number: x", + }, + { + name: "number", + cli: "123", + wants: viewOpts{ + number: 123, + }, + }, + { + name: "owner", + cli: "--owner monalisa", + wants: viewOpts{ + owner: "monalisa", + }, + }, + { + name: "web", + cli: "--web", + wants: viewOpts{ + web: true, + }, + }, + { + name: "json", + cli: "--format json", + wants: viewOpts{ + format: "json", + }, + }, + } + + os.Setenv("GH_TOKEN", "auth-token") + defer os.Unsetenv("GH_TOKEN") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + + argv, err := shlex.Split(tt.cli) + assert.NoError(t, err) + + var gotOpts viewOpts + cmd := NewCmdView(f, func(config viewConfig) error { + gotOpts = config.opts + return nil + }) + + cmd.SetArgs(argv) + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + assert.Equal(t, tt.wantsErrMsg, err.Error()) + return + } + assert.NoError(t, err) + + assert.Equal(t, tt.wants.number, gotOpts.number) + assert.Equal(t, tt.wants.owner, gotOpts.owner) + assert.Equal(t, tt.wants.format, gotOpts.format) + assert.Equal(t, tt.wants.web, gotOpts.web) + }) + } +} + +func TestBuildURLViewer(t *testing.T) { + defer gock.Off() + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(` + {"data": + {"viewer": + { + "login":"theviewer" + } + } + } + `) + + client := queries.NewTestClient() + + url, err := buildURL(viewConfig{ + opts: viewOpts{ + number: 1, + owner: "@me", + }, + client: client, + }) + assert.NoError(t, err) + assert.Equal(t, "https://github.com/users/theviewer/projects/1", url) +} + +func TestRunView_User(t *testing.T) { + defer gock.Off() + + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(` + {"data": + {"user": + { + "login":"monalisa", + "projectV2": { + "number": 1, + "items": { + "totalCount": 10 + }, + "readme": null, + "fields": { + "nodes": [ + { + "name": "Title" + } + ] + } + } + } + } + } + `) + + client := queries.NewTestClient() + + ios, _, _, _ := iostreams.Test() + config := viewConfig{ + opts: viewOpts{ + owner: "monalisa", + number: 1, + }, + io: ios, + client: client, + } + + err := runView(config) + assert.NoError(t, err) + +} + +func TestRunView_Viewer(t *testing.T) { + defer gock.Off() + + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query ViewerOwner.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(` + {"data": + {"viewer": + { + "login":"monalisa", + "projectV2": { + "number": 1, + "items": { + "totalCount": 10 + }, + "readme": null, + "fields": { + "nodes": [ + { + "name": "Title" + } + ] + } + } + } + } + } + `) + + client := queries.NewTestClient() + + ios, _, _, _ := iostreams.Test() + config := viewConfig{ + opts: viewOpts{ + owner: "@me", + number: 1, + }, + io: ios, + client: client, + } + + err := runView(config) + assert.NoError(t, err) +} + +func TestRunView_Org(t *testing.T) { + defer gock.Off() + + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(` + {"data": + {"organization": + { + "login":"monalisa", + "projectV2": { + "number": 1, + "items": { + "totalCount": 10 + }, + "readme": null, + "fields": { + "nodes": [ + { + "name": "Title" + } + ] + } + } + } + } + } + `) + + client := queries.NewTestClient() + + ios, _, _, _ := iostreams.Test() + config := viewConfig{ + opts: viewOpts{ + owner: "github", + number: 1, + }, + io: ios, + client: client, + } + + err := runView(config) + assert.NoError(t, err) +} + +func TestRunViewWeb_User(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get user ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "monalisa", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "user": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"organization"}, + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(` + {"data": + {"user": + { + "login":"monalisa", + "projectV2": { + "number": 1, + "items": { + "totalCount": 10 + }, + "readme": null, + "fields": { + "nodes": [ + { + "name": "Title" + } + ] + } + } + } + } + } + `) + + client := queries.NewTestClient() + buf := bytes.Buffer{} + config := viewConfig{ + opts: viewOpts{ + owner: "monalisa", + web: true, + number: 8, + }, + URLOpener: func(url string) error { + buf.WriteString(url) + return nil + }, + client: client, + } + + err := runView(config) + assert.NoError(t, err) + assert.Equal(t, "https://github.com/users/monalisa/projects/8", buf.String()) +} + +func TestRunViewWeb_Org(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get org ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query UserOrgOwner.*", + "variables": map[string]interface{}{ + "login": "github", + }, + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "organization": map[string]interface{}{ + "id": "an ID", + }, + }, + "errors": []interface{}{ + map[string]interface{}{ + "type": "NOT_FOUND", + "path": []string{"user"}, + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(` + {"data": + {"organization": + { + "login":"github", + "projectV2": { + "number": 1, + "items": { + "totalCount": 10 + }, + "readme": null, + "fields": { + "nodes": [ + { + "name": "Title" + } + ] + } + } + } + } + } + `) + + client := queries.NewTestClient() + buf := bytes.Buffer{} + config := viewConfig{ + opts: viewOpts{ + owner: "github", + web: true, + number: 8, + }, + URLOpener: func(url string) error { + buf.WriteString(url) + return nil + }, + client: client, + } + + err := runView(config) + assert.NoError(t, err) + assert.Equal(t, "https://github.com/orgs/github/projects/8", buf.String()) +} + +func TestRunViewWeb_Me(t *testing.T) { + defer gock.Off() + gock.Observe(gock.DumpRequest) + // get viewer ID + gock.New("https://api.github.com"). + Post("/graphql"). + MatchType("json"). + JSON(map[string]interface{}{ + "query": "query Viewer.*", + }). + Reply(200). + JSON(map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "id": "an ID", + "login": "theviewer", + }, + }, + }) + + gock.New("https://api.github.com"). + Post("/graphql"). + Reply(200). + JSON(` + {"data": + {"user": + { + "login":"github", + "projectV2": { + "number": 1, + "items": { + "totalCount": 10 + }, + "readme": null, + "fields": { + "nodes": [ + { + "name": "Title" + } + ] + } + } + } + } + } + `) + + client := queries.NewTestClient() + buf := bytes.Buffer{} + config := viewConfig{ + opts: viewOpts{ + owner: "@me", + web: true, + number: 8, + }, + URLOpener: func(url string) error { + buf.WriteString(url) + return nil + }, + client: client, + } + + err := runView(config) + assert.NoError(t, err) + assert.Equal(t, "https://github.com/users/theviewer/projects/8", buf.String()) +} diff --git a/pkg/cmd/release/delete-asset/delete_asset_test.go b/pkg/cmd/release/delete-asset/delete_asset_test.go index f28f493d0..e302ca3d1 100644 --- a/pkg/cmd/release/delete-asset/delete_asset_test.go +++ b/pkg/cmd/release/delete-asset/delete_asset_test.go @@ -157,6 +157,7 @@ func Test_deleteAssetRun(t *testing.T) { ios.SetStderrTTY(tt.isTTY) fakeHTTP := &httpmock.Registry{} + defer fakeHTTP.Verify(t) shared.StubFetchRelease(t, fakeHTTP, "OWNER", "REPO", tt.opts.TagName, `{ "tag_name": "v1.2.3", "draft": false, diff --git a/pkg/cmd/release/download/download.go b/pkg/cmd/release/download/download.go index b957ba509..2c7f4a18a 100644 --- a/pkg/cmd/release/download/download.go +++ b/pkg/cmd/release/download/download.go @@ -140,7 +140,7 @@ func downloadRun(opts *DownloadOptions) error { return err } - opts.IO.StartProgressIndicator() + opts.IO.StartProgressIndicatorWithLabel("Finding assets to download") defer opts.IO.StopProgressIndicator() ctx := context.Background() @@ -196,7 +196,7 @@ func downloadRun(opts *DownloadOptions) error { stdout: opts.IO.Out, } - return downloadAssets(&dest, httpClient, toDownload, opts.Concurrency, isArchive) + return downloadAssets(&dest, httpClient, toDownload, opts.Concurrency, isArchive, opts.IO) } func matchAny(patterns []string, name string) bool { @@ -208,7 +208,7 @@ func matchAny(patterns []string, name string) bool { return false } -func downloadAssets(dest *destinationWriter, httpClient *http.Client, toDownload []shared.ReleaseAsset, numWorkers int, isArchive bool) error { +func downloadAssets(dest *destinationWriter, httpClient *http.Client, toDownload []shared.ReleaseAsset, numWorkers int, isArchive bool, io *iostreams.IOStreams) error { if numWorkers == 0 { return errors.New("the number of concurrent workers needs to be greater than 0") } @@ -223,6 +223,7 @@ func downloadAssets(dest *destinationWriter, httpClient *http.Client, toDownload for w := 1; w <= numWorkers; w++ { go func() { for a := range jobs { + io.StartProgressIndicatorWithLabel(fmt.Sprintf("Downloading %s", a.Name)) results <- downloadAsset(dest, httpClient, a.APIURL, a.Name, isArchive) } }() @@ -240,6 +241,8 @@ func downloadAssets(dest *destinationWriter, httpClient *http.Client, toDownload } } + io.StopProgressIndicator() + return downloadError } diff --git a/pkg/cmd/release/download/download_test.go b/pkg/cmd/release/download/download_test.go index c2260568a..782056314 100644 --- a/pkg/cmd/release/download/download_test.go +++ b/pkg/cmd/release/download/download_test.go @@ -516,7 +516,7 @@ func Test_downloadRun_cloberAndSkip(t *testing.T) { tt.opts.IO = ios reg := &httpmock.Registry{} - // defer reg.Verify(t) // FIXME: intermittetly fails due to StubFetchRelease internals + defer reg.Verify(t) shared.StubFetchRelease(t, reg, "OWNER", "REPO", "v1.2.3", `{ "assets": [ { "name": "windows-64bit.zip", "size": 34, diff --git a/pkg/cmd/release/edit/edit.go b/pkg/cmd/release/edit/edit.go index cac9c0e48..0360911bf 100644 --- a/pkg/cmd/release/edit/edit.go +++ b/pkg/cmd/release/edit/edit.go @@ -26,6 +26,7 @@ type EditOptions struct { Draft *bool Prerelease *bool IsLatest *bool + VerifyTag bool } func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command { @@ -81,6 +82,7 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman cmd.Flags().StringVar(&opts.Target, "target", "", "Target `branch` or full commit SHA (default: main branch)") cmd.Flags().StringVar(&opts.TagName, "tag", "", "The name of the tag") cmd.Flags().StringVarP(¬esFile, "notes-file", "F", "", "Read release notes from `file` (use \"-\" to read from standard input)") + cmd.Flags().BoolVar(&opts.VerifyTag, "verify-tag", false, "Abort in case the git tag doesn't already exist in the remote repository") _ = cmdutil.RegisterBranchCompletionFlags(f.GitClient, cmd, "target") @@ -110,6 +112,17 @@ func editRun(tag string, opts *EditOptions) error { params["tag_name"] = release.TagName } + if opts.VerifyTag && opts.TagName != "" { + remoteTagPresent, err := remoteTagExists(httpClient, baseRepo, opts.TagName) + if err != nil { + return err + } + if !remoteTagPresent { + return fmt.Errorf("tag %s doesn't exist in the repo %s, aborting due to --verify-tag flag", + opts.TagName, ghrepo.FullName(baseRepo)) + } + } + editedRelease, err := editRelease(httpClient, baseRepo, release.DatabaseID, params) if err != nil { return err diff --git a/pkg/cmd/release/edit/edit_test.go b/pkg/cmd/release/edit/edit_test.go index 9c7085b11..180051d12 100644 --- a/pkg/cmd/release/edit/edit_test.go +++ b/pkg/cmd/release/edit/edit_test.go @@ -140,6 +140,15 @@ func Test_NewCmdEdit(t *testing.T) { Body: stringPtr("MY NOTES"), }, }, + { + name: "verify-tag", + args: "v1.2.0 --tag=v1.1.0 --verify-tag", + isTTY: false, + want: EditOptions{ + TagName: "v1.1.0", + VerifyTag: true, + }, + }, } for _, tt := range tests { @@ -189,6 +198,7 @@ func Test_NewCmdEdit(t *testing.T) { assert.Equal(t, tt.want.Draft, opts.Draft) assert.Equal(t, tt.want.Prerelease, opts.Prerelease) assert.Equal(t, tt.want.IsLatest, opts.IsLatest) + assert.Equal(t, tt.want.VerifyTag, opts.VerifyTag) }) } } @@ -406,6 +416,21 @@ func Test_editRun(t *testing.T) { wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n", wantStderr: "", }, + { + name: "error when remote tag does not exist and verify-tag flag is set", + isTTY: true, + opts: EditOptions{ + TagName: "v1.2.4", + VerifyTag: true, + }, + httpStubs: func(t *testing.T, reg *httpmock.Registry) { + reg.Register(httpmock.GraphQL("RepositoryFindRef"), + httpmock.StringResponse(`{"data":{"repository":{"ref": {"id": ""}}}}`)) + }, + wantErr: "tag v1.2.4 doesn't exist in the repo OWNER/REPO, aborting due to --verify-tag flag", + wantStdout: "", + wantStderr: "", + }, } for _, tt := range tests { @@ -416,7 +441,7 @@ func Test_editRun(t *testing.T) { ios.SetStderrTTY(tt.isTTY) fakeHTTP := &httpmock.Registry{} - // defer reg.Verify(t) // FIXME: intermittetly fails due to StubFetchRelease internals + defer fakeHTTP.Verify(t) shared.StubFetchRelease(t, fakeHTTP, "OWNER", "REPO", "v1.2.3", `{ "id": 12345, "tag_name": "v1.2.3" diff --git a/pkg/cmd/release/edit/http.go b/pkg/cmd/release/edit/http.go index 6016ad776..bf310da60 100644 --- a/pkg/cmd/release/edit/http.go +++ b/pkg/cmd/release/edit/http.go @@ -11,6 +11,7 @@ import ( "github.com/cli/cli/v2/internal/ghinstance" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmd/release/shared" + "github.com/shurcooL/githubv4" ) func editRelease(httpClient *http.Client, repo ghrepo.Interface, releaseID int64, params map[string]interface{}) (*shared.Release, error) { @@ -48,3 +49,22 @@ func editRelease(httpClient *http.Client, repo ghrepo.Interface, releaseID int64 err = json.Unmarshal(b, &newRelease) return &newRelease, err } + +func remoteTagExists(httpClient *http.Client, repo ghrepo.Interface, tagName string) (bool, error) { + gql := api.NewClientFromHTTP(httpClient) + qualifiedTagName := fmt.Sprintf("refs/tags/%s", tagName) + var query struct { + Repository struct { + Ref struct { + ID string + } `graphql:"ref(qualifiedName: $tagName)"` + } `graphql:"repository(owner: $owner, name: $name)"` + } + variables := map[string]interface{}{ + "owner": githubv4.String(repo.RepoOwner()), + "name": githubv4.String(repo.RepoName()), + "tagName": githubv4.String(qualifiedTagName), + } + err := gql.Query(repo.RepoHost(), "RepositoryFindRef", &query, variables) + return query.Repository.Ref.ID != "", err +} diff --git a/pkg/cmd/release/shared/fetch.go b/pkg/cmd/release/shared/fetch.go index c8182cc83..84a9a369b 100644 --- a/pkg/cmd/release/shared/fetch.go +++ b/pkg/cmd/release/shared/fetch.go @@ -9,6 +9,7 @@ import ( "net/http" "reflect" "strings" + "testing" "time" "github.com/cli/cli/v2/api" @@ -224,11 +225,7 @@ func fetchReleasePath(ctx context.Context, httpClient *http.Client, host string, return &release, nil } -type testingT interface { - Errorf(format string, args ...interface{}) -} - -func StubFetchRelease(t testingT, reg *httpmock.Registry, owner, repoName, tagName, responseBody string) { +func StubFetchRelease(t *testing.T, reg *httpmock.Registry, owner, repoName, tagName, responseBody string) { path := "repos/OWNER/REPO/releases/tags/v1.2.3" if tagName == "" { path = "repos/OWNER/REPO/releases/latest" @@ -239,10 +236,12 @@ func StubFetchRelease(t testingT, reg *httpmock.Registry, owner, repoName, tagNa if tagName != "" { reg.Register( httpmock.GraphQL(`query RepositoryReleaseByTag\b`), - httpmock.GraphQLQuery(`{ "data": { "repository": { "release": null }}}`, func(q string, vars map[string]interface{}) { - assert.Equal(t, owner, vars["owner"]) - assert.Equal(t, repoName, vars["name"]) - assert.Equal(t, tagName, vars["tagName"]) - })) + httpmock.GraphQLQuery(`{ "data": { "repository": { "release": null }}}`, + func(q string, vars map[string]interface{}) { + assert.Equal(t, owner, vars["owner"]) + assert.Equal(t, repoName, vars["name"]) + assert.Equal(t, tagName, vars["tagName"]) + }), + ) } } diff --git a/pkg/cmd/release/upload/upload.go b/pkg/cmd/release/upload/upload.go index b3af37c4a..12cba4542 100644 --- a/pkg/cmd/release/upload/upload.go +++ b/pkg/cmd/release/upload/upload.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "regexp" "strings" "github.com/MakeNowJust/heredoc" @@ -93,8 +94,9 @@ func uploadRun(opts *UploadOptions) error { var existingNames []string for _, a := range opts.Assets { + sanitizedFileName := sanitizeFileName(a.Name) for _, ea := range release.Assets { - if ea.Name == a.Name { + if ea.Name == sanitizedFileName { a.ExistingURL = ea.APIURL existingNames = append(existingNames, ea.Name) break @@ -122,3 +124,30 @@ func uploadRun(opts *UploadOptions) error { return nil } + +// this method attempts to mimic the same functionality on the client that the platform does on +// uploaded assets in order to allow the --clobber logic work correctly, since that feature is +// one that only exists in the client +func sanitizeFileName(name string) string { + value := text.RemoveDiacritics(name) + // Stripped all non-ascii characters, provide default name. + if strings.HasPrefix(value, ".") { + value = "default" + value + } + + // Replace special characters with the separator + value = regexp.MustCompile(`(?i)[^a-z0-9\-_\+@]+`).ReplaceAllLiteralString(value, ".") + + // No more than one of the separator in a row. + value = regexp.MustCompile(`\.{2,}`).ReplaceAllLiteralString(value, ".") + + // Remove leading/trailing separator. + value = strings.Trim(value, ".") + + // Just file extension left, add default name. + if name != value && !strings.Contains(value, ".") { + value = "default." + value + } + + return value +} diff --git a/pkg/cmd/release/upload/upload_test.go b/pkg/cmd/release/upload/upload_test.go new file mode 100644 index 000000000..7a85c82a3 --- /dev/null +++ b/pkg/cmd/release/upload/upload_test.go @@ -0,0 +1,52 @@ +package upload + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_SanitizeFileName(t *testing.T) { + tests := []struct { + name string + expected string + }{ + { + name: "foo", + expected: "foo", + }, + { + name: "foo bar", + expected: "foo.bar", + }, + { + name: ".foo", + expected: "default.foo", + }, + { + name: "Foo bar", + expected: "Foo.bar", + }, + { + name: "Hello, दुनिया", + expected: "default.Hello", + }, + { + name: "this+has+plusses.jpg", + expected: "this+has+plusses.jpg", + }, + { + name: "this@has@at@signs.jpg", + expected: "this@has@at@signs.jpg", + }, + { + name: "façade.exposé", + expected: "facade.expose", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.expected, sanitizeFileName(tt.name)) + }) + } +} diff --git a/pkg/cmd/release/view/view_test.go b/pkg/cmd/release/view/view_test.go index 5d42ba373..c8b837923 100644 --- a/pkg/cmd/release/view/view_test.go +++ b/pkg/cmd/release/view/view_test.go @@ -215,6 +215,7 @@ func Test_viewRun(t *testing.T) { ios.SetStderrTTY(tt.isTTY) fakeHTTP := &httpmock.Registry{} + defer fakeHTTP.Verify(t) shared.StubFetchRelease(t, fakeHTTP, "OWNER", "REPO", tt.opts.TagName, fmt.Sprintf(`{ "tag_name": "v1.2.3", "draft": false, diff --git a/pkg/cmd/repo/clone/clone.go b/pkg/cmd/repo/clone/clone.go index fc39013c0..8d04ac630 100644 --- a/pkg/cmd/repo/clone/clone.go +++ b/pkg/cmd/repo/clone/clone.go @@ -175,12 +175,19 @@ func cloneRun(opts *CloneOptions) error { upstreamName = canonicalRepo.Parent.RepoOwner() } - _, err = gitClient.AddRemote(ctx, upstreamName, upstreamURL, []string{canonicalRepo.Parent.DefaultBranchRef.Name}, git.WithRepoDir(cloneDir)) + gc := gitClient.Copy() + gc.RepoDir = cloneDir + + _, err = gc.AddRemote(ctx, upstreamName, upstreamURL, []string{canonicalRepo.Parent.DefaultBranchRef.Name}) if err != nil { return err } - if err := gitClient.Fetch(ctx, upstreamName, "", git.WithRepoDir(cloneDir)); err != nil { + if err := gc.Fetch(ctx, upstreamName, ""); err != nil { + return err + } + + if err := gc.SetRemoteBranches(ctx, upstreamName, `*`); err != nil { return err } } diff --git a/pkg/cmd/repo/clone/clone_test.go b/pkg/cmd/repo/clone/clone_test.go index 98fc3736c..0ea3a65af 100644 --- a/pkg/cmd/repo/clone/clone_test.go +++ b/pkg/cmd/repo/clone/clone_test.go @@ -247,6 +247,7 @@ func Test_RepoClone_hasParent(t *testing.T) { cs.Register(`git clone https://github.com/OWNER/REPO.git`, 0, "") cs.Register(`git -C REPO remote add -t trunk upstream https://github.com/hubot/ORIG.git`, 0, "") cs.Register(`git -C REPO fetch upstream`, 0, "") + cs.Register(`git -C REPO remote set-branches upstream *`, 0, "") _, err := runCloneCommand(httpClient, "OWNER/REPO") if err != nil { @@ -284,6 +285,7 @@ func Test_RepoClone_hasParent_upstreamRemoteName(t *testing.T) { cs.Register(`git clone https://github.com/OWNER/REPO.git`, 0, "") cs.Register(`git -C REPO remote add -t trunk test https://github.com/hubot/ORIG.git`, 0, "") cs.Register(`git -C REPO fetch test`, 0, "") + cs.Register(`git -C REPO remote set-branches test *`, 0, "") _, err := runCloneCommand(httpClient, "OWNER/REPO --upstream-remote-name test") if err != nil { diff --git a/pkg/cmd/repo/create/create.go b/pkg/cmd/repo/create/create.go index 640fee632..1bd37769b 100644 --- a/pkg/cmd/repo/create/create.go +++ b/pkg/cmd/repo/create/create.go @@ -662,8 +662,8 @@ func localInit(gitClient *git.Client, remoteURL, path string) error { return err } - // Clone the client so we do not modify the original client's RepoDir. - gc := cloneGitClient(gitClient) + // Copy the client so we do not modify the original client's RepoDir. + gc := gitClient.Copy() gc.RepoDir = path gitRemoteAdd, err := gc.Command(ctx, "remote", "add", "origin", remoteURL) @@ -730,7 +730,7 @@ func interactiveRepoInfo(client *http.Client, hostname string, prompter iprompte name = fmt.Sprintf("%s/%s", owner, name) } - description, err := prompter.Input("Description", defaultName) + description, err := prompter.Input("Description", "") if err != nil { return "", "", "", err } @@ -794,14 +794,3 @@ func splitNameAndOwner(name string) (string, string, error) { } return repo.RepoName(), repo.RepoOwner(), nil } - -func cloneGitClient(c *git.Client) *git.Client { - return &git.Client{ - GhPath: c.GhPath, - RepoDir: c.RepoDir, - GitPath: c.GitPath, - Stderr: c.Stderr, - Stdin: c.Stdin, - Stdout: c.Stdout, - } -} diff --git a/pkg/cmd/repo/delete/delete.go b/pkg/cmd/repo/delete/delete.go index 99997c439..1e96e404d 100644 --- a/pkg/cmd/repo/delete/delete.go +++ b/pkg/cmd/repo/delete/delete.go @@ -10,7 +10,7 @@ import ( "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - ghAuth "github.com/cli/go-gh/pkg/auth" + ghAuth "github.com/cli/go-gh/v2/pkg/auth" "github.com/spf13/cobra" ) @@ -114,7 +114,7 @@ func deleteRun(opts *DeleteOptions) error { statusCode == http.StatusTemporaryRedirect || statusCode == http.StatusPermanentRedirect { cs := opts.IO.ColorScheme() - fmt.Fprintf(opts.IO.ErrOut, "%s Failed to delete repository: %s has changed name or transfered ownership\n", cs.FailureIcon(), fullName) + fmt.Fprintf(opts.IO.ErrOut, "%s Failed to delete repository: %s has changed name or transferred ownership\n", cs.FailureIcon(), fullName) return cmdutil.SilentError } } diff --git a/pkg/cmd/repo/delete/delete_test.go b/pkg/cmd/repo/delete/delete_test.go index 16b2b4006..b52ac1de9 100644 --- a/pkg/cmd/repo/delete/delete_test.go +++ b/pkg/cmd/repo/delete/delete_test.go @@ -157,11 +157,11 @@ func Test_deleteRun(t *testing.T) { }, }, { - name: "repo transfered ownership", + name: "repo transferred ownership", opts: &DeleteOptions{RepoArg: "OWNER/REPO", Confirmed: true}, wantErr: true, errMsg: "SilentError", - wantStderr: "X Failed to delete repository: OWNER/REPO has changed name or transfered ownership\n", + wantStderr: "X Failed to delete repository: OWNER/REPO has changed name or transferred ownership\n", httpStubs: func(reg *httpmock.Registry) { reg.Register( httpmock.REST("DELETE", "repos/OWNER/REPO"), diff --git a/pkg/cmd/repo/setdefault/setdefault.go b/pkg/cmd/repo/setdefault/setdefault.go index 2cf8b19b9..d5a03baf1 100644 --- a/pkg/cmd/repo/setdefault/setdefault.go +++ b/pkg/cmd/repo/setdefault/setdefault.go @@ -86,7 +86,7 @@ func NewCmdSetDefault(f *cmdutil.Factory, runF func(*SetDefaultOptions) error) * } } - if !opts.IO.CanPrompt() && opts.Repo == nil { + if !opts.ViewMode && !opts.IO.CanPrompt() && opts.Repo == nil { return cmdutil.FlagErrorf("repository required when not running interactively") } @@ -119,10 +119,10 @@ func setDefaultRun(opts *SetDefaultOptions) error { currentDefaultRepo, _ := remotes.ResolvedRemote() if opts.ViewMode { - if currentDefaultRepo == nil { - fmt.Fprintln(opts.IO.Out, "no default repository has been set; use `gh repo set-default` to select one") - } else { + if currentDefaultRepo != nil { fmt.Fprintln(opts.IO.Out, displayRemoteRepoName(currentDefaultRepo)) + } else if opts.IO.IsStdoutTTY() { + fmt.Fprintln(opts.IO.Out, "no default repository has been set; use `gh repo set-default` to select one") } return nil } diff --git a/pkg/cmd/repo/setdefault/setdefault_test.go b/pkg/cmd/repo/setdefault/setdefault_test.go index a1c1f44ac..58871f75b 100644 --- a/pkg/cmd/repo/setdefault/setdefault_test.go +++ b/pkg/cmd/repo/setdefault/setdefault_test.go @@ -166,7 +166,8 @@ func TestDefaultRun(t *testing.T) { wantStdout: "no default repository has been set\n", }, { - name: "view mode no current default", + name: "tty view mode no current default", + tty: true, opts: SetDefaultOptions{ViewMode: true}, remotes: []*context.Remote{ { @@ -176,6 +177,16 @@ func TestDefaultRun(t *testing.T) { }, wantStdout: "no default repository has been set; use `gh repo set-default` to select one\n", }, + { + name: "view mode no current default", + opts: SetDefaultOptions{ViewMode: true}, + remotes: []*context.Remote{ + { + Remote: &git.Remote{Name: "origin"}, + Repo: repo1, + }, + }, + }, { name: "view mode with base resolved current default", opts: SetDefaultOptions{ViewMode: true}, diff --git a/pkg/cmd/repo/sync/http.go b/pkg/cmd/repo/sync/http.go index d5cc0e282..3e193daeb 100644 --- a/pkg/cmd/repo/sync/http.go +++ b/pkg/cmd/repo/sync/http.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "net/http" + "regexp" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/ghrepo" @@ -31,7 +32,8 @@ func latestCommit(client *api.Client, repo ghrepo.Interface, branch string) (com type upstreamMergeErr struct{ error } -var upstreamMergeUnavailableErr = upstreamMergeErr{errors.New("upstream merge API is unavailable")} +var missingWorkflowScopeRE = regexp.MustCompile("refusing to allow.*without `workflow` scope") +var missingWorkflowScopeErr = errors.New("Upstream commits contain workflow changes, which require the `workflow` scope to merge. To request it, run: gh auth refresh -s workflow") func triggerUpstreamMerge(client *api.Client, repo ghrepo.Interface, branch string) (string, error) { var payload bytes.Buffer @@ -52,9 +54,10 @@ func triggerUpstreamMerge(client *api.Client, repo ghrepo.Interface, branch stri if errors.As(err, &httpErr) { switch httpErr.StatusCode { case http.StatusUnprocessableEntity, http.StatusConflict: + if missingWorkflowScopeRE.MatchString(httpErr.Message) { + return "", missingWorkflowScopeErr + } return "", upstreamMergeErr{errors.New(httpErr.Message)} - case http.StatusNotFound: - return "", upstreamMergeUnavailableErr } } return "", err diff --git a/pkg/cmd/repo/sync/sync.go b/pkg/cmd/repo/sync/sync.go index 1f734b543..dd52868d1 100644 --- a/pkg/cmd/repo/sync/sync.go +++ b/pkg/cmd/repo/sync/sync.go @@ -284,6 +284,13 @@ func executeLocalRepoSync(srcRepo ghrepo.Interface, remote string, opts *SyncOpt return nil } +// ExecuteRemoteRepoSync will take several steps to sync the source and destination repositories. +// First it will try to use the merge-upstream API endpoint. If this fails due to merge conflicts +// or unknown merge issues then it will fallback to using the low level git references API endpoint. +// The reason the fallback is necessary is to better support these error cases. The git references API +// endpoint allows us to sync repositories that are not fast-forward merge compatible. Additionally, +// the git references API endpoint gives more detailed error responses as to why the sync failed. +// Unless the --force flag is specified we will not perform non-fast-forward merges. func executeRemoteRepoSync(client *api.Client, destRepo, srcRepo ghrepo.Interface, opts *SyncOptions) (string, error) { branchName := opts.Branch if branchName == "" { @@ -317,8 +324,9 @@ func executeRemoteRepoSync(client *api.Client, destRepo, srcRepo ghrepo.Interfac return "", err } - // This is not a great way to detect the error returned by the API - // Unfortunately API returns 422 for multiple reasons + // Using string comparison is a brittle way to determine the error returned by the API + // endpoint but unfortunately the API returns 422 for many reasons so we must + // interpret the message provide better error messaging for our users. err = syncFork(client, destRepo, branchName, commit.Object.SHA, opts.Force) var httpErr api.HTTPError if err != nil { diff --git a/pkg/cmd/repo/sync/sync_test.go b/pkg/cmd/repo/sync/sync_test.go index 90a216607..eafc0835e 100644 --- a/pkg/cmd/repo/sync/sync_test.go +++ b/pkg/cmd/repo/sync/sync_test.go @@ -292,7 +292,7 @@ func Test_SyncRun(t *testing.T) { httpmock.StringResponse(`{"data":{"repository":{"defaultBranchRef":{"name": "trunk"}}}}`)) reg.Register( httpmock.REST("POST", "repos/FORKOWNER/REPO-FORK/merge-upstream"), - httpmock.StatusStringResponse(404, `{}`)) + httpmock.StatusStringResponse(422, `{}`)) reg.Register( httpmock.REST("GET", "repos/OWNER/REPO/git/refs/heads/trunk"), httpmock.StringResponse(`{"object":{"sha":"0xDEADBEEF"}}`)) @@ -457,6 +457,24 @@ func Test_SyncRun(t *testing.T) { wantErr: true, errMsg: "trunk branch does not exist on OWNER/REPO-FORK repository", }, + { + name: "sync remote fork with missing workflow scope on token", + opts: &SyncOptions{ + DestArg: "FORKOWNER/REPO-FORK", + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.GraphQL(`query RepositoryInfo\b`), + httpmock.StringResponse(`{"data":{"repository":{"defaultBranchRef":{"name": "trunk"}}}}`)) + reg.Register( + httpmock.REST("POST", "repos/FORKOWNER/REPO-FORK/merge-upstream"), + httpmock.StatusJSONResponse(422, struct { + Message string `json:"message"` + }{Message: "refusing to allow an OAuth App to create or update workflow `.github/workflows/unimportant.yml` without `workflow` scope"})) + }, + wantErr: true, + errMsg: "Upstream commits contain workflow changes, which require the `workflow` scope to merge. To request it, run: gh auth refresh -s workflow", + }, } for _, tt := range tests { reg := &httpmock.Registry{} diff --git a/pkg/cmd/root/alias.go b/pkg/cmd/root/alias.go new file mode 100644 index 000000000..4f504f2b8 --- /dev/null +++ b/pkg/cmd/root/alias.go @@ -0,0 +1,130 @@ +package root + +import ( + "errors" + "fmt" + "os/exec" + "regexp" + "runtime" + "strings" + + "github.com/cli/cli/v2/internal/run" + "github.com/cli/cli/v2/internal/text" + "github.com/cli/cli/v2/pkg/findsh" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/spf13/cobra" +) + +func NewCmdShellAlias(io *iostreams.IOStreams, aliasName, aliasValue string) *cobra.Command { + return &cobra.Command{ + Use: aliasName, + Short: fmt.Sprintf("Shell alias for %q", text.Truncate(80, aliasValue)), + RunE: func(c *cobra.Command, args []string) error { + expandedArgs, err := expandShellAlias(aliasValue, args, nil) + if err != nil { + return err + } + externalCmd := exec.Command(expandedArgs[0], expandedArgs[1:]...) + externalCmd.Stderr = io.ErrOut + externalCmd.Stdout = io.Out + externalCmd.Stdin = io.In + preparedCmd := run.PrepareCmd(externalCmd) + if err = preparedCmd.Run(); err != nil { + var execError *exec.ExitError + if errors.As(err, &execError) { + return &ExternalCommandExitError{execError} + } + return fmt.Errorf("failed to run external command: %w\n", err) + } + return nil + }, + GroupID: "alias", + Annotations: map[string]string{ + "skipAuthCheck": "true", + }, + DisableFlagParsing: true, + } +} + +func NewCmdAlias(io *iostreams.IOStreams, aliasName, aliasValue string) *cobra.Command { + return &cobra.Command{ + Use: aliasName, + Short: fmt.Sprintf("Alias for %q", text.Truncate(80, aliasValue)), + RunE: func(c *cobra.Command, args []string) error { + expandedArgs, err := expandAlias(aliasValue, args) + if err != nil { + return err + } + root := c.Root() + root.SetArgs(expandedArgs) + return root.Execute() + }, + GroupID: "alias", + Annotations: map[string]string{ + "skipAuthCheck": "true", + }, + DisableFlagParsing: true, + } +} + +// ExpandAlias processes argv to see if it should be rewritten according to a user's aliases. +func expandAlias(expansion string, args []string) ([]string, error) { + extraArgs := []string{} + for i, a := range args { + if !strings.Contains(expansion, "$") { + extraArgs = append(extraArgs, a) + } else { + expansion = strings.ReplaceAll(expansion, fmt.Sprintf("$%d", i+1), a) + } + } + + lingeringRE := regexp.MustCompile(`\$\d`) + if lingeringRE.MatchString(expansion) { + return nil, fmt.Errorf("not enough arguments for alias: %s", expansion) + } + + newArgs, err := shlex.Split(expansion) + if err != nil { + return nil, err + } + + expanded := append(newArgs, extraArgs...) + + return expanded, nil +} + +// ExpandShellAlias processes argv to see if it should be rewritten according to a user's aliases. +func expandShellAlias(expansion string, args []string, findShFunc func() (string, error)) ([]string, error) { + if findShFunc == nil { + findShFunc = findSh + } + + shPath, shErr := findShFunc() + if shErr != nil { + return nil, shErr + } + + expanded := []string{shPath, "-c", expansion[1:]} + + if len(args) > 0 { + expanded = append(expanded, "--") + expanded = append(expanded, args...) + } + + return expanded, nil +} + +func findSh() (string, error) { + shPath, err := findsh.Find() + if err != nil { + if errors.Is(err, exec.ErrNotFound) { + if runtime.GOOS == "windows" { + return "", errors.New("unable to locate sh to execute the shell alias with. The sh.exe interpreter is typically distributed with Git for Windows.") + } + return "", errors.New("unable to locate sh to execute shell alias with") + } + return "", err + } + return shPath, nil +} diff --git a/pkg/cmd/root/alias_test.go b/pkg/cmd/root/alias_test.go new file mode 100644 index 000000000..4a2d3c13e --- /dev/null +++ b/pkg/cmd/root/alias_test.go @@ -0,0 +1,123 @@ +package root + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestExpandAlias(t *testing.T) { + tests := []struct { + name string + expansion string + args []string + wantExpanded []string + wantErr string + }{ + { + name: "no expansion", + expansion: "pr status", + args: []string{}, + wantExpanded: []string{"pr", "status"}, + }, + { + name: "adding arguments after expansion", + expansion: "pr checkout", + args: []string{"123"}, + wantExpanded: []string{"pr", "checkout", "123"}, + }, + { + name: "not enough arguments for expansion", + expansion: `issue list --author="$1" --label="$2"`, + args: []string{}, + wantErr: `not enough arguments for alias: issue list --author="$1" --label="$2"`, + }, + { + name: "not enough arguments for expansion 2", + expansion: `issue list --author="$1" --label="$2"`, + args: []string{"vilmibm"}, + wantErr: `not enough arguments for alias: issue list --author="vilmibm" --label="$2"`, + }, + { + name: "satisfy expansion arguments", + expansion: `issue list --author="$1" --label="$2"`, + args: []string{"vilmibm", "help wanted"}, + wantExpanded: []string{"issue", "list", "--author=vilmibm", "--label=help wanted"}, + }, + { + name: "mixed positional and non-positional arguments", + expansion: `issue list --author="$1" --label="$2"`, + args: []string{"vilmibm", "epic", "-R", "monalisa/testing"}, + wantExpanded: []string{"issue", "list", "--author=vilmibm", "--label=epic", "-R", "monalisa/testing"}, + }, + { + name: "dollar in expansion", + expansion: `issue list --author="$1" --assignee="$1"`, + args: []string{"$coolmoney$"}, + wantExpanded: []string{"issue", "list", "--author=$coolmoney$", "--assignee=$coolmoney$"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotExpanded, err := expandAlias(tt.expansion, tt.args) + if tt.wantErr != "" { + assert.Nil(t, gotExpanded) + assert.EqualError(t, err, tt.wantErr) + return + } + assert.NoError(t, err) + assert.Equal(t, tt.wantExpanded, gotExpanded) + }) + } +} + +func TestExpandShellAlias(t *testing.T) { + findShFunc := func() (string, error) { + return "/usr/bin/sh", nil + } + tests := []struct { + name string + expansion string + args []string + findSh func() (string, error) + wantExpanded []string + wantErr string + }{ + { + name: "simple expansion", + expansion: "!git branch --show-current", + args: []string{}, + findSh: findShFunc, + wantExpanded: []string{"/usr/bin/sh", "-c", "git branch --show-current"}, + }, + { + name: "adding arguments after expansion", + expansion: "!git branch checkout", + args: []string{"123"}, + findSh: findShFunc, + wantExpanded: []string{"/usr/bin/sh", "-c", "git branch checkout", "--", "123"}, + }, + { + name: "unable to find sh", + expansion: "!git branch --show-current", + args: []string{}, + findSh: func() (string, error) { + return "", errors.New("unable to locate sh") + }, + wantErr: "unable to locate sh", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotExpanded, err := expandShellAlias(tt.expansion, tt.args, tt.findSh) + if tt.wantErr != "" { + assert.Nil(t, gotExpanded) + assert.EqualError(t, err, tt.wantErr) + return + } + assert.NoError(t, err) + assert.Equal(t, tt.wantExpanded, gotExpanded) + }) + } +} diff --git a/pkg/cmd/root/extension.go b/pkg/cmd/root/extension.go new file mode 100644 index 000000000..d6d495103 --- /dev/null +++ b/pkg/cmd/root/extension.go @@ -0,0 +1,38 @@ +package root + +import ( + "errors" + "fmt" + "os/exec" + + "github.com/cli/cli/v2/pkg/extensions" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/spf13/cobra" +) + +type ExternalCommandExitError struct { + *exec.ExitError +} + +func NewCmdExtension(io *iostreams.IOStreams, em extensions.ExtensionManager, ext extensions.Extension) *cobra.Command { + return &cobra.Command{ + Use: ext.Name(), + Short: fmt.Sprintf("Extension %s", ext.Name()), + RunE: func(c *cobra.Command, args []string) error { + args = append([]string{ext.Name()}, args...) + if _, err := em.Dispatch(args, io.In, io.Out, io.ErrOut); err != nil { + var execError *exec.ExitError + if errors.As(err, &execError) { + return &ExternalCommandExitError{execError} + } + return fmt.Errorf("failed to run extension: %w\n", err) + } + return nil + }, + GroupID: "extension", + Annotations: map[string]string{ + "skipAuthCheck": "true", + }, + DisableFlagParsing: true, + } +} diff --git a/pkg/cmd/root/help.go b/pkg/cmd/root/help.go index e35486f48..860fb22c3 100644 --- a/pkg/cmd/root/help.go +++ b/pkg/cmd/root/help.go @@ -4,9 +4,11 @@ import ( "bytes" "fmt" "io" + "os" "sort" "strings" + "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/internal/text" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/spf13/cobra" @@ -16,13 +18,17 @@ import ( func rootUsageFunc(w io.Writer, command *cobra.Command) error { fmt.Fprintf(w, "Usage: %s", command.UseLine()) - subcommands := command.Commands() + var subcommands []*cobra.Command + for _, c := range command.Commands() { + if !c.IsAvailableCommand() { + continue + } + subcommands = append(subcommands, c) + } + if len(subcommands) > 0 { fmt.Fprint(w, "\n\nAvailable commands:\n") for _, c := range subcommands { - if c.Hidden { - continue - } fmt.Fprintf(w, " %s\n", c.Name()) } return nil @@ -82,8 +88,10 @@ func isRootCmd(command *cobra.Command) bool { } func rootHelpFunc(f *cmdutil.Factory, command *cobra.Command, args []string) { + flags := command.Flags() + if isRootCmd(command) { - if versionVal, err := command.Flags().GetBool("version"); err == nil && versionVal { + if versionVal, err := flags.GetBool("version"); err == nil && versionVal { fmt.Fprint(f.IOStreams.Out, command.Annotations["versionInfo"]) return } else if err != nil { @@ -95,8 +103,8 @@ func rootHelpFunc(f *cmdutil.Factory, command *cobra.Command, args []string) { cs := f.IOStreams.ColorScheme() - if isRootCmd(command.Parent()) && len(args) >= 2 && args[1] != "--help" && args[1] != "-h" { - nestedSuggestFunc(f.IOStreams.ErrOut, command, args[1]) + if help, _ := flags.GetBool("help"); !help && !command.Runnable() && len(flags.Args()) > 0 { + nestedSuggestFunc(f.IOStreams.ErrOut, command, flags.Args()[0]) hasFailed = true return } @@ -139,19 +147,11 @@ func rootHelpFunc(f *cmdutil.Factory, command *cobra.Command, args []string) { if c := findCommand(command, "actions"); c != nil { helpTopics = append(helpTopics, rpad(c.Name()+":", namePadding)+c.Short) } - for topic, params := range HelpTopics { - helpTopics = append(helpTopics, rpad(topic+":", namePadding)+params["short"]) + for _, helpTopic := range HelpTopics { + helpTopics = append(helpTopics, rpad(helpTopic.name+":", namePadding)+helpTopic.short) } sort.Strings(helpTopics) helpEntries = append(helpEntries, helpEntry{"HELP TOPICS", strings.Join(helpTopics, "\n")}) - - if exts := f.ExtensionManager.List(); len(exts) > 0 { - var names []string - for _, ext := range exts { - names = append(names, ext.Name()) - } - helpEntries = append(helpEntries, helpEntry{"EXTENSION COMMANDS", strings.Join(names, "\n")}) - } } flagUsages := command.LocalFlags().FlagUsages() @@ -189,6 +189,27 @@ Read the manual at https://cli.github.com/manual`}) } } +func authHelp() string { + if os.Getenv("GITHUB_ACTIONS") == "true" { + return heredoc.Doc(` + gh: To use GitHub CLI in a GitHub Actions workflow, set the GH_TOKEN environment variable. Example: + env: + GH_TOKEN: ${{ github.token }} + `) + } + + if os.Getenv("CI") != "" { + return heredoc.Doc(` + gh: To use GitHub CLI in automation, set the GH_TOKEN environment variable. + `) + } + + return heredoc.Doc(` + To get started with GitHub CLI, please run: gh auth login + Alternatively, populate the GH_TOKEN environment variable with a GitHub API authentication token. + `) +} + func findCommand(cmd *cobra.Command, name string) *cobra.Command { for _, c := range cmd.Commands() { if c.Name() == name { diff --git a/pkg/cmd/root/help_reference.go b/pkg/cmd/root/help_reference.go index 86132c985..76e9db24e 100644 --- a/pkg/cmd/root/help_reference.go +++ b/pkg/cmd/root/help_reference.go @@ -11,7 +11,9 @@ import ( "github.com/spf13/cobra" ) -func referenceHelpFn(io *iostreams.IOStreams) func(*cobra.Command, []string) { +// longPager provides a pager over a commands Long message. +// It is currently only used for the reference command +func longPager(io *iostreams.IOStreams) func(*cobra.Command, []string) { return func(cmd *cobra.Command, args []string) { wrapWidth := 0 if io.IsStdoutTTY() { @@ -38,7 +40,7 @@ func referenceHelpFn(io *iostreams.IOStreams) func(*cobra.Command, []string) { } } -func referenceLong(cmd *cobra.Command) string { +func stringifyReference(cmd *cobra.Command) string { buf := bytes.NewBufferString("# gh reference\n\n") for _, c := range cmd.Commands() { if c.Hidden { diff --git a/pkg/cmd/root/help_topic.go b/pkg/cmd/root/help_topic.go index 4a4d19a51..7db694668 100644 --- a/pkg/cmd/root/help_topic.go +++ b/pkg/cmd/root/help_topic.go @@ -10,10 +10,18 @@ import ( "github.com/spf13/cobra" ) -var HelpTopics = map[string]map[string]string{ - "mintty": { - "short": "Information about using gh with MinTTY", - "long": heredoc.Doc(` +type helpTopic struct { + name string + short string + long string + example string +} + +var HelpTopics = []helpTopic{ + { + name: "mintty", + short: "Information about using gh with MinTTY", + long: heredoc.Doc(` MinTTY is the terminal emulator that comes by default with Git for Windows. It has known issues with gh's ability to prompt a user for input. @@ -30,9 +38,10 @@ var HelpTopics = map[string]map[string]string{ NOTE: this can lead to some UI bugs. `), }, - "environment": { - "short": "Environment variables that can be used with gh", - "long": heredoc.Doc(` + { + name: "environment", + short: "Environment variables that can be used with gh", + long: heredoc.Doc(` GH_TOKEN, GITHUB_TOKEN (in order of precedence): an authentication token for github.com API requests. Setting this avoids being prompted to authenticate and takes precedence over previously stored credentials. @@ -41,7 +50,8 @@ var HelpTopics = map[string]map[string]string{ token for API requests to GitHub Enterprise. When setting this, also set GH_HOST. GH_HOST: specify the GitHub hostname for commands that would otherwise assume the - "github.com" host when not in a context of an existing repository. + "github.com" host when not in a context of an existing repository. When setting this, + also set GH_ENTERPRISE_TOKEN. GH_REPO: specify the GitHub repository in the "[HOST/]OWNER/REPO" format for commands that otherwise operate on a local repository. @@ -91,12 +101,14 @@ var HelpTopics = map[string]map[string]string{ its own path such as in the cygwin terminal. `), }, - "reference": { - "short": "A comprehensive reference of all gh commands", + { + name: "reference", + short: "A comprehensive reference of all gh commands", }, - "formatting": { - "short": "Formatting options for JSON data exported from gh", - "long": heredoc.Docf(` + { + name: "formatting", + short: "Formatting options for JSON data exported from gh", + long: heredoc.Docf(` By default, the result of %[1]sgh%[1]s commands are output in line-based plain text format. Some commands support passing the %[1]s--json%[1]s flag, which converts the output to JSON format. Once in JSON, the output can be further formatted according to a required formatting string by @@ -111,8 +123,9 @@ var HelpTopics = map[string]map[string]string{ The %[1]s--jq%[1]s flag requires a string argument in jq query syntax, and will only print those JSON values which match the query. jq queries can be used to select elements from an array, fields from an object, create a new array, and more. The jq utility does not need - to be installed on the system to use this formatting directive. - To learn about jq query syntax, see: + to be installed on the system to use this formatting directive. When connected to a terminal, + the output is automatically pretty-printed. To learn about jq query syntax, see: + The %[1]s--template%[1]s flag requires a string argument in Go template syntax, and will only print those JSON values which match the query. @@ -131,7 +144,7 @@ var HelpTopics = map[string]map[string]string{ To learn more about Go templates, see: . `, "`"), - "example": heredoc.Doc(` + example: heredoc.Doc(` # default output format $ gh pr list Showing 23 of 23 open pull requests in cli/cli @@ -174,7 +187,38 @@ var HelpTopics = map[string]map[string]string{ codercat cli-maintainer - + # --jq can be used to implement more complex filtering and output changes: + $ bin/gh issue list --json number,title,labels --jq \ + 'map(select((.labels | length) > 0)) # must have labels + | map(.labels = (.labels | map(.name))) # show only the label names + | .[:3] # select the first 3 results' + [ + { + "labels": [ + "enhancement", + "needs triage" + ], + "number": 123, + "title": "A helpful contribution" + }, + { + "labels": [ + "help wanted", + "docs", + "good first issue" + ], + "number": 125, + "title": "Improve the docs" + }, + { + "labels": [ + "enhancement", + ], + "number": 7221, + "title": "An exciting new feature" + } + ] + # using the --template flag with the hyperlink helper gh issue list --json title,url --template '{{range .}}{{hyperlink .url .title}}{{"\n"}}{{end}}' @@ -210,9 +254,10 @@ var HelpTopics = map[string]map[string]string{ mislav COMMENTED This is going along great! Thanks for working on this ❤️ `), }, - "exit-codes": { - "short": "Exit codes used by gh", - "long": heredoc.Doc(` + { + name: "exit-codes", + short: "Exit codes used by gh", + long: heredoc.Doc(` gh follows normal conventions regarding exit codes. - If a command completes successfully, the exit code will be 0 @@ -230,30 +275,31 @@ var HelpTopics = map[string]map[string]string{ }, } -func NewHelpTopic(ios *iostreams.IOStreams, topic string) *cobra.Command { +func NewCmdHelpTopic(ios *iostreams.IOStreams, ht helpTopic) *cobra.Command { cmd := &cobra.Command{ - Use: topic, - Short: HelpTopics[topic]["short"], - Long: HelpTopics[topic]["long"], - Example: HelpTopics[topic]["example"], + Use: ht.name, + Short: ht.short, + Long: ht.long, + Example: ht.example, Hidden: true, Annotations: map[string]string{ "markdown:generate": "true", - "markdown:basename": "gh_help_" + topic, + "markdown:basename": "gh_help_" + ht.name, }, } - cmd.SetHelpFunc(func(c *cobra.Command, args []string) { - helpTopicHelpFunc(ios.Out, c, args) - }) cmd.SetUsageFunc(func(c *cobra.Command) error { return helpTopicUsageFunc(ios.ErrOut, c) }) + cmd.SetHelpFunc(func(c *cobra.Command, _ []string) { + helpTopicHelpFunc(ios.Out, c) + }) + return cmd } -func helpTopicHelpFunc(w io.Writer, command *cobra.Command, args []string) { +func helpTopicHelpFunc(w io.Writer, command *cobra.Command) { fmt.Fprint(w, command.Long) if command.Example != "" { fmt.Fprintf(w, "\n\nEXAMPLES\n") diff --git a/pkg/cmd/root/help_topic_test.go b/pkg/cmd/root/help_topic_test.go index 5f6825912..baa4f7f56 100644 --- a/pkg/cmd/root/help_topic_test.go +++ b/pkg/cmd/root/help_topic_test.go @@ -4,76 +4,85 @@ import ( "testing" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -func TestNewHelpTopic(t *testing.T) { +func TestCmdHelpTopic(t *testing.T) { + t.Parallel() + tests := []struct { - name string - topic string - args []string - flags []string - wantsErr bool + name string + topic helpTopic + args []string + flags []string + errorAssertion require.ErrorAssertionFunc + expectedAnnotations map[string]string }{ { - name: "valid topic", - topic: "environment", - args: []string{}, - flags: []string{}, - wantsErr: false, + name: "when there are no args or flags, prints the long message to stdout", + topic: helpTopic{name: "test-topic", long: "test-topic-long"}, + args: []string{}, + flags: []string{}, + errorAssertion: require.NoError, }, { - name: "invalid topic", - topic: "invalid", - args: []string{}, - flags: []string{}, - wantsErr: false, + name: "when an arg is provided, it is ignored and help is printed", + topic: helpTopic{name: "test-topic"}, + args: []string{"anything"}, + flags: []string{}, + errorAssertion: require.NoError, }, { - name: "more than zero args", - topic: "environment", - args: []string{"invalid"}, - flags: []string{}, - wantsErr: false, + name: "when a flag is provided, returns an error", + topic: helpTopic{name: "test-topic"}, + args: []string{}, + flags: []string{"--anything"}, + errorAssertion: require.Error, }, { - name: "more than zero flags", - topic: "environment", - args: []string{}, - flags: []string{"--invalid"}, - wantsErr: true, + name: "when there is an example, include it in the stdout", + topic: helpTopic{name: "test-topic", example: "test-topic-example"}, + args: []string{}, + flags: []string{}, + errorAssertion: require.NoError, }, { - name: "help arg", - topic: "environment", - args: []string{"help"}, - flags: []string{}, - wantsErr: false, - }, - { - name: "help flag", - topic: "environment", - args: []string{}, - flags: []string{"--help"}, - wantsErr: false, + name: "sets important markdown annotations on the command", + topic: helpTopic{name: "test-topic"}, + args: []string{}, + flags: []string{}, + errorAssertion: require.NoError, + expectedAnnotations: map[string]string{ + "markdown:generate": "true", + "markdown:basename": "gh_help_test-topic", + }, }, } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { - ios, _, _, stderr := iostreams.Test() + t.Parallel() - cmd := NewHelpTopic(ios, tt.topic) + ios, _, stdout, _ := iostreams.Test() + + cmd := NewCmdHelpTopic(ios, tt.topic) cmd.SetArgs(append(tt.args, tt.flags...)) - cmd.SetOut(stderr) - cmd.SetErr(stderr) _, err := cmd.ExecuteC() - if tt.wantsErr { - assert.Error(t, err) - return + tt.errorAssertion(t, err) + + if tt.topic.long != "" { + require.Contains(t, stdout.String(), tt.topic.long) + } + + if tt.topic.example != "" { + require.Contains(t, stdout.String(), tt.topic.example) + } + + if len(tt.expectedAnnotations) > 0 { + require.Equal(t, cmd.Annotations, tt.expectedAnnotations) } - assert.NoError(t, err) }) } } diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 78b436fe1..995ca30c1 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -1,8 +1,10 @@ package root import ( + "fmt" "net/http" "os" + "strings" "sync" "github.com/MakeNowJust/heredoc" @@ -10,6 +12,7 @@ import ( codespacesAPI "github.com/cli/cli/v2/internal/codespaces/api" actionsCmd "github.com/cli/cli/v2/pkg/cmd/actions" aliasCmd "github.com/cli/cli/v2/pkg/cmd/alias" + "github.com/cli/cli/v2/pkg/cmd/alias/shared" apiCmd "github.com/cli/cli/v2/pkg/cmd/api" authCmd "github.com/cli/cli/v2/pkg/cmd/auth" browseCmd "github.com/cli/cli/v2/pkg/cmd/browse" @@ -24,9 +27,11 @@ import ( labelCmd "github.com/cli/cli/v2/pkg/cmd/label" orgCmd "github.com/cli/cli/v2/pkg/cmd/org" prCmd "github.com/cli/cli/v2/pkg/cmd/pr" + projectCmd "github.com/cli/cli/v2/pkg/cmd/project" releaseCmd "github.com/cli/cli/v2/pkg/cmd/release" repoCmd "github.com/cli/cli/v2/pkg/cmd/repo" creditsCmd "github.com/cli/cli/v2/pkg/cmd/repo/credits" + rulesetCmd "github.com/cli/cli/v2/pkg/cmd/ruleset" runCmd "github.com/cli/cli/v2/pkg/cmd/run" searchCmd "github.com/cli/cli/v2/pkg/cmd/search" secretCmd "github.com/cli/cli/v2/pkg/cmd/secret" @@ -36,15 +41,29 @@ import ( versionCmd "github.com/cli/cli/v2/pkg/cmd/version" workflowCmd "github.com/cli/cli/v2/pkg/cmd/workflow" "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/google/shlex" "github.com/spf13/cobra" ) -func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { +type AuthError struct { + err error +} + +func (ae *AuthError) Error() string { + return ae.err.Error() +} + +func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) (*cobra.Command, error) { + io := f.IOStreams + cfg, err := f.Config() + if err != nil { + return nil, fmt.Errorf("failed to read configuration: %s\n", err) + } + cmd := &cobra.Command{ Use: "gh [flags]", Short: "GitHub CLI", Long: `Work seamlessly with GitHub from the command line.`, - Example: heredoc.Doc(` $ gh issue create $ gh repo clone cli/cli @@ -53,6 +72,14 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { Annotations: map[string]string{ "versionInfo": versionCmd.Format(version, buildDate), }, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + // require that the user is authenticated before running most commands + if cmdutil.IsAuthCheckEnabled(cmd) && !cmdutil.CheckAuth(cfg) { + fmt.Fprint(io.ErrOut, authHelp()) + return &AuthError{} + } + return nil + }, } // cmd.SetOut(f.IOStreams.Out) // can't use due to https://github.com/spf13/cobra/issues/1708 @@ -85,6 +112,10 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { ID: "actions", Title: "GitHub Actions commands", }) + cmd.AddGroup(&cobra.Group{ + ID: "extension", + Title: "Extension commands", + }) // Child commands cmd.AddCommand(versionCmd.NewCmdVersion(f, version, buildDate)) @@ -103,12 +134,7 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { cmd.AddCommand(sshKeyCmd.NewCmdSSHKey(f)) cmd.AddCommand(statusCmd.NewCmdStatus(f, nil)) cmd.AddCommand(newCodespaceCmd(f)) - - // the `api` command should not inherit any extra HTTP headers - bareHTTPCmdFactory := *f - bareHTTPCmdFactory.HttpClient = bareHTTPClient(f, version) - - cmd.AddCommand(apiCmd.NewCmdApi(&bareHTTPCmdFactory, nil)) + cmd.AddCommand(projectCmd.NewCmdProject(f)) // below here at the commands that require the "intelligent" BaseRepo resolver repoResolvingCmdFactory := *f @@ -120,24 +146,81 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { cmd.AddCommand(issueCmd.NewCmdIssue(&repoResolvingCmdFactory)) cmd.AddCommand(releaseCmd.NewCmdRelease(&repoResolvingCmdFactory)) cmd.AddCommand(repoCmd.NewCmdRepo(&repoResolvingCmdFactory)) + cmd.AddCommand(rulesetCmd.NewCmdRuleset(&repoResolvingCmdFactory)) cmd.AddCommand(runCmd.NewCmdRun(&repoResolvingCmdFactory)) cmd.AddCommand(workflowCmd.NewCmdWorkflow(&repoResolvingCmdFactory)) cmd.AddCommand(labelCmd.NewCmdLabel(&repoResolvingCmdFactory)) + // the `api` command should not inherit any extra HTTP headers + bareHTTPCmdFactory := *f + bareHTTPCmdFactory.HttpClient = bareHTTPClient(f, version) + bareHTTPCmdFactory.BaseRepo = factory.SmartBaseRepoFunc(&bareHTTPCmdFactory) + + cmd.AddCommand(apiCmd.NewCmdApi(&bareHTTPCmdFactory, nil)) + // Help topics - cmd.AddCommand(NewHelpTopic(f.IOStreams, "environment")) - cmd.AddCommand(NewHelpTopic(f.IOStreams, "formatting")) - cmd.AddCommand(NewHelpTopic(f.IOStreams, "mintty")) - cmd.AddCommand(NewHelpTopic(f.IOStreams, "exit-codes")) - referenceCmd := NewHelpTopic(f.IOStreams, "reference") - referenceCmd.SetHelpFunc(referenceHelpFn(f.IOStreams)) - cmd.AddCommand(referenceCmd) + var referenceCmd *cobra.Command + for _, ht := range HelpTopics { + helpTopicCmd := NewCmdHelpTopic(f.IOStreams, ht) + cmd.AddCommand(helpTopicCmd) + + // See bottom of the function for why we explicitly care about the reference cmd + if ht.name == "reference" { + referenceCmd = helpTopicCmd + } + } + + // Extensions + em := f.ExtensionManager + for _, e := range em.List() { + extensionCmd := NewCmdExtension(io, em, e) + cmd.AddCommand(extensionCmd) + } + + // Aliases + aliases := cfg.Aliases() + validAliasName := shared.ValidAliasNameFunc(cmd) + validAliasExpansion := shared.ValidAliasExpansionFunc(cmd) + for k, v := range aliases.All() { + aliasName := k + aliasValue := v + if validAliasName(aliasName) && validAliasExpansion(aliasValue) { + split, _ := shlex.Split(aliasName) + parentCmd, parentArgs, _ := cmd.Find(split) + if !parentCmd.ContainsGroup("alias") { + parentCmd.AddGroup(&cobra.Group{ + ID: "alias", + Title: "Alias commands", + }) + } + if strings.HasPrefix(aliasValue, "!") { + shellAliasCmd := NewCmdShellAlias(io, parentArgs[0], aliasValue) + parentCmd.AddCommand(shellAliasCmd) + } else { + aliasCmd := NewCmdAlias(io, parentArgs[0], aliasValue) + split, _ := shlex.Split(aliasValue) + child, _, _ := cmd.Find(split) + aliasCmd.SetUsageFunc(func(_ *cobra.Command) error { + return rootUsageFunc(f.IOStreams.ErrOut, child) + }) + aliasCmd.SetHelpFunc(func(_ *cobra.Command, args []string) { + rootHelpFunc(f, child, args) + }) + parentCmd.AddCommand(aliasCmd) + } + } + } cmdutil.DisableAuthCheck(cmd) - // this needs to appear last: - referenceCmd.Long = referenceLong(cmd) - return cmd + // The reference command produces paged output that displays information on every other command. + // Therefore, we explicitly set the Long text and HelpFunc here after all other commands are registered. + // We experimented with producing the paged output dynamically when the HelpFunc is called but since + // doc generation makes use of the Long text, it is simpler to just be explicit here that this command + // is special. + referenceCmd.Long = stringifyReference(cmd) + referenceCmd.SetHelpFunc(longPager(f.IOStreams)) + return cmd, nil } func bareHTTPClient(f *cmdutil.Factory, version string) func() (*http.Client, error) { diff --git a/pkg/cmd/ruleset/check/check.go b/pkg/cmd/ruleset/check/check.go new file mode 100644 index 000000000..3007fe36a --- /dev/null +++ b/pkg/cmd/ruleset/check/check.go @@ -0,0 +1,163 @@ +package check + +import ( + "context" + "fmt" + "net/http" + "net/url" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/api" + "github.com/cli/cli/v2/git" + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/text" + "github.com/cli/cli/v2/pkg/cmd/ruleset/shared" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/spf13/cobra" +) + +type CheckOptions struct { + HttpClient func() (*http.Client, error) + IO *iostreams.IOStreams + Config func() (config.Config, error) + BaseRepo func() (ghrepo.Interface, error) + Git *git.Client + Browser browser.Browser + + Branch string + Default bool + WebMode bool +} + +func NewCmdCheck(f *cmdutil.Factory, runF func(*CheckOptions) error) *cobra.Command { + opts := &CheckOptions{ + IO: f.IOStreams, + Config: f.Config, + HttpClient: f.HttpClient, + Browser: f.Browser, + Git: f.GitClient, + } + cmd := &cobra.Command{ + Use: "check []", + Short: "View rules that would apply to a given branch", + Long: heredoc.Doc(` + View information about GitHub rules that apply to a given branch. + + The provided branch name does not need to exist; rules will be displayed that would apply + to a branch with that name. All rules are returned regardless of where they are configured. + + If no branch name is provided, then the current branch will be used. + + The --default flag can be used to view rules that apply to the default branch of the + repository. + `), + Example: heredoc.Doc(` + # View all rules that apply to the current branch + $ gh ruleset check + + # View all rules that apply to a branch named "my-branch" in a different repository + $ gh ruleset check my-branch --repo owner/repo + + # View all rules that apply to the default branch in a different repository + $ gh ruleset check --default --repo owner/repo + + # View a ruleset configured in a different repository or any of its parents + $ gh ruleset view 23 --repo owner/repo + + # View an organization-level ruleset + $ gh ruleset view 23 --org my-org + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + // support `-R, --repo` override + opts.BaseRepo = f.BaseRepo + + if len(args) > 0 { + opts.Branch = args[0] + } + + if err := cmdutil.MutuallyExclusive( + "specify only one of `--default` or a branch name", + opts.Branch != "", + opts.Default, + ); err != nil { + return err + } + + if runF != nil { + return runF(opts) + } + + return checkRun(opts) + }, + } + + cmd.Flags().BoolVar(&opts.Default, "default", false, "Check rules on default branch") + cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the branch rules page in a web browser") + + return cmd +} + +func checkRun(opts *CheckOptions) error { + httpClient, err := opts.HttpClient() + if err != nil { + return err + } + client := api.NewClientFromHTTP(httpClient) + + repoI, err := opts.BaseRepo() + if err != nil { + return fmt.Errorf("could not determine repo to use: %w", err) + } + + git := opts.Git + + if opts.Default { + repo, err := api.GitHubRepo(client, repoI) + if err != nil { + return fmt.Errorf("could not get repository information: %w", err) + } + opts.Branch = repo.DefaultBranchRef.Name + } + + if opts.Branch == "" { + opts.Branch, err = git.CurrentBranch(context.Background()) + if err != nil { + return fmt.Errorf("could not determine current branch: %w", err) + } + } + + if opts.WebMode { + // the query string parameter may have % signs in it, so it must be carefully used with Printf functions + queryString := fmt.Sprintf("?ref=%s", url.QueryEscape("refs/heads/"+opts.Branch)) + rawUrl := ghrepo.GenerateRepoURL(repoI, "rules") + + if opts.IO.IsStdoutTTY() { + fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", text.DisplayURL(rawUrl)) + } + + return opts.Browser.Browse(rawUrl + queryString) + } + + var rules []shared.RulesetRule + + endpoint := fmt.Sprintf("repos/%s/%s/rules/branches/%s", repoI.RepoOwner(), repoI.RepoName(), url.PathEscape(opts.Branch)) + + if err = client.REST(repoI.RepoHost(), "GET", endpoint, nil, &rules); err != nil { + return fmt.Errorf("GET %s failed: %w", endpoint, err) + } + + w := opts.IO.Out + + fmt.Fprintf(w, "%d rules apply to branch %s in repo %s/%s\n", len(rules), opts.Branch, repoI.RepoOwner(), repoI.RepoName()) + + if len(rules) > 0 { + fmt.Fprint(w, "\n") + fmt.Fprint(w, shared.ParseRulesForDisplay(rules)) + } + + return nil +} diff --git a/pkg/cmd/ruleset/check/check_test.go b/pkg/cmd/ruleset/check/check_test.go new file mode 100644 index 000000000..2d9dffae4 --- /dev/null +++ b/pkg/cmd/ruleset/check/check_test.go @@ -0,0 +1,233 @@ +package check + +import ( + "bytes" + "io" + "net/http" + "testing" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/httpmock" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_NewCmdCheck(t *testing.T) { + tests := []struct { + name string + args string + isTTY bool + want CheckOptions + wantErr string + }{ + { + name: "no arguments", + args: "", + isTTY: true, + want: CheckOptions{ + Branch: "", + Default: false, + WebMode: false, + }, + }, + { + name: "branch name", + args: "my-branch", + isTTY: true, + want: CheckOptions{ + Branch: "my-branch", + Default: false, + WebMode: false, + }, + }, + { + name: "default", + args: "--default=true", + isTTY: true, + want: CheckOptions{ + Branch: "", + Default: true, + WebMode: false, + }, + }, + { + name: "web mode", + args: "--web", + isTTY: true, + want: CheckOptions{ + Branch: "", + Default: false, + WebMode: true, + }, + }, + { + name: "both --default and branch name specified", + args: "--default asdf", + isTTY: true, + wantErr: "specify only one of `--default` or a branch name", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + ios.SetStdoutTTY(tt.isTTY) + ios.SetStdinTTY(tt.isTTY) + ios.SetStderrTTY(tt.isTTY) + + f := &cmdutil.Factory{ + IOStreams: ios, + } + + var opts *CheckOptions + cmd := NewCmdCheck(f, func(o *CheckOptions) error { + opts = o + return nil + }) + cmd.PersistentFlags().StringP("repo", "R", "", "") + + argv, err := shlex.Split(tt.args) + require.NoError(t, err) + cmd.SetArgs(argv) + + cmd.SetIn(&bytes.Buffer{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + + _, err = cmd.ExecuteC() + if tt.wantErr != "" { + require.EqualError(t, err, tt.wantErr) + return + } else { + require.NoError(t, err) + } + + assert.Equal(t, tt.want.Branch, opts.Branch) + assert.Equal(t, tt.want.Default, opts.Default) + assert.Equal(t, tt.want.WebMode, opts.WebMode) + }) + } +} + +func Test_checkRun(t *testing.T) { + tests := []struct { + name string + isTTY bool + opts CheckOptions + wantErr string + wantStdout string + wantStderr string + wantBrowse string + }{ + { + name: "view rules for branch", + isTTY: true, + opts: CheckOptions{ + Branch: "my-branch", + }, + wantStdout: heredoc.Doc(` + 6 rules apply to branch my-branch in repo my-org/repo-name + + - commit_author_email_pattern: [name: ] [negate: false] [operator: ends_with] [pattern: @example.com] + (configured in ruleset 1234 from organization my-org) + + - commit_author_email_pattern: [name: ] [negate: false] [operator: ends_with] [pattern: @example.com] + (configured in ruleset 5678 from repository my-org/repo-name) + + - commit_message_pattern: [name: ] [negate: false] [operator: starts_with] [pattern: fff] + (configured in ruleset 1234 from organization my-org) + + - commit_message_pattern: [name: ] [negate: false] [operator: contains] [pattern: asdf] + (configured in ruleset 5678 from repository my-org/repo-name) + + - creation + (configured in ruleset 5678 from repository my-org/repo-name) + + - required_signatures + (configured in ruleset 1234 from organization my-org) + + `), + wantStderr: "", + wantBrowse: "", + }, + { + name: "web mode, TTY", + isTTY: true, + opts: CheckOptions{ + Branch: "my-branch", + WebMode: true, + }, + wantStdout: "Opening github.com/my-org/repo-name/rules in your browser.\n", + wantStderr: "", + wantBrowse: "https://github.com/my-org/repo-name/rules?ref=refs%2Fheads%2Fmy-branch", + }, + { + name: "web mode, TTY, special character in branch name", + isTTY: true, + opts: CheckOptions{ + Branch: "my-feature/my-branch", + WebMode: true, + }, + wantStdout: "Opening github.com/my-org/repo-name/rules in your browser.\n", + wantStderr: "", + wantBrowse: "https://github.com/my-org/repo-name/rules?ref=refs%2Fheads%2Fmy-feature%2Fmy-branch", + }, + { + name: "web mode, non-TTY", + isTTY: false, + opts: CheckOptions{ + Branch: "my-branch", + WebMode: true, + }, + wantStdout: "", + wantStderr: "", + wantBrowse: "https://github.com/my-org/repo-name/rules?ref=refs%2Fheads%2Fmy-branch", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, stdout, stderr := iostreams.Test() + ios.SetStdoutTTY(tt.isTTY) + ios.SetStdinTTY(tt.isTTY) + ios.SetStderrTTY(tt.isTTY) + + fakeHTTP := &httpmock.Registry{} + fakeHTTP.Register( + httpmock.REST("GET", "repos/my-org/repo-name/rules/branches/my-branch"), + httpmock.FileResponse("./fixtures/rulesetCheck.json"), + ) + + tt.opts.IO = ios + tt.opts.HttpClient = func() (*http.Client, error) { + return &http.Client{Transport: fakeHTTP}, nil + } + tt.opts.BaseRepo = func() (ghrepo.Interface, error) { + return ghrepo.FromFullName("my-org/repo-name") + } + browser := &browser.Stub{} + tt.opts.Browser = browser + + err := checkRun(&tt.opts) + + if tt.wantErr != "" { + require.EqualError(t, err, tt.wantErr) + return + } else { + require.NoError(t, err) + } + + if tt.wantBrowse != "" { + browser.Verify(t, tt.wantBrowse) + } + + assert.Equal(t, tt.wantStdout, stdout.String()) + assert.Equal(t, tt.wantStderr, stderr.String()) + }) + } +} diff --git a/pkg/cmd/ruleset/check/fixtures/rulesetCheck.json b/pkg/cmd/ruleset/check/fixtures/rulesetCheck.json new file mode 100644 index 000000000..c02fddef4 --- /dev/null +++ b/pkg/cmd/ruleset/check/fixtures/rulesetCheck.json @@ -0,0 +1,62 @@ +[ + { + "type": "commit_author_email_pattern", + "parameters": { + "name": "", + "negate": false, + "pattern": "@example.com", + "operator": "ends_with" + }, + "ruleset_source_type": "Organization", + "ruleset_source": "my-org", + "ruleset_id": 1234 + }, + { + "type": "commit_message_pattern", + "parameters": { + "name": "", + "negate": false, + "pattern": "fff", + "operator": "starts_with" + }, + "ruleset_source_type": "Organization", + "ruleset_source": "my-org", + "ruleset_id": 1234 + }, + { + "type": "required_signatures", + "ruleset_source_type": "Organization", + "ruleset_source": "my-org", + "ruleset_id": 1234 + }, + { + "type": "commit_message_pattern", + "parameters": { + "name": "", + "negate": false, + "pattern": "asdf", + "operator": "contains" + }, + "ruleset_source_type": "Repository", + "ruleset_source": "my-org/repo-name", + "ruleset_id": 5678 + }, + { + "type": "commit_author_email_pattern", + "parameters": { + "name": "", + "negate": false, + "pattern": "@example.com", + "operator": "ends_with" + }, + "ruleset_source_type": "Repository", + "ruleset_source": "my-org/repo-name", + "ruleset_id": 5678 + }, + { + "type": "creation", + "ruleset_source_type": "Repository", + "ruleset_source": "my-org/repo-name", + "ruleset_id": 5678 + } +] diff --git a/pkg/cmd/ruleset/list/fixtures/rulesetList.json b/pkg/cmd/ruleset/list/fixtures/rulesetList.json new file mode 100644 index 000000000..d7c631623 --- /dev/null +++ b/pkg/cmd/ruleset/list/fixtures/rulesetList.json @@ -0,0 +1,81 @@ +{ + "data": { + "level": { + "rulesets": { + "totalCount": 3, + "nodes": [ + { + "databaseId": 4, + "name": "test", + "target": "BRANCH", + "enforcement": "EVALUATE", + "source": { + "__typename": "Repository", + "owner": "OWNER/REPO" + }, + "conditions": { + "refName": { + "include": [ + "~DEFAULT_BRANCH" + ], + "exclude": [] + }, + "repositoryName": null + }, + "rules": { + "totalCount": 1 + } + }, + { + "databaseId": 42, + "name": "asdf", + "target": "BRANCH", + "enforcement": "ACTIVE", + "source": { + "__typename": "Repository", + "owner": "OWNER/REPO" + }, + "conditions": { + "refName": { + "include": [ + "~DEFAULT_BRANCH" + ], + "exclude": [] + }, + "repositoryName": null + }, + "rules": { + "totalCount": 2 + } + }, + { + "databaseId": 77, + "name": "foobar", + "target": "BRANCH", + "enforcement": "DISABLED", + "source": { + "__typename": "Organization", + "owner": "Org-Name" + }, + "conditions": { + "refName": { + "include": [ + "~DEFAULT_BRANCH" + ], + "exclude": [] + }, + "repositoryName": null + }, + "rules": { + "totalCount": 4 + } + } + ], + "pageInfo": { + "hasNextPage": false, + "endCursor": "Y3Vyc29yOnYyOpHNA8E=" + } + } + } + } +} diff --git a/pkg/cmd/ruleset/list/list.go b/pkg/cmd/ruleset/list/list.go new file mode 100644 index 000000000..60afc69ab --- /dev/null +++ b/pkg/cmd/ruleset/list/list.go @@ -0,0 +1,170 @@ +package list + +import ( + "fmt" + "net/http" + "strconv" + "strings" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/internal/ghinstance" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/tableprinter" + "github.com/cli/cli/v2/internal/text" + "github.com/cli/cli/v2/pkg/cmd/ruleset/shared" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + ghAuth "github.com/cli/go-gh/v2/pkg/auth" + "github.com/spf13/cobra" +) + +type ListOptions struct { + HttpClient func() (*http.Client, error) + IO *iostreams.IOStreams + BaseRepo func() (ghrepo.Interface, error) + Browser browser.Browser + + Limit int + IncludeParents bool + WebMode bool + Organization string +} + +func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command { + opts := &ListOptions{ + IO: f.IOStreams, + HttpClient: f.HttpClient, + Browser: f.Browser, + } + cmd := &cobra.Command{ + Use: "list", + Short: "List rulesets for a repository or organization", + Long: heredoc.Doc(` + List GitHub rulesets for a repository or organization. + + If no options are provided, the current repository's rulesets are listed. You can query a different + repository's rulesets by using the --repo flag. You can also use the --org flag to list rulesets + configured for the provided organization. + + Use the --parents flag to control whether rulesets configured at higher levels that also apply to the provided + repository or organization should be returned. The default is true. + + Your access token must have the admin:org scope to use the --org flag, which can be granted by running "gh auth refresh -s admin:org". + `), + Example: heredoc.Doc(` + # List rulesets in the current repository + $ gh ruleset list + + # List rulesets in a different repository, including those configured at higher levels + $ gh ruleset list --repo owner/repo --parents + + # List rulesets in an organization + $ gh ruleset list --org org-name + `), + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && opts.Organization != "" { + return cmdutil.FlagErrorf("only one of --repo and --org may be specified") + } + + // support `-R, --repo` override + opts.BaseRepo = f.BaseRepo + + if opts.Limit < 1 { + return cmdutil.FlagErrorf("invalid limit: %v", opts.Limit) + } + + if runF != nil { + return runF(opts) + } + + return listRun(opts) + }, + } + + cmd.Flags().IntVarP(&opts.Limit, "limit", "L", 30, "Maximum number of rulesets to list") + cmd.Flags().StringVarP(&opts.Organization, "org", "o", "", "List organization-wide rulesets for the provided organization") + cmd.Flags().BoolVarP(&opts.IncludeParents, "parents", "p", true, "Whether to include rulesets configured at higher levels that also apply") + cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the list of rulesets in the web browser") + + return cmd +} + +func listRun(opts *ListOptions) error { + httpClient, err := opts.HttpClient() + if err != nil { + return err + } + + repoI, err := opts.BaseRepo() + if err != nil { + return err + } + + hostname, _ := ghAuth.DefaultHost() + + if opts.WebMode { + var rulesetURL string + if opts.Organization != "" { + rulesetURL = fmt.Sprintf("%sorganizations/%s/settings/rules", ghinstance.HostPrefix(hostname), opts.Organization) + } else { + rulesetURL = ghrepo.GenerateRepoURL(repoI, "rules") + } + + if opts.IO.IsStdoutTTY() { + fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", text.DisplayURL(rulesetURL)) + } + + return opts.Browser.Browse(rulesetURL) + } + + var result *shared.RulesetList + + if opts.Organization != "" { + result, err = shared.ListOrgRulesets(httpClient, opts.Organization, opts.Limit, hostname, opts.IncludeParents) + } else { + result, err = shared.ListRepoRulesets(httpClient, repoI, opts.Limit, opts.IncludeParents) + } + + if err != nil { + return err + } + + if result.TotalCount == 0 { + return shared.NoRulesetsFoundError(opts.Organization, repoI, opts.IncludeParents) + } + + opts.IO.DetectTerminalTheme() + if err := opts.IO.StartPager(); err == nil { + defer opts.IO.StopPager() + } else { + fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err) + } + + cs := opts.IO.ColorScheme() + + if opts.IO.IsStdoutTTY() { + parentsMsg := "" + if opts.IncludeParents { + parentsMsg = " and its parents" + } + + inMsg := fmt.Sprintf("%s%s", shared.EntityName(opts.Organization, repoI), parentsMsg) + fmt.Fprintf(opts.IO.Out, "\nShowing %d of %d rulesets in %s\n\n", len(result.Rulesets), result.TotalCount, inMsg) + } + + tp := tableprinter.New(opts.IO) + tp.HeaderRow("ID", "NAME", "SOURCE", "STATUS", "RULES") + + for _, rs := range result.Rulesets { + tp.AddField(strconv.Itoa(rs.DatabaseId), tableprinter.WithColor(cs.Cyan)) + tp.AddField(rs.Name, tableprinter.WithColor(cs.Bold)) + tp.AddField(shared.RulesetSource(rs)) + tp.AddField(strings.ToLower(rs.Enforcement)) + tp.AddField(strconv.Itoa(rs.Rules.TotalCount)) + tp.EndRow() + } + + return tp.Render() +} diff --git a/pkg/cmd/ruleset/list/list_test.go b/pkg/cmd/ruleset/list/list_test.go new file mode 100644 index 000000000..d075e46d6 --- /dev/null +++ b/pkg/cmd/ruleset/list/list_test.go @@ -0,0 +1,295 @@ +package list + +import ( + "bytes" + "io" + "net/http" + "testing" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/httpmock" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_NewCmdList(t *testing.T) { + tests := []struct { + name string + args string + isTTY bool + want ListOptions + wantErr string + }{ + { + name: "no arguments", + args: "", + isTTY: true, + want: ListOptions{ + Limit: 30, + IncludeParents: true, + WebMode: false, + Organization: "", + }, + }, + { + name: "limit", + args: "--limit 1", + isTTY: true, + want: ListOptions{ + Limit: 1, + IncludeParents: true, + WebMode: false, + Organization: "", + }, + }, + { + name: "include parents", + args: "--parents=false", + isTTY: true, + want: ListOptions{ + Limit: 30, + IncludeParents: false, + WebMode: false, + Organization: "", + }, + }, + { + name: "org", + args: "--org \"my-org\"", + isTTY: true, + want: ListOptions{ + Limit: 30, + IncludeParents: true, + WebMode: false, + Organization: "my-org", + }, + }, + { + name: "web mode", + args: "--web", + isTTY: true, + want: ListOptions{ + Limit: 30, + IncludeParents: true, + WebMode: true, + Organization: "", + }, + }, + { + name: "invalid limit", + args: "--limit 0", + isTTY: true, + wantErr: "invalid limit: 0", + }, + { + name: "repo and org specified", + args: "--org \"my-org\" -R \"owner/repo\"", + isTTY: true, + wantErr: "only one of --repo and --org may be specified", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + ios.SetStdoutTTY(tt.isTTY) + ios.SetStdinTTY(tt.isTTY) + ios.SetStderrTTY(tt.isTTY) + + f := &cmdutil.Factory{ + IOStreams: ios, + } + + var opts *ListOptions + cmd := NewCmdList(f, func(o *ListOptions) error { + opts = o + return nil + }) + cmd.PersistentFlags().StringP("repo", "R", "", "") + + argv, err := shlex.Split(tt.args) + require.NoError(t, err) + cmd.SetArgs(argv) + + cmd.SetIn(&bytes.Buffer{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + + _, err = cmd.ExecuteC() + if tt.wantErr != "" { + require.EqualError(t, err, tt.wantErr) + return + } else { + require.NoError(t, err) + } + + assert.Equal(t, tt.want.Limit, opts.Limit) + assert.Equal(t, tt.want.WebMode, opts.WebMode) + assert.Equal(t, tt.want.Organization, opts.Organization) + }) + } +} + +func Test_listRun(t *testing.T) { + tests := []struct { + name string + isTTY bool + opts ListOptions + httpStubs func(*httpmock.Registry) + wantErr string + wantStdout string + wantStderr string + wantBrowse string + }{ + { + name: "list repo rulesets", + isTTY: true, + wantStdout: heredoc.Doc(` + + Showing 3 of 3 rulesets in OWNER/REPO + + ID NAME SOURCE STATUS RULES + 4 test OWNER/REPO (repo) evaluate 1 + 42 asdf OWNER/REPO (repo) active 2 + 77 foobar Org-Name (org) disabled 4 + `), + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.GraphQL(`query RepoRulesetList\b`), + httpmock.FileResponse("./fixtures/rulesetList.json"), + ) + }, + wantStderr: "", + wantBrowse: "", + }, + { + name: "list org rulesets", + isTTY: true, + opts: ListOptions{ + Organization: "my-org", + }, + wantStdout: heredoc.Doc(` + + Showing 3 of 3 rulesets in my-org + + ID NAME SOURCE STATUS RULES + 4 test OWNER/REPO (repo) evaluate 1 + 42 asdf OWNER/REPO (repo) active 2 + 77 foobar Org-Name (org) disabled 4 + `), + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.GraphQL(`query OrgRulesetList\b`), + httpmock.FileResponse("./fixtures/rulesetList.json"), + ) + }, + wantStderr: "", + wantBrowse: "", + }, + { + name: "machine-readable", + isTTY: false, + wantStdout: heredoc.Doc(` + 4 test OWNER/REPO (repo) evaluate 1 + 42 asdf OWNER/REPO (repo) active 2 + 77 foobar Org-Name (org) disabled 4 + `), + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.GraphQL(`query RepoRulesetList\b`), + httpmock.FileResponse("./fixtures/rulesetList.json"), + ) + }, + wantStderr: "", + wantBrowse: "", + }, + { + name: "repo web mode, TTY", + isTTY: true, + opts: ListOptions{ + WebMode: true, + }, + wantStdout: "Opening github.com/OWNER/REPO/rules in your browser.\n", + wantStderr: "", + wantBrowse: "https://github.com/OWNER/REPO/rules", + }, + { + name: "org web mode, TTY", + isTTY: true, + opts: ListOptions{ + WebMode: true, + Organization: "my-org", + }, + wantStdout: "Opening github.com/organizations/my-org/settings/rules in your browser.\n", + wantStderr: "", + wantBrowse: "https://github.com/organizations/my-org/settings/rules", + }, + { + name: "repo web mode, non-TTY", + isTTY: false, + opts: ListOptions{ + WebMode: true, + }, + wantStdout: "", + wantStderr: "", + wantBrowse: "https://github.com/OWNER/REPO/rules", + }, + { + name: "org web mode, non-TTY", + isTTY: false, + opts: ListOptions{ + WebMode: true, + Organization: "my-org", + }, + wantStdout: "", + wantStderr: "", + wantBrowse: "https://github.com/organizations/my-org/settings/rules", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, stdout, stderr := iostreams.Test() + ios.SetStdoutTTY(tt.isTTY) + ios.SetStdinTTY(tt.isTTY) + ios.SetStderrTTY(tt.isTTY) + + reg := &httpmock.Registry{} + defer reg.Verify(t) + if tt.httpStubs != nil { + tt.httpStubs(reg) + } + + tt.opts.IO = ios + tt.opts.HttpClient = func() (*http.Client, error) { + return &http.Client{Transport: reg}, nil + } + tt.opts.BaseRepo = func() (ghrepo.Interface, error) { + return ghrepo.FromFullName("OWNER/REPO") + } + browser := &browser.Stub{} + tt.opts.Browser = browser + + err := listRun(&tt.opts) + + if tt.wantErr != "" { + require.EqualError(t, err, tt.wantErr) + return + } else { + require.NoError(t, err) + } + + if tt.wantBrowse != "" { + browser.Verify(t, tt.wantBrowse) + } + + assert.Equal(t, tt.wantStdout, stdout.String()) + assert.Equal(t, tt.wantStderr, stderr.String()) + }) + } +} diff --git a/pkg/cmd/ruleset/ruleset.go b/pkg/cmd/ruleset/ruleset.go new file mode 100644 index 000000000..86411d717 --- /dev/null +++ b/pkg/cmd/ruleset/ruleset.go @@ -0,0 +1,34 @@ +package ruleset + +import ( + "github.com/MakeNowJust/heredoc" + cmdCheck "github.com/cli/cli/v2/pkg/cmd/ruleset/check" + cmdList "github.com/cli/cli/v2/pkg/cmd/ruleset/list" + cmdView "github.com/cli/cli/v2/pkg/cmd/ruleset/view" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/spf13/cobra" +) + +func NewCmdRuleset(f *cmdutil.Factory) *cobra.Command { + cmd := &cobra.Command{ + Use: "ruleset ", + Short: "View info about repo rulesets", + Long: heredoc.Doc(` + Repository rulesets are a way to define a set of rules that apply to a repository. + These commands allow you to view information about them. + `), + Aliases: []string{"rs"}, + Example: heredoc.Doc(` + $ gh ruleset list + $ gh ruleset view --repo OWNER/REPO --web + $ gh ruleset check branch-name + `), + } + + cmdutil.EnableRepoOverride(cmd, f) + cmd.AddCommand(cmdList.NewCmdList(f, nil)) + cmd.AddCommand(cmdView.NewCmdView(f, nil)) + cmd.AddCommand(cmdCheck.NewCmdCheck(f, nil)) + + return cmd +} diff --git a/pkg/cmd/ruleset/shared/http.go b/pkg/cmd/ruleset/shared/http.go new file mode 100644 index 000000000..59480dadf --- /dev/null +++ b/pkg/cmd/ruleset/shared/http.go @@ -0,0 +1,133 @@ +package shared + +import ( + "errors" + "net/http" + "strings" + + "github.com/cli/cli/v2/api" + "github.com/cli/cli/v2/internal/ghrepo" +) + +type RulesetResponse struct { + Level struct { + Rulesets struct { + TotalCount int + Nodes []RulesetGraphQL + PageInfo struct { + HasNextPage bool + EndCursor string + } + } + } +} + +type RulesetList struct { + TotalCount int + Rulesets []RulesetGraphQL +} + +func ListRepoRulesets(httpClient *http.Client, repo ghrepo.Interface, limit int, includeParents bool) (*RulesetList, error) { + variables := map[string]interface{}{ + "owner": repo.RepoOwner(), + "repo": repo.RepoName(), + "includeParents": includeParents, + } + + return listRulesets(httpClient, rulesetsQuery(false), variables, limit, repo.RepoHost()) +} + +func ListOrgRulesets(httpClient *http.Client, orgLogin string, limit int, host string, includeParents bool) (*RulesetList, error) { + variables := map[string]interface{}{ + "login": orgLogin, + "includeParents": includeParents, + } + + return listRulesets(httpClient, rulesetsQuery(true), variables, limit, host) +} + +func listRulesets(httpClient *http.Client, query string, variables map[string]interface{}, limit int, host string) (*RulesetList, error) { + pageLimit := min(limit, 100) + + res := RulesetList{ + Rulesets: []RulesetGraphQL{}, + } + client := api.NewClientFromHTTP(httpClient) + + for { + variables["limit"] = pageLimit + var data RulesetResponse + err := client.GraphQL(host, query, variables, &data) + if err != nil { + if strings.Contains(err.Error(), "requires one of the following scopes: ['admin:org']") { + return nil, errors.New("the 'admin:org' scope is required to view organization rulesets, try running 'gh auth refresh -s admin:org'") + } + + return nil, err + } + + res.TotalCount = data.Level.Rulesets.TotalCount + res.Rulesets = append(res.Rulesets, data.Level.Rulesets.Nodes...) + + if len(res.Rulesets) >= limit { + break + } + + if data.Level.Rulesets.PageInfo.HasNextPage { + variables["endCursor"] = data.Level.Rulesets.PageInfo.EndCursor + pageLimit = min(pageLimit, limit-len(res.Rulesets)) + } else { + break + } + } + + return &res, nil +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} + +func rulesetsQuery(org bool) string { + if org { + return orgGraphQLHeader + sharedGraphQLBody + } else { + return repoGraphQLHeader + sharedGraphQLBody + } +} + +const repoGraphQLHeader = ` +query RepoRulesetList($limit: Int!, $endCursor: String, $includeParents: Boolean, $owner: String!, $repo: String!) { + level: repository(owner: $owner, name: $repo) { +` + +const orgGraphQLHeader = ` +query OrgRulesetList($limit: Int!, $endCursor: String, $includeParents: Boolean, $login: String!) { + level: organization(login: $login) { +` + +const sharedGraphQLBody = ` +rulesets(first: $limit, after: $endCursor, includeParents: $includeParents) { + totalCount + nodes { + databaseId + name + target + enforcement + source { + __typename + ... on Repository { owner: nameWithOwner } + ... on Organization { owner: login } + } + rules { + totalCount + } + } + pageInfo { + hasNextPage + endCursor + } +}}}` diff --git a/pkg/cmd/ruleset/shared/shared.go b/pkg/cmd/ruleset/shared/shared.go new file mode 100644 index 000000000..a71b20522 --- /dev/null +++ b/pkg/cmd/ruleset/shared/shared.go @@ -0,0 +1,127 @@ +package shared + +import ( + "fmt" + "sort" + "strings" + + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmdutil" +) + +type RulesetGraphQL struct { + DatabaseId int + Name string + Target string + Enforcement string + Source struct { + TypeName string `json:"__typename"` + Owner string + } + Rules struct { + TotalCount int + } +} + +type RulesetREST struct { + Id int + Name string + Target string + Enforcement string + BypassActors []struct { + ActorId int `json:"actor_id"` + ActorType string `json:"actor_type"` + BypassMode string `json:"bypass_mode"` + } `json:"bypass_actors"` + Conditions map[string]map[string]interface{} + SourceType string `json:"source_type"` + Source string + Rules []RulesetRule + Links struct { + Html struct { + Href string + } + } `json:"_links"` +} + +type RulesetRule struct { + Type string + Parameters map[string]interface{} + RulesetSourceType string `json:"ruleset_source_type"` + RulesetSource string `json:"ruleset_source"` + RulesetId int `json:"ruleset_id"` +} + +// Returns the source of the ruleset in the format "owner/name (repo)" or "owner (org)" +func RulesetSource(rs RulesetGraphQL) string { + var level string + if rs.Source.TypeName == "Repository" { + level = "repo" + } else if rs.Source.TypeName == "Organization" { + level = "org" + } else { + level = "unknown" + } + + return fmt.Sprintf("%s (%s)", rs.Source.Owner, level) +} + +func ParseRulesForDisplay(rules []RulesetRule) string { + var display strings.Builder + + // sort keys for consistent responses + sort.SliceStable(rules, func(i, j int) bool { + return rules[i].Type < rules[j].Type + }) + + for _, rule := range rules { + display.WriteString(fmt.Sprintf("- %s", rule.Type)) + + if rule.Parameters != nil && len(rule.Parameters) > 0 { + display.WriteString(": ") + + // sort these keys too for consistency + params := make([]string, 0, len(rule.Parameters)) + for p := range rule.Parameters { + params = append(params, p) + } + sort.Strings(params) + + for _, n := range params { + display.WriteString(fmt.Sprintf("[%s: %v] ", n, rule.Parameters[n])) + } + } + + // ruleset source info is only returned from the "get rules for a branch" endpoint + if rule.RulesetSource != "" { + display.WriteString( + fmt.Sprintf( + "\n (configured in ruleset %d from %s %s)\n", + rule.RulesetId, + strings.ToLower(rule.RulesetSourceType), + rule.RulesetSource, + ), + ) + } + + display.WriteString("\n") + } + + return display.String() +} + +func NoRulesetsFoundError(orgOption string, repoI ghrepo.Interface, includeParents bool) error { + entityName := EntityName(orgOption, repoI) + parentsMsg := "" + if includeParents { + parentsMsg = " or its parents" + } + return cmdutil.NewNoResultsError(fmt.Sprintf("no rulesets found in %s%s", entityName, parentsMsg)) +} + +func EntityName(orgOption string, repoI ghrepo.Interface) string { + if orgOption != "" { + return orgOption + } + return ghrepo.FullName(repoI) +} diff --git a/pkg/cmd/ruleset/view/fixtures/rulesetViewMultiple.json b/pkg/cmd/ruleset/view/fixtures/rulesetViewMultiple.json new file mode 100644 index 000000000..1b0d9e2cf --- /dev/null +++ b/pkg/cmd/ruleset/view/fixtures/rulesetViewMultiple.json @@ -0,0 +1,41 @@ +{ + "data": { + "level": { + "rulesets": { + "totalCount": 2, + "nodes": [ + { + "databaseId": 74, + "name": "My Org Ruleset", + "target": "BRANCH", + "enforcement": "EVALUATE", + "source": { + "__typename": "Organization", + "owner": "my-owner" + }, + "rules": { + "totalCount": 3 + } + }, + { + "databaseId": 42, + "name": "Test Ruleset", + "target": "BRANCH", + "enforcement": "ACTIVE", + "source": { + "__typename": "Repository", + "owner": "my-owner/repo-name" + }, + "rules": { + "totalCount": 3 + } + } + ], + "pageInfo": { + "hasNextPage": false, + "endCursor": "Mg" + } + } + } + } +} diff --git a/pkg/cmd/ruleset/view/fixtures/rulesetViewOrg.json b/pkg/cmd/ruleset/view/fixtures/rulesetViewOrg.json new file mode 100644 index 000000000..88a2bd7ee --- /dev/null +++ b/pkg/cmd/ruleset/view/fixtures/rulesetViewOrg.json @@ -0,0 +1,58 @@ +{ + "id": 74, + "name": "My Org Ruleset", + "target": "branch", + "source_type": "Organization", + "source": "my-owner", + "enforcement": "evaluate", + "conditions": { + "ref_name": { + "exclude": [], + "include": [ + "~ALL" + ] + }, + "repository_name": { + "exclude": [], + "include": [ + "~ALL" + ], + "protected": true + } + }, + "rules": [ + { + "type": "commit_message_pattern", + "parameters": { + "name": "", + "negate": false, + "pattern": "asdf", + "operator": "contains" + } + }, + { + "type": "commit_author_email_pattern", + "parameters": { + "name": "", + "negate": false, + "pattern": "@example.com", + "operator": "ends_with" + } + }, + { + "type": "creation" + } + ], + "node_id": "RRS_lACqUmVwb3NpdG9yec4dwx_uzSNG", + "_links": { + "self": { + "href": "https://api.github.com/repos/my-owner/repo-name/rulesets/74" + }, + "html": { + "href": "https://github.com/organizations/my-owner/settings/rules/74" + } + }, + "created_at": "2023-05-01T13:53:37.185-04:00", + "updated_at": "2023-06-29T17:38:03.722-04:00", + "bypass_actors": [] +} diff --git a/pkg/cmd/ruleset/view/fixtures/rulesetViewRepo.json b/pkg/cmd/ruleset/view/fixtures/rulesetViewRepo.json new file mode 100644 index 000000000..5824b341d --- /dev/null +++ b/pkg/cmd/ruleset/view/fixtures/rulesetViewRepo.json @@ -0,0 +1,62 @@ +{ + "id": 42, + "name": "Test Ruleset", + "target": "branch", + "source_type": "Repository", + "source": "my-owner/repo-name", + "enforcement": "active", + "conditions": { + "ref_name": { + "exclude": [], + "include": [ + "~ALL" + ] + } + }, + "rules": [ + { + "type": "commit_message_pattern", + "parameters": { + "name": "", + "negate": false, + "pattern": "asdf", + "operator": "contains" + } + }, + { + "type": "commit_author_email_pattern", + "parameters": { + "name": "", + "negate": false, + "pattern": "@example.com", + "operator": "ends_with" + } + }, + { + "type": "creation" + } + ], + "node_id": "RRS_lACqUmVwb3NpdG9yec4dwx_uzSNG", + "_links": { + "self": { + "href": "https://api.github.com/repos/my-owner/repo-name/rulesets/42" + }, + "html": { + "href": "https://github.com/my-owner/repo-name/rules/42" + } + }, + "created_at": "2023-05-01T13:53:37.185-04:00", + "updated_at": "2023-06-29T17:38:03.722-04:00", + "bypass_actors": [ + { + "actor_id": 5, + "actor_type": "RepositoryRole", + "bypass_mode": "always" + }, + { + "actor_id": 1, + "actor_type": "OrganizationAdmin", + "bypass_mode": "always" + } + ] +} diff --git a/pkg/cmd/ruleset/view/http.go b/pkg/cmd/ruleset/view/http.go new file mode 100644 index 000000000..d0b26f530 --- /dev/null +++ b/pkg/cmd/ruleset/view/http.go @@ -0,0 +1,32 @@ +package view + +import ( + "fmt" + "net/http" + + "github.com/cli/cli/v2/api" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmd/ruleset/shared" +) + +func viewRepoRuleset(httpClient *http.Client, repo ghrepo.Interface, databaseId string) (*shared.RulesetREST, error) { + path := fmt.Sprintf("repos/%s/%s/rulesets/%s", repo.RepoOwner(), repo.RepoName(), databaseId) + return viewRuleset(httpClient, repo.RepoHost(), path) +} + +func viewOrgRuleset(httpClient *http.Client, orgLogin string, databaseId string, host string) (*shared.RulesetREST, error) { + path := fmt.Sprintf("orgs/%s/rulesets/%s", orgLogin, databaseId) + return viewRuleset(httpClient, host, path) +} + +func viewRuleset(httpClient *http.Client, hostname string, path string) (*shared.RulesetREST, error) { + apiClient := api.NewClientFromHTTP(httpClient) + result := shared.RulesetREST{} + + err := apiClient.REST(hostname, "GET", path, nil, &result) + if err != nil { + return nil, err + } + + return &result, nil +} diff --git a/pkg/cmd/ruleset/view/view.go b/pkg/cmd/ruleset/view/view.go new file mode 100644 index 000000000..813f72c51 --- /dev/null +++ b/pkg/cmd/ruleset/view/view.go @@ -0,0 +1,270 @@ +package view + +import ( + "fmt" + "net/http" + "sort" + "strconv" + "strings" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" + "github.com/cli/cli/v2/internal/text" + "github.com/cli/cli/v2/pkg/cmd/ruleset/shared" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + ghAuth "github.com/cli/go-gh/v2/pkg/auth" + "github.com/spf13/cobra" +) + +type ViewOptions struct { + HttpClient func() (*http.Client, error) + IO *iostreams.IOStreams + BaseRepo func() (ghrepo.Interface, error) + Browser browser.Browser + Prompter prompter.Prompter + + ID string + WebMode bool + IncludeParents bool + InteractiveMode bool + Organization string +} + +func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command { + opts := &ViewOptions{ + IO: f.IOStreams, + HttpClient: f.HttpClient, + Browser: f.Browser, + Prompter: f.Prompter, + } + + cmd := &cobra.Command{ + Use: "view []", + Short: "View information about a ruleset", + Long: heredoc.Doc(` + View information about a GitHub ruleset. + + If no ID is provided, an interactive prompt will be used to choose + the ruleset to view. + + Use the --parents flag to control whether rulesets configured at higher + levels that also apply to the provided repository or organization should + be returned. The default is true. + `), + Example: heredoc.Doc(` + # Interactively choose a ruleset to view from all rulesets that apply to the current repository + $ gh ruleset view + + # Interactively choose a ruleset to view from only rulesets configured in the current repository + $ gh ruleset view --no-parents + + # View a ruleset configured in the current repository or any of its parents + $ gh ruleset view 43 + + # View a ruleset configured in a different repository or any of its parents + $ gh ruleset view 23 --repo owner/repo + + # View an organization-level ruleset + $ gh ruleset view 23 --org my-org + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && opts.Organization != "" { + return cmdutil.FlagErrorf("only one of --repo and --org may be specified") + } + + // support `-R, --repo` override + opts.BaseRepo = f.BaseRepo + + if len(args) > 0 { + // a string is actually needed later on, so verify that it's numeric + // but use the string anyway + _, err := strconv.Atoi(args[0]) + if err != nil { + return cmdutil.FlagErrorf("invalid value for ruleset ID: %v is not an integer", args[0]) + } + opts.ID = args[0] + } else if !opts.IO.CanPrompt() { + return cmdutil.FlagErrorf("a ruleset ID must be provided when not running interactively") + } else { + opts.InteractiveMode = true + } + + if runF != nil { + return runF(opts) + } + return viewRun(opts) + }, + } + + cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the ruleset in the browser") + cmd.Flags().StringVarP(&opts.Organization, "org", "o", "", "Organization name if the provided ID is an organization-level ruleset") + cmd.Flags().BoolVarP(&opts.IncludeParents, "parents", "p", true, "Whether to include rulesets configured at higher levels that also apply") + + return cmd +} + +func viewRun(opts *ViewOptions) error { + httpClient, err := opts.HttpClient() + if err != nil { + return err + } + + repoI, err := opts.BaseRepo() + if err != nil { + return err + } + + hostname, _ := ghAuth.DefaultHost() + cs := opts.IO.ColorScheme() + + if opts.InteractiveMode { + var rsList *shared.RulesetList + limit := 30 + if opts.Organization != "" { + rsList, err = shared.ListOrgRulesets(httpClient, opts.Organization, limit, hostname, opts.IncludeParents) + } else { + rsList, err = shared.ListRepoRulesets(httpClient, repoI, limit, opts.IncludeParents) + } + + if err != nil { + return err + } + + if rsList.TotalCount == 0 { + return shared.NoRulesetsFoundError(opts.Organization, repoI, opts.IncludeParents) + } + + rs, err := selectRulesetID(rsList, opts.Prompter, cs) + if err != nil { + return err + } + + if rs != nil { + opts.ID = strconv.Itoa(rs.DatabaseId) + + // can't get a ruleset lower in the chain than what was queried, so no need to handle repos here + if rs.Source.TypeName == "Organization" { + opts.Organization = rs.Source.Owner + } + } + } + + var rs *shared.RulesetREST + if opts.Organization != "" { + rs, err = viewOrgRuleset(httpClient, opts.Organization, opts.ID, hostname) + } else { + rs, err = viewRepoRuleset(httpClient, repoI, opts.ID) + } + + if err != nil { + return err + } + + w := opts.IO.Out + + if opts.WebMode { + if rs != nil { + if opts.IO.IsStdoutTTY() { + fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", text.DisplayURL(rs.Links.Html.Href)) + } + + return opts.Browser.Browse(rs.Links.Html.Href) + } else { + fmt.Fprintf(w, "ruleset not found\n") + } + } + + fmt.Fprintf(w, "\n%s\n", cs.Bold(rs.Name)) + fmt.Fprintf(w, "ID: %s\n", cs.Cyan(strconv.Itoa(rs.Id))) + fmt.Fprintf(w, "Source: %s (%s)\n", rs.Source, rs.SourceType) + + fmt.Fprint(w, "Enforcement: ") + switch rs.Enforcement { + case "disabled": + fmt.Fprintf(w, "%s\n", cs.Red("Disabled")) + case "evaluate": + fmt.Fprintf(w, "%s\n", cs.Yellow("Evaluate Mode (not enforced)")) + case "active": + fmt.Fprintf(w, "%s\n", cs.Green("Active")) + default: + fmt.Fprintf(w, "%s\n", rs.Enforcement) + } + + fmt.Fprintf(w, "\n%s\n", cs.Bold("Bypass List")) + if len(rs.BypassActors) == 0 { + fmt.Fprintf(w, "This ruleset cannot be bypassed\n") + } else { + sort.Slice(rs.BypassActors, func(i, j int) bool { + return rs.BypassActors[i].ActorId < rs.BypassActors[j].ActorId + }) + + for _, t := range rs.BypassActors { + fmt.Fprintf(w, "- %s (ID: %d), mode: %s\n", t.ActorType, t.ActorId, t.BypassMode) + } + } + + fmt.Fprintf(w, "\n%s\n", cs.Bold("Conditions")) + if len(rs.Conditions) == 0 { + fmt.Fprintf(w, "No conditions configured\n") + } else { + // sort keys for consistent responses + keys := make([]string, 0, len(rs.Conditions)) + for key := range rs.Conditions { + keys = append(keys, key) + } + sort.Strings(keys) + + for _, name := range keys { + condition := rs.Conditions[name] + fmt.Fprintf(w, "- %s: ", name) + + // sort these keys too for consistency + subkeys := make([]string, 0, len(condition)) + for subkey := range condition { + subkeys = append(subkeys, subkey) + } + sort.Strings(subkeys) + + for _, n := range subkeys { + fmt.Fprintf(w, "[%s: %v] ", n, condition[n]) + } + + fmt.Fprint(w, "\n") + } + } + + fmt.Fprintf(w, "\n%s\n", cs.Bold("Rules")) + if len(rs.Rules) == 0 { + fmt.Fprintf(w, "No rules configured\n") + } else { + fmt.Fprint(w, shared.ParseRulesForDisplay(rs.Rules)) + } + + return nil +} + +func selectRulesetID(rsList *shared.RulesetList, p prompter.Prompter, cs *iostreams.ColorScheme) (*shared.RulesetGraphQL, error) { + rulesets := make([]string, len(rsList.Rulesets)) + for i, rs := range rsList.Rulesets { + s := fmt.Sprintf( + "%s: %s | %s | contains %s | configured in %s", + cs.Cyan(strconv.Itoa(rs.DatabaseId)), + rs.Name, + strings.ToLower(rs.Enforcement), + text.Pluralize(rs.Rules.TotalCount, "rule"), + shared.RulesetSource(rs), + ) + rulesets[i] = s + } + + r, err := p.Select("Which ruleset would you like to view?", rulesets[0], rulesets) + if err != nil { + return nil, err + } + + return &rsList.Rulesets[r], nil +} diff --git a/pkg/cmd/ruleset/view/view_test.go b/pkg/cmd/ruleset/view/view_test.go new file mode 100644 index 000000000..ce9c21db4 --- /dev/null +++ b/pkg/cmd/ruleset/view/view_test.go @@ -0,0 +1,364 @@ +package view + +import ( + "bytes" + "io" + "net/http" + "testing" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/httpmock" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_NewCmdView(t *testing.T) { + tests := []struct { + name string + args string + isTTY bool + want ViewOptions + wantErr string + }{ + { + name: "no arguments", + args: "", + isTTY: true, + want: ViewOptions{ + ID: "", + WebMode: false, + IncludeParents: true, + InteractiveMode: true, + Organization: "", + }, + }, + { + name: "only ID", + args: "3", + isTTY: true, + want: ViewOptions{ + ID: "3", + WebMode: false, + IncludeParents: true, + InteractiveMode: false, + Organization: "", + }, + }, + { + name: "org", + args: "--org \"my-org\"", + isTTY: true, + want: ViewOptions{ + ID: "", + WebMode: false, + IncludeParents: true, + InteractiveMode: true, + Organization: "my-org", + }, + }, + { + name: "web mode", + args: "--web", + isTTY: true, + want: ViewOptions{ + ID: "", + WebMode: true, + IncludeParents: true, + InteractiveMode: true, + Organization: "", + }, + }, + { + name: "parents", + args: "--parents=false", + isTTY: true, + want: ViewOptions{ + ID: "", + WebMode: false, + IncludeParents: false, + InteractiveMode: true, + Organization: "", + }, + }, + { + name: "repo and org specified", + args: "--org \"my-org\" -R \"owner/repo\"", + isTTY: true, + wantErr: "only one of --repo and --org may be specified", + }, + { + name: "invalid ID", + args: "1.5", + isTTY: true, + wantErr: "invalid value for ruleset ID: 1.5 is not an integer", + }, + { + name: "ID not provided and not TTY", + args: "", + isTTY: false, + wantErr: "a ruleset ID must be provided when not running interactively", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + ios.SetStdoutTTY(tt.isTTY) + ios.SetStdinTTY(tt.isTTY) + ios.SetStderrTTY(tt.isTTY) + + f := &cmdutil.Factory{ + IOStreams: ios, + } + + var opts *ViewOptions + cmd := NewCmdView(f, func(o *ViewOptions) error { + opts = o + return nil + }) + cmd.PersistentFlags().StringP("repo", "R", "", "") + + argv, err := shlex.Split(tt.args) + require.NoError(t, err) + cmd.SetArgs(argv) + + cmd.SetIn(&bytes.Buffer{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + + _, err = cmd.ExecuteC() + if tt.wantErr != "" { + require.EqualError(t, err, tt.wantErr) + return + } else { + require.NoError(t, err) + } + + assert.Equal(t, tt.want.ID, opts.ID) + assert.Equal(t, tt.want.WebMode, opts.WebMode) + assert.Equal(t, tt.want.IncludeParents, opts.IncludeParents) + assert.Equal(t, tt.want.InteractiveMode, opts.InteractiveMode) + assert.Equal(t, tt.want.Organization, opts.Organization) + }) + } +} + +func Test_viewRun(t *testing.T) { + repoRulesetStdout := heredoc.Doc(` + + Test Ruleset + ID: 42 + Source: my-owner/repo-name (Repository) + Enforcement: Active + + Bypass List + - OrganizationAdmin (ID: 1), mode: always + - RepositoryRole (ID: 5), mode: always + + Conditions + - ref_name: [exclude: []] [include: [~ALL]] + + Rules + - commit_author_email_pattern: [name: ] [negate: false] [operator: ends_with] [pattern: @example.com] + - commit_message_pattern: [name: ] [negate: false] [operator: contains] [pattern: asdf] + - creation + `) + + tests := []struct { + name string + isTTY bool + opts ViewOptions + httpStubs func(*httpmock.Registry) + prompterStubs func(*prompter.MockPrompter) + wantErr string + wantStdout string + wantStderr string + wantBrowse string + }{ + { + name: "view repo ruleset", + isTTY: true, + opts: ViewOptions{ + ID: "42", + }, + wantStdout: repoRulesetStdout, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.REST("GET", "repos/my-owner/repo-name/rulesets/42"), + httpmock.FileResponse("./fixtures/rulesetViewRepo.json"), + ) + }, + wantStderr: "", + wantBrowse: "", + }, + { + name: "view org ruleset", + isTTY: true, + opts: ViewOptions{ + ID: "74", + Organization: "my-owner", + }, + wantStdout: heredoc.Doc(` + + My Org Ruleset + ID: 74 + Source: my-owner (Organization) + Enforcement: Evaluate Mode (not enforced) + + Bypass List + This ruleset cannot be bypassed + + Conditions + - ref_name: [exclude: []] [include: [~ALL]] + - repository_name: [exclude: []] [include: [~ALL]] [protected: true] + + Rules + - commit_author_email_pattern: [name: ] [negate: false] [operator: ends_with] [pattern: @example.com] + - commit_message_pattern: [name: ] [negate: false] [operator: contains] [pattern: asdf] + - creation + `), + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.REST("GET", "orgs/my-owner/rulesets/74"), + httpmock.FileResponse("./fixtures/rulesetViewOrg.json"), + ) + }, + wantStderr: "", + wantBrowse: "", + }, + { + name: "prompter", + isTTY: true, + opts: ViewOptions{ + InteractiveMode: true, + }, + wantStdout: repoRulesetStdout, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.GraphQL(`query RepoRulesetList\b`), + httpmock.FileResponse("./fixtures/rulesetViewMultiple.json"), + ) + reg.Register( + httpmock.REST("GET", "repos/my-owner/repo-name/rulesets/42"), + httpmock.FileResponse("./fixtures/rulesetViewRepo.json"), + ) + }, + prompterStubs: func(pm *prompter.MockPrompter) { + const repoRuleset = "42: Test Ruleset | active | contains 3 rules | configured in my-owner/repo-name (repo)" + pm.RegisterSelect("Which ruleset would you like to view?", + []string{ + "74: My Org Ruleset | evaluate | contains 3 rules | configured in my-owner (org)", + repoRuleset, + }, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, repoRuleset) + }) + }, + }, + { + name: "web mode, TTY, repo", + isTTY: true, + opts: ViewOptions{ + ID: "42", + WebMode: true, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.REST("GET", "repos/my-owner/repo-name/rulesets/42"), + httpmock.FileResponse("./fixtures/rulesetViewRepo.json"), + ) + }, + wantStdout: "Opening github.com/my-owner/repo-name/rules/42 in your browser.\n", + wantStderr: "", + wantBrowse: "https://github.com/my-owner/repo-name/rules/42", + }, + { + name: "web mode, non-TTY, repo", + isTTY: false, + opts: ViewOptions{ + ID: "42", + WebMode: true, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.REST("GET", "repos/my-owner/repo-name/rulesets/42"), + httpmock.FileResponse("./fixtures/rulesetViewRepo.json"), + ) + }, + wantStdout: "", + wantStderr: "", + wantBrowse: "https://github.com/my-owner/repo-name/rules/42", + }, + { + name: "web mode, TTY, org", + isTTY: true, + opts: ViewOptions{ + ID: "74", + Organization: "my-owner", + WebMode: true, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.REST("GET", "orgs/my-owner/rulesets/74"), + httpmock.FileResponse("./fixtures/rulesetViewOrg.json"), + ) + }, + wantStdout: "Opening github.com/organizations/my-owner/settings/rules/74 in your browser.\n", + wantStderr: "", + wantBrowse: "https://github.com/organizations/my-owner/settings/rules/74", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, stdout, stderr := iostreams.Test() + ios.SetStdoutTTY(tt.isTTY) + ios.SetStdinTTY(tt.isTTY) + ios.SetStderrTTY(tt.isTTY) + + pm := prompter.NewMockPrompter(t) + if tt.prompterStubs != nil { + tt.prompterStubs(pm) + } + tt.opts.Prompter = pm + + reg := &httpmock.Registry{} + defer reg.Verify(t) + if tt.httpStubs != nil { + tt.httpStubs(reg) + } + + tt.opts.IO = ios + tt.opts.HttpClient = func() (*http.Client, error) { + return &http.Client{Transport: reg}, nil + } + tt.opts.BaseRepo = func() (ghrepo.Interface, error) { + return ghrepo.FromFullName("my-owner/repo-name") + } + browser := &browser.Stub{} + tt.opts.Browser = browser + + err := viewRun(&tt.opts) + + if tt.wantErr != "" { + require.EqualError(t, err, tt.wantErr) + return + } else { + require.NoError(t, err) + } + + if tt.wantBrowse != "" { + browser.Verify(t, tt.wantBrowse) + } + + assert.Equal(t, tt.wantStdout, stdout.String()) + assert.Equal(t, tt.wantStderr, stderr.String()) + }) + } +} diff --git a/pkg/cmd/run/cancel/cancel.go b/pkg/cmd/run/cancel/cancel.go index ca4521b1d..3e6cd9ffb 100644 --- a/pkg/cmd/run/cancel/cancel.go +++ b/pkg/cmd/run/cancel/cancel.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/http" + "strconv" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/ghrepo" @@ -17,6 +18,7 @@ type CancelOptions struct { HttpClient func() (*http.Client, error) IO *iostreams.IOStreams BaseRepo func() (ghrepo.Interface, error) + Prompter shared.Prompter Prompt bool @@ -27,6 +29,7 @@ func NewCmdCancel(f *cmdutil.Factory, runF func(*CancelOptions) error) *cobra.Co opts := &CancelOptions{ IO: f.IOStreams, HttpClient: f.HttpClient, + Prompter: f.Prompter, } cmd := &cobra.Command{ @@ -57,6 +60,12 @@ func NewCmdCancel(f *cmdutil.Factory, runF func(*CancelOptions) error) *cobra.Co } func runCancel(opts *CancelOptions) error { + if opts.RunID != "" { + _, err := strconv.Atoi(opts.RunID) + if err != nil { + return fmt.Errorf("invalid run-id %#v", opts.RunID) + } + } httpClient, err := opts.HttpClient() if err != nil { return fmt.Errorf("failed to create http client: %w", err) @@ -83,11 +92,10 @@ func runCancel(opts *CancelOptions) error { if len(runs) == 0 { return fmt.Errorf("found no in progress runs to cancel") } - runID, err = shared.PromptForRun(cs, runs) - if err != nil { + if runID, err = shared.SelectRun(opts.Prompter, cs, runs); err != nil { return err } - // TODO silly stopgap until dust settles and PromptForRun can just return a run + // TODO silly stopgap until dust settles and SelectRun can just return a run for _, r := range runs { if fmt.Sprintf("%d", r.ID) == runID { run = &r @@ -119,7 +127,7 @@ func runCancel(opts *CancelOptions) error { return err } - fmt.Fprintf(opts.IO.Out, "%s Request to cancel workflow submitted.\n", cs.SuccessIcon()) + fmt.Fprintf(opts.IO.Out, "%s Request to cancel workflow %s submitted.\n", cs.SuccessIcon(), runID) return nil } diff --git a/pkg/cmd/run/cancel/cancel_test.go b/pkg/cmd/run/cancel/cancel_test.go index bcde453c2..d3f580870 100644 --- a/pkg/cmd/run/cancel/cancel_test.go +++ b/pkg/cmd/run/cancel/cancel_test.go @@ -7,12 +7,12 @@ import ( "testing" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/run/shared" workflowShared "github.com/cli/cli/v2/pkg/cmd/workflow/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -86,13 +86,13 @@ func TestRunCancel(t *testing.T) { inProgressRun := shared.TestRun(1234, shared.InProgress, "") completedRun := shared.TestRun(4567, shared.Completed, shared.Failure) tests := []struct { - name string - httpStubs func(*httpmock.Registry) - askStubs func(*prompt.AskStubber) - opts *CancelOptions - wantErr bool - wantOut string - errMsg string + name string + httpStubs func(*httpmock.Registry) + promptStubs func(*prompter.MockPrompter) + opts *CancelOptions + wantErr bool + wantOut string + errMsg string }{ { name: "cancel run", @@ -111,7 +111,7 @@ func TestRunCancel(t *testing.T) { httpmock.REST("POST", "repos/OWNER/REPO/actions/runs/1234/cancel"), httpmock.StatusStringResponse(202, "{}")) }, - wantOut: "✓ Request to cancel workflow submitted.\n", + wantOut: "✓ Request to cancel workflow 1234 submitted.\n", }, { name: "not found", @@ -194,11 +194,24 @@ func TestRunCancel(t *testing.T) { httpmock.REST("POST", "repos/OWNER/REPO/actions/runs/1234/cancel"), httpmock.StatusStringResponse(202, "{}")) }, - askStubs: func(as *prompt.AskStubber) { - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(0) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"* cool commit, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "* cool commit, CI (trunk) Feb 23, 2021") + }) }, - wantOut: "✓ Request to cancel workflow submitted.\n", + wantOut: "✓ Request to cancel workflow 1234 submitted.\n", + }, + { + name: "invalid run-id", + opts: &CancelOptions{ + RunID: "12\n34", + }, + httpStubs: func(reg *httpmock.Registry) { + }, + wantErr: true, + errMsg: "invalid run-id \"12\\n34\"", }, } @@ -217,11 +230,10 @@ func TestRunCancel(t *testing.T) { return ghrepo.FromFullName("OWNER/REPO") } - //nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber - as, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(as) + pm := prompter.NewMockPrompter(t) + tt.opts.Prompter = pm + if tt.promptStubs != nil { + tt.promptStubs(pm) } t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/cmd/run/download/download.go b/pkg/cmd/run/download/download.go index 25eec5d62..993e16e52 100644 --- a/pkg/cmd/run/download/download.go +++ b/pkg/cmd/run/download/download.go @@ -5,12 +5,10 @@ import ( "fmt" "path/filepath" - "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/pkg/cmd/run/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/cli/cli/v2/pkg/set" "github.com/spf13/cobra" ) @@ -18,7 +16,7 @@ import ( type DownloadOptions struct { IO *iostreams.IOStreams Platform platform - Prompter prompter + Prompter iprompter DoPrompt bool RunID string @@ -31,13 +29,14 @@ type platform interface { List(runID string) ([]shared.Artifact, error) Download(url string, dir string) error } -type prompter interface { - Prompt(message string, options []string, result interface{}) error +type iprompter interface { + MultiSelect(string, []string, []string) ([]int, error) } func NewCmdDownload(f *cmdutil.Factory, runF func(*DownloadOptions) error) *cobra.Command { opts := &DownloadOptions{ - IO: f.IOStreams, + IO: f.IOStreams, + Prompter: f.Prompter, } cmd := &cobra.Command{ @@ -85,7 +84,6 @@ func NewCmdDownload(f *cmdutil.Factory, runF func(*DownloadOptions) error) *cobr client: httpClient, repo: baseRepo, } - opts.Prompter = &surveyPrompter{} if runF != nil { return runF(opts) @@ -133,10 +131,14 @@ func runDownload(opts *DownloadOptions) error { if len(options) > 10 { options = options[:10] } - err := opts.Prompter.Prompt("Select artifacts to download:", options, &wantNames) - if err != nil { + var selected []int + if selected, err = opts.Prompter.MultiSelect("Select artifacts to download:", nil, options); err != nil { return err } + wantNames = []string{} + for _, x := range selected { + wantNames = append(wantNames, options[x]) + } if len(wantNames) == 0 { return errors.New("no artifacts selected") } @@ -194,13 +196,3 @@ func matchAnyPattern(patterns []string, name string) bool { } return false } - -type surveyPrompter struct{} - -func (sp *surveyPrompter) Prompt(message string, options []string, result interface{}) error { - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - return prompt.SurveyAskOne(&survey.MultiSelect{ - Message: message, - Options: options, - }, result) -} diff --git a/pkg/cmd/run/download/download_test.go b/pkg/cmd/run/download/download_test.go index 10c7bbe3c..3c1c8f2d8 100644 --- a/pkg/cmd/run/download/download_test.go +++ b/pkg/cmd/run/download/download_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/run/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" @@ -144,11 +145,11 @@ func Test_NewCmdDownload(t *testing.T) { func Test_runDownload(t *testing.T) { tests := []struct { - name string - opts DownloadOptions - mockAPI func(*mockPlatform) - mockPrompt func(*mockPrompter) - wantErr string + name string + opts DownloadOptions + mockAPI func(*mockPlatform) + promptStubs func(*prompter.MockPrompter) + wantErr string }{ { name: "download non-expired", @@ -281,13 +282,11 @@ func Test_runDownload(t *testing.T) { }, nil) p.On("Download", "http://download.com/artifact2.zip", ".").Return(nil) }, - mockPrompt: func(p *mockPrompter) { - p.On("Prompt", "Select artifacts to download:", []string{"artifact-1", "artifact-2"}, mock.AnythingOfType("*[]string")). - Run(func(args mock.Arguments) { - result := args.Get(2).(*[]string) - *result = []string{"artifact-2"} - }). - Return(nil) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterMultiSelect("Select artifacts to download:", nil, []string{"artifact-1", "artifact-2"}, + func(_ string, _, opts []string) ([]int, error) { + return []int{1}, nil + }) }, }, } @@ -297,7 +296,12 @@ func Test_runDownload(t *testing.T) { ios, _, stdout, stderr := iostreams.Test() opts.IO = ios opts.Platform = newMockPlatform(t, tt.mockAPI) - opts.Prompter = newMockPrompter(t, tt.mockPrompt) + + pm := prompter.NewMockPrompter(t) + opts.Prompter = pm + if tt.promptStubs != nil { + tt.promptStubs(pm) + } err := runDownload(opts) if tt.wantErr != "" { @@ -337,24 +341,3 @@ func (p *mockPlatform) Download(url string, dir string) error { args := p.Called(url, dir) return args.Error(0) } - -type mockPrompter struct { - mock.Mock -} - -func newMockPrompter(t *testing.T, config func(*mockPrompter)) *mockPrompter { - m := &mockPrompter{} - m.Test(t) - t.Cleanup(func() { - m.AssertExpectations(t) - }) - if config != nil { - config(m) - } - return m -} - -func (p *mockPrompter) Prompt(msg string, opts []string, res interface{}) error { - args := p.Called(msg, opts, res) - return args.Error(0) -} diff --git a/pkg/cmd/run/list/list.go b/pkg/cmd/run/list/list.go index 4b085a1a1..af84b089e 100644 --- a/pkg/cmd/run/list/list.go +++ b/pkg/cmd/run/list/list.go @@ -32,6 +32,8 @@ type ListOptions struct { Branch string Actor string Status string + Event string + Created string now time.Time } @@ -68,6 +70,8 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman cmd.Flags().StringVarP(&opts.WorkflowSelector, "workflow", "w", "", "Filter runs by workflow") cmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "Filter runs by branch") cmd.Flags().StringVarP(&opts.Actor, "user", "u", "", "Filter runs by user who triggered the run") + cmd.Flags().StringVarP(&opts.Event, "event", "e", "", "Filter runs by which `event` triggered the run") + cmd.Flags().StringVarP(&opts.Created, "created", "", "", "Filter runs by the `date` it was created") cmdutil.StringEnumFlag(cmd, &opts.Status, "status", "s", "", shared.AllStatuses, "Filter runs by status") cmdutil.AddJSONFlags(cmd, &opts.Exporter, shared.RunFields) @@ -89,9 +93,11 @@ func listRun(opts *ListOptions) error { client := api.NewClientFromHTTP(c) filters := &shared.FilterOptions{ - Branch: opts.Branch, - Actor: opts.Actor, - Status: opts.Status, + Branch: opts.Branch, + Actor: opts.Actor, + Status: opts.Status, + Event: opts.Event, + Created: opts.Created, } opts.IO.StartProgressIndicator() diff --git a/pkg/cmd/run/list/list_test.go b/pkg/cmd/run/list/list_test.go index d33af7167..cf5ba97ad 100644 --- a/pkg/cmd/run/list/list_test.go +++ b/pkg/cmd/run/list/list_test.go @@ -77,6 +77,22 @@ func TestNewCmdList(t *testing.T) { Status: "completed", }, }, + { + name: "event", + cli: "--event push", + wants: ListOptions{ + Limit: defaultLimit, + Event: "push", + }, + }, + { + name: "created", + cli: "--created >=2023-04-24", + wants: ListOptions{ + Limit: defaultLimit, + Created: ">=2023-04-24", + }, + }, } for _, tt := range tests { @@ -112,6 +128,9 @@ func TestNewCmdList(t *testing.T) { assert.Equal(t, tt.wants.WorkflowSelector, gotOpts.WorkflowSelector) assert.Equal(t, tt.wants.Branch, gotOpts.Branch) assert.Equal(t, tt.wants.Actor, gotOpts.Actor) + assert.Equal(t, tt.wants.Status, gotOpts.Status) + assert.Equal(t, tt.wants.Event, gotOpts.Event) + assert.Equal(t, tt.wants.Created, gotOpts.Created) }) } } @@ -424,6 +443,42 @@ func TestListRun(t *testing.T) { wantErr: true, wantErrMsg: "no runs found", }, + { + name: "event filter applied", + opts: &ListOptions{ + Limit: defaultLimit, + Event: "push", + }, + isTTY: true, + stubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.QueryMatcher("GET", "repos/OWNER/REPO/actions/runs", url.Values{ + "event": []string{"push"}, + }), + httpmock.JSONResponse(shared.RunsPayload{}), + ) + }, + wantErr: true, + wantErrMsg: "no runs found", + }, + { + name: "created filter applied", + opts: &ListOptions{ + Limit: defaultLimit, + Created: ">=2023-04-24", + }, + isTTY: true, + stubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.QueryMatcher("GET", "repos/OWNER/REPO/actions/runs", url.Values{ + "created": []string{">=2023-04-24"}, + }), + httpmock.JSONResponse(shared.RunsPayload{}), + ) + }, + wantErr: true, + wantErrMsg: "no runs found", + }, } for _, tt := range tests { diff --git a/pkg/cmd/run/rerun/rerun.go b/pkg/cmd/run/rerun/rerun.go index 2522e632c..56580c642 100644 --- a/pkg/cmd/run/rerun/rerun.go +++ b/pkg/cmd/run/rerun/rerun.go @@ -8,6 +8,7 @@ import ( "io" "net/http" + "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmd/run/shared" @@ -20,6 +21,7 @@ type RerunOptions struct { HttpClient func() (*http.Client, error) IO *iostreams.IOStreams BaseRepo func() (ghrepo.Interface, error) + Prompter shared.Prompter RunID string OnlyFailed bool @@ -32,13 +34,25 @@ type RerunOptions struct { func NewCmdRerun(f *cmdutil.Factory, runF func(*RerunOptions) error) *cobra.Command { opts := &RerunOptions{ IO: f.IOStreams, + Prompter: f.Prompter, HttpClient: f.HttpClient, } cmd := &cobra.Command{ Use: "rerun []", - Short: "Rerun a failed run", - Args: cobra.MaximumNArgs(1), + Short: "Rerun a run", + Long: heredoc.Docf(` + Rerun an entire run, only failed jobs, or a specific job from a run. + + Note that due for historical reasons, the %[1]s--job%[1]s flag may not take what you expect. + Specifically, when navigating to a job in the browser, the URL looks like this: + %[1]shttps://github.com///actions/runs//jobs/%[1]s. + + However, this %[1]snumber%[1]s should not be used with the %[1]s--job%[1]s flag and will result in the + API returning %[1]s404 NOT FOUND%[1]s. Instead, you can get the correct job IDs using the following command: + %[1]sgh run view --json jobs --jq '.jobs[] | {name, databaseId}'%[1]s. + `, "`"), + Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { // support `-R, --repo` override opts.BaseRepo = f.BaseRepo @@ -114,8 +128,7 @@ func runRerun(opts *RerunOptions) error { if len(runs) == 0 { return errors.New("no recent runs have failed; please specify a specific ``") } - runID, err = shared.PromptForRun(cs, runs) - if err != nil { + if runID, err = shared.SelectRun(opts.Prompter, cs, runs); err != nil { return err } } diff --git a/pkg/cmd/run/rerun/rerun_test.go b/pkg/cmd/run/rerun/rerun_test.go index 730952f1f..991ab5f3a 100644 --- a/pkg/cmd/run/rerun/rerun_test.go +++ b/pkg/cmd/run/rerun/rerun_test.go @@ -8,12 +8,12 @@ import ( "testing" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/run/shared" workflowShared "github.com/cli/cli/v2/pkg/cmd/workflow/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -161,15 +161,15 @@ func TestNewCmdRerun(t *testing.T) { func TestRerun(t *testing.T) { tests := []struct { - name string - httpStubs func(*httpmock.Registry) - askStubs func(*prompt.AskStubber) - opts *RerunOptions - tty bool - wantErr bool - errOut string - wantOut string - wantDebug bool + name string + httpStubs func(*httpmock.Registry) + promptStubs func(*prompter.MockPrompter) + opts *RerunOptions + tty bool + wantErr bool + errOut string + wantOut string + wantDebug bool }{ { name: "arg", @@ -336,9 +336,12 @@ func TestRerun(t *testing.T) { httpmock.REST("POST", "repos/OWNER/REPO/actions/runs/1234/rerun"), httpmock.StringResponse("{}")) }, - askStubs: func(as *prompt.AskStubber) { - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(2) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"X cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return 2, nil + }) }, wantOut: "✓ Requested rerun of run 1234\n", }, @@ -408,11 +411,10 @@ func TestRerun(t *testing.T) { return ghrepo.FromFullName("OWNER/REPO") } - //nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber - as, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(as) + pm := prompter.NewMockPrompter(t) + tt.opts.Prompter = pm + if tt.promptStubs != nil { + tt.promptStubs(pm) } t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/cmd/run/shared/shared.go b/pkg/cmd/run/shared/shared.go index 94a5590a3..a755ce93c 100644 --- a/pkg/cmd/run/shared/shared.go +++ b/pkg/cmd/run/shared/shared.go @@ -9,15 +9,16 @@ import ( "strings" "time" - "github.com/AlecAivazis/survey/v2" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/ghrepo" - "github.com/cli/cli/v2/internal/prompter" workflowShared "github.com/cli/cli/v2/pkg/cmd/workflow/shared" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" ) +type Prompter interface { + Select(string, string, []string) (int, error) +} + const ( // Run statuses Queued Status = "queued" @@ -303,6 +304,8 @@ type FilterOptions struct { // avoid loading workflow name separately and use the provided one WorkflowName string Status string + Event string + Created string } // GetRunsWithFilter fetches 50 runs from the API and filters them in-memory @@ -348,6 +351,12 @@ func GetRuns(client *api.Client, repo ghrepo.Interface, opts *FilterOptions, lim if opts.Status != "" { path += fmt.Sprintf("&status=%s", url.QueryEscape(opts.Status)) } + if opts.Event != "" { + path += fmt.Sprintf("&event=%s", url.QueryEscape(opts.Event)) + } + if opts.Created != "" { + path += fmt.Sprintf("&created=%s", url.QueryEscape(opts.Created)) + } } var result *RunsPayload @@ -443,7 +452,7 @@ func GetJob(client *api.Client, repo ghrepo.Interface, jobID string) (*Job, erro } // SelectRun prompts the user to select a run from a list of runs by using the recommended prompter interface -func SelectRun(p prompter.Prompter, cs *iostreams.ColorScheme, runs []Run) (string, error) { +func SelectRun(p Prompter, cs *iostreams.ColorScheme, runs []Run) (string, error) { now := time.Now() candidates := []string{} @@ -464,36 +473,6 @@ func SelectRun(p prompter.Prompter, cs *iostreams.ColorScheme, runs []Run) (stri return fmt.Sprintf("%d", runs[selected].ID), nil } -// TODO: this should be deprecated in favor of SelectRun -func PromptForRun(cs *iostreams.ColorScheme, runs []Run) (string, error) { - var selected int - now := time.Now() - - candidates := []string{} - - for _, run := range runs { - symbol, _ := Symbol(cs, run.Status, run.Conclusion) - candidates = append(candidates, - // TODO truncate commit message, long ones look terrible - fmt.Sprintf("%s %s, %s (%s) %s", symbol, run.Title(), run.WorkflowName(), run.HeadBranch, preciseAgo(now, run.StartedTime()))) - } - - // TODO consider custom filter so it's fuzzier. right now matches start anywhere in string but - // become contiguous - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - err := prompt.SurveyAskOne(&survey.Select{ - Message: "Select a workflow run", - Options: candidates, - PageSize: 10, - }, &selected) - - if err != nil { - return "", err - } - - return fmt.Sprintf("%d", runs[selected].ID), nil -} - func GetRun(client *api.Client, repo ghrepo.Interface, runID string, attempt uint64) (*Run, error) { var result Run diff --git a/pkg/cmd/run/view/fixtures/run_log.zip b/pkg/cmd/run/view/fixtures/run_log.zip index 270310e37..5b0bb1e30 100644 Binary files a/pkg/cmd/run/view/fixtures/run_log.zip and b/pkg/cmd/run/view/fixtures/run_log.zip differ diff --git a/pkg/cmd/run/view/view.go b/pkg/cmd/run/view/view.go index 34c5fcbc7..6b96c90f8 100644 --- a/pkg/cmd/run/view/view.go +++ b/pkg/cmd/run/view/view.go @@ -12,9 +12,9 @@ import ( "regexp" "sort" "strconv" + "strings" "time" - "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/browser" @@ -24,7 +24,6 @@ import ( "github.com/cli/cli/v2/pkg/cmd/run/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/spf13/cobra" ) @@ -65,6 +64,7 @@ type ViewOptions struct { IO *iostreams.IOStreams BaseRepo func() (ghrepo.Interface, error) Browser browser.Browser + Prompter shared.Prompter RunLogCache runLogCache RunID string @@ -86,6 +86,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman opts := &ViewOptions{ IO: f.IOStreams, HttpClient: f.HttpClient, + Prompter: f.Prompter, Now: time.Now, Browser: f.Browser, RunLogCache: rlc{}, @@ -205,7 +206,7 @@ func runView(opts *ViewOptions) error { if err != nil { return fmt.Errorf("failed to get runs: %w", err) } - runID, err = shared.PromptForRun(cs, runs.WorkflowRuns) + runID, err = shared.SelectRun(opts.Prompter, cs, runs.WorkflowRuns) if err != nil { return err } @@ -228,7 +229,7 @@ func runView(opts *ViewOptions) error { } if opts.Prompt && len(jobs) > 1 { - selectedJob, err = promptForJob(cs, jobs) + selectedJob, err = promptForJob(opts.Prompter, cs, jobs) if err != nil { return err } @@ -284,7 +285,7 @@ func runView(opts *ViewOptions) error { } defer runLogZip.Close() - attachRunLog(runLogZip, jobs) + attachRunLog(&runLogZip.Reader, jobs) return displayRunLog(opts.IO.Out, jobs, opts.LogFailed) } @@ -459,20 +460,14 @@ func getRunLog(cache runLogCache, httpClient *http.Client, repo ghrepo.Interface return cache.Open(filepath) } -func promptForJob(cs *iostreams.ColorScheme, jobs []shared.Job) (*shared.Job, error) { +func promptForJob(prompter shared.Prompter, cs *iostreams.ColorScheme, jobs []shared.Job) (*shared.Job, error) { candidates := []string{"View all jobs in this run"} for _, job := range jobs { symbol, _ := shared.Symbol(cs, job.Status, job.Conclusion) candidates = append(candidates, fmt.Sprintf("%s %s", symbol, job.Name)) } - var selected int - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - err := prompt.SurveyAskOne(&survey.Select{ - Message: "View a specific job in this run?", - Options: candidates, - PageSize: 12, - }, &selected) + selected, err := prompter.Select("View a specific job in this run?", "", candidates) if err != nil { return nil, err } @@ -486,7 +481,19 @@ func promptForJob(cs *iostreams.ColorScheme, jobs []shared.Job) (*shared.Job, er } func logFilenameRegexp(job shared.Job, step shared.Step) *regexp.Regexp { - re := fmt.Sprintf(`%s\/%d_.*\.txt`, regexp.QuoteMeta(job.Name), step.Number) + // As described in https://github.com/cli/cli/issues/5011#issuecomment-1570713070, there are a number of steps + // the server can take when producing the downloaded zip file that can result in a mismatch between the job name + // and the filename in the zip including: + // * Removing characters in the job name that aren't allowed in file paths + // * Truncating names that are too long for zip files + // * Adding collision deduplicating numbers for jobs with the same name + // + // We are hesitant to duplicate all the server logic due to the fragility but while we explore our options, it + // is sensible to fix the issue that is unavoidable for users, that when a job uses a composite action, the server + // constructs a job name by constructing a job name of ` / `. This means that logs will + // never be found for jobs that use composite actions. + sanitizedJobName := strings.ReplaceAll(job.Name, "/", "") + re := fmt.Sprintf(`%s\/%d_.*\.txt`, regexp.QuoteMeta(sanitizedJobName), step.Number) return regexp.MustCompile(re) } @@ -503,10 +510,10 @@ func logFilenameRegexp(job shared.Job, step shared.Step) *regexp.Regexp { // ├── 1_stepname.txt // └── 2_somestepname.txt // -// It iterates through the list of jobs and trys to find the matching +// It iterates through the list of jobs and tries to find the matching // log in the zip file. If the matching log is found it is attached // to the job. -func attachRunLog(rlz *zip.ReadCloser, jobs []shared.Job) { +func attachRunLog(rlz *zip.Reader, jobs []shared.Job) { for i, job := range jobs { for j, step := range job.Steps { re := logFilenameRegexp(job, step) diff --git a/pkg/cmd/run/view/view_test.go b/pkg/cmd/run/view/view_test.go index 99a9ef30e..3d4fc4cf0 100644 --- a/pkg/cmd/run/view/view_test.go +++ b/pkg/cmd/run/view/view_test.go @@ -12,14 +12,15 @@ import ( "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/internal/browser" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/run/shared" workflowShared "github.com/cli/cli/v2/pkg/cmd/workflow/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNewCmdView(t *testing.T) { @@ -169,15 +170,15 @@ func TestNewCmdView(t *testing.T) { func TestViewRun(t *testing.T) { tests := []struct { - name string - httpStubs func(*httpmock.Registry) - askStubs func(*prompt.AskStubber) - opts *ViewOptions - tty bool - wantErr bool - wantOut string - browsedURL string - errMsg string + name string + httpStubs func(*httpmock.Registry) + promptStubs func(*prompter.MockPrompter) + opts *ViewOptions + tty bool + wantErr bool + wantOut string + browsedURL string + errMsg string }{ { name: "associate with PR", @@ -532,9 +533,12 @@ func TestViewRun(t *testing.T) { httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/123"), httpmock.JSONResponse(shared.TestWorkflow)) }, - askStubs: func(as *prompt.AskStubber) { - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(2) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"X cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "✓ cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "✓ cool commit, CI (trunk) Feb 23, 2021") + }) }, opts: &ViewOptions{ Prompt: true, @@ -579,11 +583,17 @@ func TestViewRun(t *testing.T) { }, })) }, - askStubs: func(as *prompt.AskStubber) { - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(2) - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(1) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"X cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "✓ cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "✓ cool commit, CI (trunk) Feb 23, 2021") + }) + pm.RegisterSelect("View a specific job in this run?", + []string{"View all jobs in this run", "✓ cool job", "X sad job"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "✓ cool job") + }) }, wantOut: coolJobRunLogOutput, }, @@ -626,11 +636,17 @@ func TestViewRun(t *testing.T) { }, })) }, - askStubs: func(as *prompt.AskStubber) { - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(2) - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(1) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"X cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "✓ cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "✓ cool commit, CI (trunk) Feb 23, 2021") + }) + pm.RegisterSelect("View a specific job in this run?", + []string{"View all jobs in this run", "✓ cool job", "X sad job"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "✓ cool job") + }) }, wantOut: coolJobRunLogOutput, }, @@ -717,11 +733,17 @@ func TestViewRun(t *testing.T) { }, })) }, - askStubs: func(as *prompt.AskStubber) { - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(2) - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(0) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"X cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "✓ cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "✓ cool commit, CI (trunk) Feb 23, 2021") + }) + pm.RegisterSelect("View a specific job in this run?", + []string{"View all jobs in this run", "✓ cool job", "X sad job"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "View all jobs in this run") + }) }, wantOut: expectedRunLogOutput, }, @@ -791,11 +813,17 @@ func TestViewRun(t *testing.T) { }, })) }, - askStubs: func(as *prompt.AskStubber) { - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(4) - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(2) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"X cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "✓ cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return 4, nil + }) + pm.RegisterSelect("View a specific job in this run?", + []string{"View all jobs in this run", "✓ cool job", "X sad job"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "X sad job") + }) }, wantOut: quuxTheBarfLogOutput, }, @@ -838,11 +866,17 @@ func TestViewRun(t *testing.T) { }, })) }, - askStubs: func(as *prompt.AskStubber) { - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(4) - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(2) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"X cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "✓ cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return 4, nil + }) + pm.RegisterSelect("View a specific job in this run?", + []string{"View all jobs in this run", "✓ cool job", "X sad job"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "X sad job") + }) }, wantOut: quuxTheBarfLogOutput, }, @@ -906,11 +940,17 @@ func TestViewRun(t *testing.T) { httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/123"), httpmock.JSONResponse(shared.TestWorkflow)) }, - askStubs: func(as *prompt.AskStubber) { - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(4) - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(0) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"X cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "✓ cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return 4, nil + }) + pm.RegisterSelect("View a specific job in this run?", + []string{"View all jobs in this run", "✓ cool job", "X sad job"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "View all jobs in this run") + }) }, wantOut: quuxTheBarfLogOutput, }, @@ -1054,11 +1094,17 @@ func TestViewRun(t *testing.T) { httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/123"), httpmock.JSONResponse(shared.TestWorkflow)) }, - askStubs: func(as *prompt.AskStubber) { - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(2) - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(0) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"X cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "✓ cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "✓ cool commit, CI (trunk) Feb 23, 2021") + }) + pm.RegisterSelect("View a specific job in this run?", + []string{"View all jobs in this run", "✓ cool job", "X sad job"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "View all jobs in this run") + }) }, wantOut: "\n✓ trunk CI · 3\nTriggered via push about 59 minutes ago\n\nJOBS\n✓ cool job in 4m34s (ID 10)\nX sad job in 4m34s (ID 20)\n ✓ barf the quux\n X quux the barf\n\nANNOTATIONS\nX the job is sad\nsad job: blaze.py#420\n\n\nFor more information about a job, try: gh run view --job=\nView this run on GitHub: https://github.com/runs/3\n", }, @@ -1099,11 +1145,17 @@ func TestViewRun(t *testing.T) { httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/123"), httpmock.JSONResponse(shared.TestWorkflow)) }, - askStubs: func(as *prompt.AskStubber) { - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(2) - //nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt - as.StubOne(1) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"X cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "✓ cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "- cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "* cool commit, CI (trunk) Feb 23, 2021", "X cool commit, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "✓ cool commit, CI (trunk) Feb 23, 2021") + }) + pm.RegisterSelect("View a specific job in this run?", + []string{"View all jobs in this run", "✓ cool job", "X sad job"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "✓ cool job") + }) }, wantOut: "\n✓ trunk CI · 3\nTriggered via push about 59 minutes ago\n\n✓ cool job in 4m34s (ID 10)\n ✓ fob the barz\n ✓ barz the fob\n\nTo see the full job log, try: gh run view --log --job=10\nView this run on GitHub: https://github.com/runs/3\n", }, @@ -1211,11 +1263,10 @@ func TestViewRun(t *testing.T) { return ghrepo.FromFullName("OWNER/REPO") } - //nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber - as, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(as) + pm := prompter.NewMockPrompter(t) + tt.opts.Prompter = pm + if tt.promptStubs != nil { + tt.promptStubs(pm) } browser := &browser.Stub{} @@ -1331,18 +1382,36 @@ func Test_attachRunLog(t *testing.T) { }, wantMatch: false, }, + { + name: "job name with forward slash matches dir with slash removed", + job: shared.Job{ + Name: "cool job / with slash", + Steps: []shared.Step{{ + Name: "fob the barz", + Number: 1, + }}, + }, + wantMatch: true, + // not the double space in the dir name, as the slash has been removed + wantFilename: "cool job with slash/1_fob the barz.txt", + }, } - rlz, _ := zip.OpenReader("./fixtures/run_log.zip") + + rlz, err := zip.OpenReader("./fixtures/run_log.zip") + require.NoError(t, err) defer rlz.Close() + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - attachRunLog(rlz, []shared.Job{tt.job}) + + attachRunLog(&rlz.Reader, []shared.Job{tt.job}) + for _, step := range tt.job.Steps { log := step.Log logPresent := log != nil - assert.Equal(t, tt.wantMatch, logPresent) + require.Equal(t, tt.wantMatch, logPresent) if logPresent { - assert.Equal(t, tt.wantFilename, log.Name) + require.Equal(t, tt.wantFilename, log.Name) } } }) diff --git a/pkg/cmd/run/watch/watch.go b/pkg/cmd/run/watch/watch.go index 05b207693..090737557 100644 --- a/pkg/cmd/run/watch/watch.go +++ b/pkg/cmd/run/watch/watch.go @@ -23,6 +23,7 @@ type WatchOptions struct { IO *iostreams.IOStreams HttpClient func() (*http.Client, error) BaseRepo func() (ghrepo.Interface, error) + Prompter shared.Prompter RunID string Interval int @@ -37,6 +38,7 @@ func NewCmdWatch(f *cmdutil.Factory, runF func(*WatchOptions) error) *cobra.Comm opts := &WatchOptions{ IO: f.IOStreams, HttpClient: f.HttpClient, + Prompter: f.Prompter, Now: time.Now, } @@ -102,11 +104,10 @@ func watchRun(opts *WatchOptions) error { if len(runs) == 0 { return fmt.Errorf("found no in progress runs to watch") } - runID, err = shared.PromptForRun(cs, runs) - if err != nil { + if runID, err = shared.SelectRun(opts.Prompter, cs, runs); err != nil { return err } - // TODO silly stopgap until dust settles and PromptForRun can just return a run + // TODO silly stopgap until dust settles and SelectRun can just return a run for _, r := range runs { if fmt.Sprintf("%d", r.ID) == runID { run = &r diff --git a/pkg/cmd/run/watch/watch_test.go b/pkg/cmd/run/watch/watch_test.go index 89c75e72c..8ff77516b 100644 --- a/pkg/cmd/run/watch/watch_test.go +++ b/pkg/cmd/run/watch/watch_test.go @@ -8,12 +8,12 @@ import ( "time" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/run/shared" workflowShared "github.com/cli/cli/v2/pkg/cmd/workflow/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -193,14 +193,14 @@ func TestWatchRun(t *testing.T) { } tests := []struct { - name string - httpStubs func(*httpmock.Registry) - askStubs func(*prompt.AskStubber) - opts *WatchOptions - tty bool - wantErr bool - errMsg string - wantOut string + name string + httpStubs func(*httpmock.Registry) + promptStubs func(*prompter.MockPrompter) + opts *WatchOptions + tty bool + wantErr bool + errMsg string + wantOut string }{ { name: "run ID provided run already completed", @@ -269,10 +269,12 @@ func TestWatchRun(t *testing.T) { Prompt: true, }, httpStubs: successfulRunStubs, - askStubs: func(as *prompt.AskStubber) { - as.StubPrompt("Select a workflow run"). - AssertOptions([]string{"* commit1, CI (trunk) Feb 23, 2021", "* commit2, CI (trunk) Feb 23, 2021"}). - AnswerWith("* commit2, CI (trunk) Feb 23, 2021") + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"* commit1, CI (trunk) Feb 23, 2021", "* commit2, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "* commit2, CI (trunk) Feb 23, 2021") + }) }, wantOut: "\x1b[?1049h\x1b[0;0H\x1b[JRefreshing run status every 0 seconds. Press Ctrl+C to quit.\n\n* trunk CI · 2\nTriggered via push about 59 minutes ago\n\nJOBS\n✓ cool job in 4m34s (ID 10)\n ✓ fob the barz\n ✓ barz the fob\n\x1b[?1049l✓ trunk CI · 2\nTriggered via push about 59 minutes ago\n\nJOBS\n✓ cool job in 4m34s (ID 10)\n ✓ fob the barz\n ✓ barz the fob\n\n✓ Run CI (2) completed with 'success'\n", }, @@ -285,10 +287,12 @@ func TestWatchRun(t *testing.T) { ExitStatus: true, }, httpStubs: failedRunStubs, - askStubs: func(as *prompt.AskStubber) { - as.StubPrompt("Select a workflow run"). - AssertOptions([]string{"* commit1, CI (trunk) Feb 23, 2021", "* commit2, CI (trunk) Feb 23, 2021"}). - AnswerWith("* commit2, CI (trunk) Feb 23, 2021") + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Select a workflow run", + []string{"* commit1, CI (trunk) Feb 23, 2021", "* commit2, CI (trunk) Feb 23, 2021"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "* commit2, CI (trunk) Feb 23, 2021") + }) }, wantOut: "\x1b[?1049h\x1b[0;0H\x1b[JRefreshing run status every 0 seconds. Press Ctrl+C to quit.\n\n* trunk CI · 2\nTriggered via push about 59 minutes ago\n\n\x1b[?1049lX trunk CI · 2\nTriggered via push about 59 minutes ago\n\nJOBS\nX sad job in 4m34s (ID 20)\n ✓ barf the quux\n X quux the barf\n\nANNOTATIONS\nX the job is sad\nsad job: blaze.py#420\n\n\nX Run CI (2) completed with 'failure'\n", wantErr: true, @@ -317,10 +321,10 @@ func TestWatchRun(t *testing.T) { } t.Run(tt.name, func(t *testing.T) { - //nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock - as := prompt.NewAskStubber(t) - if tt.askStubs != nil { - tt.askStubs(as) + pm := prompter.NewMockPrompter(t) + tt.opts.Prompter = pm + if tt.promptStubs != nil { + tt.promptStubs(pm) } err := watchRun(tt.opts) diff --git a/pkg/cmd/search/code/code.go b/pkg/cmd/search/code/code.go new file mode 100644 index 000000000..39f9d2f0c --- /dev/null +++ b/pkg/cmd/search/code/code.go @@ -0,0 +1,204 @@ +package code + +import ( + "fmt" + "strings" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/internal/text" + "github.com/cli/cli/v2/pkg/cmd/search/shared" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/cli/v2/pkg/search" + "github.com/spf13/cobra" +) + +type CodeOptions struct { + Browser browser.Browser + Exporter cmdutil.Exporter + IO *iostreams.IOStreams + Query search.Query + Searcher search.Searcher + WebMode bool +} + +func NewCmdCode(f *cmdutil.Factory, runF func(*CodeOptions) error) *cobra.Command { + opts := &CodeOptions{ + Browser: f.Browser, + IO: f.IOStreams, + Query: search.Query{Kind: search.KindCode}, + } + + cmd := &cobra.Command{ + Use: "code ", + Short: "Search within code", + Long: heredoc.Doc(` + Search within code in GitHub repositories. + + The search syntax is documented at: + + + Note that these search results are powered by what is now a legacy GitHub code search engine. + The results might not match what is seen on GitHub.com, and new features like regex search + are not yet available via the GitHub API. + `), + Example: heredoc.Doc(` + # search code matching "react" and "lifecycle" + $ gh search code react lifecycle + + # search code matching "error handling" + $ gh search code "error handling" + + # search code matching "deque" in Python files + $ gh search code deque --language=python + + # search code matching "cli" in repositories owned by microsoft organization + $ gh search code cli --owner=microsoft + + # search code matching "panic" in the GitHub CLI repository + $ gh search code panic --repo cli/cli + + # search code matching keyword "lint" in package.json files + $ gh search code lint --filename package.json + `), + RunE: func(c *cobra.Command, args []string) error { + if len(args) == 0 && c.Flags().NFlag() == 0 { + return cmdutil.FlagErrorf("specify search keywords or flags") + } + if opts.Query.Limit < 1 || opts.Query.Limit > shared.SearchMaxResults { + return cmdutil.FlagErrorf("`--limit` must be between 1 and 1000") + } + opts.Query.Keywords = args + if runF != nil { + return runF(opts) + } + var err error + opts.Searcher, err = shared.Searcher(f) + if err != nil { + return err + } + return codeRun(opts) + }, + } + + // Output flags + cmdutil.AddJSONFlags(cmd, &opts.Exporter, search.CodeFields) + cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the search query in the web browser") + + // Query parameter flags + cmd.Flags().IntVarP(&opts.Query.Limit, "limit", "L", 30, "Maximum number of code results to fetch") + + // Query qualifier flags + cmd.Flags().StringVar(&opts.Query.Qualifiers.Extension, "extension", "", "Filter on file extension") + cmd.Flags().StringVar(&opts.Query.Qualifiers.Filename, "filename", "", "Filter on filename") + cmdutil.StringSliceEnumFlag(cmd, &opts.Query.Qualifiers.In, "match", "", nil, []string{"file", "path"}, "Restrict search to file contents or file path") + cmd.Flags().StringVarP(&opts.Query.Qualifiers.Language, "language", "", "", "Filter results by language") + cmd.Flags().StringSliceVarP(&opts.Query.Qualifiers.Repo, "repo", "R", nil, "Filter on repository") + cmd.Flags().StringVar(&opts.Query.Qualifiers.Size, "size", "", "Filter on size range, in kilobytes") + cmd.Flags().StringSliceVar(&opts.Query.Qualifiers.User, "owner", nil, "Filter on owner") + + return cmd +} + +func codeRun(opts *CodeOptions) error { + io := opts.IO + if opts.WebMode { + // FIXME: convert legacy `filename` and `extension` ES qualifiers to Blackbird's `path` qualifier + // when opening web search, otherwise the Blackbird search UI will complain. + url := opts.Searcher.URL(opts.Query) + if io.IsStdoutTTY() { + fmt.Fprintf(io.ErrOut, "Opening %s in your browser.\n", text.DisplayURL(url)) + } + return opts.Browser.Browse(url) + } + io.StartProgressIndicator() + results, err := opts.Searcher.Code(opts.Query) + io.StopProgressIndicator() + if err != nil { + return err + } + if len(results.Items) == 0 && opts.Exporter == nil { + return cmdutil.NewNoResultsError("no code results matched your search") + } + if err := io.StartPager(); err == nil { + defer io.StopPager() + } else { + fmt.Fprintf(io.ErrOut, "failed to start pager: %v\n", err) + } + if opts.Exporter != nil { + return opts.Exporter.Write(io, results.Items) + } + + return displayResults(io, results) +} + +func displayResults(io *iostreams.IOStreams, results search.CodeResult) error { + cs := io.ColorScheme() + if io.IsStdoutTTY() { + fmt.Fprintf(io.Out, "\nShowing %d of %d results\n\n", len(results.Items), results.Total) + for i, code := range results.Items { + if i > 0 { + fmt.Fprint(io.Out, "\n") + } + fmt.Fprintf(io.Out, "%s %s\n", cs.Blue(code.Repository.FullName), cs.GreenBold(code.Path)) + for _, match := range code.TextMatches { + lines := formatMatch(match.Fragment, match.Matches, io.ColorEnabled()) + for _, line := range lines { + fmt.Fprintf(io.Out, "\t%s\n", strings.TrimSpace(line)) + } + } + } + return nil + } + for _, code := range results.Items { + for _, match := range code.TextMatches { + lines := formatMatch(match.Fragment, match.Matches, io.ColorEnabled()) + for _, line := range lines { + fmt.Fprintf(io.Out, "%s:%s: %s\n", cs.Blue(code.Repository.FullName), cs.GreenBold(code.Path), strings.TrimSpace(line)) + } + } + } + return nil +} + +func formatMatch(t string, matches []search.Match, colorize bool) []string { + startIndices := map[int]struct{}{} + endIndices := map[int]struct{}{} + for _, m := range matches { + if len(m.Indices) < 2 { + continue + } + startIndices[m.Indices[0]] = struct{}{} + endIndices[m.Indices[1]] = struct{}{} + } + + var lines []string + var b strings.Builder + var found bool + for i, c := range t { + if c == '\n' { + if found { + lines = append(lines, b.String()) + } + found = false + b.Reset() + continue + } + if _, ok := startIndices[i]; ok { + if colorize { + b.WriteString("\x1b[30;43m") // black text on yellow background + } + found = true + } else if _, ok := endIndices[i]; ok { + if colorize { + b.WriteString("\x1b[m") // color reset + } + } + b.WriteRune(c) + } + if found { + lines = append(lines, b.String()) + } + return lines +} diff --git a/pkg/cmd/search/code/code_test.go b/pkg/cmd/search/code/code_test.go new file mode 100644 index 000000000..253bd2889 --- /dev/null +++ b/pkg/cmd/search/code/code_test.go @@ -0,0 +1,342 @@ +package code + +import ( + "bytes" + "fmt" + "testing" + + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/cli/v2/pkg/search" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" +) + +func TestNewCmdCode(t *testing.T) { + tests := []struct { + name string + input string + output CodeOptions + wantErr bool + errMsg string + }{ + { + name: "no arguments", + input: "", + wantErr: true, + errMsg: "specify search keywords or flags", + }, + { + name: "keyword arguments", + input: "react lifecycle", + output: CodeOptions{ + Query: search.Query{ + Keywords: []string{"react", "lifecycle"}, + Kind: "code", + Limit: 30, + }, + }, + }, + { + name: "web flag", + input: "--web", + output: CodeOptions{ + Query: search.Query{ + Keywords: []string{}, + Kind: "code", + Limit: 30, + }, + WebMode: true, + }, + }, + { + name: "limit flag", + input: "--limit 10", + output: CodeOptions{ + Query: search.Query{ + Keywords: []string{}, + Kind: "code", + Limit: 10, + }, + }, + }, + { + name: "invalid limit flag", + input: "--limit 1001", + wantErr: true, + errMsg: "`--limit` must be between 1 and 1000", + }, + { + name: "qualifier flags", + input: ` + --extension=extension + --filename=filename + --match=path,file + --language=language + --repo=owner/repo + --size=5 + --owner=owner + `, + output: CodeOptions{ + Query: search.Query{ + Keywords: []string{}, + Kind: "code", + Limit: 30, + Qualifiers: search.Qualifiers{ + Extension: "extension", + Filename: "filename", + In: []string{"path", "file"}, + Language: "language", + Repo: []string{"owner/repo"}, + Size: "5", + User: []string{"owner"}, + }, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ios, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: ios, + } + argv, err := shlex.Split(tt.input) + assert.NoError(t, err) + var gotOpts *CodeOptions + cmd := NewCmdCode(f, func(opts *CodeOptions) error { + gotOpts = opts + return nil + }) + cmd.SetArgs(argv) + cmd.SetIn(&bytes.Buffer{}) + cmd.SetOut(&bytes.Buffer{}) + cmd.SetErr(&bytes.Buffer{}) + + _, err = cmd.ExecuteC() + if tt.wantErr { + assert.EqualError(t, err, tt.errMsg) + return + } + + assert.NoError(t, err) + assert.Equal(t, tt.output.Query, gotOpts.Query) + assert.Equal(t, tt.output.WebMode, gotOpts.WebMode) + }) + } +} + +func TestCodeRun(t *testing.T) { + var query = search.Query{ + Keywords: []string{"map"}, + Kind: "code", + Limit: 30, + Qualifiers: search.Qualifiers{ + Language: "go", + Repo: []string{"cli/cli"}, + }, + } + tests := []struct { + errMsg string + name string + opts *CodeOptions + tty bool + wantErr bool + wantStderr string + wantStdout string + }{ + { + name: "displays results tty", + opts: &CodeOptions{ + Query: query, + Searcher: &search.SearcherMock{ + CodeFunc: func(query search.Query) (search.CodeResult, error) { + return search.CodeResult{ + IncompleteResults: false, + Items: []search.Code{ + { + Name: "context.go", + Path: "context/context.go", + Repository: search.Repository{ + FullName: "cli/cli", + }, + TextMatches: []search.TextMatch{ + { + Fragment: "\t}\n\n\tvar repos []*api.Repository\n\trepoMap := map[string]bool{}\n\n\tadd := func(r *api.Repository) {\n\t\tfn := ghrepo.FullName(r)", + Matches: []search.Match{ + { + Text: "Map", + Indices: []int{38, 41}, + }, + { + Text: "map", + Indices: []int{45, 48}, + }, + }, + }, + }, + }, + { + Name: "pr.go", + Path: "pkg/cmd/pr/pr.go", + Repository: search.Repository{ + FullName: "cli/cli", + }, + TextMatches: []search.TextMatch{ + { + + Fragment: "\t\t\t$ gh pr create --fill\n\t\t\t$ gh pr view --web\n\t\t`),\n\t\tAnnotations: map[string]string{\n\t\t\t\"help:arguments\": heredoc.Doc(`\n\t\t\t\tA pull request can be supplied as argument in any of the following formats:\n\t\t\t\t- by number, e.g. \"123\";", + Matches: []search.Match{ + { + Text: "map", + Indices: []int{68, 71}, + }, + }, + }, + }, + }, + }, + Total: 2, + }, nil + }, + }, + }, + tty: true, + wantStdout: "\nShowing 2 of 2 results\n\ncli/cli context/context.go\n\trepoMap := map[string]bool{}\n\ncli/cli pkg/cmd/pr/pr.go\n\tAnnotations: map[string]string{\n", + }, + { + name: "displays results notty", + opts: &CodeOptions{ + Query: query, + Searcher: &search.SearcherMock{ + CodeFunc: func(query search.Query) (search.CodeResult, error) { + return search.CodeResult{ + IncompleteResults: false, + Items: []search.Code{ + { + Name: "context.go", + Path: "context/context.go", + Repository: search.Repository{ + FullName: "cli/cli", + }, + TextMatches: []search.TextMatch{ + { + Fragment: "\t}\n\n\tvar repos []*api.Repository\n\trepoMap := map[string]bool{}\n\n\tadd := func(r *api.Repository) {\n\t\tfn := ghrepo.FullName(r)", + Matches: []search.Match{ + { + Text: "Map", + Indices: []int{38, 41}, + }, + { + Text: "map", + Indices: []int{45, 48}, + }, + }, + }, + }, + }, + { + Name: "pr.go", + Path: "pkg/cmd/pr/pr.go", + Repository: search.Repository{ + FullName: "cli/cli", + }, + TextMatches: []search.TextMatch{ + { + + Fragment: "\t\t\t$ gh pr create --fill\n\t\t\t$ gh pr view --web\n\t\t`),\n\t\tAnnotations: map[string]string{\n\t\t\t\"help:arguments\": heredoc.Doc(`\n\t\t\t\tA pull request can be supplied as argument in any of the following formats:\n\t\t\t\t- by number, e.g. \"123\";", + Matches: []search.Match{ + { + Text: "map", + Indices: []int{68, 71}, + }, + }, + }, + }, + }, + }, + Total: 2, + }, nil + }, + }, + }, + tty: false, + wantStdout: "cli/cli:context/context.go: repoMap := map[string]bool{}\ncli/cli:pkg/cmd/pr/pr.go: Annotations: map[string]string{\n", + }, + { + name: "displays no results", + opts: &CodeOptions{ + Query: query, + Searcher: &search.SearcherMock{ + CodeFunc: func(query search.Query) (search.CodeResult, error) { + return search.CodeResult{}, nil + }, + }, + }, + wantErr: true, + errMsg: "no code results matched your search", + }, + { + name: "displays search error", + opts: &CodeOptions{ + Query: query, + Searcher: &search.SearcherMock{ + CodeFunc: func(query search.Query) (search.CodeResult, error) { + return search.CodeResult{}, fmt.Errorf("error with query") + }, + }, + }, + wantErr: true, + errMsg: "error with query", + }, + { + name: "opens browser for web mode tty", + opts: &CodeOptions{ + Browser: &browser.Stub{}, + Query: query, + Searcher: &search.SearcherMock{ + URLFunc: func(query search.Query) string { + return "https://github.com/search?type=code&q=map+repo%3Acli%2Fcli" + }, + }, + WebMode: true, + }, + tty: true, + wantStderr: "Opening github.com/search in your browser.\n", + }, + { + name: "opens browser for web mode notty", + opts: &CodeOptions{ + Browser: &browser.Stub{}, + Query: query, + Searcher: &search.SearcherMock{ + URLFunc: func(query search.Query) string { + return "https://github.com/search?type=code&q=map+repo%3Acli%2Fcli" + }, + }, + WebMode: true, + }, + }, + } + + for _, tt := range tests { + ios, _, stdout, stderr := iostreams.Test() + ios.SetStdinTTY(tt.tty) + ios.SetStdoutTTY(tt.tty) + ios.SetStderrTTY(tt.tty) + tt.opts.IO = ios + t.Run(tt.name, func(t *testing.T) { + err := codeRun(tt.opts) + if tt.wantErr { + assert.EqualError(t, err, tt.errMsg) + return + } else if err != nil { + t.Fatalf("codeRun unexpected error: %v", err) + } + assert.Equal(t, tt.wantStdout, stdout.String()) + assert.Equal(t, tt.wantStderr, stderr.String()) + }) + } +} diff --git a/pkg/cmd/search/search.go b/pkg/cmd/search/search.go index 7b9a4a653..e8714065b 100644 --- a/pkg/cmd/search/search.go +++ b/pkg/cmd/search/search.go @@ -4,6 +4,7 @@ import ( "github.com/cli/cli/v2/pkg/cmdutil" "github.com/spf13/cobra" + searchCodeCmd "github.com/cli/cli/v2/pkg/cmd/search/code" searchCommitsCmd "github.com/cli/cli/v2/pkg/cmd/search/commits" searchIssuesCmd "github.com/cli/cli/v2/pkg/cmd/search/issues" searchPrsCmd "github.com/cli/cli/v2/pkg/cmd/search/prs" @@ -17,6 +18,7 @@ func NewCmdSearch(f *cmdutil.Factory) *cobra.Command { Long: "Search across all of GitHub.", } + cmd.AddCommand(searchCodeCmd.NewCmdCode(f, nil)) cmd.AddCommand(searchCommitsCmd.NewCmdCommits(f, nil)) cmd.AddCommand(searchIssuesCmd.NewCmdIssues(f, nil)) cmd.AddCommand(searchPrsCmd.NewCmdPrs(f, nil)) diff --git a/pkg/cmd/status/status.go b/pkg/cmd/status/status.go index 438175d72..7e39d63d0 100644 --- a/pkg/cmd/status/status.go +++ b/pkg/cmd/status/status.go @@ -20,7 +20,7 @@ import ( "github.com/cli/cli/v2/pkg/iostreams" "github.com/cli/cli/v2/pkg/set" "github.com/cli/cli/v2/utils" - ghAPI "github.com/cli/go-gh/pkg/api" + ghAPI "github.com/cli/go-gh/v2/pkg/api" "github.com/spf13/cobra" "golang.org/x/sync/errgroup" ) @@ -429,7 +429,7 @@ func (s *StatusGetter) LoadSearchResults() error { if err != nil { var gqlErrResponse api.GraphQLError if errors.As(err, &gqlErrResponse) { - gqlErrors := make([]ghAPI.GQLErrorItem, 0, len(gqlErrResponse.Errors)) + gqlErrors := make([]ghAPI.GraphQLErrorItem, 0, len(gqlErrResponse.Errors)) // Exclude any FORBIDDEN errors and show status for what we can. for _, gqlErr := range gqlErrResponse.Errors { if gqlErr.Type == "FORBIDDEN" { @@ -442,7 +442,7 @@ func (s *StatusGetter) LoadSearchResults() error { err = nil } else { err = api.GraphQLError{ - GQLError: ghAPI.GQLError{ + GraphQLError: &ghAPI.GraphQLError{ Errors: gqlErrors, }, } @@ -467,7 +467,7 @@ func (s *StatusGetter) LoadSearchResults() error { case "PullRequest": prs = append(prs, *node) default: - return fmt.Errorf("unkown Assignments type: %q", node.Type) + return fmt.Errorf("unknown Assignments type: %q", node.Type) } } diff --git a/pkg/cmd/workflow/list/list.go b/pkg/cmd/workflow/list/list.go index a080c4cf9..f590f1602 100644 --- a/pkg/cmd/workflow/list/list.go +++ b/pkg/cmd/workflow/list/list.go @@ -58,7 +58,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman } cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, "Maximum number of workflows to fetch") - cmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all workflows, including disabled workflows") + cmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Also show disabled workflows") return cmd } diff --git a/pkg/cmdutil/json_flags.go b/pkg/cmdutil/json_flags.go index 3d44c0ada..0aef38db4 100644 --- a/pkg/cmdutil/json_flags.go +++ b/pkg/cmdutil/json_flags.go @@ -13,8 +13,8 @@ import ( "github.com/cli/cli/v2/pkg/iostreams" "github.com/cli/cli/v2/pkg/jsoncolor" "github.com/cli/cli/v2/pkg/set" - "github.com/cli/go-gh/pkg/jq" - "github.com/cli/go-gh/pkg/template" + "github.com/cli/go-gh/v2/pkg/jq" + "github.com/cli/go-gh/v2/pkg/template" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -138,7 +138,13 @@ func (e *exportFormat) Write(ios *iostreams.IOStreams, data interface{}) error { w := ios.Out if e.filter != "" { - return jq.Evaluate(&buf, w, e.filter) + indent := "" + if ios.IsStdoutTTY() { + indent = " " + } + if err := jq.EvaluateFormatted(&buf, w, e.filter, indent, ios.ColorEnabled()); err != nil { + return err + } } else if e.template != "" { t := template.New(w, ios.TerminalWidth(), ios.ColorEnabled()) if err := t.Parse(e.template); err != nil { diff --git a/pkg/cmdutil/json_flags_test.go b/pkg/cmdutil/json_flags_test.go index dd65647ea..975530df4 100644 --- a/pkg/cmdutil/json_flags_test.go +++ b/pkg/cmdutil/json_flags_test.go @@ -126,6 +126,7 @@ func Test_exportFormat_Write(t *testing.T) { args args wantW string wantErr bool + istty bool }{ { name: "regular JSON output", @@ -135,6 +136,7 @@ func Test_exportFormat_Write(t *testing.T) { }, wantW: "{\"name\":\"hubot\"}\n", wantErr: false, + istty: false, }, { name: "call ExportData", @@ -144,6 +146,7 @@ func Test_exportFormat_Write(t *testing.T) { }, wantW: "{\"field1\":\"item1:field1\",\"field2\":\"item1:field2\"}\n", wantErr: false, + istty: false, }, { name: "recursively call ExportData", @@ -156,6 +159,7 @@ func Test_exportFormat_Write(t *testing.T) { }, wantW: "{\"s1\":[{\"f1\":\"i1:f1\",\"f2\":\"i1:f2\"},{\"f1\":\"i2:f1\",\"f2\":\"i2:f2\"}],\"s2\":[{\"f1\":\"i3:f1\",\"f2\":\"i3:f2\"}]}\n", wantErr: false, + istty: false, }, { name: "with jq filter", @@ -165,6 +169,17 @@ func Test_exportFormat_Write(t *testing.T) { }, wantW: "hubot\n", wantErr: false, + istty: false, + }, + { + name: "with jq filter pretty printing", + exporter: exportFormat{filter: "."}, + args: args{ + data: map[string]string{"name": "hubot"}, + }, + wantW: "{\n \"name\": \"hubot\"\n}\n", + wantErr: false, + istty: true, }, { name: "with Go template", @@ -174,11 +189,13 @@ func Test_exportFormat_Write(t *testing.T) { }, wantW: "hubot", wantErr: false, + istty: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { io, _, w, _ := iostreams.Test() + io.SetStdoutTTY(tt.istty) if err := tt.exporter.Write(io, tt.args.data); (err != nil) != tt.wantErr { t.Errorf("exportFormat.Write() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/pkg/extensions/extension.go b/pkg/extensions/extension.go index 95af0ccea..2583472c0 100644 --- a/pkg/extensions/extension.go +++ b/pkg/extensions/extension.go @@ -20,6 +20,7 @@ type Extension interface { Path() string // Path to executable URL() string CurrentVersion() string + LatestVersion() string IsPinned() bool UpdateAvailable() bool IsBinary() bool diff --git a/pkg/extensions/extension_mock.go b/pkg/extensions/extension_mock.go index a97de8f62..500603661 100644 --- a/pkg/extensions/extension_mock.go +++ b/pkg/extensions/extension_mock.go @@ -29,6 +29,9 @@ var _ Extension = &ExtensionMock{} // IsPinnedFunc: func() bool { // panic("mock out the IsPinned method") // }, +// LatestVersionFunc: func() string { +// panic("mock out the LatestVersion method") +// }, // NameFunc: func() string { // panic("mock out the Name method") // }, @@ -60,6 +63,9 @@ type ExtensionMock struct { // IsPinnedFunc mocks the IsPinned method. IsPinnedFunc func() bool + // LatestVersionFunc mocks the LatestVersion method. + LatestVersionFunc func() string + // NameFunc mocks the Name method. NameFunc func() string @@ -86,6 +92,9 @@ type ExtensionMock struct { // IsPinned holds details about calls to the IsPinned method. IsPinned []struct { } + // LatestVersion holds details about calls to the LatestVersion method. + LatestVersion []struct { + } // Name holds details about calls to the Name method. Name []struct { } @@ -103,6 +112,7 @@ type ExtensionMock struct { lockIsBinary sync.RWMutex lockIsLocal sync.RWMutex lockIsPinned sync.RWMutex + lockLatestVersion sync.RWMutex lockName sync.RWMutex lockPath sync.RWMutex lockURL sync.RWMutex @@ -217,6 +227,33 @@ func (mock *ExtensionMock) IsPinnedCalls() []struct { return calls } +// LatestVersion calls LatestVersionFunc. +func (mock *ExtensionMock) LatestVersion() string { + if mock.LatestVersionFunc == nil { + panic("ExtensionMock.LatestVersionFunc: method is nil but Extension.LatestVersion was just called") + } + callInfo := struct { + }{} + mock.lockLatestVersion.Lock() + mock.calls.LatestVersion = append(mock.calls.LatestVersion, callInfo) + mock.lockLatestVersion.Unlock() + return mock.LatestVersionFunc() +} + +// LatestVersionCalls gets all the calls that were made to LatestVersion. +// Check the length with: +// +// len(mockedExtension.LatestVersionCalls()) +func (mock *ExtensionMock) LatestVersionCalls() []struct { +} { + var calls []struct { + } + mock.lockLatestVersion.RLock() + calls = mock.calls.LatestVersion + mock.lockLatestVersion.RUnlock() + return calls +} + // Name calls NameFunc. func (mock *ExtensionMock) Name() string { if mock.NameFunc == nil { diff --git a/pkg/httpmock/stub.go b/pkg/httpmock/stub.go index 031db449e..9332ac9dd 100644 --- a/pkg/httpmock/stub.go +++ b/pkg/httpmock/stub.go @@ -57,6 +57,33 @@ func GraphQL(q string) Matcher { } } +func GraphQLMutationMatcher(q string, cb func(map[string]interface{}) bool) Matcher { + re := regexp.MustCompile(q) + + return func(req *http.Request) bool { + if !strings.EqualFold(req.Method, "POST") { + return false + } + if req.URL.Path != "/graphql" && req.URL.Path != "/api/graphql" { + return false + } + + var bodyData struct { + Query string + Variables struct { + Input map[string]interface{} + } + } + _ = decodeJSONBody(req, &bodyData) + + if re.MatchString(bodyData.Query) { + return cb(bodyData.Variables.Input) + } + + return false + } +} + func QueryMatcher(method string, path string, query url.Values) Matcher { return func(req *http.Request) bool { if !REST(method, path)(req) { @@ -116,7 +143,20 @@ func StatusStringResponse(status int, body string) Responder { func JSONResponse(body interface{}) Responder { return func(req *http.Request) (*http.Response, error) { b, _ := json.Marshal(body) - return httpResponse(200, req, bytes.NewBuffer(b)), nil + header := http.Header{ + "Content-Type": []string{"application/json"}, + } + return httpResponseWithHeader(200, req, bytes.NewBuffer(b), header), nil + } +} + +func StatusJSONResponse(status int, body interface{}) Responder { + return func(req *http.Request) (*http.Response, error) { + b, _ := json.Marshal(body) + header := http.Header{ + "Content-Type": []string{"application/json"}, + } + return httpResponseWithHeader(status, req, bytes.NewBuffer(b), header), nil } } @@ -188,10 +228,14 @@ func ScopesResponder(scopes string) func(*http.Request) (*http.Response, error) } func httpResponse(status int, req *http.Request, body io.Reader) *http.Response { + return httpResponseWithHeader(status, req, body, http.Header{}) +} + +func httpResponseWithHeader(status int, req *http.Request, body io.Reader, header http.Header) *http.Response { return &http.Response{ StatusCode: status, Request: req, Body: io.NopCloser(body), - Header: http.Header{}, + Header: header, } } diff --git a/pkg/iostreams/color.go b/pkg/iostreams/color.go index 1d35c05d9..6cb5a607a 100644 --- a/pkg/iostreams/color.go +++ b/pkg/iostreams/color.go @@ -9,15 +9,16 @@ import ( ) var ( - magenta = ansi.ColorFunc("magenta") - cyan = ansi.ColorFunc("cyan") - red = ansi.ColorFunc("red") - yellow = ansi.ColorFunc("yellow") - blue = ansi.ColorFunc("blue") - green = ansi.ColorFunc("green") - gray = ansi.ColorFunc("black+h") - bold = ansi.ColorFunc("default+b") - cyanBold = ansi.ColorFunc("cyan+b") + magenta = ansi.ColorFunc("magenta") + cyan = ansi.ColorFunc("cyan") + red = ansi.ColorFunc("red") + yellow = ansi.ColorFunc("yellow") + blue = ansi.ColorFunc("blue") + green = ansi.ColorFunc("green") + gray = ansi.ColorFunc("black+h") + bold = ansi.ColorFunc("default+b") + cyanBold = ansi.ColorFunc("cyan+b") + greenBold = ansi.ColorFunc("green+b") gray256 = func(t string) string { return fmt.Sprintf("\x1b[%d;5;%dm%s\x1b[m", 38, 242, t) @@ -82,6 +83,13 @@ func (c *ColorScheme) Greenf(t string, args ...interface{}) string { return c.Green(fmt.Sprintf(t, args...)) } +func (c *ColorScheme) GreenBold(t string) string { + if !c.enabled { + return t + } + return greenBold(t) +} + func (c *ColorScheme) Gray(t string) string { if !c.enabled { return t diff --git a/pkg/iostreams/iostreams.go b/pkg/iostreams/iostreams.go index 0d4558b3b..2b41288d2 100644 --- a/pkg/iostreams/iostreams.go +++ b/pkg/iostreams/iostreams.go @@ -13,7 +13,7 @@ import ( "time" "github.com/briandowns/spinner" - ghTerm "github.com/cli/go-gh/pkg/term" + ghTerm "github.com/cli/go-gh/v2/pkg/term" "github.com/cli/safeexec" "github.com/google/shlex" "github.com/mattn/go-colorable" diff --git a/pkg/liveshare/client_test.go b/pkg/liveshare/client_test.go index a39c48f7b..4ccc42729 100644 --- a/pkg/liveshare/client_test.go +++ b/pkg/liveshare/client_test.go @@ -24,7 +24,7 @@ func TestConnect(t *testing.T) { joinWorkspace := func(conn *jsonrpc2.Conn, req *jsonrpc2.Request) (interface{}, error) { var joinWorkspaceReq joinWorkspaceArgs if err := json.Unmarshal(*req.Params, &joinWorkspaceReq); err != nil { - return nil, fmt.Errorf("error unmarshaling req: %w", err) + return nil, fmt.Errorf("error unmarshalling req: %w", err) } if joinWorkspaceReq.ID != opts.SessionID { return nil, errors.New("connection session id does not match") diff --git a/pkg/liveshare/ports.go b/pkg/liveshare/ports.go index 6cef1584b..b39a4f630 100644 --- a/pkg/liveshare/ports.go +++ b/pkg/liveshare/ports.go @@ -52,7 +52,7 @@ func (s *Session) WaitForPortNotification(ctx context.Context, port int, notifTy notification := new(PortNotification) if err := json.Unmarshal(*req.Params, ¬ification); err != nil { select { - case errCh <- fmt.Errorf("error unmarshaling notification: %w", err): + case errCh <- fmt.Errorf("error unmarshalling notification: %w", err): default: } return diff --git a/pkg/liveshare/session_test.go b/pkg/liveshare/session_test.go index 9de50ff92..ab7cf18bd 100644 --- a/pkg/liveshare/session_test.go +++ b/pkg/liveshare/session_test.go @@ -51,7 +51,7 @@ func TestServerStartSharing(t *testing.T) { startSharing := func(conn *jsonrpc2.Conn, req *jsonrpc2.Request) (interface{}, error) { var args []interface{} if err := json.Unmarshal(*req.Params, &args); err != nil { - return nil, fmt.Errorf("error unmarshaling request: %w", err) + return nil, fmt.Errorf("error unmarshalling request: %w", err) } if len(args) < 3 { return nil, errors.New("not enough arguments to start sharing") diff --git a/pkg/markdown/markdown.go b/pkg/markdown/markdown.go index fd43e23c0..1aab0bcd3 100644 --- a/pkg/markdown/markdown.go +++ b/pkg/markdown/markdown.go @@ -2,7 +2,7 @@ package markdown import ( "github.com/charmbracelet/glamour" - ghMarkdown "github.com/cli/go-gh/pkg/markdown" + ghMarkdown "github.com/cli/go-gh/v2/pkg/markdown" ) func WithoutIndentation() glamour.TermRendererOption { @@ -10,7 +10,7 @@ func WithoutIndentation() glamour.TermRendererOption { } // WithoutWrap is a rendering option that set the character limit for soft -// wraping the markdown rendering. There is a max limit of 120 characters. +// wrapping the markdown rendering. There is a max limit of 120 characters. // If 0 is passed then wrapping is disabled. func WithWrap(w int) glamour.TermRendererOption { if w > 120 { diff --git a/pkg/search/query.go b/pkg/search/query.go index 50c7029bd..0181a2240 100644 --- a/pkg/search/query.go +++ b/pkg/search/query.go @@ -10,6 +10,7 @@ import ( const ( KindRepositories = "repositories" + KindCode = "code" KindIssues = "issues" KindCommits = "commits" ) @@ -41,6 +42,8 @@ type Qualifiers struct { CommitterName string Created string Draft *bool + Extension string + Filename string Followers string Fork string Forks string diff --git a/pkg/search/query_test.go b/pkg/search/query_test.go index b82bec85f..ddec211cb 100644 --- a/pkg/search/query_test.go +++ b/pkg/search/query_test.go @@ -23,6 +23,8 @@ func TestQueryString(t *testing.T) { AuthorEmail: "foo@example.com", CommitterDate: "2021-02-28", Created: "created", + Extension: "go", + Filename: ".vimrc", Followers: "1", Fork: "true", Forks: "2", @@ -41,7 +43,7 @@ func TestQueryString(t *testing.T) { }, }, out: "some keywords archived:true author-email:foo@example.com committer-date:2021-02-28 " + - "created:created followers:1 fork:true forks:2 good-first-issues:3 help-wanted-issues:4 " + + "created:created extension:go filename:.vimrc followers:1 fork:true forks:2 good-first-issues:3 help-wanted-issues:4 " + "in:description in:readme is:public language:language license:license pushed:updated size:5 " + "stars:6 topic:topic topics:7 user:user1 user:user2", }, @@ -89,6 +91,8 @@ func TestQualifiersMap(t *testing.T) { AuthorEmail: "foo@example.com", CommitterDate: "2021-02-28", Created: "created", + Extension: "go", + Filename: ".vimrc", Followers: "1", Fork: "true", Forks: "2", @@ -110,6 +114,8 @@ func TestQualifiersMap(t *testing.T) { "author-email": {"foo@example.com"}, "committer-date": {"2021-02-28"}, "created": {"created"}, + "extension": {"go"}, + "filename": {".vimrc"}, "followers": {"1"}, "fork": {"true"}, "forks": {"2"}, diff --git a/pkg/search/result.go b/pkg/search/result.go index d7113bfda..de00001db 100644 --- a/pkg/search/result.go +++ b/pkg/search/result.go @@ -1,11 +1,27 @@ package search import ( + "encoding/json" "reflect" "strings" "time" ) +var CodeFields = []string{ + "path", + "repository", + "sha", + "textMatches", + "url", +} + +var textMatchFields = []string{ + "fragment", + "matches", + "type", + "property", +} + var CommitFields = []string{ "author", "commit", @@ -72,6 +88,12 @@ var PullRequestFields = append(IssueFields, "isDraft", ) +type CodeResult struct { + IncompleteResults bool `json:"incomplete_results"` + Items []Code `json:"items"` + Total int `json:"total_count"` +} + type CommitsResult struct { IncompleteResults bool `json:"incomplete_results"` Items []Commit `json:"items"` @@ -90,6 +112,27 @@ type IssuesResult struct { Total int `json:"total_count"` } +type Code struct { + Name string `json:"name"` + Path string `json:"path"` + Repository Repository `json:"repository"` + Sha string `json:"sha"` + TextMatches []TextMatch `json:"text_matches"` + URL string `json:"html_url"` +} + +type TextMatch struct { + Fragment string `json:"fragment"` + Matches []Match `json:"matches"` + Type string `json:"object_type"` + Property string `json:"property"` +} + +type Match struct { + Indices []int `json:"indices"` + Text string `json:"text"` +} + type Commit struct { Author User `json:"author"` Committer User `json:"committer"` @@ -230,6 +273,38 @@ func (u User) ExportData() map[string]interface{} { } } +func (code Code) ExportData(fields []string) map[string]interface{} { + v := reflect.ValueOf(code) + data := map[string]interface{}{} + for _, f := range fields { + switch f { + case "textMatches": + matches := make([]interface{}, 0, len(code.TextMatches)) + for _, match := range code.TextMatches { + matches = append(matches, match.ExportData(textMatchFields)) + } + data[f] = matches + default: + sf := fieldByName(v, f) + data[f] = sf.Interface() + } + } + return data +} + +func (textMatch TextMatch) ExportData(fields []string) map[string]interface{} { + v := reflect.ValueOf(textMatch) + data := map[string]interface{}{} + for _, f := range fields { + switch f { + default: + sf := fieldByName(v, f) + data[f] = sf.Interface() + } + } + return data +} + func (commit Commit) ExportData(fields []string) map[string]interface{} { v := reflect.ValueOf(commit) data := map[string]interface{}{} @@ -306,6 +381,16 @@ func (repo Repository) ExportData(fields []string) map[string]interface{} { return data } +func (repo Repository) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]interface{}{ + "id": repo.ID, + "nameWithOwner": repo.FullName, + "url": repo.URL, + "isPrivate": repo.IsPrivate, + "isFork": repo.IsFork, + }) +} + // The state of an issue or a pull request, may be either open or closed. // For a pull request, the "merged" state is inferred from a value for merged_at and // which we take return instead of the "closed" state. diff --git a/pkg/search/result_test.go b/pkg/search/result_test.go index cdf424b0b..3d9fb67b3 100644 --- a/pkg/search/result_test.go +++ b/pkg/search/result_test.go @@ -11,6 +11,53 @@ import ( "github.com/stretchr/testify/require" ) +func TestCodeExportData(t *testing.T) { + tests := []struct { + name string + fields []string + code Code + output string + }{ + { + name: "exports requested fields", + fields: []string{"path", "textMatches"}, + code: Code{ + Repository: Repository{ + Name: "repo", + }, + Path: "path", + Name: "name", + TextMatches: []TextMatch{ + { + Fragment: "fragment", + Matches: []Match{ + { + Text: "fr", + Indices: []int{ + 0, + 1, + }, + }, + }, + Property: "property", + Type: "type", + }, + }, + }, + output: `{"path":"path","textMatches":[{"fragment":"fragment","matches":[{"indices":[0,1],"text":"fr"}],"property":"property","type":"type"}]}`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + exported := tt.code.ExportData(tt.fields) + buf := bytes.Buffer{} + enc := json.NewEncoder(&buf) + require.NoError(t, enc.Encode(exported)) + assert.Equal(t, tt.output, strings.TrimSpace(buf.String())) + }) + } +} + func TestCommitExportData(t *testing.T) { var authoredAt = time.Date(2021, 2, 27, 11, 30, 0, 0, time.UTC) var committedAt = time.Date(2021, 2, 28, 12, 30, 0, 0, time.UTC) diff --git a/pkg/search/searcher.go b/pkg/search/searcher.go index baa84c03c..4168dc7f3 100644 --- a/pkg/search/searcher.go +++ b/pkg/search/searcher.go @@ -25,6 +25,7 @@ var jsonTypeRE = regexp.MustCompile(`[/+]json($|;)`) //go:generate moq -rm -out searcher_mock.go . Searcher type Searcher interface { + Code(Query) (CodeResult, error) Commits(Query) (CommitsResult, error) Repositories(Query) (RepositoriesResult, error) Issues(Query) (IssuesResult, error) @@ -57,6 +58,30 @@ func NewSearcher(client *http.Client, host string) Searcher { } } +func (s searcher) Code(query Query) (CodeResult, error) { + result := CodeResult{} + toRetrieve := query.Limit + var resp *http.Response + var err error + for toRetrieve > 0 { + query.Limit = min(toRetrieve, maxPerPage) + query.Page = nextPage(resp) + if query.Page == 0 { + break + } + page := CodeResult{} + resp, err = s.search(query, &page) + if err != nil { + return result, err + } + result.IncompleteResults = page.IncompleteResults + result.Total = page.Total + result.Items = append(result.Items, page.Items...) + toRetrieve = toRetrieve - len(page.Items) + } + return result, nil +} + func (s searcher) Commits(query Query) (CommitsResult, error) { result := CommitsResult{} toRetrieve := query.Limit @@ -148,6 +173,10 @@ func (s searcher) search(query Query, result interface{}) (*http.Response, error } req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Set("Accept", "application/vnd.github.v3+json") + if query.Kind == KindCode { + req.Header.Set("Accept", "application/vnd.github.text-match+json") + } + resp, err := s.client.Do(req) if err != nil { return nil, err diff --git a/pkg/search/searcher_mock.go b/pkg/search/searcher_mock.go index c1eecdaf4..332539121 100644 --- a/pkg/search/searcher_mock.go +++ b/pkg/search/searcher_mock.go @@ -17,6 +17,9 @@ var _ Searcher = &SearcherMock{} // // // make and configure a mocked Searcher // mockedSearcher := &SearcherMock{ +// CodeFunc: func(query Query) (CodeResult, error) { +// panic("mock out the Code method") +// }, // CommitsFunc: func(query Query) (CommitsResult, error) { // panic("mock out the Commits method") // }, @@ -36,6 +39,9 @@ var _ Searcher = &SearcherMock{} // // } type SearcherMock struct { + // CodeFunc mocks the Code method. + CodeFunc func(query Query) (CodeResult, error) + // CommitsFunc mocks the Commits method. CommitsFunc func(query Query) (CommitsResult, error) @@ -50,6 +56,11 @@ type SearcherMock struct { // calls tracks calls to the methods. calls struct { + // Code holds details about calls to the Code method. + Code []struct { + // Query is the query argument value. + Query Query + } // Commits holds details about calls to the Commits method. Commits []struct { // Query is the query argument value. @@ -71,12 +82,45 @@ type SearcherMock struct { Query Query } } + lockCode sync.RWMutex lockCommits sync.RWMutex lockIssues sync.RWMutex lockRepositories sync.RWMutex lockURL sync.RWMutex } +// Code calls CodeFunc. +func (mock *SearcherMock) Code(query Query) (CodeResult, error) { + if mock.CodeFunc == nil { + panic("SearcherMock.CodeFunc: method is nil but Searcher.Code was just called") + } + callInfo := struct { + Query Query + }{ + Query: query, + } + mock.lockCode.Lock() + mock.calls.Code = append(mock.calls.Code, callInfo) + mock.lockCode.Unlock() + return mock.CodeFunc(query) +} + +// CodeCalls gets all the calls that were made to Code. +// Check the length with: +// +// len(mockedSearcher.CodeCalls()) +func (mock *SearcherMock) CodeCalls() []struct { + Query Query +} { + var calls []struct { + Query Query + } + mock.lockCode.RLock() + calls = mock.calls.Code + mock.lockCode.RUnlock() + return calls +} + // Commits calls CommitsFunc. func (mock *SearcherMock) Commits(query Query) (CommitsResult, error) { if mock.CommitsFunc == nil { diff --git a/pkg/search/searcher_test.go b/pkg/search/searcher_test.go index 8cc90c533..8642feed0 100644 --- a/pkg/search/searcher_test.go +++ b/pkg/search/searcher_test.go @@ -10,6 +10,156 @@ import ( "github.com/stretchr/testify/assert" ) +func TestSearcherCode(t *testing.T) { + query := Query{ + Keywords: []string{"keyword"}, + Kind: "code", + Limit: 30, + Qualifiers: Qualifiers{ + Language: "go", + }, + } + + values := url.Values{ + "page": []string{"1"}, + "per_page": []string{"30"}, + "q": []string{"keyword language:go"}, + } + + tests := []struct { + name string + host string + query Query + result CodeResult + wantErr bool + errMsg string + httpStubs func(reg *httpmock.Registry) + }{ + { + name: "searches code", + query: query, + result: CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}}, + Total: 1, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.QueryMatcher("GET", "search/code", values), + httpmock.JSONResponse(CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}}, + Total: 1, + }), + ) + }, + }, + { + name: "searches code for enterprise host", + host: "enterprise.com", + query: query, + result: CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}}, + Total: 1, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.QueryMatcher("GET", "api/v3/search/code", values), + httpmock.JSONResponse(CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}}, + Total: 1, + }), + ) + }, + }, + { + name: "paginates results", + query: query, + result: CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}, {Name: "file2.go"}}, + Total: 2, + }, + httpStubs: func(reg *httpmock.Registry) { + firstReq := httpmock.QueryMatcher("GET", "search/code", values) + firstRes := httpmock.JSONResponse(CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file.go"}}, + Total: 2, + }, + ) + firstRes = httpmock.WithHeader(firstRes, "Link", `; rel="next"`) + secondReq := httpmock.QueryMatcher("GET", "search/code", url.Values{ + "page": []string{"2"}, + "per_page": []string{"29"}, + "q": []string{"keyword language:go"}, + }, + ) + secondRes := httpmock.JSONResponse(CodeResult{ + IncompleteResults: false, + Items: []Code{{Name: "file2.go"}}, + Total: 2, + }, + ) + reg.Register(firstReq, firstRes) + reg.Register(secondReq, secondRes) + }, + }, + { + name: "handles search errors", + query: query, + wantErr: true, + errMsg: heredoc.Doc(` + Invalid search query "keyword language:go". + "blah" is not a recognized date/time format. Please provide an ISO 8601 date/time value, such as YYYY-MM-DD.`), + httpStubs: func(reg *httpmock.Registry) { + reg.Register( + httpmock.QueryMatcher("GET", "search/code", values), + httpmock.WithHeader( + httpmock.StatusStringResponse(422, + `{ + "message": "Validation Failed", + "errors": [ + { + "message":"\"blah\" is not a recognized date/time format. Please provide an ISO 8601 date/time value, such as YYYY-MM-DD.", + "resource":"Search", + "field":"q", + "code":"invalid" + } + ], + "documentation_url":"https://developer.github.com/v3/search/" + }`, + ), "Content-Type", "application/json"), + ) + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reg := &httpmock.Registry{} + defer reg.Verify(t) + if tt.httpStubs != nil { + tt.httpStubs(reg) + } + client := &http.Client{Transport: reg} + if tt.host == "" { + tt.host = "github.com" + } + searcher := NewSearcher(client, tt.host) + result, err := searcher.Code(tt.query) + if tt.wantErr { + assert.EqualError(t, err, tt.errMsg) + return + } + assert.NoError(t, err) + assert.Equal(t, tt.result, result) + }) + } +} + func TestSearcherCommits(t *testing.T) { query := Query{ Keywords: []string{"keyword"}, @@ -208,10 +358,14 @@ func TestSearcherRepositories(t *testing.T) { httpStubs: func(reg *httpmock.Registry) { reg.Register( httpmock.QueryMatcher("GET", "search/repositories", values), - httpmock.JSONResponse(RepositoriesResult{ - IncompleteResults: false, - Items: []Repository{{Name: "test"}}, - Total: 1, + httpmock.JSONResponse(map[string]interface{}{ + "incomplete_results": false, + "total_count": 1, + "items": []interface{}{ + map[string]interface{}{ + "name": "test", + }, + }, }), ) }, @@ -228,10 +382,14 @@ func TestSearcherRepositories(t *testing.T) { httpStubs: func(reg *httpmock.Registry) { reg.Register( httpmock.QueryMatcher("GET", "api/v3/search/repositories", values), - httpmock.JSONResponse(RepositoriesResult{ - IncompleteResults: false, - Items: []Repository{{Name: "test"}}, - Total: 1, + httpmock.JSONResponse(map[string]interface{}{ + "incomplete_results": false, + "total_count": 1, + "items": []interface{}{ + map[string]interface{}{ + "name": "test", + }, + }, }), ) }, @@ -246,12 +404,15 @@ func TestSearcherRepositories(t *testing.T) { }, httpStubs: func(reg *httpmock.Registry) { firstReq := httpmock.QueryMatcher("GET", "search/repositories", values) - firstRes := httpmock.JSONResponse(RepositoriesResult{ - IncompleteResults: false, - Items: []Repository{{Name: "test"}}, - Total: 2, - }, - ) + firstRes := httpmock.JSONResponse(map[string]interface{}{ + "incomplete_results": false, + "total_count": 2, + "items": []interface{}{ + map[string]interface{}{ + "name": "test", + }, + }, + }) firstRes = httpmock.WithHeader(firstRes, "Link", `; rel="next"`) secondReq := httpmock.QueryMatcher("GET", "search/repositories", url.Values{ "page": []string{"2"}, @@ -261,12 +422,15 @@ func TestSearcherRepositories(t *testing.T) { "q": []string{"keyword stars:>=5 topic:topic"}, }, ) - secondRes := httpmock.JSONResponse(RepositoriesResult{ - IncompleteResults: false, - Items: []Repository{{Name: "cli"}}, - Total: 2, - }, - ) + secondRes := httpmock.JSONResponse(map[string]interface{}{ + "incomplete_results": false, + "total_count": 2, + "items": []interface{}{ + map[string]interface{}{ + "name": "cli", + }, + }, + }) reg.Register(firstReq, firstRes) reg.Register(secondReq, secondRes) }, diff --git a/script/label-assets b/script/label-assets new file mode 100755 index 000000000..322e35998 --- /dev/null +++ b/script/label-assets @@ -0,0 +1,20 @@ +#!/bin/bash +set -e + +if [ $# -eq 0 ]; then + echo "usage: script/label-assets dist/gh_*" >&2 + exit 1 +fi + +for asset; do + label="$(basename "$asset")" + label="${label%.*}" + label="${label%.tar}" + label="GitHub CLI $(tr '_' ' ' <<<"${label#gh_}")" + case "$asset" in + *.msi ) label="${label} installer" ;; + *.deb ) label="${label} deb" ;; + *.rpm ) label="${label} RPM" ;; + esac + printf '"%s#%s"\n' "$asset" "$label" +done \ No newline at end of file diff --git a/script/release b/script/release new file mode 100755 index 000000000..774a8c1dd --- /dev/null +++ b/script/release @@ -0,0 +1,115 @@ +#!/bin/bash +set -e + +print_help() { + cat < [--platform {linux|macos|windows}] [--branch ] + +To build staging binaries from the current branch: + script/release --current [--platform {linux|macos|windows}] + +To build binaries locally with goreleaser: + script/release --local --platform {linux|macos|windows} +EOF +} + +if [ $# -eq 0 ]; then + print_help >&2 + exit 1 +fi + +tag_name="" +is_local="" +do_push="" +platform="" +branch="trunk" +deploy_env="production" + +while [ $# -gt 0 ]; do + case "$1" in + -h | --help ) + print_help + exit 0 + ;; + -b | --branch ) + branch="$2" + shift 2 + ;; + -p | --platform ) + platform="$2" + shift 2 + ;; + --local ) + is_local=1 + shift 1 + ;; + --staging ) + deploy_env="staging" + shift 1 + ;; + --current ) + deploy_env="staging" + tag_name="$(git describe --tags --abbrev=0)" + branch="$(git rev-parse --symbolic-full-name '@{upstream}' 2>/dev/null || git branch --show-current)" + branch="${branch#refs/remotes/*/}" + do_push=1 + shift 1 + ;; + -* ) + printf "unrecognized flag: %s\n" "$1" >&2 + exit 1 + ;; + * ) + tag_name="$1" + shift 1 + ;; + esac +done + +announce() { + local tmpdir="${TMPDIR:-/tmp}" + echo "$*" | sed "s:${tmpdir%/}:\$TMPDIR:" + "$@" +} + +trigger_deployment() { + announce gh workflow -R cli/cli run deployment.yml --ref "$branch" -f tag_name="$tag_name" -f environment="$deploy_env" +} + +build_local() { + local goreleaser_config=".goreleaser.yml" + case "$platform" in + linux ) + sed '/#build:windows/,/^$/d; /#build:macos/,/^$/d' .goreleaser.yml >.goreleaser.generated.yml + goreleaser_config=".goreleaser.generated.yml" + ;; + macos ) + sed '/#build:windows/,/^$/d; /#build:linux/,/^$/d' .goreleaser.yml >.goreleaser.generated.yml + goreleaser_config=".goreleaser.generated.yml" + ;; + windows ) + sed '/#build:linux/,/^$/d; /#build:macos/,/^$/d' .goreleaser.yml >.goreleaser.generated.yml + goreleaser_config=".goreleaser.generated.yml" + ;; + esac + [ -z "$tag_name" ] || export GORELEASER_CURRENT_TAG="$tag_name" + announce goreleaser release -f "$goreleaser_config" --clean --skip-validate --skip-publish --release-notes="$(mktemp)" +} + +if [ -n "$is_local" ]; then + build_local +else + if [ -n "$do_push" ]; then + if ! git diff --quiet || ! git diff --cached --quiet; then + echo "refusing to continue due to uncomitted local changes" >&2 + exit 1 + fi + announce git push + fi + trigger_deployment + if [ "$deploy_env" = "production" ]; then + echo + echo "Go to Slack to manually approve this production deployment." + fi +fi diff --git a/script/scoop-gen b/script/scoop-gen deleted file mode 100755 index 2e4adf647..000000000 --- a/script/scoop-gen +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash -# Usage: cat checksums.txt | script/scoop-gen -set -e - -tagname="${1?}" -jsonfile="${2?}" - -jq_args=( - --arg version "${tagname#v}" - --arg urlprefix "https://github.com/cli/cli/releases/download/$tagname/" - $(cat | awk ' - /windows_386/ { - printf "--arg win32hash %s\n", $1 - printf "--arg win32file %s\n", $2 - } - /windows_amd64/ { - printf "--arg win64hash %s\n", $1 - printf "--arg win64file %s\n", $2 - } - ') -) - -jq ' - .version = $version | - .architecture."32bit".url = $urlprefix + $win32file | - .architecture."32bit".hash = $win32hash | - .architecture."64bit".url = $urlprefix + $win64file | - .architecture."64bit".hash = $win64hash -' "${jq_args[@]}" --indent 4 "$jsonfile" > "$jsonfile"~ - -mv "$jsonfile"{~,} diff --git a/script/sign b/script/sign new file mode 100755 index 000000000..1630a06b5 --- /dev/null +++ b/script/sign @@ -0,0 +1,65 @@ +#!/bin/bash +# usage: script/sign +# +# Signs macOS binaries using codesign, notarizes macOS zip archives using notarytool, and signs +# Windows EXE and MSI files using osslsigncode. +# +set -e + +sign_windows() { + if [ -z "$CERT_FILE" ]; then + echo "skipping Windows code-signing; CERT_FILE not set" >&2 + return 0 + fi + + if [ ! -f "$CERT_FILE" ]; then + echo "error Windows code-signing; file '$CERT_FILE' not found" >&2 + return 1 + fi + + if [ -z "$CERT_PASSWORD" ]; then + echo "error Windows code-signing; no value for CERT_PASSWORD" >&2 + return 1 + fi + + osslsigncode sign -n "GitHub CLI" -t http://timestamp.digicert.com \ + -pkcs12 "$CERT_FILE" -readpass <(printf "%s" "$CERT_PASSWORD") -h sha256 \ + -in "$1" -out "$1"~ + + mv "$1"~ "$1" +} + +sign_macos() { + if [ -z "$APPLE_DEVELOPER_ID" ]; then + echo "skipping macOS code-signing; APPLE_DEVELOPER_ID not set" >&2 + return 0 + fi + + if [[ $1 == *.zip ]]; then + xcrun notarytool submit "$1" --apple-id "${APPLE_ID?}" --team-id "${APPLE_DEVELOPER_ID?}" --password "${APPLE_ID_PASSWORD?}" + else + codesign --timestamp --options=runtime -s "${APPLE_DEVELOPER_ID?}" -v "$1" + fi +} + +if [ $# -eq 0 ]; then + echo "usage: script/sign " >&2 + exit 1 +fi + +platform="$(uname -s)" + +for input_file; do + case "$input_file" in + *.exe | *.msi ) + sign_windows "$input_file" + ;; + * ) + if [ "$platform" = "Darwin" ]; then + sign_macos "$input_file" + else + printf "warning: don't know how to sign %s on %s\n" "$1", "$platform" >&2 + fi + ;; + esac +done \ No newline at end of file diff --git a/script/sign-windows-executable.sh b/script/sign-windows-executable.sh deleted file mode 100755 index d89e6dbe4..000000000 --- a/script/sign-windows-executable.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash -set -e - -EXE="$1" - -if [ -z "$CERT_FILE" ]; then - echo "skipping Windows code-signing; CERT_FILE not set" >&2 - exit 0 -fi - -if [ ! -f "$CERT_FILE" ]; then - echo "error Windows code-signing; file '$CERT_FILE' not found" >&2 - exit 1 -fi - -if [ -z "$CERT_PASSWORD" ]; then - echo "error Windows code-signing; no value for CERT_PASSWORD" >&2 - exit 1 -fi - -osslsigncode sign -n "GitHub CLI" -t http://timestamp.digicert.com \ - -pkcs12 "$CERT_FILE" -readpass <(printf "%s" "$CERT_PASSWORD") -h sha256 \ - -in "$EXE" -out "$EXE"~ - -mv "$EXE"~ "$EXE" diff --git a/script/sign.bat b/script/sign.bat new file mode 100644 index 000000000..4fbe8a7af --- /dev/null +++ b/script/sign.bat @@ -0,0 +1,8 @@ +@echo off + +if "%CERT_FILE%" == "" ( + echo skipping Windows code-signing; CERT_FILE not set + exit /b +) + +.\script\signtool sign /d "GitHub CLI" /f "%CERT_FILE%" /p "%CERT_PASSWORD%" /fd sha256 /tr http://timestamp.digicert.com /v "%1" \ No newline at end of file diff --git a/script/sign.ps1 b/script/sign.ps1 deleted file mode 100644 index 336e0204c..000000000 --- a/script/sign.ps1 +++ /dev/null @@ -1,12 +0,0 @@ -param ( - [string]$Certificate = $(throw "-Certificate is required."), - [string]$Executable = $(throw "-Executable is required.") -) - -Set-StrictMode -Version Latest -$ErrorActionPreference = "Stop" - -$ProgramName = "GitHub CLI" -$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition - -& $scriptPath\signtool.exe sign /d $ProgramName /f $Certificate /p $env:CERT_PASSWORD /fd sha256 /tr http://timestamp.digicert.com /v $Executable diff --git a/utils/table_printer.go b/utils/table_printer.go index 73f65a6a6..1bf9a955b 100644 --- a/utils/table_printer.go +++ b/utils/table_printer.go @@ -5,7 +5,7 @@ import ( "strings" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/go-gh/pkg/tableprinter" + "github.com/cli/go-gh/v2/pkg/tableprinter" ) type TablePrinter interface { @@ -63,7 +63,7 @@ func (p printer) IsTTY() bool { func (p *printer) AddField(s string, truncateFunc func(int, string) string, colorFunc func(string) string) { if truncateFunc == nil { - // Disallow ever truncating the 1st colum or any URL value + // Disallow ever truncating the 1st column or any URL value if p.colIndex == 0 || isURL(s) { p.tp.AddField(s, tableprinter.WithTruncate(nil), tableprinter.WithColor(colorFunc)) } else {