Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8265e0d
Add an icon that might be the new icon
MariusStorhaug Sep 23, 2023
d28fd3b
Added Auth component
MariusStorhaug Sep 23, 2023
8ba1a44
Add docs and started on Auth topic
MariusStorhaug Sep 23, 2023
4a0d85d
Fix for refresh token
MariusStorhaug Sep 23, 2023
8d35b21
Fix datatype on refreshtoken
MariusStorhaug Sep 23, 2023
b7fffec
Fix to store CLient ID + reset UserAuth when DeviceFlow
MariusStorhaug Sep 23, 2023
ab8c260
Fix discon var
MariusStorhaug Sep 23, 2023
577e295
ok changes
MariusStorhaug Sep 23, 2023
7f035e1
Create class for config
MariusStorhaug Sep 23, 2023
fbbac81
Fixes
MariusStorhaug Sep 23, 2023
fedc4b7
Additional changes :D
MariusStorhaug Sep 23, 2023
1b6d319
Move data import to module loader
MariusStorhaug Sep 23, 2023
7fb2f53
Fix loader
MariusStorhaug Sep 23, 2023
f9f9e6e
Fix loader
MariusStorhaug Sep 23, 2023
62f1c62
Fix path
MariusStorhaug Sep 23, 2023
95113b5
Fix Path
MariusStorhaug Sep 23, 2023
3ee357f
Fix path
MariusStorhaug Sep 23, 2023
04abb19
Fix pathing
MariusStorhaug Sep 23, 2023
3352b72
Fix pathing
MariusStorhaug Sep 23, 2023
8afa76b
Add Config as a hashtable again
MariusStorhaug Sep 23, 2023
ea634f8
Doc update
MariusStorhaug Sep 23, 2023
5cf705e
Fix :)
MariusStorhaug Sep 23, 2023
672eec0
Moiving the init file
MariusStorhaug Sep 23, 2023
23db3b1
Use PSCustomObject copy :)
MariusStorhaug Sep 23, 2023
1558c73
temp
MariusStorhaug Sep 23, 2023
2fe6551
fix
MariusStorhaug Sep 23, 2023
13324e8
Update to loader
MariusStorhaug Sep 24, 2023
19435b9
Update docs
MariusStorhaug Sep 24, 2023
240ba7c
Added docs
MariusStorhaug Sep 24, 2023
f93e72c
remove template checking function
MariusStorhaug Sep 24, 2023
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
81 changes: 81 additions & 0 deletions .github/workflows/Test.GitHub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Test [GitHub]

on:
workflow_dispatch:

permissions: write-all

jobs:
TestGitHub:
name: Test GitHub
if: always()
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
env:
GH_TOKEN: ${{ github.token }} # Used for GitHub CLI authentication
steps:
- name: Test Authentication using auto PAT
if: always()
shell: pwsh
run: |
Write-Output '::group::[Debug info] - Environment variables'
$env:GITHUB_REPOSITORY_NAME = $env:GITHUB_REPOSITORY.Split('/')[1]
Get-ChildItem Env:
Write-Output '::endgroup::'

Write-Output '::group::[Debug info] - File structure'
Write-Verbose "Current directory: $((Get-Location).Path)" -Verbose
Write-Verbose "------------------------------------" -Verbose
Write-Verbose "Current directory content:" -Verbose
Get-ChildItem -Path . -Recurse | Select-Object -ExpandProperty FullName | Sort-Object
Write-Output '::endgroup::'

Write-Output '::group::Install-Module -Name GitHub -Verbose -Force'
Install-Module -Name GitHub -Verbose -Force
Write-Output '::endgroup::'

Write-Output '::group::Get-GitHubConfig'
Get-GitHubConfig
Write-Output '::endgroup::'

Write-Output '::group::Get-GitHubWorkflow -Repo $env:GITHUB_REPOSITORY_NAME -Owner $env:GITHUB_REPOSITORY_OWNER -Verbose'
Get-GitHubWorkflow -Repo $env:GITHUB_REPOSITORY_NAME -Owner $env:GITHUB_REPOSITORY_OWNER -Verbose
Write-Output '::endgroup::'

- name: Test Authentication using Specific PAT
if: always()
shell: pwsh
run: |
Write-Output '::group::[Debug info] - Environment variables'
$env:GITHUB_REPOSITORY_NAME = $env:GITHUB_REPOSITORY.Split('/')[1]
Get-ChildItem Env:
Write-Output '::endgroup::'

Write-Output '::group::[Debug info] - File structure'
Write-Verbose "Current directory: $((Get-Location).Path)" -Verbose
Write-Verbose "------------------------------------" -Verbose
Write-Verbose "Current directory content:" -Verbose
Get-ChildItem -Path . -Recurse | Select-Object -ExpandProperty FullName | Sort-Object
Write-Output '::endgroup::'

