Skip to content

Commit

Permalink
Merge pull request #14 from thalleslmF/pr-14
Browse files Browse the repository at this point in the history
Pr 14
  • Loading branch information
thalleslmF authored Jul 28, 2021
2 parents f44f0e5 + 9e15b51 commit ea2c053
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
"upgrades/**",
".storybook",
".svg"
]
],
"startDateLicense": 2020
}
62 changes: 45 additions & 17 deletions licence.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,20 @@ function getExtensionCommentPattern(extension) {
return result
}

const checkLicense = async (fileNames, copyrightContent) => {
const token = core.getInput('token')
function hasCorrectCopyrightDate(copyrightFile, status, startDateLicense) {
let requiredDate = ''
if (status === 'modified'){
requiredDate = `${startDateLicense, new Date().getFullYear()}`
}
if (status == 'created'){
requiredDate = new Date().getFullYear()
}
return copyrightFile.includes(requiredDate)
}

const checkLicense = async (fileNames, config) => {
let errors = []
const token = core.getInput('token') || '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 '
const octokit = github.getOctokit(token)
const prNumber = github.context.payload.pull_request.number
const owner = github.context.payload.repository.owner.login
Expand All @@ -45,22 +57,22 @@ const checkLicense = async (fileNames, copyrightContent) => {
pull_number: prNumber
}))

console.log(responsePr.data.compare.split('/').unshift())
console.log(responsePr.data.compare.split('/').pop())

const responseCompare = await octokit.request('GET /repos/{owner}/{repo}/compare/{basehead}', {
owner: owner,
repo: repo,
basehead: `${responsePr.data.head.sha}...${responsePr.data.base.sha}`
q basehead: `${responsePr.data.base.sha}...${responsePr.data.head.sha}`
})
const listFilesPr = responseCompare.data.files.map(
file => file.filename
file => {
return {
name: file.filename,
status: file.status
}
}
)
console.log(listFilesPr)
for ( let name of fileNames) {

if( !listFilesPr.includes(name)) {
console.info(`${name} not in PR: ignoring...`)
if( !listFilesPr.some(
file => file.name === name)) {
continue
}
fs.open(name, 'r', (status,fd) => {
Expand All @@ -71,23 +83,39 @@ const checkLicense = async (fileNames, copyrightContent) => {
console.error(`Error reading file ${err}`)
}
const copyrightFile = buffer.toString('utf-8')
const allCopyrightIncluded = copyrightContent.every(
const allCopyrightIncluded = config.copyrightContent.every(
line => copyrightFile.includes(line)
)

if (!allCopyrightIncluded) {
console.error(`File ${name} :No copyright header!`)
errors.push(
name
)
} else {
console.error(`File ${name} :ok!`)
const fileSearched = listFilesPr.find(
file => file.name === name
)
const correctDate = hasCorrectCopyrightDate(copyrightFile, fileSearched.status, config.startDateLicense)
if (correctDate) {
console.log(`File ${name} :ok!`)
} else {
console.error(`fix file: ${name} copyright date!`)
errors.push(
name
)
}
}
})
})
}



if(errors.length) {
throw new Error(`
Quantity of copyright errors: ${errors.length}
Fix copyright on the following files: ${errors}
`)
}
}



exports.checkLicense = checkLicense
3 changes: 2 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ const data = fs.readFileSync(file, 'utf-8');
let dataObject = JSON.parse(data)
let copyrightContent = dataObject.copyright
let ignore = dataObject.ignore
let startDateLicense = dataObject.startDateLicense
glob(
"**/*.*",{cwd: process.cwd(), ignore: ignore }, (err,fileNames) => {
if (err) {
console.log(err)
}
checkLicense(fileNames, copyrightContent).then(r =>
checkLicense(fileNames, { copyrightContent: copyrightContent, startDateLicense: startDateLicense }).then(r =>
console.log(r))
.catch(
err => console.error(err)
Expand Down

0 comments on commit ea2c053

Please sign in to comment.