Skip to content

Commit

Permalink
Merge pull request #26 from thalleslmF/pr-200
Browse files Browse the repository at this point in the history
Pr 200
  • Loading branch information
thalleslmF authored Jul 29, 2021
2 parents e394a11 + 1f232cc commit 5db71c3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
51 changes: 40 additions & 11 deletions licence.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020, 2021 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
* Copyright 2021 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,13 +19,16 @@ const github = require('@actions/github')
const fs = require("fs");
const util = require("util");
const chalk = require("chalk");
function hasCorrectCopyrightDate(copyrightFile, status, startDateLicense) {
function hasCorrectCopyrightDate(copyrightFile, file, startDateLicense) {
let requiredDate = ''
if (status === 'modified'){
requiredDate = `${startDateLicense, new Date().getFullYear()}`
if (file.status === 'modified'){
if(file.year < new Date().getFullYear()) {
requiredDate = `Copyright ${startDateLicense, new Date().getFullYear()}`
}
requiredDate = `Copyright ${new Date().getFullYear()}`
}
if (status == 'created'){
requiredDate = new Date().getFullYear()
if (file.status === 'created'){
requiredDate = `Copyright ${new Date().getFullYear()}`
}
return copyrightFile.includes(requiredDate)
}
Expand Down Expand Up @@ -61,7 +64,7 @@ async function checkLicenseFile(file, config, fd) {
reject(file.name)
} else {

const correctDate = hasCorrectCopyrightDate(copyrightFile, file.status, config.startDateLicense)
const correctDate = hasCorrectCopyrightDate(copyrightFile, file, config.startDateLicense)
if (correctDate) {
console.log('File ' + chalk.yellow(file.name+": ") + chalk.green('ok!'))
resolve()
Expand Down Expand Up @@ -98,21 +101,40 @@ function removeIgnoredFiles(filesPr, fileNames) {
)
}

async function getCreationYear(file, config) {
const response = await config.octokit.request(`GET /repos/{owner}/{repo}/commits?path=${file.name}`, {
owner: config.owner,
repo: config.repo
})
const commitsDates = response.data.map(
data => new Date(data.commit.author.date)
)
const creationDate = Math.min.apply(null, commitsDates)
return new Date(creationDate).getFullYear()
}

const checkLicense = async (fileNames, config) => {
const token = core.getInput('token')

const octokit = github.getOctokit(token)
const prNumber = github.context.payload.pull_request.number
const prNumber = github.context.payload.pull_request. number
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}', ({
config = {
...config,
owner: owner,
repo: repo,
octokit: octokit
}
const responsePr = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', ({
owner: config.owner,
repo: config.repo,
pull_number: prNumber
}))

const responseCompare = await octokit.request('GET /repos/{owner}/{repo}/compare/{basehead}', {
owner: owner,
repo: repo,
owner: config.owner,
repo: config.repo,
basehead: `${responsePr.data.base.sha}...${responsePr.data.head.sha}`
})
const filesPr = responseCompare.data.files.map(
Expand All @@ -124,6 +146,13 @@ const checkLicense = async (fileNames, config) => {
}
)
const filesFiltered = removeIgnoredFiles(filesPr, fileNames)
await filesFiltered.map(
(file) => {
return {
...file,
year : getCreationYear(file, config)
}
})
return await checkFilesLicense(filesFiltered, config)

}
Expand Down
5 changes: 2 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020, 2021 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
* Copyright 2021 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,5 +34,4 @@ glob(
core.setFailed('Action failed');
}
}
)
console.log('should fail')
)

0 comments on commit 5db71c3

Please sign in to comment.