Write-Output '::group::Install-Module -Name GitHub -Verbose -Force'
Install-Module -Name GitHub -Verbose -Force
Write-Output '::endgroup::'

Write-Output '::group::Get-GitHubConfig'
Get-GitHubConfig
Write-Output '::endgroup::'

Write-Output '::group::Connect-GitHubAccount -AccessToken $env:GH_TOKEN -Verbose'
Connect-GitHubAccount -AccessToken $env:GH_TOKEN -Verbose
Write-Output '::endgroup::'

Write-Output '::group::Get-GitHubConfig'
Get-GitHubConfig
Write-Output '::endgroup::'

Write-Output '::group::Get-GitHubWorkflow -Repo $env:GITHUB_REPOSITORY_NAME -Owner $env:GITHUB_REPOSITORY_OWNER -Verbose'
Get-GitHubWorkflow -Repo $env:GITHUB_REPOSITORY_NAME -Owner $env:GITHUB_REPOSITORY_OWNER -Verbose
Write-Output '::endgroup::'
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,34 @@ To dive into the world of GitHub automation with PowerShell, follow these steps:

1. **Installation**: Download and install the GitHub PowerShell module from the provided link or the PowerShell Gallery.

2. **Authentication**: Authenticate using your GitHub credentials or access tokens to begin executing commands.
```powershell
Install-Module -Name GitHub -Force -AllowClobber
```

3. **Command Exploration**: Familiarize yourself with the available cmdlets using the module's comprehensive documentation or inline help.
1. **Authentication**: Authenticate using your GitHub credentials or access tokens to begin executing commands.

4. **Sample Scripts**: Check out sample scripts and usage patterns to jumpstart your automation tasks on GitHub.
Logging in using device flow:
```powershell
Connect-GitHubAccount

Please visit: https://github.com/login/device
and enter code: ABCD-1234
Successfully authenticated!
```

Logging in using PAT token:
```powershell
>_ Connect-GitHubAccount -AccessToken 'ghp_abcdefghklmnopqrstuvwxyz123456789123'
>_
```

2. **Command Exploration**: Familiarize yourself with the available cmdlets using the module's comprehensive documentation or inline help.

```powershell
Get-Command -Module GitHub
```

3. **Sample Scripts**: Check out sample scripts and usage patterns to jumpstart your automation tasks on GitHub.

## More Information & Resources

Expand Down
Binary file added media/github.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions src/GitHub/GitHub.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Write-Verbose "Initializing GitHub module..." -Verbose

$script:Config = $script:ConfigTemplate | ConvertTo-Json -Depth 100 | ConvertFrom-Json
Initialize-SecretVault -Name $script:SecretVault.Name -Type $script:SecretVault.Type
Restore-GitHubConfig

if (-not [string]::IsNullOrEmpty($env:GH_TOKEN)) {
Write-Verbose 'Logging on using GH_TOKEN'
Connect-GitHubAccount -AccessToken $env:GH_TOKEN
}
if (-not [string]::IsNullOrEmpty($env:GITHUB_TOKEN)) {
Write-Verbose 'Logging on using GITHUB_TOKEN'
Connect-GitHubAccount -AccessToken $env:GITHUB_TOKEN
}
54 changes: 39 additions & 15 deletions src/GitHub/GitHub.psm1
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
[Cmdletbinding()]
param()

$sciptName = $MyInvocation.MyCommand.Name
$scriptName = $MyInvocation.MyCommand.Name
Write-Verbose "[$scriptName] - Importing module"

Write-Verbose "[$sciptName] Importing subcomponents"
$folders = 'classes', 'private', 'public'
# Import everything in these folders
#region - Importing data files
Write-Verbose "[$scriptName] - [data] - Processing folder"
$dataFolder = (Join-Path $PSScriptRoot 'data')
Write-Verbose "[$scriptName] - [data] - [$dataFolder]"
Get-ChildItem -Path "$dataFolder" -Recurse -Force -Include '*.psd1' | ForEach-Object {
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Importing data file"
New-Variable -Name $_.BaseName -Value (Import-PowerShellDataFile -Path $_.FullName) -Force
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Done"
}
Write-Verbose "[$scriptName] - [data] - Done"
#endregion - Importing datas

#region - Importing script files
$folders = 'init', 'classes', 'private', 'public'
foreach ($folder in $folders) {
Write-Verbose "[$sciptName] - Processing folder [$folder]"
Write-Verbose "[$scriptName] - [$folder] - Processing folder"
$folderPath = Join-Path -Path $PSScriptRoot -ChildPath $folder
Write-Verbose "[$sciptName] - [$folderPath]"
if (Test-Path -Path $folderPath) {
Write-Verbose "[$sciptName] - [$folderPath] - Getting all files"
$files = $null
$files = Get-ChildItem -Path $folderPath -Include '*.ps1', '*.psm1' -Recurse
# dot source each file
$files = Get-ChildItem -Path $folderPath -Include '*.ps1', '*.psm1' -Recurse | Sort-Object -Property FullName
foreach ($file in $files) {
Write-Verbose "[$sciptName] - [$folderPath] - [$($file.Name)] - Importing"
Import-Module $file
Write-Verbose "[$sciptName] - [$folderPath] - [$($file.Name)] - Done"
Write-Verbose "[$scriptName] - [$folder] - [$($file.Name)] - Importing script file"
Import-Module $file -Verbose:$false
Write-Verbose "[$scriptName] - [$folder] - [$($file.Name)] - Done"
}
}
Write-Verbose "[$scriptName] - [$folder] - Done"
}
#endregion - Importing script files

