-
Notifications
You must be signed in to change notification settings - Fork 24
/
Create-ActionsPRs.ps1
151 lines (129 loc) · 4.1 KB
/
Create-ActionsPRs.ps1
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
function CreatePullRequestsFromFile {
param (
[string]
$FileName,
[string] $CommitMessage,
[string] $PRBody,
[string] $BranchName
)
$repos = Get-Content $FileName
# Clone repos
foreach ($repo in $repos)
{
Set-Location $PSScriptRoot
$chunks = $repo.split("/")
$repo_nwo = $chunks[3] + "/" + $chunks[4]
gh repo clone $repo_nwo $repo_nwo
Set-Location $repo_nwo
git checkout -b $BranchName
if ((Get-ChildItem .github -ErrorAction SilentlyContinue).Count -eq 0) {
mkdir .github
}
if ((Get-ChildItem .github/workflows -ErrorAction SilentlyContinue).Count -eq 0) {
mkdir .github/workflows
}
copy-item "$PSScriptRoot/workflows" -destination ".github/" -Recurse
git add -A
git commit -a -m $CommitMessage
gh pr create -b $PRBody -t $CommitMessage
Set-Location ../..
rm -rf $repo_nwo
}
Set-Location $PSScriptRoot
}
function CreatePullRequestForRepositories {
param (
# An Array of Repository objects as returned by the GitHub API
[array] $Repositories,
[string] $CommitMessage,
[string] $PRBody,
[string] $BranchName
)
foreach ($repo in $repos)
{
Set-Location $PSScriptRoot
gh repo clone $repo.full_name $repo.full_name
Set-Location $repo.full_name
git checkout -b $BranchName
copy-item "$PSScriptRoot/workflows" -Destination ".github/" -Recurse
git add -A
git commit -a -m $CommitMessage
gh pr create -b $PRBody -t $CommitMessage
}
}
function getAuthenticationToken {
$token = Get-ChildItem Env:\GITHUB_TOKEN -ErrorAction SilentlyContinue
if ($null -eq $token) {
$envFile = Get-Content .env
foreach ($line in $envFile) {
if ($line.startsWith("GITHUB_TOKEN=")) {
$token = $line.Substring($line.IndexOf("=") + 1)
}
}
}
return $token
}
function getHeaders {
$token = getAuthenticationToken
$authheader = "Bearer " + $token
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization",$authheader)
return $headers
}
function GetReposFromOrganization {
param (
[string] $Organization
)
$url = "https://api.github.com/orgs/$Organization/repos"
$headers = getHeaders
$repos = Invoke-RestMethod -Uri $url -Method Get -Headers $headers -ResponseHeadersVariable 'response'
if ($null -ne $response) {
if ($null -ne $response.Link) {
# Parse the Link header to paginate
$response.Link[0] -match '.*?page=([0-9]*)>; rel="last"'
$pages = [int]($Matches[1])
# Paginate
for ($page = 2; $page -lt $pages; $page++) {
$pageUrl = $url + "?page=" + $page
$repos += Invoke-RestMethod -Uri $pageUrl -Method Get -Headers $headers
}
}
}
return $repos
}
function FilterForSupportedLanguages($repos) {
$languages = '"C"', '"C++"', '"Go"', '"C#"', '"Python"', '"Java"', '"JavaScript"', '"TypeScript"'
$filteredRepos = @()
foreach ($repo in $repos) {
if ($repo.GetType().Name -eq "PSCustomObject") {
$url = $repo.url + "/languages"
$headers = getHeaders
$langs = Invoke-WebRequest -Uri $url -Method Get -Headers $headers
$value = $langs.Content
if ($value -ne "{}") {
if (ContainsAny -string $value -values $languages) {
$filteredRepos += $repo
}
}
}
}
return $filteredRepos
}
function ContainsAny {
param([string]$string, [array]$values)
foreach ($value in $values) {
if ($string.Contains($value)) {
return $true;
}
}
return $false;
}
function CreatePullRequestsForCodeQLLanguages {
param (
[string] $Organization,
[string] $CommitMessage = "Add CodeQL Analysis workflow",
[string] $PRBody = "Adds an Actions workflow which enables CodeQL analysis and will perform static analysis security testing on your code. You'll see results show up in pull requests and/or the Security tab. "
)
$repos = FilterForSupportedLanguages(GetReposFromOrganization -Organization $Organization);
CreatePullRequestForRepositories -Repositories $repos -CommitMessage $CommitMessage -PRBody $PRBody -BranchName codeql
}