Skip to content

Commit

Permalink
Bitbucket ok ! :)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvuillam committed Jan 3, 2025
1 parent a8a26bd commit ef75682
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"extract-zip": "^2.0.1",
"farmhash": "^4.0.1",
"fast-xml-parser": "^4.5.1",
"form-data": "^4.0.1",
"fs-extra": "^11.2.0",
"fs-readdir-recursive": "^1.1.0",
"glob": "^11.0.0",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/hardis/project/deploy/smart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ If you want to disable the calculation and display of Flow Visual Git Diff in Pu
'$ sf hardis:project:deploy:smart --check --testlevel RunRepositoryTestsExceptSeeAllData',
'$ sf hardis:project:deploy:smart',
'$ FORCE_TARGET_BRANCH=preprod NODE_OPTIONS=--inspect-brk sf hardis:project:deploy:smart --check --websocket localhost:2702 --skipauth --target-org [email protected]',
'$ SYSTEM_ACCESSTOKEN=toto SYSTEM_COLLECTIONURI=titi BUILD_REPOSITORY_ID=tata SYSTEM_PULLREQUEST_PULLREQUESTID=tutu FORCE_TARGET_BRANCH=uat NODE_OPTIONS=--inspect-brk sf hardis:project:deploy:smart --check --websocket localhost:2702 --skipauth --target-org [email protected]'
'$ SYSTEM_ACCESSTOKEN=xxxxxx SYSTEM_COLLECTIONURI=https://dev.azure.com/xxxxxxx/ SYSTEM_TEAMPROJECT="xxxxxxx" BUILD_REPOSITORY_ID=xxxxx SYSTEM_PULLREQUEST_PULLREQUESTID=1418 FORCE_TARGET_BRANCH=uat NODE_OPTIONS=--inspect-brk sf hardis:project:deploy:smart --check --websocket localhost:2702 --skipauth --target-org [email protected]',
'$ CI_SFDX_HARDIS_BITBUCKET_TOKEN=xxxxxx BITBUCKET_WORKSPACE=sfdxhardis-demo BITBUCKET_REPO_SLUG=test BITBUCKET_BUILD_NUMBER=1 BITBUCKET_BRANCH=uat BITBUCKET_PR_ID=2 FORCE_TARGET_BRANCH=uat NODE_OPTIONS=--inspect-brk sf hardis:project:deploy:smart --check --websocket localhost:2702 --skipauth --target-org [email protected]'
];


Expand Down
34 changes: 20 additions & 14 deletions src/common/gitProvider/bitbucket.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { GitProviderRoot } from './gitProviderRoot.js';
import c from 'chalk';
import fs from "fs-extra";
import FormData from 'form-data'
import * as path from "path";
import { PullRequestMessageRequest, PullRequestMessageResult } from './index.js';
import { git, uxLog } from '../utils/index.js';
import bbPkg, { Schema } from 'bitbucket';
import { CONSTANTS } from '../../config/index.js';
import * as path from 'path';
const { Bitbucket } = bbPkg;

