-
Notifications
You must be signed in to change notification settings - Fork 3
/
Update-Repository.ps1
98 lines (84 loc) · 3.44 KB
/
Update-Repository.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
$version = "0.0.1"
$isAction = $null -ne $Env:GITHUB_WORKSPACE
# Update Repository from Template Repository
Write-Host "Update repo from template"
Write-Host "Version $version"
# Grab the template repo JSON
$templateAuthorName = "nattadasu"
$templateRepoName = "animeManga-autoBackup"
$templateRepo = "$templateAuthorName/$templateRepoName"
$templateUri = "https://api.github.com/repos/$templateRepo"
$templateContent = (Invoke-WebRequest -Uri $templateUri -Method Get -ContentType "application/json").Content | ConvertFrom-Json
$templateLastUpdate = Get-Date -Date $templateContent.pushed_at -UFormat "%s"
$localClone = git config --get remote.origin.url
# If format is git@github.com
If ($localClone -Match "^git@github.com") {
# Split the url into the user and repo
$localClone = $localClone.Split("/")
$localAuthorName = $localClone[0] -Replace "git@github.com:", ""
$localRepoName = $localClone[1] -Replace "\.git$", ""
}
Else {
# Split the url from https://github.com/nattadasu/animeManga-autoBackup to nattadasu/animeManga-autoBackup
$localClone = $localClone.Split("/")
$localAuthorName = $localClone[3]
$localRepoName = $localClone[4] -Replace "\.git$", ""
}
$localRepoApi = "https://api.github.com/repos/$localAuthorName/$localRepoName"
Try {
$localRepoContent = (Invoke-WebRequest -Uri $localRepoApi -Method Get -ContentType "application/json" -ErrorAction Continue).Content | ConvertFrom-Json
$localRepoIsFork = $localRepoContent.fork
}
Catch {
Write-Host "Repo is most likely a template and set to private" -ForegroundColor Yellow
$localRepoIsFork = $False
}
$localLastUpdate = git log -1 --format="%ct"
If ($templateRepo -eq "$localAuthorName/$localRepoName") {
Write-Host "Repo is a template"
Exit
}
Else {
Write-Host "Checking if template repo is newer than local repo"
If ($templateLastUpdate -gt $localLastUpdate) {
Write-Host "Template repo is newer than local repo"
Write-Host "Updating local repo"
If ($localRepoIsFork) {
Write-Host "Repo is a fork"
Write-Host "Updating fork"
git pull --unshallow
git remote add upstream "https://github.com/$(templateRepo).git"
git fetch upstream
git checkout main
git merge upstream/main
If (!($isAction)) {
git push
}
}
Else {
Write-Host "Repo is not a fork"
$gitUrl = "https://github.com/$templateRepo"
git clone $gitUrl update
If (($Null -eq $Env:REPO_PAT) -or ($Env:REPO_PAT -eq '')) {
Remove-Item -Path "./update/.github/workflows" -Recurse -Force -ErrorAction SilentlyContinue
}
# Remove dependabot for stability
Remove-Item -Path "./update/.github/dependabot.yml" -Force
Remove-Item -Path "./update/.git" -Recurse -Force
Copy-Item -Path "./update/*" -Destination "./" -Recurse -Force
Remove-Item -Path "./update" -Recurse -Verbose -Force
}
}
Else {
Write-Host "Local repo is newer than template repo"
Exit
}
}
If (!($isAction)) {
./Modules/Initialize-PostUpdate.ps1
git add .
$templateCommits = (Invoke-WebRequest -Uri "$templateUri/commits" -Method Get -ContentType "application/json").Content | ConvertFrom-Json
$latestCommit = $templateCommits[0].sha
git commit -m "Update script based on $latestCommit"
git push
}