forked from contributor-assistant/github-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupClaCheck.ts
More file actions
127 lines (110 loc) · 4.67 KB
/
Copy pathsetupClaCheck.ts
File metadata and controls
127 lines (110 loc) · 4.67 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
import { checkAllowList } from './checkAllowList'
import getCommitters from './graphql'
import prCommentSetup from './pullrequest/pullRequestComment'
import { CommitterMap, CommittersDetails, ReactedCommitterMap, ClafileContentAndSha } from './interfaces'
import { context } from '@actions/github'
import { createFile, getFileContent, updateFile } from './persistence/persistence'
import { reRunLastWorkFlowIfRequired } from './pullRerunRunner'
import { isPersonalAccessTokenPresent } from './octokit'
import * as _ from 'lodash'
import * as core from '@actions/core'
export async function setupClaCheck() {
let committerMap = getInitialCommittersMap()
if (!isPersonalAccessTokenPresent()) {
core.setFailed('Please enter a personal access token as a environment variable in the CLA workflow file as described in the https://github.com/cla-assistant/github-action documentation')
return
}
let signed: boolean = false, response
let committers = await getCommitters() as CommittersDetails[]
committers = checkAllowList(committers) as CommittersDetails[]
try {
response = await getCLAFileContentandSHA(committers, committerMap) as ClafileContentAndSha
} catch (error) {
core.setFailed(error)
return
}
const claFileContent = response?.claFileContent
const sha: string = response?.sha
committerMap = prepareCommiterMap(committers, claFileContent) as CommitterMap
if (committerMap?.notSigned && committerMap?.notSigned.length === 0) {
signed = true
}
try {
const reactedCommitters: any = (await prCommentSetup(signed, committerMap, committers)) as ReactedCommitterMap
if (signed) {
core.info(`All committers have signed the CLA`)
return reRunLastWorkFlowIfRequired()
}
if (reactedCommitters?.newSigned.length) {
/* pushing the recently signed contributors to the CLA Json File */
await updateFile(sha, claFileContent, reactedCommitters)
}
if (reactedCommitters?.allSignedFlag) {
core.info(`All contributors have signed the CLA`)
return reRunLastWorkFlowIfRequired()
}
if (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) {
core.setFailed(`Could not update the JSON file: ${err.message}`)
}
}
async function getCLAFileContentandSHA(committers: CommittersDetails[], committerMap: CommitterMap): Promise<any> {
let result, claFileContentString, claFileContent, sha
try {
result = await getFileContent()
} catch (error) {
if (error.status === 404) {
await createClaFileAndPRComment(committers, committerMap)
return
} else {
core.setFailed(`Could not retrieve repository contents: ${error.message}. Status: ${error.status || 'unknown'}`)
}
}
sha = result?.data?.sha
claFileContentString = Buffer.from(result.data.content, 'base64').toString()
claFileContent = JSON.parse(claFileContentString)
return { claFileContent: claFileContent, sha: sha } as ClafileContentAndSha
}
async function createClaFileAndPRComment(committers: CommittersDetails[], committerMap: CommitterMap): Promise<any> {
const signed = false
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(signed, 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: []
})