Skip to content

Commit

Permalink
Merge pull request #18 from thalleslmF/pr-19
Browse files Browse the repository at this point in the history
Pr 19
  • Loading branch information
thalleslmF authored Jul 29, 2021
2 parents 71c729a + 9392c87 commit 3bb5068
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 78 deletions.
6 changes: 3 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
],
"ignore": [
"node_modules/**",
".md",
".json",
"**.md",
"**.json",
".png",
".idea",
"**.idea",
".github",
".git",
".gitignore",
Expand Down
140 changes: 75 additions & 65 deletions licence.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,7 @@
const core = require('@actions/core')
const github = require('@actions/github')
const fs = require("fs");

function getExtensionCommentPattern(extension) {
let result = ''
switch(extension) {

case "yaml" : case "sh": case "properties":
result = "#"
break;
case "sql":
result = "--"
default:
result = "/*"
}
return result
}
const util = require("util");

function hasCorrectCopyrightDate(copyrightFile, status, startDateLicense) {
let requiredDate = ''
Expand All @@ -44,13 +30,80 @@ function hasCorrectCopyrightDate(copyrightFile, status, startDateLicense) {
return copyrightFile.includes(requiredDate)
}

const checkLicense = async (fileNames, config) => {
async function openFile(name) {
return await new Promise(
(resolve,reject) => {
fs.open(name, 'r', (error, fd) => {
if (error) {
reject(error)
} else {
resolve(fd)
}
})
})
}

async function checkLicenseFile(file, config, fd) {
let buffer = new Buffer(8000)
return await new Promise(
(resolve, reject) => {
fs.read(fd, buffer, 0, 8000, 0, (err) => {
if (err) {
console.error(`Error reading file ${err}`)
}
const copyrightFile = buffer.toString('utf-8')
const allCopyrightIncluded = config.copyrightContent.every(
line => copyrightFile.includes(line)
)

if (!allCopyrightIncluded) {
console.error(`File ${file.name} :No copyright header!`)
reject(file.name)
} else {

const correctDate = hasCorrectCopyrightDate(copyrightFile, file.status, config.startDateLicense)
if (correctDate) {
console.log(`File ${file.name} :ok!`)
resolve()
} else {
console.error(`fix file: ${file.name} copyright date!`)
reject(file.name)
}
}
})
})
}

async function checkFilesLicense(filesPr, config) {
let errors = []
const token = core.getInput('token') || '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 '
for ( let file of filesPr) {
const fd = await openFile(file.name)
try{
await checkLicenseFile(file, config, fd)
} catch (error) {
errors.push(error)
}
}
if (errors.length) {
return({
title: `Quantity of files with copyright errors: ${errors.length}`,
details: `Files : ${util.inspect(errors)}`
})
}
}

function removeIgnoredFiles(filesPr, fileNames) {
return filesPr.filter(
file => fileNames.includes(file.name)
)
}

const checkLicense = async (fileNames, config) => {
const token = core.getInput('token')
const octokit = github.getOctokit(token)
const prNumber = github.context.payload.pull_request.number
const owner = github.context.payload.repository.owner.login
const repo = github.context.payload.repository.name
const owner = github.context.payload.repository.owner.login
const repo = github.context.payload.repository.name
const responsePr = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', ({
owner: owner,
repo: repo,
Expand All @@ -62,60 +115,17 @@ const checkLicense = async (fileNames, config) => {
repo: repo,
basehead: `${responsePr.data.base.sha}...${responsePr.data.head.sha}`
})
const listFilesPr = responseCompare.data.files.map(
const filesPr = responseCompare.data.files.map(
file => {
return {
name: file.filename,
status: file.status
}
}
)
for ( let name of fileNames) {

if( !listFilesPr.some(
file => file.name === name)) {
continue
}
fs.open(name, 'r', (status,fd) => {
const filesFiltered = removeIgnoredFiles(filesPr, fileNames)
return await checkFilesLicense(filesFiltered, config)

var buffer = new Buffer(8000)
fs.read(fd, buffer, 0, 8000, 0, (err) => {
if (err) {
console.error(`Error reading file ${err}`)
}
const copyrightFile = buffer.toString('utf-8')
const allCopyrightIncluded = config.copyrightContent.every(
line => copyrightFile.includes(line)
)

if (!allCopyrightIncluded) {
console.error(`File ${name} :No copyright header!`)
errors.push(
name
)
} else {
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}
`)
}
}


Expand Down
19 changes: 9 additions & 10 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { checkLicense } = require("./licence");
const util = require("util");
fs = require('fs');
glob = require('glob')
let file = "config.json"
Expand All @@ -8,14 +9,12 @@ 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: copyrightContent, startDateLicense: startDateLicense }).then(r =>
console.log(r))
.catch(
err => console.error(err)
)
"**/*.*",{cwd: process.cwd(), ignore }, async (err,fileNames) => {
const errors = await checkLicense(fileNames, { copyrightContent: copyrightContent, startDateLicense: startDateLicense })
if(errors) {
console.log(errors.title)
console.log(errors.details)
process.exit()
}
}
)
)

0 comments on commit 3bb5068

Please sign in to comment.