1
1
const axios = require ( 'axios' ) ;
2
+ const fs = require ( 'fs' ) . promises ;
2
3
3
4
// GitHub API 配置
4
5
const API_URL = "https://api.github.com" ;
5
6
const REPO_OWNER = "SwiftGGTeam" ;
6
7
const REPO_NAME = "the-swift-programming-language-in-chinese" ;
7
8
const BRANCH = "swift-6-beta-translation" ;
8
9
9
- // TODO: 请在运行脚本之前设置您的个人访问令牌
10
+ // 您的个人访问令牌
10
11
const TOKEN = process . env . GITHUB_TOKEN ;
11
12
12
13
// 设置请求头
@@ -28,24 +29,24 @@ async function getExistingIssues() {
28
29
return issues ;
29
30
}
30
31
31
- async function createIssue ( title , body , labels , retries = 3 ) {
32
- for ( let i = 0 ; i < retries ; i ++ ) {
33
- try {
34
- const url = `${ API_URL } /repos/${ REPO_OWNER } /${ REPO_NAME } /issues` ;
35
- const data = { title, body, labels } ;
36
- const response = await axios . post ( url , data , { headers } ) ;
32
+ async function createIssue ( title , body , labels ) {
33
+ try {
34
+ const url = `${ API_URL } /repos/${ REPO_OWNER } /${ REPO_NAME } /issues` ;
35
+ const data = { title, body, labels } ;
36
+ const response = await axios . post ( url , data , { headers } ) ;
37
37
38
- if ( response . status === 201 ) {
39
- console . log ( `Issue created successfully: ${ title } ` ) ;
40
- return response . data ;
41
- }
42
- } catch ( error ) {
43
- console . error ( `Error creating issue (attempt ${ i + 1 } ):` , error . message ) ;
44
- if ( i === retries - 1 ) throw error ;
45
- await new Promise ( resolve => setTimeout ( resolve , 1000 * ( i + 1 ) ) ) ; // 等待时间递增
38
+ if ( response . status === 201 ) {
39
+ console . log ( `Issue created successfully: ${ title } ` ) ;
40
+ return response . data ;
41
+ } else {
42
+ console . log ( `Failed to create issue: ${ response . status } ` ) ;
43
+ console . log ( response . data ) ;
44
+ return null ;
46
45
}
46
+ } catch ( error ) {
47
+ console . error ( "Error creating issue:" , error . message ) ;
48
+ return null ;
47
49
}
48
- return null ;
49
50
}
50
51
51
52
async function getFilesInDirectory ( path ) {
@@ -64,27 +65,56 @@ async function getFilesInDirectory(path) {
64
65
}
65
66
}
66
67
67
- function formatIssueTitle ( filePath ) {
68
+ function formatIssueTitle ( filePath , estimatedTime ) {
68
69
const parts = filePath . split ( '/' ) ;
69
- const fileName = parts . pop ( ) . toLowerCase ( ) . replace ( '.md' , '' ) ;
70
+ const fileName = parts . pop ( ) . toLowerCase ( ) ;
70
71
const folderName = parts [ parts . length - 1 ] ;
71
- return `${ folderName } / ${ fileName } .md` ;
72
+
73
+ // Convert camelCase or PascalCase to kebab-case
74
+ const kebabFileName = fileName
75
+ . replace ( / ( [ a - z 0 - 9 ] ) ( [ A - Z ] ) / g, '$1-$2' )
76
+ . replace ( / ( [ A - Z ] ) ( [ A - Z ] [ a - z ] ) / g, '$1-$2' )
77
+ . toLowerCase ( ) ;
78
+
79
+ return `${ folderName } / ${ kebabFileName } ${ estimatedTime } ` ;
72
80
}
73
81
74
82
function formatIssueBody ( filePath ) {
75
83
const fileUrl = `https://github.com/${ REPO_OWNER } /${ REPO_NAME } /blob/${ BRANCH } /${ filePath } ` ;
76
84
return `翻译文件:${ fileUrl } \n请在认领任务前查看 Markdown 文件内容,并了解对应的 Swift 原文档链接和翻译预估时长` ;
77
85
}
78
86
87
+ async function getFileContent ( filePath ) {
88
+ try {
89
+ const url = `${ API_URL } /repos/${ REPO_OWNER } /${ REPO_NAME } /contents/${ filePath } ?ref=${ BRANCH } ` ;
90
+ const response = await axios . get ( url , { headers } ) ;
91
+ if ( response . status === 200 ) {
92
+ return Buffer . from ( response . data . content , 'base64' ) . toString ( 'utf-8' ) ;
93
+ }
94
+ } catch ( error ) {
95
+ console . error ( `Error getting file content: ${ error . message } ` ) ;
96
+ }
97
+ return null ;
98
+ }
99
+
100
+ function extractEstimatedTime ( content ) {
101
+ const match = content . match ( / 翻 译 估 计 用 时 : ( .* ) / ) ;
102
+ return match ? match [ 1 ] . trim ( ) : '' ;
103
+ }
104
+
79
105
async function processDirectory ( path , existingIssues ) {
80
106
const files = await getFilesInDirectory ( path ) ;
81
107
for ( const file of files ) {
82
- const issueTitle = formatIssueTitle ( file . path ) ;
83
- if ( ! existingIssues . has ( issueTitle ) ) {
84
- const issueBody = formatIssueBody ( file . path ) ;
85
- await createIssue ( issueTitle , issueBody , [ "Swift 6 beta translation" ] ) ;
86
- } else {
87
- console . log ( `Issue already exists: ${ issueTitle } ` ) ;
108
+ const content = await getFileContent ( file . path ) ;
109
+ if ( content ) {
110
+ const estimatedTime = extractEstimatedTime ( content ) ;
111
+ const issueTitle = formatIssueTitle ( file . path , estimatedTime ) ;
112
+ if ( ! existingIssues . has ( issueTitle ) ) {
113
+ const issueBody = formatIssueBody ( file . path ) ;
114
+ await createIssue ( issueTitle , issueBody , [ "Swift 6 beta translation" ] ) ;
115
+ } else {
116
+ console . log ( `Issue already exists: ${ issueTitle } ` ) ;
117
+ }
88
118
}
89
119
}
90
120
}
0 commit comments