export class BitbucketProvider extends GitProviderRoot {
private bitbucket: InstanceType<typeof Bitbucket>;
public serverUrl: string = '';
public serverUrl: string = 'https://bitbucket.org';
public token: string;

constructor() {
Expand All @@ -26,7 +27,7 @@ export class BitbucketProvider extends GitProviderRoot {

public async getCurrentJobUrl(): Promise<string | null> {
if (process.env.BITBUCKET_WORKSPACE && process.env.BITBUCKET_REPO_SLUG && process.env.BITBUCKET_BUILD_NUMBER) {
const jobUrl = `https://bitbucket.org/${process.env.BITBUCKET_WORKSPACE}/${process.env.BITBUCKET_REPO_SLUG}/pipelines/results/${process.env.BITBUCKET_BUILD_NUMBER}`;
const jobUrl = `${this.serverUrl}/${process.env.BITBUCKET_WORKSPACE}/${process.env.BITBUCKET_REPO_SLUG}/pipelines/results/${process.env.BITBUCKET_BUILD_NUMBER}`;
return jobUrl;
}
uxLog(
Expand All @@ -42,7 +43,7 @@ export class BitbucketProvider extends GitProviderRoot {

public async getCurrentBranchUrl(): Promise<string | null> {
if (process.env.BITBUCKET_WORKSPACE && process.env.BITBUCKET_REPO_SLUG && process.env.BITBUCKET_BRANCH) {
const currentBranchUrl = `https://bitbucket.org/${process.env.BITBUCKET_WORKSPACE}/${process.env.BITBUCKET_REPO_SLUG}/branch/${process.env.BITBUCKET_BRANCH}`;
const currentBranchUrl = `${this.serverUrl}/${process.env.BITBUCKET_WORKSPACE}/${process.env.BITBUCKET_REPO_SLUG}/branch/${process.env.BITBUCKET_BRANCH}`;
return currentBranchUrl;
}
uxLog(
Expand Down Expand Up @@ -250,6 +251,7 @@ export class BitbucketProvider extends GitProviderRoot {
posted: (pullRequestComment?.data?.id || -1) > 0,
providerResult: pullRequestComment,
};
uxLog(this, c.grey(`[Bitbucket integration] Updated Pull Request comment ${existingCommentId}`));
return prResult;
} else {
// Create new comment if no existing comment was found
Expand All @@ -266,6 +268,11 @@ export class BitbucketProvider extends GitProviderRoot {
posted: (pullRequestComment?.data?.id || -1) > 0,
providerResult: pullRequestComment,
};
if (prResult.posted) {
uxLog(this, c.grey(`[Bitbucket integration] Posted Pull Request comment on ${pullRequestId}`));
} else {
uxLog(this, c.yellow(`[Bitbucket integration] Unable to post Pull Request comment on ${pullRequestId}:\n${JSON.stringify(pullRequestComment, null, 2)}`));
}
return prResult;
}
}
Expand All @@ -280,28 +287,27 @@ export class BitbucketProvider extends GitProviderRoot {
// Upload the image to Bitbucket
public async uploadImage(localImagePath: string): Promise<string | null> {
try {
const imageBuffer = fs.readFileSync(localImagePath);
const imageBlob = new Blob([imageBuffer]);
const imageName = path.basename(localImagePath);
const filesForm = new FormData();
filesForm.append('files', imageBlob, imageName);
const attachmentResponse = await this.bitbucket.repositories.createIssueAttachments({
filesForm.append("files", fs.createReadStream(localImagePath));
const attachmentResponse = await this.bitbucket.repositories.createDownload({
workspace: process.env.BITBUCKET_WORKSPACE || "",
repo_slug: process.env.BITBUCKET_REPO_SLUG || "",
issue_id: process.env.BITBUCKET_PR_ID || "", // Attach to the pull request as an "issue"
_body: filesForm,
_body: filesForm as any,
});
if (attachmentResponse?.data?.links?.self?.href) {
uxLog(this, c.grey(`[Bitbucket Integration] Image uploaded for comment: ${attachmentResponse.data.links.self.href}`));
return attachmentResponse.data.links.self.href;
if (attachmentResponse) {
const imageRef = `${this.serverUrl}/${process.env.BITBUCKET_WORKSPACE}/${process.env.BITBUCKET_REPO_SLUG}/downloads/${imageName}`;
uxLog(this, c.grey(`[Bitbucket Integration] Image uploaded for comment: ${imageRef}`));
return imageRef;
}
else {
uxLog(this, c.yellow(`[Bitbucket Integration] Image uploaded but unable to get URL from response\n${JSON.stringify(attachmentResponse, null, 2)}`));
}
} catch (e) {
uxLog(this, c.yellow(`[Bitbucket Integration] Error while uploading image ${localImagePath}\n${(e as Error).message}`));
uxLog(this, c.yellow(`[Bitbucket Integration] Error while uploading image as issue attachment ${localImagePath}\n${(e as Error).message}`));
}
return null;
}


}
3 changes: 3 additions & 0 deletions src/common/utils/flowVisualiser/flowParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ function getGeneralInfoMd(flowObj: any, flowMap: FlowMap) {
(metadataValue.TriggerType ? "Type: <b>" + stringifyValue(metadataValue.TriggerType, "triggerType", Object.keys(flowObjCopy)) + "</b><br/>" : '')
startElementReference = flowObj.startElementReference;
}
else if (flowObj.startElementReference) {
startElementReference = flowObj.startElementReference;
}
const generalInfoMd = mdEndSection(buildGenericMarkdownTable(flowObjCopy, ["allFields"], "## General Information", Object.keys(flowMap)) + detailTablesMd);
if (startNodeLabel.endsWith("<br/>")) {
startNodeLabel = startNodeLabel.slice(0, -5);
Expand Down
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6188,6 +6188,15 @@ form-data@^4.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.1.tgz#ba1076daaaa5bfd7e99c1a6cb02aa0a5cff90d48"
integrity sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

formdata-node@^4.3.2:
version "4.4.1"
resolved "https://registry.npmjs.org/formdata-node/-/formdata-node-4.4.1.tgz"
Expand Down

0 comments on commit ef75682

Please sign in to comment.