Skip to content

Commit

Permalink
chore: update actions script
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Nov 28, 2021
1 parent 44ea6ce commit a2abf37
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
35 changes: 13 additions & 22 deletions app/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
import rsa
from retry import retry

from app import log
from app.const import DOMAIN, USER_AGENT, TIMEOUT, PUBLIC_KEY
from app.const import domain, ua, timeout, pubkey

requests.packages.urllib3.disable_warnings()


class Action:
"""Gitee Pages Action"""

def __init__(self, username: str, password: str,
repo: str, branch: str = 'master',
directory: str = '', https: str = 'true'):
Expand Down Expand Up @@ -44,40 +41,40 @@ def get_csrf_token(html: str) -> str:
requests.exceptions.ConnectionError),
tries=3, delay=1, backoff=2)
def login(self):
login_index_url = f'{DOMAIN}/login'
check_login_url = f'{DOMAIN}/check_user_login'
login_index_url = f'{domain}/login'
check_login_url = f'{domain}/check_user_login'
form_data = {'user_login': self.username}

index_headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;'
'q=0.9,image/webp,image/apng,*/*;'
'q=0.8,application/signed-exchange;v=b3;q=0.9',
'Host': 'gitee.com',
'User-Agent': USER_AGENT
'User-Agent': ua
}

resp = self.session.get(url=login_index_url,
headers=index_headers,
timeout=TIMEOUT,
timeout=timeout,
verify=False)
csrf_token = Action.get_csrf_token(resp.text)
headers = {
'Referer': login_index_url,
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': csrf_token,
'User-Agent': USER_AGENT
'User-Agent': ua
}
self.session.post(url=check_login_url,
headers=headers,
data=form_data,
timeout=TIMEOUT,
timeout=timeout,
verify=False)

# https://assets.gitee.com/assets/encrypt.js
separator = '$gitee$'
data = f'{csrf_token[-8:]}{separator}{self.password}'
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(PUBLIC_KEY.encode())
encrypt_data = rsa.encrypt(data.encode(), pubkey)
pk = rsa.PublicKey.load_pkcs1_openssl_pem(pubkey.encode())
encrypt_data = rsa.encrypt(data.encode(), pk)
encrypt_data = base64.b64encode(encrypt_data).decode()

form_data = {
Expand All @@ -92,7 +89,7 @@ def login(self):
res = self.session.post(url=login_index_url,
headers=index_headers,
data=form_data,
timeout=TIMEOUT,
timeout=timeout,
verify=False).text

case1 = ['"message": "帐号或者密码错误"', '"message": "Invalid email or password."',
Expand All @@ -112,7 +109,6 @@ def login(self):
'official account "Gitee" to bind account to turn off authentication.')
if not any(e in res for e in case4):
raise Exception(f'Unknown error occurred in login method, resp: {res}')
log.info('Login successfully')

@retry((requests.exceptions.ReadTimeout,
requests.exceptions.ConnectTimeout,
Expand All @@ -121,7 +117,7 @@ def login(self):
def rebuild_pages(self):
if '/' not in self.repo:
self.repo = f'{self.username}/{self.repo}'
pages_url = f'{DOMAIN}/{self.repo}/pages'
pages_url = f'{domain}/{self.repo}/pages'
rebuild_url = f'{pages_url}/rebuild'

pages = self.session.get(pages_url)
Expand All @@ -132,7 +128,7 @@ def rebuild_pages(self):
'Referer': pages_url,
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': csrf_token,
'User-Agent': USER_AGENT
'User-Agent': ua
}
form_data = {
'branch': self.branch,
Expand All @@ -142,18 +138,13 @@ def rebuild_pages(self):
resp = self.session.post(url=rebuild_url,
headers=headers,
data=form_data,
timeout=TIMEOUT,
timeout=timeout,
verify=False)
if resp.status_code != 200:
raise Exception(f'Rebuild page error, status code: {resp.status_code}, resp: {resp.text}')
if '请勿频繁更新部署,稍等1分钟再试试看' in resp.text:
raise Exception('Do not deploy frequently, try again one minute later.')
log.info('Rebuild Gitee Pages successfully')

def run(self):
log.info('Welcome to use Gitee Pages Action ❤\n\n'
'📕 Getting Started Guide: https://github.com/marketplace/actions/gitee-pages-action\n'
'📣 Maintained by Yang Libin: https://github.com/yanglbme\n')
self.login()
self.rebuild_pages()
log.info('Success, thanks for using @yanglbme/gitee-pages-action!')
10 changes: 5 additions & 5 deletions app/const.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
TIMEOUT = 6
DOMAIN = 'https://gitee.com'
timeout = 6
domain = 'https://gitee.com'

USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' \
'(KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'
ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' \
'(KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'

PUBLIC_KEY = """-----BEGIN PUBLIC KEY-----
pubkey = """-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIrn+WB2Yi4ABAL5Tq6E09tumY
qVTFdpU01kCDUmClczJOCGZriLNMrshmN9NJxazpqizPthwS1OIK3HwRLEP9D3GL
7gCnvN6lpIpoVwppWd65f/rK2ewv6dstN0fCmtVj4WsLUchWlgNuVTfWljiBK/Dc
Expand Down
20 changes: 19 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
from app import log
from app.action import Action

author = {
'name': 'Yang Libin',
'link': 'https://github.com/yanglbme'
}
marketplace = 'https://github.com/marketplace/actions/gitee-pages-action'

log.info(f'Welcome to use Gitee Pages Action ❤\n\n'
f'📕 Getting Started Guide: {marketplace}\n'
f'📣 Maintained by {author["name"]}: {author["link"]}\n')

try:
username = core.get_input('gitee-username', required=True)
password = core.get_input('gitee-password', required=True)
Expand All @@ -11,7 +21,15 @@
branch = core.get_input('branch') or 'master'
directory = core.get_input('directory') or ''
https = core.get_input('https') or 'true'

action = Action(username, password, repo, branch, directory, https)
action.run()

action.login()
log.info('Login successfully')

action.rebuild_pages()
log.info('Rebuild Gitee Pages successfully')

log.info('Success, thanks for using @yanglbme/gitee-pages-action!')
except Exception as e:
log.set_failed(str(e))

0 comments on commit a2abf37

Please sign in to comment.