-
Notifications
You must be signed in to change notification settings - Fork 2.4k
149 lines (131 loc) · 5.98 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
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_dependencies:
name: "Submodules/deps"
runs-on: ubuntu-latest
steps:
- uses: hmarr/[email protected]
- name: Validate
uses: actions/github-script@v7
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/'];
const submodules = [
{
owner: owner,
repo: 'label-studio-frontend',
file_path: 'label_studio/frontend/dist/lsf/version.json',
regexp: /"commit": "(?<sha>[a-f0-9]{40})",/,
},
{
owner: owner,
repo: 'dm2',
file_path: 'label_studio/frontend/dist/dm/version.json',
regexp: /"commit": "(?<sha>[a-f0-9]{40})",/,
},
{
owner: owner,
repo: 'label-studio-sdk',
file_path: 'pyproject.toml',
regexp: /label-studio-sdk(\/archive|.git).*(?<sha>[a-f0-9]{40})/,
},
];
async function getSubmoduleVersion(submodule, sha) {
const response = await github.rest.repos.getContent({
owner: owner,
repo: repo,
ref: sha,
path: submodule.file_path,
});
const content = Buffer.from(response.data.content, response.data.encoding).toString('utf-8');
const match = content.match(submodule.regexp);
return match.groups.sha;
}
async function validateSubmodule(submodule, base_sha, head_sha) {
const before = await getSubmoduleVersion(submodule, base_sha);
const after = await getSubmoduleVersion(submodule, head_sha);
if (before === after && !strictCheck) {
return [true, 'Not changed']
}
const {data: compare} = await github.rest.repos.compareCommits({
owner: submodule.owner,
repo: submodule.repo,
base: before,
head: after,
});
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(after)
if (headCommitNumber === -1) {
return [false, `${after} from PR is not found in submodule ${submoduleBranch} branch`]
}
if (strictCheck && headCommitNumber !== 0) {
return [false, `For the release branch, submodule should be pointed to the latest commit in submodule's corresponding release branch which is ${listCommits[0].html_url}`]
}
const baseCommitNumber = commits.indexOf(before)
if (baseCommitNumber === -1) {
return [true, `${submodule.repo}: ${before} from ${targetBranch} is not found in submodule ${submoduleBranch} branch`]
}
return [compare.behind_by === 0, `${after} is ${compare.ahead_by} ahead and ${compare.behind_by} behind ${before}: ${compare.html_url}`]
}
const strictCheck = strictCheckBranchPrefixes.some(e => targetBranch.startsWith(e))
console.log(`Strict check is ${strictCheck? 'on': 'off'}`)
let base_sha_redacted = base_sha
if (base_sha === '0000000000000000000000000000000000000000') {
const {data: commit} = await github.rest.git.getCommit({
owner,
repo,
commit_sha: head_sha,
});
base_sha_redacted = commit.parents[0].sha
console.log(`Branch creation event. Using head_sha (${base_sha_redacted}) parent as base_sha`)
}
const {data: lsCompare} = await github.rest.repos.compareCommits({
owner: owner,
repo: repo,
base: base_sha_redacted,
head: head_sha,
});
for (let submodule of submodules) {
let status = {}
if (lsCompare.files.some(e => e.filename === submodule.file_path)) {
status = await validateSubmodule(submodule, base_sha, head_sha);
} else {
status = [true, `Not changed`]
}
if (status[0] === false) {
core.setFailed(`${submodule.owner}/${submodule.repo}: ${status[1]}`)
} else {
core.info(`${submodule.owner}/${submodule.repo}: ${status[1]}`)
}
}