Skip to content

Instantly share code, notes, and snippets.

@masutaka
Last active June 19, 2018 14:42
Show Gist options
  • Save masutaka/3409b789859d4e7ed64fdcaec2de2d7a to your computer and use it in GitHub Desktop.
Save masutaka/3409b789859d4e7ed64fdcaec2de2d7a to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
exit 0 unless ENV['CIRCLECI']
require_relative './github_updater'
require_relative './pushover_glue'
result = GithubUpdater.run
if result.count > 0
PushoverGlue.new(result).notify
end
# frozen_string_literal: true
require 'fileutils'
class GithubUpdater
class << self
# @return [Array<String>] Updated files
def run
git_setup
git_clone
copy_files
result = []
Dir.chdir(TARGET_REPO_NAME) do
git_add
break unless need_git_commit?
git_commit
git_push
result = updated_files
end
result
end
TARGET_REPO_NAME = 'dotfiles-public'
private_constant :TARGET_REPO_NAME
TARGET_FILES = %w(
.ackrc
.aspell.conf
.emacs.d/init.el
.eslintrc.js
.gemrc
.nodenv/default-packages
.peco/config.json
.rbenv/default-gems
.tmux.conf
.zlogin
.zlogout
.zprofile
.zshenv
.zshrc
bin/ascii
bin/my-brew-bundle
bin/rm-local-branches
bin/trash
).freeze
private_constant :TARGET_FILES
private
def git_setup
system('git config --global user.name "Takashi Masuda"')
system('git config --global user.email [email protected]')
end
def git_clone
system("git clone --depth 1 [email protected]:masutaka/#{TARGET_REPO_NAME}.git")
end
def copy_files
TARGET_FILES.each do |f|
FileUtils.mkdir_p("#{TARGET_REPO_NAME}/#{File.dirname(f)}")
FileUtils.cp(f, "#{TARGET_REPO_NAME}/#{f}")
end
end
def git_add
system("git add #{TARGET_FILES.join(' ')}")
end
def need_git_commit?
!!(%x(git status -sb).match(/^M /))
end
def git_commit
system('git commit -m "Auto commit in CircleCI"')
end
def git_push
system('git push origin master')
end
def updated_files
%x(git show --pretty='' --name-only).split
end
end
end
# frozen_string_literal: true
require 'pushover'
class PushoverGlue
# @result [Array<String>] Updated files
def initialize(result)
@result = result
end
def notify
Pushover.notification(
user: pushover_user,
token: pushover_token,
device: device,
priority: priority(:normal),
title: title,
message: message,
url: url
)
end
private
attr_reader :result
def pushover_user
@pushover_user ||= ENV['PUSHOVER_USER_TOKEN']
end
def pushover_token
@pushover_token ||= ENV['PUSHOVER_APP_TOKEN']
end
def device
'iPhone'
end
def priority(level)
# https://pushover.net/api#priority
{ lowest: -2, low: -1, normal: 0, high: 1 }[level]
end
def title
'Update GitHub'
end
def message
@message ||= "Update %d files (%s): %s's build (#%s) in %s (%s)" %
[result.count, updated_files, ENV['CIRCLE_USERNAME'],
ENV['CIRCLE_BUILD_NUM'], repo_full_name, ENV['CIRCLE_BRANCH']]
end
def url
"https://circleci.com/gh/#{repo_full_name}/#{ENV['CIRCLE_BUILD_NUM']}"
end
def repo_full_name
@repo_full_name ||= "#{ENV['CIRCLE_PROJECT_USERNAME']}/#{ENV['CIRCLE_PROJECT_REPONAME']}"
end
def updated_files
result.join(', ')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment