Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions auto_push_to_github.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
30 changes: 22 additions & 8 deletions auto_push_to_github.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ param (
[string]$RepoOwner,
[string]$RepoName,
[string]$BranchName = "automated-branch",
[string]$FilePath,
[string]$FilePaths,
[string]$CommitMessage
)

Expand All @@ -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 {
Expand All @@ -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: $_"
}
}

Expand Down Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion auto_push_to_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions auto_push_to_github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down