diff --git a/auto_push_to_github.bat b/auto_push_to_github.bat index dbd3120..bfc3b63 100644 --- a/auto_push_to_github.bat +++ b/auto_push_to_github.bat @@ -19,19 +19,27 @@ set /p BRANCH_NAME=Enter the branch name (default: automated-branch): if "%BRANCH_NAME%"=="" ( set BRANCH_NAME=automated-branch ) -set /p FILE_PATH=Enter the file path to push: +set /p FILE_PATHS=Enter the file paths to push (comma-separated): set /p COMMIT_MESSAGE=Enter the commit message: setlocal enabledelayedexpansion -for /f "delims=" %%i in ('type %FILE_PATH%') do set FILE_CONTENT=!FILE_CONTENT!%%i -set ENCODED_CONTENT=%FILE_CONTENT:~0,1000% +for %%F in (%FILE_PATHS%) do ( + if not exist "%%F" ( + echo Error: File %%F does not exist. + exit /b 1 + ) + for /f "delims=" %%i in ('type %%F') do set FILE_CONTENT=!FILE_CONTENT!%%i + set ENCODED_CONTENT=!FILE_CONTENT:~0,1000! +) :: Create branch for /f "tokens=*" %%i in ('curl -s -H "Authorization: token %GITHUB_TOKEN" "https://api.github.com/repos/%REPO_OWNER%/%REPO_NAME%/git/refs/heads/main"') do set MAIN_SHA=%%i for /f "tokens=*" %%i in ('curl -s -X POST -H "Authorization: token %GITHUB_TOKEN" -d "{\"ref\": \"refs/heads/%BRANCH_NAME%\", \"sha\": \"%MAIN_SHA%\"}" "https://api.github.com/repos/%REPO_OWNER%/%REPO_NAME%/git/refs"') do set BRANCH_RESPONSE=%%i :: Create file -for /f "tokens=*" %%i in ('curl -s -X PUT -H "Authorization: token %GITHUB_TOKEN" -d "{\"message\": \"%COMMIT_MESSAGE%\", \"content\": \"%ENCODED_CONTENT%\", \"branch\": \"%BRANCH_NAME%\"}" "https://api.github.com/repos/%REPO_OWNER%/%REPO_NAME%/contents/%FILE_PATH%"') do set FILE_RESPONSE=%%i +for %%F in (%FILE_PATHS%) do ( + for /f "tokens=*" %%i in ('curl -s -X PUT -H "Authorization: token %GITHUB_TOKEN" -d "{\"message\": \"%COMMIT_MESSAGE%\", \"content\": \"%ENCODED_CONTENT%\", \"branch\": \"%BRANCH_NAME%\"}" "https://api.github.com/repos/%REPO_OWNER%/%REPO_NAME%/contents/%%F"') do set FILE_RESPONSE=%%i +) :: Create pull request for /f "tokens=*" %%i in ('curl -s -X POST -H "Authorization: token %GITHUB_TOKEN" -d "{\"title\": \"Automated Pull Request\", \"head\": \"%BRANCH_NAME%\", \"base\": \"main\", \"body\": \"This is an automated pull request.\"}" "https://api.github.com/repos/%REPO_OWNER%/%REPO_NAME%/pulls"') do set PR_NUMBER=%%i @@ -45,7 +53,7 @@ set /p PROFILE_NAME=Enter Profile Name: echo REPO_OWNER=%REPO_OWNER%>profiles\%PROFILE_NAME%.env echo REPO_NAME=%REPO_NAME%>>profiles\%PROFILE_NAME%.env echo BRANCH_NAME=%BRANCH_NAME%>>profiles\%PROFILE_NAME%.env -echo FILE_PATH=%FILE_PATH%>>profiles\%PROFILE_NAME%.env +echo FILE_PATHS=%FILE_PATHS%>>profiles\%PROFILE_NAME%.env echo COMMIT_MESSAGE=%COMMIT_MESSAGE%>>profiles\%PROFILE_NAME%.env echo Settings profile saved as %PROFILE_NAME%.env diff --git a/auto_push_to_github.ps1 b/auto_push_to_github.ps1 index d9e69e3..1b0d2a8 100644 --- a/auto_push_to_github.ps1 +++ b/auto_push_to_github.ps1 @@ -2,7 +2,7 @@ param ( [string]$RepoOwner, [string]$RepoName, [string]$BranchName = "automated-branch", - [string]$FilePath, + [string]$FilePaths, [string]$CommitMessage ) @@ -21,8 +21,17 @@ if (-not $GitHubToken) { $GitHubToken = Read-Host -Prompt "Enter your GitHub Personal Access Token" } -$FileContent = Get-Content -Raw -Path $FilePath -$EncodedContent = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($FileContent)) +$FilePathsArray = $FilePaths -split "," + +function Get-FileContent { + param ($FilePath) + if (-not (Test-Path $FilePath)) { + Write-Error "Error: File $FilePath does not exist." + return $null + } + $FileContent = Get-Content -Raw -Path $FilePath + return [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($FileContent)) +} function Create-Branch { try { @@ -39,16 +48,21 @@ function Create-Branch { } function Create-File { + param ($FilePath) + $EncodedContent = Get-FileContent -FilePath $FilePath + if (-not $EncodedContent) { + return + } try { $Response = Invoke-RestMethod -Method Put -Headers @{Authorization = "token $GitHubToken"} -Uri "https://api.github.com/repos/$RepoOwner/$RepoName/contents/$FilePath" -Body (@{ message = $CommitMessage content = $EncodedContent branch = $BranchName } | ConvertTo-Json) -UseBasicParsing - Write-Output "File created successfully." + Write-Output "File $FilePath created successfully." return $Response } catch { - Write-Error "Failed to create file: $_" + Write-Error "Failed to create file $FilePath: $_" } } @@ -86,7 +100,7 @@ function Save-SettingsProfile { RepoOwner = $RepoOwner RepoName = $RepoName BranchName = $BranchName - FilePath = $FilePath + FilePaths = $FilePaths CommitMessage = $CommitMessage } $ProfileContent | ConvertTo-Json | Set-Content -Path ".\profiles\$ProfileName.json" @@ -100,7 +114,7 @@ function Load-SettingsProfile { $global:RepoOwner = $ProfileContent.RepoOwner $global:RepoName = $ProfileContent.RepoName $global:BranchName = $ProfileContent.BranchName - $global:FilePath = $ProfileContent.FilePath + $global:FilePaths = $ProfileContent.FilePaths $global:CommitMessage = $ProfileContent.CommitMessage Write-Output "Settings profile $ProfileName.json loaded." } else { @@ -131,7 +145,7 @@ function Main-Menu { $Option = Read-Host -Prompt "Enter your choice" switch ($Option) { 1 { Create-Branch } - 2 { Create-File } + 2 { foreach ($FilePath in $FilePathsArray) { Create-File -FilePath $FilePath } } 3 { Create-PullRequest } 4 { $PrNumber = Read-Host -Prompt "Enter Pull Request Number"; Merge-PullRequest -PrNumber $PrNumber } 5 { $ProfileName = Read-Host -Prompt "Enter Profile Name"; Save-SettingsProfile -ProfileName $ProfileName } diff --git a/auto_push_to_github.py b/auto_push_to_github.py index 7c56741..3c7592e 100644 --- a/auto_push_to_github.py +++ b/auto_push_to_github.py @@ -82,7 +82,7 @@ def merge_pull_request(pr_number): url = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/pulls/{pr_number}/merge" headers = {"Authorization": f"token {GITHUB_TOKEN}"} data = {"commit_message": "Automated merge"} - response = requests.put(url, headers=headers, json=data) + response = requests.put(url, headers=headers, json(data)) response.raise_for_status() logging.info("Pull request merged successfully.") return response.json() diff --git a/auto_push_to_github.sh b/auto_push_to_github.sh index a20afbb..0ab664e 100644 --- a/auto_push_to_github.sh +++ b/auto_push_to_github.sh @@ -25,6 +25,10 @@ create_branch() { create_file() { for FILE_PATH in "${FILE_PATHS_ARRAY[@]}"; do + if [ ! -f "$FILE_PATH" ]; then + echo "Error: File $FILE_PATH does not exist." + continue + fi FILE_CONTENT=$(base64 -w 0 $FILE_PATH) curl -s -X PUT -H "Authorization: token $GITHUB_TOKEN" -d "{\"message\": \"$COMMIT_MESSAGE\", \"content\": \"$FILE_CONTENT\", \"branch\": \"$BRANCH_NAME\"}" https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/contents/$FILE_PATH done