Skip to content

CR-7012 Step for report PR #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/codefresh.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
Expand Down
3 changes: 3 additions & 0 deletions src/configuration/prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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) {
Expand Down
15 changes: 14 additions & 1 deletion src/initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down