Skip to content

Commit 82e08a1

Browse files
Martin EvansMartin Evans
authored andcommitted
Initial offerring incl readme
1 parent 3f53b41 commit 82e08a1

File tree

2 files changed

+324
-0
lines changed

2 files changed

+324
-0
lines changed

GitModule.psm1

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
function Create-GitHubHeaders(){
2+
$token= GitHubPersonalAccessToken
3+
$headers = @{}
4+
$headers.Add("Authorization","Bearer $token")
5+
return $headers
6+
}
7+
8+
<#
9+
.SYNOPSIS
10+
New-GitHubRepo
11+
12+
.DESCRIPTION
13+
Creates a new GitHub repo on a user account
14+
15+
.LINK
16+
https://developer.github.com/v3/repos/#create
17+
18+
.PARAMETER repoName
19+
The name of the repo to be created
20+
21+
.PARAMETER private
22+
Creates the repo as private when set
23+
24+
.EXAMPLE
25+
PS C:\> New-GitHubRepo -repoName "MyRepo"
26+
Creates a new public repo on GitHub named "MyRepo", for the specified user
27+
28+
.EXAMPLE
29+
PS C:\> New-GitHubRepo -repoName "MyRepo" -private
30+
Creates a new private repo on GitHub named "MyRepo", for the specified user
31+
32+
#>
33+
function New-GitHubRepo(){
34+
35+
[CmdletBinding()]
36+
param(
37+
[Parameter(Mandatory=$true)]
38+
[String]$repoName,
39+
[Parameter()]
40+
[switch]$private = $false
41+
)
42+
43+
if(-NOT(Token-Present))
44+
{
45+
return;
46+
}
47+
48+
$body= @{
49+
name="$repoName";
50+
description="test description";
51+
homepage="https://github.com";
52+
private= $private;
53+
gitignore_template = "VisualStudio"
54+
}
55+
56+
$j = (ConvertTo-Json $body)
57+
$headers = Create-GitHubHeaders
58+
59+
Invoke-WebRequest -Uri "https://api.github.com/user/repos" -Method Post -Body $j -Headers $headers -UseBasicParsing
60+
}
61+
62+
63+
<#
64+
.SYNOPSIS
65+
Remove-GitHubRepo
66+
67+
.DESCRIPTION
68+
deletes a GitHub repo
69+
70+
.LINK
71+
https://developer.github.com/v3/repos/#delete-a-repository
72+
73+
.PARAMETER repoName
74+
The name of the repo to be deleted
75+
76+
.EXAMPLE
77+
PS C:\> Remove-GitHubRepo -repoName "MyRepo"
78+
Creates a new public repo on GitHub named "MyRepo", for the specified user
79+
80+
81+
#>
82+
function Remove-GitHubRepo(){
83+
84+
[CmdletBinding()]
85+
param(
86+
[Parameter(Mandatory=$true)]
87+
[String]$repoName
88+
)
89+
90+
if(-NOT(Token-Present))
91+
{
92+
return;
93+
}
94+
95+
$headers = Create-GitHubHeaders
96+
97+
$accountName = Get-GitHubPersonalAccountName
98+
99+
Invoke-WebRequest -Uri "https://api.github.com/repos/$accountName/$repoName" -Method DELETE -Headers $headers -UseBasicParsing
100+
101+
}
102+
103+
104+
<#
105+
.SYNOPSIS
106+
Add-Collaborator
107+
108+
.DESCRIPTION
109+
Adds GitHubUsers as collaborrators to an existing GitHub repository
110+
111+
.LINK
112+
https://developer.github.com/v3/repos/collaborators/#add-user-as-a-collaborator
113+
114+
.PARAMETER repoName
115+
The name of the repo to be deleted
116+
117+
.PARAMETER gitHubUserNames
118+
A list of gitHub usernames to be added as collaborator
119+
120+
121+
.EXAMPLE
122+
PS C:\> Add-Collaborator -repoName "MyRepo" -gitHubUserName "johnsmith"
123+
Adds GitHub username JohnSmith as the collaborator to the repo MyRepo
124+
125+
126+
#>
127+
function Add-Collaborator(){
128+
129+
[CmdletBinding()]
130+
param(
131+
[Parameter(Mandatory=$true)]
132+
[String] $repoName,
133+
[Parameter(Mandatory=$true)]
134+
[String[]] $gitHubUserNames
135+
)
136+
137+
if(-NOT(Token-Present))
138+
{
139+
return;
140+
}
141+
142+
$headers = Create-GitHubHeaders
143+
$headers.Add("Content-Length",0)
144+
145+
$accountName = Get-GitHubPersonalAccountName
146+
147+
foreach ($username in $gitHubUserNames)
148+
{
149+
150+
$url = "https://api.github.com/repos/$accountName/$repoName/collaborators/$gitHubName"
151+
152+
write-host "Adding $username as collaborator to repo $repoName"
153+
154+
Invoke-WebRequest -Uri $url -Method PUT -Headers $headers -UseBasicParsing
155+
}
156+
}
157+
158+
159+
<#
160+
.SYNOPSIS
161+
Set-GitHubPersonalAccessToken
162+
163+
.DESCRIPTION
164+
Saves the supplied personal access token for use in GitHub API interactions
165+
166+
.LINK
167+
https://github.com/settings/tokens
168+
169+
170+
.PARAMETER token
171+
The Personal Access token value supplied by GitHub to be used to identify the user account when perfoming API requests
172+
173+
.EXAMPLE
174+
PS C:\> Set-GitHubPersonalAccessToken -token "abjkhi121121kjkjk"
175+
176+
#>
177+
function Set-GitHubPersonalAccessToken(){
178+
179+
[CmdletBinding()]
180+
param(
181+
[Parameter(Mandatory=$true)]
182+
[String] $token
183+
)
184+
185+
[Environment]::SetEnvironmentVariable("GitHubPersonalAccessToken",$token,"User")
186+
}
187+
188+
<#
189+
.SYNOPSIS
190+
Clear-GitHubPersonalAccessToken
191+
192+
.DESCRIPTION
193+
rtemoves the saved personal access token for use in GitHub API interactions
194+
195+
.LINK
196+
https://github.com/settings/tokens
197+
198+
.PARAMETER token
199+
The Personal Access token value supplied by GitHub to be used to identify the user account when perfoming API requests
200+
201+
.EXAMPLE
202+
PS C:\> Clear-GitHubPersonalAccessToken
203+
204+
#>
205+
206+
function Clear-GitHubPersonalAccessToken(){
207+
[Environment]::SetEnvironmentVariable("GitHubPersonalAccessToken",$null,"User")
208+
}
209+
210+
211+
<#
212+
.SYNOPSIS
213+
Set-GitHubPersonalAccountName
214+
215+
.DESCRIPTION
216+
Saves the supplied personal GitHub profile name for use in GitHub API interactions
217+
218+
.LINK
219+
https://github.com/settings/profile
220+
221+
.PARAMETER userName
222+
The profile name to be used to identify the user account when perfoming API requests
223+
224+
.EXAMPLE
225+
PS C:\> Set-GitHubPersonalAccessToken -token "abjkhi121121kjkjk"
226+
227+
#>
228+
function Set-GitHubPersonalAccountName(){
229+
230+
[CmdletBinding()]
231+
param(
232+
[Parameter(Mandatory=$true)]
233+
[String] $userName
234+
)
235+
236+
[Environment]::SetEnvironmentVariable("GitHubPersonalAccountName",$userName,"User")
237+
}
238+
239+
240+
<#
241+
.SYNOPSIS
242+
Clear-GitHubPersonalAccountName
243+
244+
.DESCRIPTION
245+
removes the profile name used in GitHub API interactions
246+
247+
.LINK
248+
https://github.com/settings/profile
249+
250+
251+
.EXAMPLE
252+
PS C:\> Clear-GitHubPersonalAccountName
253+
254+
#>
255+
256+
function Clear-GitHubPersonalAccountName(){
257+
[Environment]::SetEnvironmentVariable("GitHubPersonalAccountName",$null,"User")
258+
}
259+
260+
function Get-GitHubPersonalAccessToken(){
261+
[Environment]::GetEnvironmentVariable("GitHubPersonalAccessToken","User")
262+
}
263+
264+
function Get-GitHubPersonalAccountName(){
265+
[Environment]::GetEnvironmentVariable("GitHubPersonalAccountName","User")
266+
}
267+
268+
function Token-Present(){
269+
270+
$ret = $true
271+
272+
if((Get-GitHubPersonalAccessToken) -eq $null){
273+
Write-Host "Personal Access Token NOT set" -ForegroundColor DarkYellow
274+
Write-Host "Create a personal access token at https://github.com/settings/tokens then use Set-GitHubPersonalAccessToken to save it as an environment variable for future use."
275+
276+
$ret = $false
277+
}
278+
279+
280+
if((Get-GitHubPersonalAccountName) -eq $null){
281+
Write-Host "GitHub username NOT set" -ForegroundColor DarkYellow
282+
Write-Host "Use Set-GitHubPersonalAccountName to save it as an environment variable for future use."
283+
284+
$ret = $false
285+
}
286+
287+
288+
return $ret
289+
290+
}
291+
292+
Token-Present
293+
294+
Set-Alias nghr New-GitHubRepo -Description "Creates new GitHub repo"
295+
Set-Alias rghr Remove-GitHubRepo-GitHubRepo -Description "Deletes GitHub repo"
296+
297+
export-modulemember -alias * -function New-GitHubRepo,
298+
Remove-GitHubRepo,
299+
Add-Collaborator,
300+
New-RepoWithCollaborator,
301+
Set-GitHubPersonalAccessToken,
302+
Set-GitHubPersonalAccountName,
303+
Clear-GitHubPersonalAccessToken

README.MD

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# GitHubPsModule #
2+
3+
This repo contains a Powershell Module to assisit in interacting with the GitHub API.
4+
5+
## The problem ##
6+
I needed to be able to quickly create a git hub repo and other github users as a collaborator. Manually, this was an interruption to my workflow.
7+
I also wanted the opportunity to learn more about the GitHub Api : https://developer.github.com/v3/ and brush up on my powershell :-)
8+
9+
## GitModule.psm1 ##
10+
This is a simple powershell module that may be referenced and invoked when needed to create and delete personal repositories. For wexisting repositories, you'll be able to add collaborators.
11+
12+
13+
When the Module is referenced, you'll be prompted to provide your username and a personal access token that you'll have to generate via your GitHub account at https://github.com/settings/tokens
14+
You'll have to supply these criteria before the module will work. Once supplied you'll no longer be prompted.
15+
16+
For a full list of available commands offered by the module, from a PS Command line, run the command
17+
18+
> Get-Command -Module GitModule
19+
20+
21+

0 commit comments

Comments
 (0)