How to Clone a GitHub Issue Using PowerShell and Shell Scripts
Overview
This comprehensive guide demonstrates how to clone GitHub issues using both PowerShell and shell scripts. The solution automatically creates a duplicate issue with the same title and body content, then closes the original issue with a reference to the newly created one.
What is Issue Cloning?
Issue cloning is the process of creating a new GitHub issue that duplicates the content of an existing issue. This is useful for:
- Moving issues between repositories
- Creating templates from existing issues
- Archiving resolved issues while maintaining their content
- Reorganizing project workflows
Prerequisites
1. Installing GitHub CLI
On Windows using winget
First, check if winget is available:
Get-Command winget -ErrorAction SilentlyContinue
If winget is not found, you can try adding it to your PATH:
$env:PATH += ";C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_1.27.440.0_x64__8wekyb3d8bbwe"
winget --version
Install GitHub CLI:
winget install --id GitHub.cli
Alternative Installation Methods
Method 1: Manual Download
- Download from GitHub: https://github.com/cli/cli/releases
- Install the
.msifile for Windows
Method 2: Using Chocolatey
- Chocolatey is a package manager for Windows. Install GitHub CLI with:
choco install gh
Method 3: Direct Download
$progressPreference = 'silentlyContinue'
Invoke-WebRequest -Uri https://github.com/cli/cli/releases/latest/download/gh_windows_amd64.msi -OutFile gh_installer.msi
Start-Process msiexec.exe -ArgumentList "/i", "gh_installer.msi", "/quiet" -Wait
2. Add GitHub CLI to PATH
After installation, add GitHub CLI to your system PATH:
$env:PATH += ";C:\Program Files\GitHub CLI"
gh --version
3. GitHub Authentication
Authenticate with GitHub:
gh auth login
Follow the prompts to authenticate using your browser or personal access token.
Solution Overview
We’ve created two scripts that provide the same functionality:
- PowerShell Script (
clone_issue.ps1) - Cross-platform PowerShell solution - Shell Script (
clone_issue.sh) - Traditional bash script
Both scripts:
- Accept issue number as a parameter
- Retrieve the original issue’s title and body
- Create a new issue with identical content
- Add a reference note (“Cloned from #[original_issue_number]”)
- Automatically close the original issue with a comment referencing the new issue
clone_issue.ps1
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Clone a GitHub issue by creating a new issue with the same title and body.
.AUTHOR
Mahedee Hasan
.DESCRIPTION
This script uses GitHub CLI to retrieve an existing issue and create a new one
with the same content, adding a note that it was cloned from the original.
After successful creation, it closes the original issue with a comment referencing the new issue.
.PARAMETER IssueNumber
The issue number to clone.
.PARAMETER Repository
The repository in format "owner/repo". Defaults to "mahedee/rnd".
.EXAMPLE
.\clone_issue.ps1 -IssueNumber 7
.EXAMPLE
.\clone_issue.ps1 -IssueNumber 7 -Repository "username/repository"
.EXAMPLE
.\clone_issue.ps1 9
#>
param(
[Parameter(Mandatory=$true)]
[int]$IssueNumber,
[Parameter(Mandatory=$false)]
[string]$Repository = "arisha/myrepo"
)
# Check if GitHub CLI is available
try {
$null = Get-Command gh -ErrorAction Stop
} catch {
Write-Error "GitHub CLI (gh) is not found. Please install GitHub CLI and ensure it's in your PATH."
exit 1
}
try {
Write-Host "Retrieving issue #$IssueNumber from repository $Repository..."
# Get issue title
$title = gh issue view $IssueNumber -R $Repository --json title -q .title
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to retrieve issue #$IssueNumber. Please check if the issue exists and you have access to the repository."
exit 1
}
# Get issue body
$body = gh issue view $IssueNumber -R $Repository --json body -q .body
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to retrieve issue body for #$IssueNumber."
exit 1
}
# Create the cloned issue body with reference to original
$clonedBody = "$body`n`nCloned from #$IssueNumber"
Write-Host "Creating new issue with title: $title"
# Create new issue and capture the result
$createOutput = gh issue create -R $Repository --title "$title" --body "$clonedBody" 2>&1
if ($LASTEXITCODE -eq 0) {
# Extract issue number from URL (format: https://github.com/owner/repo/issues/123)
if ($createOutput -match '/issues/(\d+)$') {
$newIssueNumber = $matches[1]
Write-Host "Successfully created new issue #$newIssueNumber!" -ForegroundColor Green
# Close the original issue with a comment
Write-Host "Closing original issue #$IssueNumber..."
$closeComment = "Created new issue #$newIssueNumber"
gh issue close $IssueNumber -R $Repository --comment "$closeComment"
if ($LASTEXITCODE -eq 0) {
Write-Host "Successfully closed original issue #$IssueNumber with reference to new issue #$newIssueNumber!" -ForegroundColor Green
} else {
Write-Warning "New issue #$newIssueNumber was created, but failed to close the original issue #$IssueNumber."
}
} else {
Write-Host "New issue created successfully, but couldn't extract issue number from output: $createOutput" -ForegroundColor Yellow
Write-Warning "Please manually close issue #$IssueNumber if needed."
}
} else {
Write-Error "Failed to create the cloned issue. Error: $createOutput"
exit 1
}
} catch {
Write-Error "An error occurred: $($_.Exception.Message)"
exit 1
}
clone_issue.sh
#!/bin/bash
# Clone a GitHub issue by creating a new issue with the same title and body,
# then close the original issue with a comment referencing the new one.
#
# Author: Mahedee Hasan
#
# Usage: ./clone_issue.sh <issue_number> [repository]
#
# Parameters:
# issue_number: The issue number to clone (required)
# repository: The repository in format "owner/repo" (optional, defaults to "mahedee/rnd")
#
# Examples:
# ./clone_issue.sh 7
# ./clone_issue.sh 7 "username/repository"
# Add GitHub CLI to PATH
export PATH="$PATH:/c/Program Files/GitHub CLI"
# Check if issue number is provided
if [ -z "$1" ]; then
echo "Error: Issue number is required"
echo "Usage: $0 <issue_number> [repository]"
exit 1
fi
ISSUE=$1
REPO=${2:-"arisha/myrepo"}
# Check if GitHub CLI is available
if ! command -v gh &> /dev/null; then
echo "Error: GitHub CLI (gh) is not found. Please install GitHub CLI and ensure it's in your PATH."
exit 1
fi
echo "Retrieving issue #$ISSUE from repository $REPO..."
# Get issue title
TITLE=$(gh issue view $ISSUE -R $REPO --json title -q .title)
if [ $? -ne 0 ]; then
echo "Error: Failed to retrieve issue #$ISSUE. Please check if the issue exists and you have access to the repository."
exit 1
fi
# Get issue body
BODY=$(gh issue view $ISSUE -R $REPO --json body -q .body)
if [ $? -ne 0 ]; then
echo "Error: Failed to retrieve issue body for #$ISSUE."
exit 1
fi
echo "Creating new issue with title: $TITLE"
# Create the cloned issue body with reference to original
CLONED_BODY="$BODY
Cloned from #$ISSUE"
# Create new issue and capture the result
CREATE_OUTPUT=$(gh issue create -R $REPO --title "$TITLE" --body "$CLONED_BODY" 2>&1)
CREATE_EXIT_CODE=$?
if [ $CREATE_EXIT_CODE -eq 0 ]; then
# Extract issue number from URL (format: https://github.com/owner/repo/issues/123)
if [[ $CREATE_OUTPUT =~ /issues/([0-9]+)$ ]]; then
NEW_ISSUE_NUMBER="${BASH_REMATCH[1]}"
echo "Successfully created new issue #$NEW_ISSUE_NUMBER!"
# Close the original issue with a comment
echo "Closing original issue #$ISSUE..."
CLOSE_COMMENT="Created new issue #$NEW_ISSUE_NUMBER"
gh issue close $ISSUE -R $REPO --comment "$CLOSE_COMMENT"
if [ $? -eq 0 ]; then
echo "Successfully closed original issue #$ISSUE with reference to new issue #$NEW_ISSUE_NUMBER!"
else
echo "Warning: New issue #$NEW_ISSUE_NUMBER was created, but failed to close the original issue #$ISSUE."
fi
else
echo "New issue created successfully, but couldn't extract issue number from output: $CREATE_OUTPUT"
echo "Warning: Please manually close issue #$ISSUE if needed."
fi
else
echo "Error: Failed to create the cloned issue. Error: $CREATE_OUTPUT"
exit 1
fi
PowerShell Script Implementation
Script Features
The PowerShell script (clone_issue.ps1) offers:
- Parameter validation with mandatory issue number
- Flexible repository specification (defaults to “arisha/myrepo”)
- Comprehensive error handling with detailed messages
- Built-in help documentation using PowerShell comment-based help
- Cross-platform compatibility (Windows, Linux, macOS)
Usage Examples
Basic usage:
.\clone_issue.ps1 -IssueNumber 9
Short parameter syntax:
.\clone_issue.ps1 9
With custom repository:
.\clone_issue.ps1 -IssueNumber 9 -Repository "username/repository"
Get help:
Get-Help .\clone_issue.ps1 -Full
Script Workflow
- Validation: Checks if GitHub CLI is installed and accessible
- Retrieval: Fetches the original issue’s title and body using
gh issue view - Creation: Creates a new issue with the same content plus a clone reference
- Extraction: Parses the new issue URL to extract the issue number
- Closure: Closes the original issue with a comment linking to the new issue
Shell Script Implementation
Script Features
The shell script (clone_issue.sh) provides:
- Command-line parameter support for issue number and repository
- POSIX-compliant bash scripting for maximum compatibility
- Regex pattern matching for URL parsing
- Comprehensive error checking with exit codes
- Git Bash compatibility on Windows
Usage Examples
Basic usage:
./clone_issue.sh 9
With custom repository:
./clone_issue.sh 9 "username/repository"
Make script executable (Linux/macOS):
chmod +x clone_issue.sh
./clone_issue.sh 9
Script Workflow
- Parameter Check: Validates that issue number is provided
- CLI Verification: Ensures GitHub CLI is available in PATH
- Data Retrieval: Uses
gh issue viewwith JSON output parsing - Issue Creation: Creates new issue and captures the output
- URL Parsing: Extracts issue number using bash regex matching
- Original Closure: Closes the original issue with reference comment
Conclusion
This comprehensive solution provides robust GitHub issue cloning capabilities using both PowerShell and shell scripts. The automated workflow saves time while maintaining data integrity and providing proper cross-references between original and cloned issues.
Choose the PowerShell script for cross-platform compatibility and advanced parameter handling, or use the shell script for lightweight, POSIX-compliant environments. Both scripts provide the same core functionality with appropriate error handling and user feedback. Happy coding!
Comments