Github Management

github-management.ps1 · Download Script

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# GitHub Management Functions
# Functions for managing GitHub repositories with automated workflows

# This script includes functions for managing GitHub repository development, handling Git tags, and pushing changes with temporary public visibility. It is designed to streamline common GitHub operations for developers.
function Manage-GitHubAppDev {
    <#
    .SYNOPSIS
    Manages GitHub repository development workflow
    
    .DESCRIPTION
    Automates GitHub repository management including commits, tags, and releases
    
    .PARAMETER managerPath
    Path to the GitHub management scripts directory
    
    .PARAMETER appName
    Name of the application/repository
    
    .PARAMETER commitTag
    Tag for the commit (e.g., v1.0.0)
    
    .PARAMETER commitMessage
    Commit message describing changes
    
    .PARAMETER method
    Method to use: 'update', 'init', or 'download'
    
    .EXAMPLE
    Manage-GitHubAppDev -appName "my-app" -commitTag "v0.3.1" -commitMessage "Updated features" -method "update"
    #>
    param (
        [string]$managerPath = "<path/to/github-management-folder>",
        [string]$appName,
        [string]$commitTag,
        [string]$commitMessage,
        [string]$method = "update"
    )
    
    if ($method[0].ToString().ToLower() -eq "u") {
        $method = "update"
    } elseif ($method[0].ToString().ToLower() -eq "i") {
        $method = "init"
    } elseif ($method[0].ToString().ToLower() -eq "d") {
        $method = "download"
    } else {
        Write-Host "Error: Invalid method parameter. Use 'update', 'init', or 'download'." -ForegroundColor Red
        return
    }

    Write-Host "--------------*GITHUB APP MANAGER*---------------------" -ForegroundColor Cyan
    Write-Host "Managing GitHub repository: $appName" -ForegroundColor Cyan
    $confirmation = Read-Host "You are about to make hard to revert changes to '$appName' on your GitHub repository. Type 'y' to continue, any other key to cancel"
    if ($confirmation -ne "y") {
        Write-Host "CANCELLED! Exiting without making changes" -ForegroundColor Red
        return
    }

    $originalDir = Get-Location
    
    Write-Host "--------------------------------------------------------"
    if (Test-Path $managerPath) {
        Set-Location -Path $managerPath
        Write-Host "Changed directory to '$managerPath'."
    } else {
        Write-Host "Directory '$managerPath' does not exist." -ForegroundColor Red
        return
    }
    
    if ($method -eq "update" -or $method -eq "init") {
        $managerApp = "app-manager.ps1"
        
        if ($null -eq $appName -or $appName -eq "" -or
            $null -eq $commitTag -or $commitTag -eq "" -or
            $null -eq $commitMessage -or $commitMessage -eq "") {
            Write-Host "Error: appName, commitTag, and commitMessage parameters are required." -ForegroundColor Red
            Set-Location -Path $originalDir
            return
        }

        $scriptPath = Join-Path $managerPath -ChildPath "$managerApp"
        if (Test-Path $scriptPath) {
            . $scriptPath -appName $appName -commitTag $commitTag -commitMessage $commitMessage -method $method
            Write-Host "Script '$managerApp' executed." -ForegroundColor Green
        } else {
            Write-Host "Script '$managerApp' not found in '$managerPath'." -ForegroundColor Red
        }
    }
    
    Set-Location -Path $originalDir
    Write-Host "--------------------------------------------------------"
}

