Skip to content

在使用过程中发现的一些小优化 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/update_readme.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: update_readme
name: update_DailyLC

on:
workflow_dispatch:
Expand All @@ -13,7 +13,7 @@ on:
- main.py

jobs:
update-readme:
update-DailyLC:
runs-on: ubuntu-latest
if: github.repository_owner_id == github.event.issue.user.id || github.event_name == 'push'
steps:
Expand All @@ -30,12 +30,12 @@ jobs:
python -m pip install --upgrade pip
pip install requests PyGithub

- name: Get issue data and update README.md
- name: Get issue data and update DailyLC.md
run: python main.py --issue_number '${{ github.event.issue.number }}'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Push README
- name: Push DailyLC
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
Expand Down
31 changes: 13 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
# 📝 Algorithm
算法题练习记录博客基于GitHub Action和GitHub Issue功能记录
算法题练习记录博客基于GitHub Action和GitHub Issue实现)。可通过提交issue,自动将刷题记录更新在项目的DailyLC.md中,并将issue内容备份在项目backup文件夹下。

## 🎯 Calendar
## 🎯 Usage
1. fork这个项目(注意激活fork后的项目的Actions, 并将项目的Settings -> issue打开)
2. 每当你刷完一道算法题,可在你的项目上提交一个issue(!!issue title需要以‘R'开头,作为和真正issue的区分标识),推荐将'R+所刷题目的标题'(比如R110.平衡二叉树)作为issue title,将题目的归属标签(比如二叉树、搜索)作为issue Labels,将你想记录的对这道题的思考作为issue的description。此时[DailyLC.md](./DailyLC.md)

1) Calendar部分会基于issue创建日期在日历上标记一个star

2) Records部分会新增一行记录, 各列分别记录:

* 2023/12
#: issue号
Title: 截去R的issue title(点击可跳转至你所创建的issue)
Tag: issue Labels
Date: issue创建时间

3. 同时,backup文件夹下会以“issue号+#+截去R的issue title"作为文件名,生成一个对issue的备份文件,备份issue的description和comments
4. 当你编辑issue的title或description、修改issue的Labels、issue有新comment或者issue的comment被编辑时, [DailyLC.md](./DailyLC.md)和backup文件夹下issue的备份文件都会进行相应更新(FIXME: 当issue title被改变时,会生成一个新的备份文件,但原来的备份文件并不会被删除)

|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|27|28|29|30|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|


## 🍃 Records

