Created
August 30, 2021 01:17
-
-
Save twistedpair/5d805d3f90978500a24bca67866739bc to your computer and use it in GitHub Desktop.
Find matching added string in GitHub Pull Request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const parse = require('parse-diff'); | |
const axios = require('axios'); | |
// Load pull request diff from GitHub REST API | |
const requestConfig = { | |
headers: { | |
'Accept': 'application/vnd.github.diff' | |
} | |
}; | |
const pullId = 14956; // NOTE: 73 files changed! | |
const repoFullname = 'eslint/eslint'; | |
const url = `https://api.github.com/repos/${repoFullname}/pulls/${pullId}.diff`; | |
const diffStr = (await axios.get(url,requestConfig)).data; | |
// Search for this word | |
const KEYWORD = 'Requirements'; | |
// Analyze all files | |
const files = parse(diffStr); | |
const filesWithMatchingAdds = files.map( | |
file => ({ | |
file: file.to, | |
adds: file.chunks.map( | |
chunk => chunk.changes | |
// Only look for added lines | |
.filter(chunk => chunk.type === 'add') | |
// That match our keyword | |
.filter(chunk => chunk.content.includes(KEYWORD)) | |
).flat()}) // collapse into one array | |
// Only files with at least one match | |
).filter(file => file.adds.length); | |
console.log('%j', filesWithMatchingAdds); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment