diff --git a/.env.example b/.env.example index ffe3ff84..3fcea34a 100755 --- a/.env.example +++ b/.env.example @@ -12,4 +12,5 @@ GITLAB_CLIENT_SECRET="***" COVERAGE_DATA_URL=http://s3.xxx REDIRECT_URI=http://localhost:3000/login +APP_URI=http://localhost:3000 UPLOAD_URL=http://localhost:3000 diff --git a/packages/canyon-backend/src/upload/upload.controller.ts b/packages/canyon-backend/src/upload/upload.controller.ts index a42c217c..c734a24f 100755 --- a/packages/canyon-backend/src/upload/upload.controller.ts +++ b/packages/canyon-backend/src/upload/upload.controller.ts @@ -6,31 +6,76 @@ import { Query, Body, UseInterceptors, + Req, } from '@nestjs/common'; import { RawBodyMiddleware } from './raw-body.middleware'; import { uploadAnalyze } from './helpers/uploadAnalyze'; +import axios from 'axios'; +import * as process from 'process'; @Controller('upload') export class UploadController { constructor() {} @Post('v4') @HttpCode(200) - test(@Query() q, @Body() b) { - console.log(q, b); + test(@Query() q, @Body() b, @Req() req) { // TODO: 需要一个唯一id在post和put之间传递 - // 第一个参数 resultURL // 第二个参数 putURL return `${process.env.UPLOAD_URL}/upload/query - ${process.env.UPLOAD_URL}/upload/v4?query=1`; + ${process.env.UPLOAD_URL}${req.originalUrl}`; } @Put('v4') @HttpCode(200) @UseInterceptors(RawBodyMiddleware) - test1(@Query() q, @Body() buffer: Buffer) { - const cov = uploadAnalyze(buffer.toString()); - console.log(cov, 'cov'); + async test1(@Query() query, @Body() buffer: Buffer) { + const { commit, branch, instrument_cwd, slug } = query; + const { coverage } = uploadAnalyze(buffer.toString()); // TODO: 实现转换成canyon的数据结构 - return cov; + const projectID = await axios + .get( + `${process.env.GITLAB_URL}/api/v4/projects/${encodeURIComponent(slug)}`, + { + headers: { + // 我自己的token + 'PRIVATE-TOKEN': 'dpxTutmZv_wPogCkpCmc', + }, + }, + ) + .then((res) => { + return res.data.id; + }); + const url = process.env.APP_URI; + console.log({ + branch: branch, + commitSha: commit, + projectID: String(projectID), + instrumentCwd: instrument_cwd, + }); + await axios + .post( + `${url}/coverage/client`, + { + branch: branch, + coverage: coverage, + commitSha: commit, + projectID: String(projectID), + instrumentCwd: instrument_cwd, + }, + { + headers: { + Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InR6aGFuZ20iLCJpZCI6ODQxNywiaWF0IjoxNzAxODUxNTQ1LCJleHAiOjIwMTc0Mjc1NDV9.Bx8pYLNP9XlmrPDlHNCz_M1A-VoEbhTx0njYyTr9n6Y`, + }, + }, + ) + .then((res) => { + console.log(res.data.message); + }); + console.log('上传成功...'); + await axios.post(`${url}/coverage/triggeragg`, { + reportID: commit, + }); + console.log('触发聚合...'); + return 'ok'; } } diff --git a/packages/canyon-cli/src/helpers/web.ts b/packages/canyon-cli/src/helpers/web.ts index 078ccee8..7243d6f8 100755 --- a/packages/canyon-cli/src/helpers/web.ts +++ b/packages/canyon-cli/src/helpers/web.ts @@ -153,7 +153,7 @@ export async function uploadToCanyonPOST( * @param {Object} queryParams * @returns {string} */ -export function generateQuery(queryParams: Partial): string { +export function generateQuery(queryParams: Partial): string { return new URLSearchParams( Object.entries(queryParams).map(([key, value]) => [snakeCase(key), value]), ).toString() diff --git a/packages/canyon-cli/src/index.ts b/packages/canyon-cli/src/index.ts index 27777162..7b2ec791 100755 --- a/packages/canyon-cli/src/index.ts +++ b/packages/canyon-cli/src/index.ts @@ -390,7 +390,10 @@ export async function main( } } - const query = webHelpers.generateQuery(buildParams) + const query = webHelpers.generateQuery({ + ...buildParams, + instrumentCwd: projectRoot, + }) if (args.dryRun) { dryRun(uploadHost, token, query, uploadFile.toString(), args.source || '')