Skip to content

Instantly share code, notes, and snippets.

@twistedpair
Created August 30, 2021 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twistedpair/5d805d3f90978500a24bca67866739bc to your computer and use it in GitHub Desktop.
Save twistedpair/5d805d3f90978500a24bca67866739bc to your computer and use it in GitHub Desktop.
Find matching added string in GitHub Pull Request
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