#region - Importing root script files
Write-Verbose "[$scriptName] - [PSModuleRoot] - Processing folder"
Get-ChildItem -Path $PSScriptRoot -Filter '*.ps1' | ForEach-Object {
Write-Verbose "[$scriptName] - [PSModuleRoot] - [$($_.Name)] - Importing root script files"
Import-Module $_ -Verbose:$false
Write-Verbose "[$scriptName] - [PSModuleRoot] - [$($_.Name)] - Done"
}
Write-Verbose "[$scriptName] - [Root] - Done"
#endregion - Importing root script files

#region Export module members
$foldersToProcess = Get-ChildItem -Path $PSScriptRoot -Directory | Where-Object -Property Name -In $folders
$moduleFiles = $foldersToProcess | Get-ChildItem -Include '*.ps1' -Recurse -File -Force
$functions = $moduleFiles.BaseName
$Param = @{
$param = @{
Function = $functions
Variable = ''
Cmdlet = ''
Expand All @@ -35,4 +56,7 @@ $Param = @{

Write-Verbose 'Exporting module members'

Export-ModuleMember @Param
Export-ModuleMember @param
#endregion Export module members

Write-Verbose "[$scriptName] - Done"
10 changes: 0 additions & 10 deletions src/GitHub/classes/Data/Config.ps1

This file was deleted.

7 changes: 0 additions & 7 deletions src/GitHub/classes/Data/SecretVault.ps1

This file was deleted.

8 changes: 8 additions & 0 deletions src/GitHub/data/Auth.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@{
GitHubApp = @{
ClientID = 'Iv1.f26b61bc99e69405' # $script:Auth.GitHubApp.ClientID
}
OAuthApp = @{
ClientID = '7204ae9b0580f2cb8288' # $script:Auth.OAuthApp.ClientID
}
}
7 changes: 7 additions & 0 deletions src/GitHub/data/SecretVault.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@{
Name = 'GitHub' # $script:SecretVault.Name
Type = 'Microsoft.PowerShell.SecretStore' # $script:SecretVault.Type
Secret = @{
Name = 'Config' # $script:SecretVault.Secret.Name
}
}
45 changes: 45 additions & 0 deletions src/GitHub/en_US/about_Auth.help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
TOPIC
about_Auth

SHORT DESCRIPTION
Describes the authentication methods provided in the PowerShell module for interacting with GitHub's REST API.

LONG DESCRIPTION
This module provides several functions to manage authentication for GitHub's REST API. There are primarily two ways to authenticate:

1. GitHub Device Flow: This method prompts the user to visit a specific URL on GitHub where they must enter a user verification code. Once this is done, the module retrieves the necessary access tokens to make authenticated API requests.

2. Personal Access Token: The user can provide a Personal Access Token (PAT) to authenticate. This PAT allows the module to interact with the API on the user's behalf. The module can automatically use environment variables `GH_TOKEN` or `GITHUB_TOKEN` if they are present.

The module also provides functionalities to refresh the access token and to disconnect or logout from the GitHub account.

EXAMPLES
Example 1:
Connect-GitHubAccount
Connects to GitHub using the device flow login. You'll be prompted to visit a specific URL on GitHub and enter the provided user verification code.

Example 2:
Connect-GitHubAccount -AccessToken 'ghp_####'
Connects to GitHub using a provided personal access token (PAT).

Example 3:
Connect-GitHubAccount -Refresh
Refreshes the access token for continued session validity.

Example 4:
Disconnect-GitHubAccount
Disconnects from GitHub and removes the current GitHub configuration.

Example 5 (Automatic login using environment variables):
If either the `GH_TOKEN` or `GITHUB_TOKEN` environment variables are set, the module will automatically use them for authentication during module initialization.

KEYWORDS
GitHub, Authentication, Device Flow, Personal Access Token, PowerShell, REST API

SEE ALSO
For more information on the Device Flow visit:
- https://docs.github.com/en/apps/creating-github-apps/writing-code-for-a-github-app/building-a-cli-with-a-github-app

For information about scopes and other authentication methods on GitHub:
- https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/scopes-for-oauth-apps
- https://docs.github.com/en/rest/overview/other-authentication-methods#authenticating-for-saml-sso
Loading