Last active
August 29, 2015 14:16
-
-
Save rsky/c0909353f56768cf7a8d to your computer and use it in GitHub Desktop.
.apk/.ipaをDeployGateにアップロード、配布ページをChatWorkで通知する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
""" | |
.apk/.ipaをDeployGateにアップロード、配布ページをChatWorkで通知する | |
""" | |
from __future__ import unicode_literals | |
from __future__ import print_function | |
from argparse import ArgumentParser | |
import os | |
import requests | |
# DeployGate Settings | |
DEPLOYGATE_OWNER = '__OWNER__' | |
DEPLOYGATE_API_TOKEN = '__API_TOKEN__' | |
DEPLOYGATE_DISTRIBUTION_KEYS = { | |
'__FILE_NAME__': '__DISTRIBUTYION_KEY__', | |
} | |
# ChatWork Settings | |
CHATWORK_API_TOKEN = '__API_TOKEN__' | |
CHATWORK_ROOM_ID = '__ROOM_ID__' | |
# URLs | |
DEPLOYGATE_DISTRIBUTION_URL = 'https://deploygate.com/distributions/{key}' | |
DEPLOYGATE_POST_APP_URL = 'https://deploygate.com/api/users/{owner}/apps' | |
CHATWORK_POST_MESSAGE_URL = 'https://api.chatwork.com/v1/rooms/{room_id}/messages' | |
# Misc. | |
ENVIRONMENT_NAMES = { | |
'__PACKAGE_NAME__': '__ENVIRONMENT_NAME__', | |
} | |
def upload_app(owner, token, filename, distribution_key, | |
release_note=None, verbose=False, dry_run=False): | |
""" | |
アプリケーションをDeployGateにアップロード、配布ページを更新する | |
(前提として、初回アップロード済み、配布ページが存在すること) | |
""" | |
url = DEPLOYGATE_POST_APP_URL.format(owner=owner) | |
files = {'file': open(filename, 'rb')} | |
payload = { | |
'token': token, | |
'distribution_key': distribution_key, | |
} | |
if release_note: | |
payload['release_note'] = release_note | |
if dry_run: | |
print('DRY RUN: POST {url}'.format(url=url)) | |
payload['token'] = '********' | |
payload['file'] = filename | |
print(payload) | |
return True | |
r = requests.post(url, data=payload, files=files) | |
if verbose: | |
print('POST {url} -> {status}'.format(url=url, status=r.status_code)) | |
print(r.text) | |
result = r.json() | |
if r.status_code == 200 and result['error'] == False: | |
return result['results'] | |
else: | |
return None | |
def notify_update(room_id, token, upload_result, distribution_key, | |
release_note=None, verbose=False, dry_run=False): | |
""" | |
アプリケーションの更新をChatWorkで通知する | |
""" | |
url = CHATWORK_POST_MESSAGE_URL.format(room_id=room_id) | |
message_template = ('【{os_name}{env_name}】{name} ({package_name})' | |
' v{version_name} ({version_code}) #{revision}' | |
'をDeployGateにアップロードしました。\n') | |
env_name = ENVIRONMENT_NAMES.get(upload_result['package_name'], '') | |
message = message_template.format(env_name=env_name, **upload_result) | |
if release_note: | |
message += '\n' + release_note.strip() + '\n\n' | |
message += DEPLOYGATE_DISTRIBUTION_URL.format(key=distribution_key) | |
payload = {'body': message} | |
headers = {'X-ChatWorkToken': token} | |
if dry_run: | |
print('DRY RUN: POST {url}'.format(url=url)) | |
print(payload) | |
return True | |
r = requests.post(url, data=payload, headers=headers) | |
if verbose: | |
print('POST {url} -> {status}'.format(url=url, status=r.status_code)) | |
print(r.text) | |
return r.status_code == requests.status_codes.codes.ok | |
def main(): | |
parser = ArgumentParser(description=''' | |
.apk/.ipaをDeployGateにアップロード、ChatWorkで通知する'''.strip()) | |
parser.add_argument('filename', metavar='APP', nargs=1, | |
help='.apk OR .ipa') | |
parser.add_argument('--release-note', dest='release_note', | |
help='リリースノート') | |
parser.add_argument('--dg-owner', dest='owner', | |
default=DEPLOYGATE_OWNER, | |
help='DeployGate ユーザー名') | |
parser.add_argument('--dg-token', dest='dg_token', | |
default=DEPLOYGATE_API_TOKEN, | |
help='DeployGate APIトークン') | |
parser.add_argument('--dg-distribution-key', dest='distribution_key', | |
help='DeployGate 配布ページキー') | |
parser.add_argument('--cw-token', dest='cw_token', | |
default=CHATWORK_API_TOKEN, | |
help='ChatWork APIトークン') | |
parser.add_argument('--cw-room-id', dest='room_id', | |
default=CHATWORK_ROOM_ID, | |
help='ChatWork チャットルームID') | |
parser.add_argument('-v', '--verbose', dest='verbose', | |
action='store_true', default=False) | |
parser.add_argument('-n', '--dry-run', dest='dry_run', | |
action='store_true', default=False) | |
args = parser.parse_args() | |
filename = args.filename[0].decode('utf-8') | |
default_key = DEPLOYGATE_DISTRIBUTION_KEYS.get(os.path.basename(filename)) | |
distribution_key = args.distribution_key or default_key | |
release_note = args.release_note | |
if release_note is not None: | |
release_note = release_note.decode('utf-8') | |
verbose = args.verbose | |
dry_run = args.dry_run | |
if not distribution_key: | |
raise ValueError('no distribution_key for {0}'.format(filename)) | |
result = upload_app(args.owner, args.dg_token, | |
filename, distribution_key, | |
release_note, verbose, dry_run) | |
if not dry_run and result is not None: | |
notify_update(args.room_id, args.cw_token, | |
result, distribution_key, | |
release_note, verbose, dry_run) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment