|
| 1 | +require 'octokit' |
| 2 | + |
| 3 | +# Lists all files attached to issues and pull requests on a instance. |
| 4 | +# The list doesn't contain files that are uploaded but not referenced. |
| 5 | + |
| 6 | +## Check for environment variables |
| 7 | +begin |
| 8 | + access_token = ENV.fetch("GITHUB_TOKEN") |
| 9 | + hostname = ENV.fetch("GITHUB_HOSTNAME") |
| 10 | +rescue KeyError |
| 11 | + puts |
| 12 | + puts "To run this script, please set the following environment variables:" |
| 13 | + puts "- GITHUB_TOKEN: A valid access token" |
| 14 | + puts "- GITHUB_HOSTNAME: A valid GitHub Enterprise hostname" |
| 15 | + exit 1 |
| 16 | +end |
| 17 | + |
| 18 | +# Set up Octokit |
| 19 | +Octokit.configure do |kit| |
| 20 | + kit.api_endpoint = "#{hostname}/api/v3" |
| 21 | + kit.access_token = access_token |
| 22 | + kit.auto_paginate = true |
| 23 | +end |
| 24 | + |
| 25 | +# Extract links to attached files using regexp |
| 26 | +pattern = /\[[^\]]*\]\((#{hostname}[^\)]*\/files\/[^\)]*)\)/ |
| 27 | + |
| 28 | +Octokit.repositories.map{|repo| repo.full_name}.each do |r| |
| 29 | + # Extract issues containing links to attached files |
| 30 | + issues = Octokit.issues(r, {state: :all}).select do |i| |
| 31 | + i.body.match(pattern) |
| 32 | + end |
| 33 | + issues.each do |issue| |
| 34 | + # Extract the link pattern from issues' body |
| 35 | + matched_links = issue.body.scan(pattern) |
| 36 | + matched_links.each do |file| |
| 37 | + puts "#{issue.html_url},#{file[0]}" |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + # Issue comments as well (including pull request comments) |
| 42 | + issue_comments = Octokit.issues_comments(r).select do |ic| |
| 43 | + ic.body.match(pattern) |
| 44 | + end |
| 45 | + issue_comments.each do |issue_comment| |
| 46 | + matched_links = issue_comment.body.scan(pattern) |
| 47 | + matched_links.each do |file| |
| 48 | + puts "#{issue_comment.html_url},#{file[0]}" |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + # Pull request review comments as well |
| 53 | + pr_comments = Octokit.pulls_comments(r).select do |prc| |
| 54 | + prc.body.match(pattern) |
| 55 | + end |
| 56 | + pr_comments.each do |pr_comment| |
| 57 | + matched_links = pr_comment.body.scan(pattern) |
| 58 | + matched_links.each do |file| |
| 59 | + puts "#{pr_comment.html_url},#{file[0]}" |
| 60 | + end |
| 61 | + end |
| 62 | +end |
0 commit comments