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 { Logger } from '@nestjs/common'; 17import { exec } from 'child_process'; 18import axios from 'axios'; 19import { ACCESS_TOKEN, PROJECT_PATH } from 'config.dev'; 20 21const FormData = require('form-data'); 22 23// 日志打印 24const logger = (appName: string): Logger => new Logger(appName, { timestamp: true }); 25 26// 执行命令行 27const executeCommand = (command: string, cwd: string = './'): Promise<string> => { 28 return new Promise((resolve, reject) => { 29 exec(command, { cwd }, (error, stdout, stderr) => { 30 if (error) { 31 logger('Command').error(JSON.stringify(error)); 32 reject(error); 33 } else { 34 logger('Command').log(JSON.stringify(stdout)); 35 resolve(JSON.stringify(stdout)); 36 } 37 }); 38 }); 39}; 40 41const apiUrl = 'https://gitee.com/api/v5'; 42const owner = 'OpenHarmony'; 43const repo = 'applications_app_samples'; 44 45// 根据PR号获取 46const getPRRequest = async (prNumber: string) => { 47 try { 48 const getPRUrl = `${apiUrl}/repos/${owner}/${repo}/pulls/${prNumber}`; 49 const response = await axios.get(getPRUrl, { 50 headers: { 51 'Content-Type': 'application/json', 52 'Authorization': `Bearer ${ACCESS_TOKEN}` 53 } 54 }); 55 logger('GetPR').log('获取PR成功 '); 56 return response.data; 57 } catch (error) { 58 logger('GetPR').error('获取PR失败 ' + JSON.stringify(error)); 59 } 60} 61 62const pullPRCode = async (prNumber: string) => { 63 let PRMsg 64 try { 65 PRMsg = await getPRRequest(prNumber); 66 } catch (error) { 67 logger('pullPRCode').error('获取PR失败'); 68 return { 69 status: 'failed', 70 msg: '获取PR失败' + JSON.stringify(error) 71 }; 72 } 73 let msg = ''; 74 try { 75 await executeCommand(`git remote add ${prNumber} git@gitee.com:${PRMsg.user.login}/${PRMsg.head.repo.path}.git`, '../../applications_app_samples'); 76 await executeCommand(`git fetch ${prNumber}`, '../../applications_app_samples'); 77 await executeCommand(`git checkout ${prNumber}/${PRMsg.head.ref}`, '../../applications_app_samples'); 78 logger('pullPRCode').log('拉取PR代码成功'); 79 msg = '拉取PR代码成功'; 80 } catch (error) { 81 logger('pullPRCode').error('拉取PR代码失败 ' + JSON.stringify(error)); 82 msg = '拉取PR代码失败 ' + JSON.stringify(error); 83 } 84 return { 85 status: 'success', 86 msg 87 }; 88} 89 90 91// 评论指定PR 92const commitPRRequest = async (prNumber: number, content: string) => { 93 const commitPRUrl = `${apiUrl}/repos/${owner}/${repo}/pulls/${prNumber}/comments`; 94 const form = new FormData(); 95 form.append('access_token', ACCESS_TOKEN); 96 form.append('body', content); 97 return axios.post(commitPRUrl, form, { 98 headers: { 99 'Content-Type': 'multipart/form-data' 100 } 101 }).then(response => { 102 if (response.status === 201) { 103 logger('CommitPR').log('评论指定PR成功'); 104 return { 105 status: 'success' 106 }; 107 } 108 logger('CommitPR').log('评论指定PR失败'); 109 return { 110 status: 'failed' 111 }; 112 }, error => { }); 113} 114 115 116export { 117 executeCommand, 118 logger, 119 getPRRequest, 120 commitPRRequest, 121 pullPRCode 122};