From af1c3143fac3b4b9a0d3f5ec85c02fb11faf75c1 Mon Sep 17 00:00:00 2001 From: Vadim Kharin Date: Mon, 13 Dec 2021 15:11:16 +0200 Subject: [PATCH 1/2] CR-7012 Step for report PR --- src/codefresh.api.js | 35 +++++++++++++++++++++++++++++++++++ src/configuration/prod.js | 3 +++ src/index.js | 12 +++++++++++- src/initializer.js | 15 ++++++++++++++- 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/codefresh.api.js b/src/codefresh.api.js index 5d8753a..3bc794e 100644 --- a/src/codefresh.api.js +++ b/src/codefresh.api.js @@ -30,6 +30,41 @@ class CodefreshAPI { } } + async createPullRequestV2(pullRequest) { + console.log(chalk.green(`Create pull request ${pullRequest.number}=${pullRequest.url}, image ${image}`)); + try { + const body = { + "operationName":"saveAnnotation", + "variables":{ + "annotation":{ + "logicEntityId": {"id": image}, + "entityType":"image", + "key": `#${pullRequest.number}`, + "type": "pr", + "pullRequestValue": { + url: pullRequest.url, + title: pullRequest.title, + committers: pullRequest.committers + } + } + }, + "query":"mutation saveAnnotation( $annotation: AnnotationArgs!) {\n saveAnnotation(annotation: $annotation)\n}" + } + return await rp({ + method: 'POST', + uri: `${host}/2.0/api/graphql`, + body, + headers: { + 'Authorization': `Bearer ${apiToken}` + }, + json: true + }); + } catch (e) { + return this._handleError(e); + } + + } + async createPullRequest(pullRequest) { console.log(chalk.green(`Create pull request ${pullRequest.number}=${pullRequest.url}, image ${image}`)); diff --git a/src/configuration/prod.js b/src/configuration/prod.js index 92dec3d..f6cb617 100644 --- a/src/configuration/prod.js +++ b/src/configuration/prod.js @@ -9,6 +9,9 @@ module.exports = { githubToken: process.env.GITHUB_TOKEN, workingDirectory: process.env.WORKING_DIRECTORY || '/codefresh/volume', contextName: process.env.GIT_PROVIDER_NAME, + v2: process.env.ARGO_PLATFORM === 'true', + githubAPI: process.env.GITHUB_API, + apiPathPrefix: process.env.API_PATH_PREFIX, // setup these variables during init phase diff --git a/src/index.js b/src/index.js index 2023cb3..99b71fa 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ const Promise = require('bluebird'); const chalk = require('chalk'); -const { image } = require('./configuration'); +const { image, v2 } = require('./configuration'); const codefreshApi = require('./codefresh.api'); const pullRequest = require('./pull-request'); const initializer = require('./initializer'); @@ -17,6 +17,16 @@ async function execute() { let isFailed = false; await Promise.all(pullRequests.map(async pr => { + if (v2) { + try { + console.log(`The image you are trying to enrich ${image} does not exist`); + await codefreshApi.createPullRequestV2(pr); + } catch (e) { + console.log(`Failed to assign pull request ${pr.number} to your image ${image}, reason ${chalk.red(e.message)}`); + } + return; + } + try { const result = await codefreshApi.createPullRequest(pr); if (!result) { diff --git a/src/initializer.js b/src/initializer.js index e54760f..1d18822 100644 --- a/src/initializer.js +++ b/src/initializer.js @@ -4,12 +4,25 @@ const config = require('./configuration'); class Initializer { - async _prepareConfig() { + async getToken() { + if (config.githubToken) { + return { + type: 'git.github', + token: config.githubToken, + apiHost: config.githubAPI || 'api.github.com', + apiPathPrefix: config.apiPathPrefix || '/' + }; + } const context = await codefreshApi.getContext(config.contextName); const type = context.spec.type; const token = context.spec.data.auth.password; const apiPathPrefix = _.get(context, 'spec.data.auth.apiPathPrefix', '/'); const apiHost = _.get(context, 'spec.data.auth.apiHost', 'api.github.com'); + return { type, token, apiPathPrefix, apiHost }; + } + + async _prepareConfig() { + const { type, token, apiHost, apiPathPrefix } = await this.getToken(); config.baseUrl = this._buildRequestUrl(apiHost, apiPathPrefix); config.contextType = type; From 9247405fbbd450b6413e1b3c33d6623dd537c754 Mon Sep 17 00:00:00 2001 From: Vadim Kharin Date: Mon, 13 Dec 2021 15:17:35 +0200 Subject: [PATCH 2/2] CR-7012 Step for report PR --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 99b71fa..840ed14 100644 --- a/src/index.js +++ b/src/index.js @@ -19,7 +19,7 @@ async function execute() { await Promise.all(pullRequests.map(async pr => { if (v2) { try { - console.log(`The image you are trying to enrich ${image} does not exist`); + console.log(`Creating argo platform annotation for ${image}`); await codefreshApi.createPullRequestV2(pr); } catch (e) { console.log(`Failed to assign pull request ${pr.number} to your image ${image}, reason ${chalk.red(e.message)}`);