Skip to content

Commit

Permalink
feat: update main script
Browse files Browse the repository at this point in the history
Output error message in Gitee Pages Action
  • Loading branch information
yanglbme committed Nov 17, 2020
1 parent 7cb0cd6 commit 54baa29
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
注:

1. 如果在使用过程中遇到了 Gitee 短信验证码导致 Gitee Pages Action 无法自动登录部署 Pages,参考 [#6](https://github.com/yanglbme/gitee-pages-action/issues/6) 。
1. Gitee 仓库名严格区分大小写,若 Action 报 `deploy error occurred, message: 'NoneType' object has no attribute 'group'` 错误,请检查 `gitee-repo` 参数是否正确配置,参考 [#10](https://github.com/yanglbme/gitee-pages-action/issues/10) 。
1. Gitee 仓库名严格区分大小写,若 Action 报 `deploy error occurred` 错误,请检查 `gitee-repo` 参数是否正确配置,参考 [#10](https://github.com/yanglbme/gitee-pages-action/issues/10) 。
1. 若执行 Sync 过程中 Action 提示 `Your push would publish a private email address`,参考 [#18](https://github.com/yanglbme/gitee-pages-action/issues/18) 。

## 谁在使用
Expand Down
162 changes: 89 additions & 73 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64
import os
import re
import sys

import requests
import requests.packages.urllib3
Expand Down Expand Up @@ -34,12 +35,17 @@ def __init__(self, username, password, repo,
self.https = https

@staticmethod
def get_csrf_token(html):
return re.search(
def get_csrf_token(html) -> str:
res = re.search(
'<meta content="authenticity_token" name="csrf-param" />(.*?)'
'<meta content="(.*?)" name="csrf-token" />', html, re.S).group(2)
'<meta content="(.*?)" name="csrf-token" />', html, re.S)
if res is None:
print('::error::deploy error occurred, '
'check your input `gitee-repo`')
sys.exit(1)
return res.group(2)

def login(self):
def login(self) -> bool:
login_index_url = 'https://gitee.com/login'
check_login_url = 'https://gitee.com/check_user_login'
form_data = {'user_login': self.username}
Expand All @@ -51,79 +57,89 @@ def login(self):
'Host': 'gitee.com',
'User-Agent': self.ua.random
}
try:
resp = self.session.get(url=login_index_url,
headers=index_headers,
timeout=Action.timeout,
verify=False)
csrf_token = Action.get_csrf_token(resp.text)
headers = {
'Referer': 'https://gitee.com/login',
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': csrf_token,
'User-Agent': self.ua.random
}
self.session.post(url=check_login_url,
headers=headers,
data=form_data,
timeout=Action.timeout,
verify=False)
data = f'{csrf_token}$gitee${self.password}'
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(PUBLIC_KEY.encode())
encrypt_data = rsa.encrypt(data.encode(), pubkey)
encrypt_data = base64.b64encode(encrypt_data).decode()

form_data = {
'encrypt_key': 'password',
'utf8': '✓',
'authenticity_token': csrf_token,
'redirect_to_url': '',
'user[login]': self.username,
'encrypt_data[user[password]]': encrypt_data,
'user[remember_me]': 1
}
resp = self.session.post(url=login_index_url,
headers=index_headers,
data=form_data,
timeout=Action.timeout,
verify=False)
return '个人主页' in resp.text or '我的工作台' in resp.text
except Exception as e:
print(f'login error occurred, message: {e}')
exit(1)

def rebuild_pages(self):

resp = self.session.get(url=login_index_url,
headers=index_headers,
timeout=Action.timeout,
verify=False)
csrf_token = Action.get_csrf_token(resp.text)
headers = {
'Referer': 'https://gitee.com/login',
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': csrf_token,
'User-Agent': self.ua.random
}
self.session.post(url=check_login_url,
headers=headers,
data=form_data,
timeout=Action.timeout,
verify=False)
data = f'{csrf_token}$gitee${self.password}'
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(PUBLIC_KEY.encode())
encrypt_data = rsa.encrypt(data.encode(), pubkey)
encrypt_data = base64.b64encode(encrypt_data).decode()

form_data = {
'encrypt_key': 'password',
'utf8': '✓',
'authenticity_token': csrf_token,
'redirect_to_url': '',
'user[login]': self.username,
'encrypt_data[user[password]]': encrypt_data,
'user[remember_me]': 1
}
resp = self.session.post(url=login_index_url,
headers=index_headers,
data=form_data,
timeout=Action.timeout,
verify=False)
if '个人主页' in resp.text or '我的工作台' in resp.text:
return True
if '"message": "帐号或者密码错误"' in resp.text or \
'"message": "not_found_in_database"' in resp.text or \
'"message": "not_found_and_show_captcha"' in resp.text:
print('::error::wrong username or password, login failed')
elif '"message": "captcha_expired"' in resp.text or \
'"message": "captcha_fail"' in resp.text:
print('::error::need captcha validation, please visit '
'https://gitee.com/login, login to validate your account')
elif '"message": "phone_captcha_fail"' in resp.text:
print('::error::need phone captcha validation, please follow '
'gitee wechat subscription and bind your account')
sys.exit(1)

def rebuild_pages(self) -> bool:
pages_url = f'https://gitee.com/{self.repo}/pages'
rebuild_url = f'{pages_url}/rebuild'
try:
pages = self.session.get(pages_url)
csrf_token = Action.get_csrf_token(pages.text)
headers = {
'Content-Type': 'application/x-www-form-urlencoded; '
'charset=UTF-8',
'Referer': pages_url,
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': csrf_token,
'User-Agent': self.ua.random
}
form_data = {
'branch': self.branch,
'build_directory': self.directory,
'force_https': self.https
}
resp = self.session.post(url=rebuild_url,
headers=headers,
data=form_data,
timeout=Action.timeout,
verify=False)
return resp.status_code == 200
except Exception as e:
print(f'deploy error occurred, message: {e}')
exit(1)

pages = self.session.get(pages_url)
csrf_token = Action.get_csrf_token(pages.text)
headers = {
'Content-Type': 'application/x-www-form-urlencoded; '
'charset=UTF-8',
'Referer': pages_url,
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': csrf_token,
'User-Agent': self.ua.random
}
form_data = {
'branch': self.branch,
'build_directory': self.directory,
'force_https': self.https
}
resp = self.session.post(url=rebuild_url,
headers=headers,
data=form_data,
timeout=Action.timeout,
verify=False)
if resp.status_code != 200:
print(f'::error::rebuild status code: {resp.status_code}')
sys.exit(1)
print('rebuild Gitee Pages successfully, congratulation!')
return True

def run(self):
res = self.login() and self.rebuild_pages()
print(f'::set-output name=result::{res}')
self.login() and self.rebuild_pages()


if __name__ == '__main__':
Expand Down

0 comments on commit 54baa29

Please sign in to comment.