SOLVED

Issue running Powershell script to clone GitHub repositories

Copper Contributor

I'm running into a problem where my script is unable to clone private GitHub repositories into a destination folder using a GitHub API key. When I run the script, I receive the following error:

 

Invoke-RestMethod : {"message":"Not Found","documentation_url":"https://docs.github.com/rest"}
At C:\Users\florekd\Desktop\script.ps1:5 char:17
+     $repoInfo = Invoke-RestMethod -Uri $url -Headers $headers
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

 

 

Original script:

 

# Function to clone a repository using GitHub API 
function CloneGitHubRepo($repoName, $accessToken, $repoBranch, $cloneDirectory) { 
    $headers = @{ "Authorization" = "Bearer $accessToken" } 
    $url = "https://api.github.com/<org_name>/$repoName" 
    $repoInfo = Invoke-RestMethod -Uri $url -Headers $headers 
    
    if ($repoInfo) { 
        $cloneUrl = $repoInfo.clone_url 
        git clone $cloneUrl -b $repoBranch $cloneDirectory --progress -v
    } 
    else { 
        Write-Host "Failed to retrieve repository information for $url" 
    } 
} 

Function Get-Filename($initialDirectory){
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    $OpenFileDialog=New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.InitialDirectory=$initialDirectory
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.FileName
}

#Prompt user for a home directory to make
$save_directory = Read-Host -Prompt "Enter the root directory name to save all project files: "

#Prompt user for file containing list of GitHub repository URLs
#$git_list = Read-Host -Prompt "Provide the list of GitHub URLs to pull: "
$git_list = Get-Filename "$env:USERPROFILE\Desktop\"

#Prompt user for GitHub Release Branch to target
$git_branch = Read-Host -Prompt "Provide Git release branch to target: "

#Prompt user for GitHub API Token
$git_token = Read-Host -Prompt "Provide the GitHub token to use: "

#Create Save Directory
If(!(test-path -PathType container $save_directory))
{
      New-Item -Path $save_directory -ItemType Directory
}

# Loop through each repository and clone it 
foreach ($git_uri in Get-Content -Path $git_list) {
    CloneGitHubRepo $git_uri $git_token $git_branch "$save_directory\$git_uri"
}

 

Would it be easier to just use curl to pull the repos?

2 Replies
best response confirmed by Dave Florek (Copper Contributor)
Solution

Hi @Dave Florek,

The error message "Not Found" indicates that the API endpoint might not be valid or doesn't exist.

To troubleshoot this, here are some steps you can take:

  1. Double-check the $url variable in your CloneGitHubRepo function to ensure it contains the correct organization name and repository name.

  2. Make sure the GitHub API token you're using has the necessary permissions to access the repository. It should have at least "repo" scope to clone private repositories.

  3. Test the API request separately using tools like Postman or cURL to see if you can successfully access the repository information. This will help you identify any issues with the API URL and headers.

  4. Consider using a personal access token instead of a GitHub API token for authentication. Personal access tokens are easier to manage and should provide sufficient permissions for cloning private repositories.

Regarding using cURL, while it's an option, PowerShell's Invoke-RestMethod is a powerful cmdlet that can efficiently handle API requests. Once you resolve the authentication issue, it should work fine for cloning GitHub repositories.


I suggest focusing on debugging the existing PowerShell script and fixing the API request problem. PowerShell's Invoke-RestMethod is quite flexible and should be suitable for this task once the authentication problem is resolved.



Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.


If the post was useful in other ways, please consider giving it Like.


Kindest regards,


Leon Pavesic

Yep, it's an issue with GitHub v3 API endpoints not matching up with GitHub's API documentation.
1 best response

Accepted Solutions
best response confirmed by Dave Florek (Copper Contributor)
Solution

Hi @Dave Florek,

The error message "Not Found" indicates that the API endpoint might not be valid or doesn't exist.

To troubleshoot this, here are some steps you can take:

  1. Double-check the $url variable in your CloneGitHubRepo function to ensure it contains the correct organization name and repository name.

  2. Make sure the GitHub API token you're using has the necessary permissions to access the repository. It should have at least "repo" scope to clone private repositories.

  3. Test the API request separately using tools like Postman or cURL to see if you can successfully access the repository information. This will help you identify any issues with the API URL and headers.

  4. Consider using a personal access token instead of a GitHub API token for authentication. Personal access tokens are easier to manage and should provide sufficient permissions for cloning private repositories.

Regarding using cURL, while it's an option, PowerShell's Invoke-RestMethod is a powerful cmdlet that can efficiently handle API requests. Once you resolve the authentication issue, it should work fine for cloning GitHub repositories.


I suggest focusing on debugging the existing PowerShell script and fixing the API request problem. PowerShell's Invoke-RestMethod is quite flexible and should be suitable for this task once the authentication problem is resolved.



Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.


If the post was useful in other ways, please consider giving it Like.


Kindest regards,


Leon Pavesic

View solution in original post