Skip to content

Commit 2bb9924

Browse files
🚀 [Feature]: Make Script optional, and support consecutive runs (#4)
## Description - `Script` is no longer required. - Update script to check if GitHub module is installed and imported, to avoid doing that if it already is installed or imported. - Add a tests for running without a token, running with version and running consecutive runs. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 8b33aa1 commit 2bb9924

File tree

4 files changed

+136
-17
lines changed

4 files changed

+136
-17
lines changed

.github/workflows/Action-Test.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,56 @@ jobs:
1616
ActionTestBasic:
1717
name: Action-Test - [Basic]
1818
runs-on: ubuntu-latest
19+
steps:
20+
# Need to check out as part of the test, as its a local action
21+
- name: Checkout repo
22+
uses: actions/checkout@v4
23+
24+
- name: Action-Test
25+
uses: ./
26+
27+
ActionTestWithScript:
28+
name: Action-Test - [WithScript]
29+
runs-on: ubuntu-latest
30+
steps:
31+
# Need to check out as part of the test, as its a local action
32+
- name: Checkout repo
33+
uses: actions/checkout@v4
34+
35+
- name: Action-Test
36+
uses: ./
37+
with:
38+
Verbose: true
39+
Script: |
40+
LogGroup "My group" {
41+
"This is a group"
42+
}
43+
44+
ActionTestWithToken:
45+
name: Action-Test - [WithToken]
46+
runs-on: ubuntu-latest
47+
steps:
48+
# Need to check out as part of the test, as its a local action
49+
- name: Checkout repo
50+
uses: actions/checkout@v4
51+
52+
- name: Action-Test
53+
uses: ./
54+
with:
55+
Token: ${{ github.token }}
56+
Verbose: true
57+
Script: |
58+
LogGroup "Get-GitHubZen" {
59+
Get-GitHubZen
60+
}
61+
62+
LogGroup "Get-GitHubOctocat" {
63+
Get-GitHubOctocat
64+
}
65+
66+
ActionTestWithVersion:
67+
name: Action-Test - [WithVersion]
68+
runs-on: ubuntu-latest
1969
steps:
2070
# Need to check out as part of the test, as its a local action
2171
- name: Checkout repo
@@ -25,7 +75,51 @@ jobs:
2575
uses: ./
2676
with:
2777
Token: ${{ github.token }}
78+
Verbose: true
79+
Version: 0.3.118
2880
Script: |
2981
LogGroup "Get-GitHubZen" {
3082
Get-GitHubZen
3183
}
84+
85+
LogGroup "Get-GitHubOctocat" {
86+
Get-GitHubOctocat
87+
}
88+
89+
ActionTestConsecutive:
90+
name: Action-Test - [Consecutive]
91+
runs-on: ubuntu-latest
92+
steps:
93+
# Need to check out as part of the test, as its a local action
94+
- name: Checkout repo
95+
uses: actions/checkout@v4
96+
97+
- name: Action-Test 1
98+
uses: ./
99+
with:
100+
Token: ${{ github.token }}
101+
Verbose: true
102+
Script: |
103+
LogGroup "Get-GitHubZen" {
104+
Get-GitHubZen
105+
}
106+
107+
- name: Action-Test 2
108+
uses: ./
109+
with:
110+
Token: ${{ github.token }}
111+
Verbose: true
112+
Script: |
113+
LogGroup "Get-GitHubOctocat" {
114+
Get-GitHubOctocat
115+
}
116+
117+
- name: Action-Test 3
118+
uses: ./
119+
with:
120+
Token: ${{ github.token }}
121+
Verbose: true
122+
Script: |
123+
LogGroup "Get-GitHubRateLimit" {
124+
Get-GitHubRateLimit
125+
}

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ For more information on the available functions and automatic loaded variables,
1010

1111
| Name | Description | Required | Default |
1212
| - | - | - | - |
13-
| `Script` | The script to run | true | |
14-
| `Token` | The GitHub token to use | false | ${{ github.token }} |
15-
| `Debug` | Enable debug output | false | 'false' |
16-
| `Verbose` | Enable verbose output | false | 'false' |
17-
| `Version` | Specifies the version of the resource to be returned. The value can be an exact version or a version range using the NuGet versioning syntax. | false | 'latest' |
18-
| `Prerelease` | Allow prerelease versions if available | false | 'false' |
19-
| `WorkingDirectory` | The working directory where the script will run from | false | ${{ github.workspace }} |
13+
| `Script` | The script to run | false | |
14+
| `Token` | The GitHub token to use | false | `${{ github.token }}` |
15+
| `Debug` | Enable debug output | false | `'false'` |
16+
| `Verbose` | Enable verbose output | false | `'false'` |
17+
| `Version` | Specifies the version of the resource to be returned. The value must be an exact version. | false | |
18+
| `Prerelease` | Allow prerelease versions if available | false | `'false'` |
19+
| `WorkingDirectory` | The working directory where the script will run from | false | `${{ github.workspace }}` |
2020

2121
### Examples
2222

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ branding:
88
inputs:
99
Script:
1010
description: The script to run.
11-
required: true
11+
required: false
1212
Token:
1313
description: The GitHub token to use.
1414
required: false

scripts/main.ps1

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
[CmdletBinding()]
22
param()
33

4-
$params = @{
5-
Name = 'GitHub'
6-
Repository = 'PSGallery'
7-
TrustRepository = $true
8-
Prerelease = $env:GITHUB_ACTION_INPUT_Prerelease -eq 'true'
4+
$Name = 'GitHub'
5+
$Version = [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Version) ? $null : $env:GITHUB_ACTION_INPUT_Version
6+
$Prerelease = $env:GITHUB_ACTION_INPUT_Prerelease -eq 'true'
7+
8+
$alreadyInstalled = Get-InstalledPSResource -Name $Name -ErrorAction SilentlyContinue
9+
if ($Version) {
10+
Write-Verbose "Filtering by version: $Version"
11+
$alreadyInstalled = $alreadyInstalled | Where-Object Version -EQ $Version
12+
}
13+
if ($Prerelease) {
14+
Write-Verbose "Filtering by prerelease"
15+
$alreadyInstalled = $alreadyInstalled | Where-Object Prerelease -EQ $Prerelease
916
}
10-
if (-not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Version)) {
11-
$params['Version'] = $env:GITHUB_ACTION_INPUT_Version
17+
Write-Verbose "Already installed:"
18+
Write-Verbose ($alreadyInstalled | Format-Table | Out-String)
19+
if (-not $alreadyInstalled) {
20+
Write-Verbose "Installing module. Name: [$Name], Version: [$Version], Prerelease: [$Prerelease]"
21+
$params = @{
22+
Name = $Name
23+
Repository = 'PSGallery'
24+
TrustRepository = $true
25+
Prerelease = $Prerelease
26+
}
27+
if ($Version) {
28+
$params['Version'] = $Version
29+
}
30+
Install-PSResource @params
31+
}
32+
33+
$alreadyImported = Get-Module -Name $Name
34+
Write-Verbose "Already imported:"
35+
Write-Verbose ($alreadyImported | Format-Table | Out-String)
36+
if (-not $alreadyImported) {
37+
Write-Verbose "Importing module: $Name"
38+
Import-Module -Name $Name
1239
}
13-
Install-PSResource @params
14-
Import-Module -Name 'GitHub' -Force

0 commit comments

Comments
 (0)