This repository was archived by the owner on Mar 23, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathsetupClaCheck.ts
More file actions
140 lines (127 loc) · 4.11 KB
/
setupClaCheck.ts
File metadata and controls
140 lines (127 loc) · 4.11 KB
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
import { checkAllowList } from './checkAllowList'
import getCommitters from './graphql'
import prCommentSetup from './pullrequest/pullRequestComment'
import {
ClafileContentAndSha,
CommitterMap,
CommittersDetails,
ReactedCommitterMap
} from './interfaces'
import { context } from '@actions/github'
import {
createFile,
getFileContent,
updateFile
} from './persistence/persistence'
import { reRunLastWorkFlowIfRequired } from './pullRerunRunner'
import * as core from '@actions/core'
export async function setupClaCheck() {
let committerMap = getInitialCommittersMap()
let committers = await getCommitters()
committers = checkAllowList(committers)
const { claFileContent, sha } = (await getCLAFileContentandSHA(
committers,
committerMap
)) as ClafileContentAndSha
committerMap = prepareCommiterMap(committers, claFileContent) as CommitterMap
try {
const reactedCommitters = (await prCommentSetup(
committerMap,
committers
)) as ReactedCommitterMap
if (reactedCommitters?.newSigned.length) {
/* pushing the recently signed contributors to the CLA Json File */
await updateFile(sha, claFileContent, reactedCommitters)
}
if (
reactedCommitters?.allSignedFlag ||
committerMap?.notSigned === undefined ||
committerMap.notSigned.length === 0
) {
core.info(`All contributors have signed the CLA 📝 ✅ `)
return reRunLastWorkFlowIfRequired()
} else {
core.setFailed(
`Committers of Pull Request number ${context.issue.number} have to sign the CLA 📝`
)
}
} catch (err) {
let extraInfo = '';
if (err.message.includes('Changes must be made through a pull request')) extraInfo = ' (The branch appears to be protected - please disable the protection)';
core.setFailed(`Could not update the JSON file: ${err.message}${extraInfo}`)
}
}
async function getCLAFileContentandSHA(
committers: CommittersDetails[],
committerMap: CommitterMap
): Promise<void | ClafileContentAndSha> {
let result, claFileContentString, claFileContent, sha
try {
result = await getFileContent()
} catch (error) {
if (error.status === 404) {
return createClaFileAndPRComment(committers, committerMap)
} else {
throw new Error(
`Could not retrieve repository contents. Status: ${
error.status || 'unknown'
}`
)
}
}
sha = result?.data?.sha
claFileContentString = Buffer.from(result.data.content, 'base64').toString()
claFileContent = JSON.parse(claFileContentString)
return { claFileContent, sha }
}
async function createClaFileAndPRComment(
committers: CommittersDetails[],
committerMap: CommitterMap
): Promise<void> {
committerMap.notSigned = committers
committerMap.signed = []
committers.map(committer => {
if (!committer.id) {
committerMap.unknown.push(committer)
}
})
const initialContent = { signedContributors: [] }
const initialContentString = JSON.stringify(initialContent, null, 3)
const initialContentBinary =
Buffer.from(initialContentString).toString('base64')
await createFile(initialContentBinary).catch(error =>
core.setFailed(
`Error occurred when creating the signed contributors file: ${
error.message || error
}. Make sure the branch where signatures are stored is NOT protected.`
)
)
await prCommentSetup(committerMap, committers)
throw new Error(
`Committers of pull request ${context.issue.number} have to sign the CLA`
)
}
function prepareCommiterMap(
committers: CommittersDetails[],
claFileContent
): CommitterMap {
let committerMap = getInitialCommittersMap()
committerMap.notSigned = committers.filter(
committer =>
!claFileContent?.signedContributors.some(cla => committer.id === cla.id)
)
committerMap.signed = committers.filter(committer =>
claFileContent?.signedContributors.some(cla => committer.id === cla.id)
)
committers.map(committer => {
if (!committer.id) {
committerMap.unknown.push(committer)
}
})
return committerMap
}
const getInitialCommittersMap = (): CommitterMap => ({
signed: [],
notSigned: [],
unknown: []
})