Skip to content

Commit

Permalink
🚀 Commit by: beiklive <[email protected]>
Browse files Browse the repository at this point in the history
📝 Changes:
M .obsidian/workspace.json
?? "beiklive/Note_Other/04.使用Git Action自动初始化Gitalk评论.md"
  • Loading branch information
beiklive committed May 16, 2024
1 parent b39c88d commit 3d0e358
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 8 deletions.
17 changes: 9 additions & 8 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"state": {
"type": "markdown",
"state": {
"file": "beiklive/Note_C/01.控制台实现2048小游戏.md",
"file": "beiklive/Note_Other/04.使用Git Action自动初始化Gitalk评论.md",
"mode": "source",
"source": false
}
Expand Down Expand Up @@ -71,7 +71,7 @@
"state": {
"type": "outline",
"state": {
"file": "beiklive/Note_C/01.控制台实现2048小游戏.md"
"file": "beiklive/Note_Other/04.使用Git Action自动初始化Gitalk评论.md"
}
}
}
Expand All @@ -95,7 +95,7 @@
"state": {
"type": "backlink",
"state": {
"file": "beiklive/Note_C/01.控制台实现2048小游戏.md",
"file": "beiklive/Note_Other/04.使用Git Action自动初始化Gitalk评论.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -112,7 +112,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "beiklive/Note_C/01.控制台实现2048小游戏.md",
"file": "beiklive/Note_Other/04.使用Git Action自动初始化Gitalk评论.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand Down Expand Up @@ -158,6 +158,11 @@
},
"active": "b17e861e7080aef8",
"lastOpenFiles": [
"beiklive/Note_Other/03.wireshark-抓取指定程序.md",
"beiklive/Note_Other/04.使用Git Action自动初始化Gitalk评论.md",
"beiklive/01.学习/03.夏姬八学/01.other.md",
"beiklive/02.记录/02.折腾记录/00.obsidian.md",
"beiklive/Note_C/01.控制台实现2048小游戏.md",
"beiklive/Note_Godot/07.着色器学习(四) 一些内建变量的使用.md",
"tools/自动创建评论/GtalkComment.py",
"tools/自动创建评论/新建文本文档.txt",
Expand All @@ -166,7 +171,6 @@
"beiklive/Note_Godot/06.着色器学习(三) 内建函数.md",
"beiklive/Note_Godot/05.着色器学习(二) 内建变量.md",
"beiklive/Note_Godot/04.着色器学习(一)-基本语法.md",
"beiklive/Note_C/01.控制台实现2048小游戏.md",
"README.MD",
"tools/百度站长推送/baiduPush.py",
"tools/百度站长推送/新建文本文档.txt",
Expand All @@ -190,9 +194,6 @@
"beiklive/03.资源/03.Github项目/01.Github项目.md",
"beiklive/03.资源/02.工具网站/02.摸鱼网站.md",
"beiklive/03.资源/02.工具网站/01.工具网站.md",
"beiklive/03.资源/01.书籍文档/01.书籍文档.md",
"beiklive/02.记录/02.折腾记录/01.raspberrypi.md",
"beiklive/02.记录/02.折腾记录/00.obsidian.md",
"node_modules/npm/node_modules/promise-retry/node_modules/retry/equation.gif",
"node_modules/yamljs/test/lib/jasmine-2.0.0/jasmine_favicon.png",
"node_modules/stream-http/test/server/static/browserify.png",
Expand Down
183 changes: 183 additions & 0 deletions beiklive/Note_Other/04.使用Git Action自动初始化Gitalk评论.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
---
title: 使用Git Action自动初始化Gitalk评论
permalink: /pages/f9ee7418e4e04edf93c1fc8bce5afb62/
date: 2024-05-16 15:55:35
categories:
- 学习
- 夏姬八学
tags:
- github
- gitalk
author:
name: beiklive
link: https://github.com/beiklive
---

::: tip 前一篇
[wireshark-抓取指定程序](03.wireshark-抓取指定程序.md)
:::

因为每个页面的Gitalk评论都需要初始化一次才能正常评论,因此可以借助git action在提交时自动进行初始化


直接贴代码
### CI文件

`gitalkcomment.yml`

```yml
name: Execute python script to create github issue

on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: checkout actions
uses: actions/checkout@v1

- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7

- name: Create github issue
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
pip install requests
cd tools/自动创建评论
GITHUB_TOKEN=${GITHUB_TOKEN} python GtalkComment.py
```
### python脚本文件
```python
import requests
import subprocess
import os
import time

def get_issues_titles(repo_owner, repo_name, token):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues"
headers = {
'Authorization': f'token {token}',
'Accept': 'application/vnd.github.v3+json'
}
titles = []
page = 1

while True:
response = requests.get(url, headers=headers, params={'page': page, 'per_page': 100})
if response.status_code != 200:
print(f"Error: Unable to fetch issues (Status code: {response.status_code})")
break

issues = response.json()
if not issues:
break

for issue in issues:
titles.append(issue['title'])

page += 1

return titles

def create_github_issue(repo_owner, repo_name, token, issue_title, issue_body="", labels=[]):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues"
headers = {
'Authorization': f'token {token}',
'Accept': 'application/vnd.github.v3+json'
}
payload = {
'title': issue_title,
'body': issue_body,
'labels': labels # 标签名称列表,如 ['bug', 'enhancement']
}

response = requests.post(url, headers=headers, json=payload)

if response.status_code == 201:
print(f"Issue {issue_title} created successfully: {response.json()['html_url']}")
else:
print(f"Failed to create issue (Status code: {response.status_code})")
print(f"Error message: {response.text}")

def find_md_files(directory):
md_files = [] # 存储所有找到的 .md 文件路径

# 使用 os.walk 遍历指定目录及其子目录
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.md'): # 找到后缀为 .md 的文件
file_path = os.path.join(root, file)
md_files.append(file_path)

return md_files

def check_is_file_suffix_is_md(file_list):
md_list = []
for file_name in file_list:
if file_name.endswith('.md'):
md_list += [file_name]
return md_list

def Get_file_title(file_name):
# 获取文件标题
with open(file_name, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
if line.startswith('title:'):
return line.strip('title:').strip()

def Get_file_permalink(file_name):
# 获取文件链接
with open(file_name, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
if line.startswith('permalink:'):
return line.strip('permalink:').strip()

def CreateNewCommentIssue(repo_owner, repo_name, token):

# 获取前一次提交的文件列表
file_list = find_md_files("../../beiklive/")

# 筛选出md文件
md_list = check_is_file_suffix_is_md(file_list)

# 获取已经创建的issue列表
issue_list = get_issues_titles(repo_owner, repo_name, token)

for file_name in md_list:
print("------------------------------------")
title = Get_file_title(file_name)
# print(title)
if None != title:
search_str = '「评论」' + title
if search_str in issue_list:
print(f"Issue already exists for {title}")
pass
else:
print(f"Issue not exists for {title}")
permalink = Get_file_permalink(file_name)
print(permalink)
issue_title = search_str # 新Issue的标题
issue_body = f"页面: https://beiklive.github.io/vuepresswiki{permalink}" # 新Issue的内容(可选)
labels = ["Comment", "Gitalk", permalink] # 要添加的标签列表
create_github_issue(repo_owner, repo_name, token, issue_title, issue_body, labels)
time.sleep(10)


if __name__ == "__main__":
repo_owner = "beiklive" # 替换为仓库所有者
repo_name = "vuepresswiki" # 替换为仓库名称
token=os.environ.get('GITHUB_TOKEN') #申请的github访问口令

CreateNewCommentIssue(repo_owner, repo_name, token);

```

0 comments on commit 3d0e358

Please sign in to comment.