| 135 | |
| 136 | // Search for a string in a file in code and return the array of paths to files that contain string |
| 137 | export async function getPathsWithMatchingStrings(strArr, org, repo) { |
| 138 | const perPage = 100 |
| 139 | const paths = new Set() |
| 140 | |
| 141 | for (const str of strArr) { |
| 142 | try { |
| 143 | const q = `q=${str}+in:file+repo:${org}/${repo}` |
| 144 | let currentPage = 1 |
| 145 | let totalCount = 0 |
| 146 | let currentCount = 0 |
| 147 | |
| 148 | do { |
| 149 | const data = await searchCode(q, perPage, currentPage) |
| 150 | data.items.map((el) => paths.add(el.path)) |
| 151 | totalCount = data.total_count |
| 152 | currentCount += data.items.length |
| 153 | currentPage++ |
| 154 | } while (currentCount < totalCount) |
| 155 | } catch (err) { |
| 156 | console.log(`error searching for ${str} in ${org}/${repo}`) |
| 157 | throw err |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return paths |
| 162 | } |
| 163 | |
| 164 | async function searchCode(q, perPage, currentPage) { |
| 165 | try { |