-
Notifications
You must be signed in to change notification settings - Fork 3
191 lines (174 loc) · 7.67 KB
/
UpdateReviewSessionFilesPullRequestsBaseCommit.yml
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Copyright (c) Robert Bosch GmbH
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
name: Update Review Session Files for Pull Requests on base commit
on:
push:
env:
ModelName: DemoModel
ModelExtension: eapx
ReviewSessionURL: https://nexus.lieberlieber.com/repository/lemontree-session
LemonTreePipelineToolsRemovePrerenderedDiagramsExecutable: https://nexus.lieberlieber.com/repository/lemontree-pipeline-tools/LemonTree.Pipeline.Tools.RemovePrerenderedDiagrams.exe
jobs:
build:
defaults:
run:
shell: pwsh
# The type of runner that the job will run on
runs-on: windows-latest
timeout-minutes: 15
steps:
- name: Check for related Pull Requests
id: linked_prs
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const response = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo
})
console.log("this repo ref:", context.ref)
if (response.status != 200) {
console.error("ohnoes")
process.exit()
}
const openPR = response.data
const affectedPR = openPR.filter(pr => `refs/heads/${pr.base.ref}` == context.ref)
console.log(`Found ${affectedPR.length} affected PRs: ${affectedPR.map(pr => pr.number).join(", ")}`)
return JSON.stringify(affectedPR, 2)
- name: Check number of affected PRs
id: checkNumber
env:
jsVar: ${{steps.linked_prs.outputs.result}}
run: |
$prList = $env:jsVar | ConvertFrom-Json
echo "count=$($prList.length)" >> $env:GITHUB_OUTPUT
- uses: actions/checkout@v4
if: ${{ steps.checkNumber.outputs.count > 0}}
with:
lfs: true
fetch-depth: 0
# download Lemontree.Automation on a runner and setup the license
- uses: LieberLieber/setup-LemonTree.Automation@v5
if: ${{ steps.checkNumber.outputs.count > 0}}
id: GetLTA
with:
License: ${{secrets.LTALICENSE}}
#ExeLocation &"${{steps.GetLTA.outputs.LemonTreeAutomationExecutable}}"
- name: Iterate over Pull Requests
if: ${{ steps.checkNumber.outputs.count > 0}}
id: WorkOnPr
env:
jsVar: ${{steps.linked_prs.outputs.result}}
run: |
#Create review session files for each pull requestReviewers
echo "Starting process"
function getCommitIds($baseRef, $headRef)
{
$ret =@{}
$ret["baseId"] = git merge-base origin/$baseRef origin/$headRef
$ret["sourceId"] = git show-ref --hash origin/$headRef
$ret["targetId"] = git show-ref --hash origin/$baseRef
echo "target branch: $baseRef"
echo "source branch: $headRef"
echo "target commit: $($ret.targetId)"
echo "source commit: $($ret.sourceId)"
echo "base commit: $($ret.baseId)"
return $ret
}
function downloadFileVersion($fileName, $commitId)
{
echo "Filename: $fileName@$commitId"
#git fetch origin $commitId
$pointer = git cat-file blob $commitId":"$fileName
$sha = ($pointer[1] -split(":"))[1]
echo "SHA: $sha"
$shaPart1 = $sha.Substring(0,2)
$shaPart2 = $sha.Substring(2,2)
git cat-file --filters $commitId":"$fileName | Out-Null
$targetFile = $fileName+"_"+$commitId
copy ".git\lfs\objects\$shaPart1\$shaPart2\$sha" $targetFile
echo "Version file: $targetFile"
}
function getSessionFileName($repoName, $prNumber, $sourceCommit, $targetCommit)
{
$filename = $repoName.Split('/')[1]+"-"+'PR'+"-"+$prNumber+"-"+$sourceCommit+"-"+$targetCommit+'.ltsfs'
return $fileName
}
$prList = $env:jsVar | ConvertFrom-Json
$messageList = [System.Collections.ArrayList]::new()
$modelFileName = "${{env.ModelName}}.${{env.ModelExtension}}"
foreach($pr in $prList)
{
echo "Processing PR $($pr.number)"
$commitIds = getCommitIds $pr.base.ref $pr.head.ref
$sessionFileName = getSessionFileName ${{ github.repository }} $pr.number $commitIds.sourceId $commitIds.targetId
echo "Session filename: $sessionFileName"
echo "Downloading base file"
downloadFileVersion $modelFileName $commitIds.baseId
echo "Downloading source file"
downloadFileVersion $modelFileName $commitIds.sourceId
echo "Downloading target file"
downloadFileVersion $modelFileName $commitIds.targetId
echo "Starting check for merge conflicts"
$baseFile = $modelFileName +"_"+$commitIds.baseId
$targetFile = $modelFileName +"_"+$commitIds.targetId
$sourceFile = $modelFileName +"_"+$commitIds.sourceId
$targetUrl = "${{env.ReviewSessionURL}}/$sessionFileName"
&"${{steps.GetLTA.outputs.LemonTreeAutomationExecutable}}"`
merge `
--base $baseFile `
--theirs $targetFile `
--mine $sourceFile `
--dryrun `
--sfs $sessionFileName `
--abortOnConflict true
if($LASTEXITCODE -eq 0)
{
echo "No merge conflicts, update only on target branch. No new review session file required."
}
elseif($LASTEXITCODE -eq 2)
{
echo "Internal Error when diffing. Please report such errors to [email protected]"
}
elseif($LASTEXITCODE -eq 3)
{
echo "Merge conflicts"
echo "Uploading $sessionFileName to Nexus: $targetUrl"
while (Test-Path Alias:curl) {Remove-Item Alias:curl} #remove the alias binding from curl to Invoke-WebRequest
curl "-u${{secrets.NEXUSAUTHENTICATION}}" -T $sessionFileName $targetUrl
$prMessage =@{}
$prMessage.number=$pr.number
$prMessage.message="New commit on target branch\n[Review Session file]($targetUrl)\n:x: **Please resolve merge conflicts in model first**\n\nInstall [LemonTree 3.3+](https://www.lieberlieber.com/lemontree/en/) to open the Review Session file."
$messageList.Add($prMessage)
}
elseif($LASTEXITCODE -eq 6)
{
echo "Licensing issue of LemonTree.Automation"
}
else
{
echo "Unknown error, exit code: $LASTEXITCODE"
}
}
$jsonResult = ConvertTo-Json -InputObject $messageList -Compress
echo $jsonResult
echo "result=$jsonResult" >> $env:GITHUB_OUTPUT
- name: CreateComments
if: ${{ steps.checkNumber.outputs.count > 0}}
uses: actions/github-script@v6
with:
script: |
const prList = JSON.parse(`${{steps.WorkOnPr.outputs.result}}`)
for (const pr of prList)
{
console.log(`Updating PR#${pr.number}: ${pr.message}`)
await github.rest.issues.createComment({
issue_number: pr.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: pr.message
})
}