forked from HumanSignal/label-studio
-
Notifications
You must be signed in to change notification settings - Fork 3
172 lines (154 loc) · 7.44 KB
/
submodules-validator.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
name: "Dependencies check"
on:
push:
branches:
- develop
- 'ls-release/**'
pull_request:
types:
- opened
- synchronize
- reopened
- ready_for_review
branches:
- develop
- 'ls-release/**'
env:
ACTIONS_STEP_DEBUG: '${{ secrets.ACTIONS_STEP_DEBUG }}'
jobs:
validate_submodules_and_ls_dependencies:
name: "Submodules/deps"
runs-on: ubuntu-latest
steps:
- uses: hmarr/[email protected]
- name: Validate
uses: actions/github-script@v6
with:
script: |
const {repo, owner} = context.repo;
const head_sha = '${{ github.event.pull_request.head.sha || github.event.after }}'
const base_sha = '${{ github.event.pull_request.base.sha || github.event.before }}'
const targetBranch = '${{ github.event.pull_request.base.ref || github.event.ref }}'.replace('refs/heads/', '')
const strictCheckBranchPrefixes = ['ls-release/']
let submodules = [
{owner: owner, repo: 'label-studio-frontend'},
{owner: owner, repo: 'dm2'}
]
async function getLSSubmoduleVersions(sha) {
let {data: lsTreeData} = await github.rest.git.getTree({
owner,
repo,
tree_sha: sha
})
lsTreeData = (await github.rest.git.getTree({
owner,
repo,
tree_sha: lsTreeData.tree.find(e => e.path === 'label_studio' && e.type === 'tree').sha
})).data
lsTreeData = (await github.rest.git.getTree({
owner,
repo,
tree_sha: lsTreeData.tree.find(e => e.path === 'frontend' && e.type === 'tree').sha
})).data
lsTreeData = (await github.rest.git.getTree({
owner,
repo,
tree_sha: lsTreeData.tree.find(e => e.path === 'dist' && e.type === 'tree').sha
})).data
const {data: lsDMTreeData} = await github.rest.git.getTree({
owner,
repo,
tree_sha: lsTreeData.tree.find(e => e.path === 'dm' && e.type === 'tree').sha
})
const {data: dmfVersion} = await github.rest.git.getBlob({
owner,
repo,
file_sha: lsDMTreeData.tree.find(e => e.path === 'version.json' && e.type === 'blob').sha
})
const dmVersionContent = Buffer.from(dmfVersion.content, dmfVersion.encoding).toString("utf8")
const matchDM = dmVersionContent.match('"commit": "(.*)",')
const {data: lsLSFTreeData} = await github.rest.git.getTree({
owner,
repo,
tree_sha: lsTreeData.tree.find(e => e.path === 'lsf' && e.type === 'tree').sha
})
const {data: lsfVersion} = await github.rest.git.getBlob({
owner,
repo,
file_sha: lsLSFTreeData.tree.find(e => e.path === 'version.json' && e.type === 'blob').sha
})
const lsfVersionContent = Buffer.from(lsfVersion.content, lsfVersion.encoding).toString("utf8")
const matchLSF = lsfVersionContent.match('"commit": "(.*)",')
return {
'label-studio-frontend': matchLSF[1],
'dm2': matchDM[1],
}
}
let base_sha_redacted = base_sha
if (base_sha_redacted === '0000000000000000000000000000000000000000') {
console.log(`Branch creation event. Using head_sha (${head_sha}) parent as base_sha`)
const {data: commit} = await github.rest.git.getCommit({
owner,
repo,
commit_sha: head_sha,
});
console.log(commit.parents)
base_sha_redacted = commit.parents[0].sha
}
const baseVersions = await getLSSubmoduleVersions(base_sha_redacted)
console.log(`before: ${base_sha_redacted}`)
console.log(baseVersions)
const headVersions = await getLSSubmoduleVersions(head_sha)
console.log(`after: ${head_sha}`)
console.log(headVersions)
const strictCheck = strictCheckBranchPrefixes.some(e => targetBranch.startsWith(e))
console.log(`Strict check: ${strictCheck}`)
let failed = []
for (let submodule of submodules) {
if (baseVersions[submodule.repo] === headVersions[submodule.repo] && !strictCheck) {
console.log(`${submodule.repo}: Is not changed`)
continue
}
const {data: submoduleRepo} = await github.rest.repos.get({
owner: submodule.owner,
repo: submodule.repo,
});
const submoduleBranch = targetBranch === 'develop' ? submoduleRepo.default_branch : targetBranch
const {data: listCommits} = await github.rest.repos.listCommits({
owner: submodule.owner,
repo: submodule.repo,
per_page: 100,
sha: submoduleBranch
});
const commits = listCommits.map(e => e.sha)
const headCommitNumber = commits.indexOf(headVersions[submodule.repo])
if (headCommitNumber === -1) {
console.log(`${submodule.repo}: ${headVersions[submodule.repo]} from PR is not found in submodule ${submoduleBranch} branch`)
failed.push(submodule.repo)
continue
}
if (strictCheck && headCommitNumber !== 0) {
console.log(`${submodule.repo}: For the release branch, submodule should be pointed to the latest commit in submodule corresponding release branch which is ${listCommits[0].html_url}`)
failed.push(submodule.repo)
continue
}
const baseCommitNumber = commits.indexOf(baseVersions[submodule.repo])
if (baseCommitNumber === -1) {
console.log(`${submodule.repo}: ${baseVersions[submodule.repo]} from ${targetBranch} is not found in submodule ${submoduleBranch} branch`)
continue
}
const {data: compare} = await github.rest.repos.compareCommits({
owner: submodule.owner,
repo: submodule.repo,
base: baseVersions[submodule.repo],
head: headVersions[submodule.repo],
});
console.log(`${submodule.repo}: ${headVersions[submodule.repo]} is ${compare.ahead_by} ahead and ${compare.behind_by} behind ${baseVersions[submodule.repo]}: ${compare.html_url}`)
if (compare.behind_by > 0) {
failed.push(submodule.repo)
continue
}
}
if (failed.length !== 0) {
throw `Versions for ${failed.toString()} are downgraded or not found`;
}