# --- This function manages Git tags for a repository, allowing listing, creation, or deletion of tags. ---
function Manage-GitTags {
    <#
    .SYNOPSIS
    Manages Git tags for a repository
    
    .DESCRIPTION
    Lists, creates, or deletes Git tags for a repository
    
    .PARAMETER appName
    Name of the application/repository
    
    .PARAMETER commitTag
    Tag name to manage
    
    .PARAMETER method
    Method to use: 'update', 'list', or 'delete'
    
    .EXAMPLE
    Manage-GitTags -appName "my-app" -method "list"
    Manage-GitTags -appName "my-app" -commitTag "v0.3.1" -method "delete"
    #>
    param (
        [string]$managerPath = "<path/to/github-management-folder>",
        [string]$appName,
        [string]$commitTag,
        [string]$method = "list"
    )
    
    Write-Host "-------------*GITHUB TAG MANAGER*----------------------" -ForegroundColor Cyan
    Write-Host "Managing GitHub Tags for repository: $appName" -ForegroundColor Cyan
    
    if ($method[0].ToString().ToLower() -eq "l") {
        $method = "list"
        $managerApp = "tag-list.ps1"
    } elseif ($method[0].ToString().ToLower() -eq "d" -or $method[0].ToString().ToLower() -eq "u") {
        $method = "delete"
        $managerApp = "tag-delete.ps1"
    } else {
        Write-Host "Error: Invalid method parameter. Use 'list', 'delete', or 'update'." -ForegroundColor Red
        return
    }

    $originalDir = Get-Location
    
    if (Test-Path $managerPath) {
        Set-Location -Path $managerPath
    } else {
        Write-Host "Directory '$managerPath' does not exist." -ForegroundColor Red
        return
    }
    
    $scriptPath = Join-Path $managerPath -ChildPath "$managerApp"
    if (Test-Path $scriptPath) {
        . $scriptPath -appName $appName -commitTag $commitTag -method $method
        Write-Host "Script '$managerApp' executed." -ForegroundColor Green
    } else {
        Write-Host "Script '$managerApp' not found." -ForegroundColor Red
    }
    
    Set-Location -Path $originalDir
    Write-Host "--------------------------------------------------------"
}

# --- This function pushes changes to GitHub, makes the repository public for a specified duration, and then reverts it back to private. ---
function GitPushPublicSleepPrivate {
    <#
    .SYNOPSIS
    Pushes changes to GitHub, makes the repository public for a specified duration, and then reverts it back to private.
    
    .DESCRIPTION
    This function pushes changes to the GitHub repository, makes it public for a specified duration, and then reverts it back to private.
    
    .PARAMETER sleep
    The duration (in seconds) to keep the repository public.
    
    .PARAMETER branch
    The branch to push changes to.
    
    .PARAMETER msgType
    The type of commit message: 'message' or 'file'.
    
    .PARAMETER msg
    The commit message (used if msgType is 'message').
    
    .PARAMETER file
    The file containing the commit message (used if msgType is 'file').
    
    .EXAMPLE
    GitPushPublicSleepPrivate -sleep 180 -branch "main" -msgType "message" -msg "Update dependencies"
    #>
    param (
        [int]$sleep = 180,
        [string]$branch = "main",
        [string]$msgType = "message",
        [string]$msg = "",
        [string]$file = ""
    )
    Write-Host "--------------------------------------------------------"
    Write-Host "Pushing public sleep private changes to GitHub..." -ForegroundColor Cyan
    Write-Host "Making repository public..."
    gh repo edit --visibility public --accept-visibility-change-consequences
    git add .
    if ($null -eq $msgType -or $msgType -eq "") {
        $msgType = "message"
    }
    if ($msgType -eq "file") {
        git commit -F $file
    } else {
        $commitMessage = Read-Host "Enter commit message"
        git commit -m "$commitMessage"
    }
    git push origin $branch
    Write-Host "Changes pushed to GitHub. Sleeping $sleep seconds to allow deployment..."
    $sleepEnd = (Get-Date).AddSeconds($sleep)
    while ((Get-Date) -lt $sleepEnd) {
        $remaining = $sleepEnd - (Get-Date)
        Write-Host -NoNewline "`rTime remaining: $([math]::Ceiling($remaining.TotalSeconds)) seconds     " -ForegroundColor DarkGray
        Start-Sleep -Milliseconds 1000
    }
    Write-Host "Finished sleeping for $sleep seconds."
    Write-Host "Making repository private again..." -ForegroundColor Green
    gh repo edit --visibility private --accept-visibility-change-consequences
    Write-Host "--------------------------------------------------------"
}

# Export functions
Export-ModuleMember -Function Manage-GitHubAppDev, Manage-GitTags