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..840ed14 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(`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)}`); + } + 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;