aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/developer/bin/fix_outdated_appcasts
blob: 9915b5339b6d14eadc5094de2d6e66f63fd142e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash

IFS=$'\n'

readonly caskroom_repos_dir='/tmp/caskroom_repos'
readonly caskroom_repos=(homebrew-cask homebrew-versions homebrew-fonts homebrew-eid)

if [[ ! $(which 'ghi') ]] || ! security find-internet-password -s github.com -l 'ghi token' &> /dev/null; then
  echo -e "$(tput setaf 1)
    This script requires 'ghi' installed and configured.
    If you have [Homebrew](http://brew.sh), you can install it with 'brew install ghi'.
    To configure it, run 'ghi config --auth <username>'. Your Github password will be required, but is never stored.
  $(tput sgr0)" | sed -E 's/ {4}//' >&2
  exit 1
fi

if [[ ! $(which 'cask-repair') ]]; then
  echo -e "$(tput setaf 1)
    This script requires 'cask-repair'.
    If you have [Homebrew](http://brew.sh), you can install it with 'brew install vitorgalvao/tiny-scripts/cask-repair'.
  $(tput sgr0)" | sed -E 's/ {4}//' >&2
  exit 1
fi

function message {
  echo "${1}"
}

function go_to_repos_dir {
  [[ ! -d "${caskroom_repos_dir}" ]] && mkdir -p "${caskroom_repos_dir}"
  cd "${caskroom_repos_dir}" || exit 1
}

function go_to_repo_and_update {
  local repo_name repo_dir casks_dir

  repo_name="${1}"
  repo_dir="${caskroom_repos_dir}/${repo_name}"
  casks_dir="${repo_dir}/Casks"

  if [[ ! -d "${repo_dir}" ]]; then
    go_to_repos_dir

    message "Cloning ${repo_name}…"
    git clone "https://github.com/caskroom/${repo_name}.git" --quiet

    cd "${casks_dir}" || exit 1
  else
    cd "${casks_dir}" || exit 1

    message "Updating ${repo_name}…"
    git pull --rebase origin master --quiet
  fi
}

function fix_outdated_appcasts {
  local issue_number cask_name pr_number

  for line in $(ghi list --state open --no-pulls --label 'outdated appcast' --reverse | tail +2); do
    [[ "${line}" == 'None.' ]] && break # exit early if there are no relevant issues in repo

    issue_number="$(awk '{print $1}' <<< "${line}")"
    cask_name="$(awk '{print $4}' <<< "${line}")"

    cask-repair --pull origin --push origin --open-appcast --closes-issue "${issue_number}" --blind-submit "${cask_name}"

    if [[ "$?" -eq 0 ]]; then
      pr_number="$(ghi list --pulls --creator | sed -n 2p | awk '{print $1}')"
      ghi edit --label 'outdated appcast' "${pr_number}" &>/dev/null
      ghi comment --close --message "Closing in favour of #${pr_number}." "${issue_number}" &>/dev/null
    fi
  done
}

for repo in "${caskroom_repos[@]}"; do
  go_to_repo_and_update "${repo}"
  fix_outdated_appcasts
done