• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { getProjectPath } from '../utils/getProjectPath';
17import { logger, executeCommand } from 'src/util';
18import { ACCESS_TOKEN, PROJECT_PATH } from 'config.dev';
19const fetch = require('node-fetch');
20const fs = require('fs');
21
22async function getModifyFilelist(PRId: number) {
23    console.log(PRId);
24    const axios = require('axios');
25    const owner = 'OpenHarmony';
26    const repo = 'applications_app_samples';
27    let filesModify = [];
28
29    // 重置本地代码
30    let re1 = await executeCommand('git fetch --all', PROJECT_PATH);
31    let re2 = await executeCommand('git reset --hard origin/master', PROJECT_PATH);
32    let re3 = await executeCommand('git pull', PROJECT_PATH);
33
34    await axios.get(`https://gitee.com/api/v5/repos/${owner}/${repo}/pulls/${PRId}/files?access_token=${ACCESS_TOKEN}`)
35        .then((response: { data: any }) => {
36            response.data.forEach(async (item: { raw_url: string, filename: string, patch: { new_path: string } }) => {
37                console.log(JSON.stringify(item.patch.new_path));
38                // 获取修改代码源文件
39                const dirArr = item.filename.split('/');
40                const savePath = PROJECT_PATH + '/' + item.filename;
41                if (dirArr.length >= 2) {
42                    let dir = PROJECT_PATH + '/' + dirArr[0]
43                    for (let i = 1; i < dirArr.length; i++) {
44                        if (!fs.existsSync(dir)) {
45                            fs.mkdirSync(dir)
46                        }
47                        dir += ('/' + dirArr[i])
48                    }
49                }
50
51                fetch(item.raw_url).then(res => {
52                    const dest = fs.createWriteStream(savePath)
53                    res.body.pipe(dest)
54                }).catch(err => {
55                    logger('GetModifyFile').log(`保存文件失败,err: ${JSON.stringify(err)}`);
56                })
57
58                filesModify.push(item.patch.new_path);
59            })
60            return filesModify;
61        })
62        .catch((error: any) => {
63            console.log(JSON.stringify(error))
64            return JSON.stringify(error)
65        })
66    console.log(filesModify)
67    return getProjectPath(PRId, filesModify);
68}
69export { getModifyFilelist }