Skip to content

Commit

Permalink
Linter fixes (#564)
Browse files Browse the repository at this point in the history
* add logs

* More logs

* get diff from github api

* ignore error if file is not in diff

* fix typo

* skip errors

* add log

* fix

* add log

* add log

* fix

* split by \n

* remove logs

---------

Co-authored-by: Steffen Waldmann <[email protected]>
  • Loading branch information
MatthiasAtSAP and swaldmann authored Dec 11, 2023
1 parent 77caef4 commit 6e276c9
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions .github/etc/create-review.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ module.exports = async ({ github, require, exec, core }) => {
linterErrors.push(...(review.body.match(/\*(.*) <!--Linter Error-->/g) || []))
})

const { data } = await github.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/files', {
owner: REPO_OWNER,
repo: REPO,
pull_number: PULL_NUMBER,
headers: {
accept: 'application/vnd.github.diff'
}
})

const diffs = {}
data.forEach(obj => {
diffs[obj.filename.replace('./', '')] = obj.patch.split('\n')
})

if (existsSync(markdownlintLogFile)) {
const matches = readFileSync(markdownlintLogFile, 'utf-8')
.split('\n')
Expand All @@ -87,6 +101,8 @@ module.exports = async ({ github, require, exec, core }) => {
let contextText = ''
let comment;

if (!fileIsInDiff(path)) continue

if (rule === 'MD011/no-reversed-links') {
const detailValue = details.slice(1, -1)

Expand Down Expand Up @@ -197,6 +213,7 @@ module.exports = async ({ github, require, exec, core }) => {
const wordsWithoutSuggestions = []

for (const [error, path, pointer, word, context, suggestionString] of matches) {
if (!fileIsInDiff(path)) continue

const text = `* **${path}**${pointer} Unknown word "**${word}**" <!--Spelling Mistake-->`

Expand All @@ -209,6 +226,7 @@ module.exports = async ({ github, require, exec, core }) => {
.split(',')
.filter(Boolean) // remove empty strings


const { line, position } = await findPositionInDiff(context, path)

if (!line || position < 0) {
Expand Down Expand Up @@ -266,29 +284,21 @@ module.exports = async ({ github, require, exec, core }) => {
})
}

async function getDiff(file) {
let diff = ''
const opts = {
listeners: {

stdout: (data) => {
diff += data.toString();
}
},
cwd: BASE_DIR
}

await exec.exec(`git diff ${BASE_SHA} ${SHA} -- ${file}`, [], opts)
function fileIsInDiff(file) {
return typeof getDiff(file) !== 'undefined'
}

return diff.split('\n')
function getDiff(file) {
return diffs[file.replace('./', '')]
}

async function findPositionInDiff(context, file) {
const diff = await getDiff(file)
const diff = getDiff(file)

if (!diff) return { position: -1 }

const idxToStartingCoutingFrom = diff.findIndex(line => line.startsWith('@@') && !line.includes('<!--'))
const idxOfLineToSearch = diff.findIndex(line => line.trim().startsWith('+') && line.replace(/ /g, '').includes(context.replace(/ /g, '')) && !line.includes('<!--'))

// context does not exist in diff --> errors is in file with diff, but errors was not introduced with current PR
if (idxToStartingCoutingFrom === -1 || idxOfLineToSearch === -1) {
return { position: -1 }
Expand All @@ -300,7 +310,9 @@ module.exports = async ({ github, require, exec, core }) => {
}

async function findCodeBlockInDiff(lines, file) {
const diff = await getDiff(file)
const diff = getDiff(file)

if (!diff) return { position: -1 }

let start = -1
let end = -1
Expand Down

0 comments on commit 6e276c9

Please sign in to comment.