forked from rajbos/github-fork-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-fork.ps1
107 lines (79 loc) · 3.42 KB
/
update-fork.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
# example calls:
# .\update-fork.ps1 -orgName "rajbos-actions" -userName "xxx" -PAT $env:GitHubPAT $issueTitle "Parent repository for [rajbos/azure-docs] has updates available"
param (
[string] $orgName,
[string] $userName,
[string] $PAT,
[string] $issuesRepository,
[string] $issueTitle,
[int] $issueId,
[string] $repoName
)
# include local library code
. $PSScriptRoot\github-calls.ps1
function ParseIssueTitle {
param (
[string] $issueTitle
)
$start = $issueTitle.IndexOf("[")+1;
$end = $issueTitle.IndexOf("]");
$fork = $issueTitle.Substring($start, $end-$start)
Write-Host "Found fork repo name to update [$fork]"
return $fork
}
$sourceDirectory = "source"
function UpdateFork {
param (
[string] $fork,
[string] $PAT
)
$forkUrl = GetForkCloneUrl -fork $fork -PAT $PAT
# set user settings
git config --global user.email "noreply@githubupdater.com"
git config --global user.name "GitHub Fork Updater"
# create new temp dir to hold the fork
New-Item -ItemType Directory $sourceDirectory
Set-Location $sourceDirectory
git clone $forkUrl .
$parent = GetParentInfo -fork $fork
Write-Host "Found forks parent with url [$($parent.parentUrl)]"
# add remote to the parent
git remote add github $parent.parentUrl
# fetch the changes from the parent
Write-Host "Fetching changes from parent repo"
git fetch github $parent.parentDefaultBranch --tags
# make sure you are on the right branch
Write-Host "Pulling all changes from the parent on branch [$($parent.parentDefaultBranch)]"
git checkout $parent.parentDefaultBranch
# merge in any changes from the branch
Write-Host "Merging changes from parent repo"
git merge github/$($parent.parentDefaultBranch) --ff
# push the changes back to your repo
Write-Host "Pushing changes back to fork"
git push origin $parent.parentDefaultBranch --tags
Write-Host "Completed fork update"
}
function Main {
param (
[string] $issueTitle,
[string] $PAT,
[int] $issueId,
[string] $issuesRepository
)
Write-Host "Starting the update for issue with title [$issueTitle] having number [$issueId] on repository [$issuesRepository] and a PAT that has length [$($PAT.Length)]"
$workflowRunUrl = "$($env:GITHUB_SERVER_URL)/$($env:GITHUB_REPOSITORY)/actions/runs/$($env:GITHUB_RUN_ID)"
Write-Host "Found workflowRunUrl: [$workflowRunUrl]"
$fork = ParseIssueTitle -issueTitle $issueTitle
AddCommentToIssue -number $issueId -message "Updating the fork with the incoming changes from the parent repository through [update-workflow]($workflowRunUrl)" -repoName $issuesRepository -PAT $PAT
UpdateFork -fork $fork -PAT $PAT
Write-Host "Cleaning up"
Set-Location ..
Remove-Item -Force -Recurse $sourceDirectory
# make sure we are back where we started (for easier local testing)
Set-Location $PSScriptRoot
AddCommentToIssue -number $issueId -message "Fork has been updated" -repoName $issuesRepository -PAT $PAT
CloseIssue -number $issueId -issuesRepositoryName $issuesRepository -PAT $PAT
}
# uncomment for local testing
#$issueTitle = "Parent repository for [rajbos/pickles] has updates available"; $PAT=$env:GitHubPAT; $repoName = "rajbos/github-fork-updater"; $issueId = 24
Main -issueTitle $issueTitle -PAT $PAT -issueId $issueId -issuesRepository $issuesRepository