|#|Title|Tag|Date|
|:-:|:-:|:-:|:-:|
|3|[100. 相同的树](https://github.com/Doragd/Algorithm/issues/3)|`二叉树` `递归`|2023-12-10T12:14:09Z|
|2|[110. 平衡二叉树](https://github.com/Doragd/Algorithm/issues/2)|`二叉树` `递归`|2023-12-10T10:56:53Z|
24 changes: 24 additions & 0 deletions backup/DailyLC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 📝 Algorithm
算法题练习记录博客,基于GitHub Action和GitHub Issue功能记录

## 🎯 Calendar



* 2023/12

|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|27|28|29|30|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|


## 🍃 Records

|#|Title|Tag|Date|
|:-:|:-:|:-:|:-:|
|3|[100. 相同的树](https://github.com/Doragd/Algorithm/issues/3)|`二叉树` `递归`|2023-12-10T12:14:09Z|
|2|[110. 平衡二叉树](https://github.com/Doragd/Algorithm/issues/2)|`二叉树` `递归`|2023-12-10T10:56:53Z|
17 changes: 9 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def update_records(issue, issue_number=None):
if issue_number is None:
issue_number = os.getenv("ISSUE_NUMBER")

issue_title = issue["title"]
issue_title = issue["title"][1:]
issue_labels = ["`" + label["name"] + "`" for label in issue["labels"]]
issue_link = issue["html_url"]

with open("README.md", "r+") as file:
with open("DailyLC.md", "r") as file:
lines = file.readlines()

table_start_index = None
Expand All @@ -45,9 +45,9 @@ def update_records(issue, issue_number=None):
for i in range(len(lines)):
if lines[i].strip() == "|#|Title|Tag|Date|":
table_start_index = i + 2
if lines[i].strip().startswith(f"|{issue_number}|"):
if lines[i].strip().startswith(f"|{issue_number}|") and table_start_index:
existing_issue_index = i
if table_start_index and existing_issue_index:
if existing_issue_index:
break

new_line = f"|{issue_number}|[{issue_title}]({issue_link})|{' '.join(issue_labels)}|{issue['created_at']}|\n"
Expand All @@ -56,10 +56,11 @@ def update_records(issue, issue_number=None):
else:
lines.insert(table_start_index, new_line)

file.seek(0)
with open('DailyLC.md', 'w') as file:
file.writelines(lines)
file.close()

return "Successfully updated Records of README.md"
return "Successfully updated Records of DailyLC.md"

def update_star(issue):
created_at_str = issue['created_at']
Expand Down Expand Up @@ -92,7 +93,7 @@ def backup_issue_as_md(issue, issue_number):
if issue_number is None:
issue_number = os.getenv("ISSUE_NUMBER")

issue_title = issue["title"]
issue_title = issue["title"][1:]
issue_body = issue['body']
issue_labels = ["`" + label['name'] + "`" for label in issue['labels']]
issue_link = issue['html_url']
Expand Down Expand Up @@ -124,7 +125,7 @@ def backup_issue_as_md(issue, issue_number):
def main(issue_number):
try:
issue = get_issue(issue_number)
if issue is not None:
if issue is not None and issue["title"].startswith("R"):
print(update_records(issue, issue_number))
print(update_star(issue))
print(backup_issue_as_md(issue, issue_number))
Expand Down
20 changes: 10 additions & 10 deletions mdcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ def get_dict(lang='en'):
return dic

def update_calendar(year, month, day, with_isoweek=False, start_from_Sun=False, lang="en"):
# Check if README.md exists, if not, create one
if not os.path.exists('README.md'):
with open('README.md', 'w') as f:
print("The file README.md does not exist. Creating a new one...")
# Check if DailyLC.md exists, if not, create one
if not os.path.exists('DailyLC.md'):
with open('DailyLC.md', 'w') as f:
print("The file DailyLC.md does not exist. Creating a new one...")
f.write('## 🎯 Calendar\n\n## Records\n\n')

# Read the content of README.md
with open('README.md', 'r') as file:
# Read the content of DailyLC.md
with open('DailyLC.md', 'r') as file:
content = file.read()

# Extract the part between "## 🎯 Calendar" to the start of the next section "## Records".
calendar_section_match = re.search("## 🎯 Calendar(.*)(?=## 🍃 Records)", content, re.DOTALL)

# If "## 🎯 Calendar" section doesn't exist or there is no calendar data
if calendar_section_match is None:
return "The 'Calendar' section does not exist in README.md or there is no calendar data."
return "The 'Calendar' section does not exist in DailyLC.md or there is no calendar data."

calendar_section = calendar_section_match.group(1)

Expand Down Expand Up @@ -100,13 +100,13 @@ def update_calendar(year, month, day, with_isoweek=False, start_from_Sun=False,
star_flag = False
calendar_section_lines[i] = "|".join(day_cells)

# Replace 'Calendar' section in README.md with the updated section
# Replace 'Calendar' section in DailyLC.md with the updated section
new_content = re.sub(r"## 🎯 Calendar(.*)(?=## 🍃 Records)", "\n".join(calendar_section_lines), content, flags=re.DOTALL)

with open('README.md', 'w') as file:
with open('DailyLC.md', 'w') as file:
file.write(new_content)

return "Successfully updated Calendar of README.md"
return "Successfully updated Calendar of DailyLC.md"

if __name__ == "__main__":
argv = sys.argv
Expand Down