|
| 1 | +#!/bin/bash |
| 2 | +if [ -n "$DEBUG" ]; then |
| 3 | + set -x |
| 4 | +fi |
| 5 | +if [ $(uname) = "Linux" ]; then |
| 6 | + date_to_epoch() { |
| 7 | + date -u -d "$1" +'%s' |
| 8 | + } |
| 9 | +else |
| 10 | + date_to_epoch() { |
| 11 | + date -u -j -f '%Y-%m-%dT%H:%M:%SZ' "$1" +'%s' |
| 12 | + } |
| 13 | +fi |
| 14 | +timeframe=${timeframe:-60} |
| 15 | +time_limit=$(( $timeframe * 60 )) |
| 16 | +strip_quotes() { |
| 17 | + tr '"' ' ' |
| 18 | +} |
| 19 | +now() { |
| 20 | + date +'%s' |
| 21 | +} |
| 22 | +start=$(now) |
| 23 | + |
| 24 | +pulls=$temp/pulls.json |
| 25 | +escaped=$temp/escaped.b64 |
| 26 | +pull=$temp/pull.json |
| 27 | +fake_event=$temp/fake_event.json |
| 28 | +headers=$temp/headers |
| 29 | + |
| 30 | +if [ -e "$pulls" ]; then |
| 31 | + echo using cached $pulls |
| 32 | +else |
| 33 | + curl -s -s -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" -H "Accept: application/vnd.github.shadow-cat-preview+json" https://api.github.com/repos/check-spelling/examples-testing/pulls > $pulls |
| 34 | +fi |
| 35 | +cat "$pulls" | jq -c '.[]'|jq -c -r '{ |
| 36 | + head_repo : .head.repo.full_name, |
| 37 | + base_repo: .base.repo.full_name, |
| 38 | + head_ref: .head.ref, |
| 39 | + head_sha: .head.sha, |
| 40 | + base_sha: .base.sha, |
| 41 | + clone_url: .head.repo.clone_url, |
| 42 | + merge_commit_sha: .merge_commit_sha, |
| 43 | + created_at: .created_at, |
| 44 | + issue_url: .issue_url, |
| 45 | + commits_url: .commits_url, |
| 46 | + comments_url: .comments_url |
| 47 | + } | @base64' > "$escaped" |
| 48 | + |
| 49 | +get_created_from_events() { |
| 50 | + rm -f "$headers" |
| 51 | + created_at=$(curl -s -S \ |
| 52 | + -H "Authorization: token $GITHUB_TOKEN" \ |
| 53 | + --header "Content-Type: application/json" \ |
| 54 | + -D "$headers" \ |
| 55 | + "$1" | |
| 56 | + jq -M -r '[ .[]|select (.event=="head_ref_force_pushed") ][-1].created_at') |
| 57 | + if [ "$created_at" = "null" ]; then |
| 58 | + created_time=0 |
| 59 | + else |
| 60 | + created_time=$(date_to_epoch $created_at) |
| 61 | + fi |
| 62 | + if [ -e "$headers" ]; then |
| 63 | + next_url=$(perl -ne 'next unless s/^Link: //;s/,\s+/\n/g; print "$1" if /<(.*)>; rel="last"/' $headers) |
| 64 | + rm -f "$headers" |
| 65 | + if [ -n "$next_url" ]; then |
| 66 | + other_time=$(get_created_from_events "$next_url") |
| 67 | + if [ "$created_time" -lt "$other_time" ]; then |
| 68 | + created_time=$other_time |
| 69 | + fi |
| 70 | + fi |
| 71 | + fi |
| 72 | + echo "$created_time" |
| 73 | +} |
| 74 | + |
| 75 | +for a in $(cat "$escaped"); do |
| 76 | + echo "$a" | base64 --decode | jq -r . > $pull |
| 77 | + issue_url=$(cat $pull | jq -r .issue_url) |
| 78 | + created_at=$(get_created_from_events "${issue_url}/events") |
| 79 | + if [ "$created_at" -eq 0 ]; then |
| 80 | + created_at=$(date_to_epoch $(cat $pull | jq -r .created_at)) |
| 81 | + fi |
| 82 | + age=$(( $start - $created_at )) |
| 83 | + if [ $age -gt $time_limit ]; then |
| 84 | + continue |
| 85 | + fi |
| 86 | + head_repo=$(cat $pull | jq -r .head_repo) |
| 87 | + base_repo=$(cat $pull | jq -r .base_repo) |
| 88 | + if [ "$head_repo" = "$base_repo" ]; then |
| 89 | + continue |
| 90 | + fi |
| 91 | + head_sha=$(cat $pull | jq -r .head_sha) |
| 92 | + base_sha=$(cat $pull | jq -r .base_sha) |
| 93 | + merge_commit_sha=$(cat $pull | jq -r .merge_commit_sha) |
| 94 | + comments_url=$(cat $pull | jq -r .comments_url) |
| 95 | + commits_url=$(cat $pull | jq -r .commits_url) |
| 96 | + clone_url=$(cat $pull | jq -r .clone_url) |
| 97 | + clone_url=$(echo "$clone_url" | sed -e 's/https/http/') |
| 98 | + head_ref=$(cat $pull | jq -r .head_ref) |
| 99 | + echo "do work for $head_repo -> $base_repo: $head_sha as $merge_commit_sha" |
| 100 | + export GITHUB_SHA="$head_sha" |
| 101 | + export GITHUB_EVENT_NAME=pull_request |
| 102 | + echo '{}' | jq \ |
| 103 | + --arg head_sha "$head_sha" \ |
| 104 | + --arg base_sha "$base_sha" \ |
| 105 | + --arg comments_url "$comments_url" \ |
| 106 | + --arg commits_url "$commits_url" \ |
| 107 | + -r '{pull_request: {base: {sha: $base_sha}, head: {sha: $head_sha}, comments_url: $comments_url, commits_url: $commits_url }}' \ |
| 108 | + > "$fake_event" |
| 109 | + export GITHUB_EVENT_PATH="$fake_event" |
| 110 | + git remote rm pr 2>/dev/null || true |
| 111 | + git remote add pr $clone_url |
| 112 | + cat .git/config |
| 113 | + git fetch pr $head_ref |
| 114 | + git checkout $head_sha |
| 115 | + git remote rm pr 2>/dev/null || true |
| 116 | + "$spellchecker/unknown-words.sh" || true |
| 117 | +done |
0 commit comments