@docupace/pipeline 1.0.4 → 1.0.5-4.ervins

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/.prettierrc.cjs +12 -0
  2. package/README.md +136 -0
  3. package/bin/azuread_update_callbacks_for_app.sh +58 -0
  4. package/bin/next-changeset-validate.sh +8 -8
  5. package/eslint.config.js +26 -0
  6. package/next/changeset.sh +75 -75
  7. package/next/copy.sh +34 -34
  8. package/next/deploy.sh +40 -40
  9. package/next/execute.sh +123 -117
  10. package/next/sonar.sh +23 -23
  11. package/package.json +50 -28
  12. package/src/auth0/alias.js +72 -0
  13. package/src/auth0/cli.js +76 -0
  14. package/src/auth0/configure.js +783 -0
  15. package/src/auth0/export.js +43 -0
  16. package/src/auth0/metadata.js +132 -0
  17. package/src/auth0/vault.js +107 -0
  18. package/src/changeset/changeset-config.js +34 -0
  19. package/src/changeset/cli.js +200 -245
  20. package/src/changeset/create.js +205 -0
  21. package/src/changeset/package-util.js +86 -0
  22. package/src/changeset/util.js +43 -0
  23. package/src/changeset/validate.js +84 -44
  24. package/src/changeset/version.js +102 -0
  25. package/src/jenkins/setParams.groovy +58 -0
  26. package/src/next/alias.js +53 -0
  27. package/src/next/apache.js +73 -0
  28. package/src/next/cli.js +228 -59
  29. package/src/next/config.js +137 -0
  30. package/src/next/connect.js +227 -0
  31. package/src/next/copy.js +61 -27
  32. package/src/next/deploy.js +595 -128
  33. package/src/next/haproxy.js +71 -0
  34. package/src/next/jenkins.js +123 -0
  35. package/src/next/jira.js +84 -0
  36. package/src/next/logger.js +17 -0
  37. package/src/next/pipline.js +114 -0
  38. package/src/next/publish.js +71 -46
  39. package/src/next/pull.js +7 -7
  40. package/src/next/sonar.js +23 -23
  41. package/src/next/util.js +93 -0
  42. package/src/next/vault.js +45 -0
  43. package/terraform/apply.sh +37 -0
  44. package/terraform/init.sh +72 -0
  45. package/terraform/plan.sh +76 -0
  46. package/terraform/validate.sh +17 -0
@@ -0,0 +1,71 @@
1
+ import { $ } from 'zx';
2
+ import fs from 'fs-extra';
3
+ import path from 'path';
4
+
5
+ export async function haproxySync() {
6
+ const cluster = process.env.HAPROXY_CLUSTER;
7
+ if (cluster !== 'preprod' && cluster !== 'stage' && cluster !== 'prod-www3') {
8
+ console.log('Error: Cluster must be preprod or stage');
9
+ process.exit(1);
10
+ }
11
+
12
+ const repoPath = '/root/repo/docupace-ansible';
13
+ const inventory = path.join(repoPath, `inventories/${cluster}/inventory`);
14
+
15
+ // Create haproxy.sh
16
+ const cluserHostMap = {
17
+ preprod: {
18
+ host: '10.10.28.198',
19
+ branch: 'master',
20
+ },
21
+ stage: {
22
+ host: '10.12.28.10',
23
+ branch: 'INFP-6565-haproxy-prod-implementation',
24
+ },
25
+ 'prod-www3': {
26
+ host: '10.13.28.45',
27
+ branch: 'INFP-6565-haproxy-prod-implementation',
28
+ },
29
+ };
30
+
31
+ const scriptContent = `
32
+ #!/bin/bash
33
+
34
+ [[ ! -f ${inventory} ]] && ( echo "Alert: no inventory file: ${inventory}" ; exit 1 )
35
+ cd ${repoPath}
36
+ pwd
37
+ git stash
38
+ git fetch --all
39
+ git pull origin master
40
+ git checkout ${cluserHostMap[cluster].branch}
41
+ git submodule update --remote
42
+ git submodule foreach --recursive git checkout master
43
+ git submodule foreach --recursive git pull origin master
44
+
45
+ ansible-playbook -i inventories/${cluster}/inventory haproxy.yml -e "cluster=${cluster} -e build=true"
46
+
47
+ echo "proceeding"
48
+ echo "committing changes"
49
+ git add ${cluster}-haproxy.cfg
50
+ git commit -m "Sync haproxy"
51
+ git push origin master
52
+ `;
53
+
54
+ await fs.writeFile('haproxy.sh', scriptContent);
55
+
56
+ // Private key
57
+ const haproxyPrivateKey = process.env.HAPROXY_PRIVATE_KEY;
58
+ if (!haproxyPrivateKey) {
59
+ console.log('Error: HAPROXY_PRIVATE_KEY not set.');
60
+ process.exit(1);
61
+ }
62
+
63
+ await fs.writeFile('private.key', Buffer.from(haproxyPrivateKey, 'base64'));
64
+ await fs.chmod('private.key', 0o600);
65
+
66
+ // Copy script to remote
67
+ await $`scp -i private.key -o StrictHostKeyChecking=no haproxy.sh root@${cluserHostMap[cluster].host}:/tmp/haproxy.sh`;
68
+
69
+ // Execute script on remote
70
+ await $`ssh -i private.key -o StrictHostKeyChecking=no root@${cluserHostMap[cluster].host} "bash /tmp/haproxy.sh"`;
71
+ }
@@ -0,0 +1,123 @@
1
+ import axios from 'axios';
2
+
3
+ const CORE_JENKINS_URL = 'http://devops.corp.docupace.com';
4
+ const CORE_JOB_NAME = 'auto-deploy-site';
5
+ const CORE_USERNAME = process.env.CORE_JENKINS_USERNAME;
6
+ const CORE_API_TOKEN = process.env.CORE_JENKINS_API_TOKEN;
7
+
8
+ const CONNECT_JENKINS_URL = 'http://usdoc-s-buildui.corp.docupace.com/jenkins';
9
+ const CONNECT_JOB_NAME = 'docupace_build_and_run_site_v2';
10
+ const CONNECT_USERNAME = process.env.CONNECT_JENKINS_USERNAME;
11
+ const CONNECT_API_TOKEN = process.env.CONNECT_JENKINS_API_TOKEN;
12
+
13
+ export async function deployCore(env, site, liquibase, gitBranch) {
14
+ const parameters = {
15
+ environment: env,
16
+ site: site.toUpperCase(),
17
+ runLiquibase: liquibase,
18
+ gitBranch,
19
+ };
20
+
21
+ return callJenkinsJob({
22
+ jenkinsUrl: CORE_JENKINS_URL,
23
+ jobName: CORE_JOB_NAME,
24
+ auth: { username: CORE_USERNAME, password: CORE_API_TOKEN },
25
+ parameters,
26
+ });
27
+ }
28
+
29
+ // eslint-disable-next-line no-unused-vars
30
+ export async function deployConnect(env, site, liquibase, branch) {
31
+ const parameters = {
32
+ BUILD_ENVIRONMENT: env,
33
+ BUILD_SITE: site,
34
+ RUN_LIQUIBASE: liquibase,
35
+ };
36
+
37
+ return callJenkinsJob({
38
+ jenkinsUrl: CONNECT_JENKINS_URL,
39
+ jobName: CONNECT_JOB_NAME,
40
+ auth: { username: CONNECT_USERNAME, password: CONNECT_API_TOKEN },
41
+ parameters,
42
+ });
43
+ }
44
+
45
+ export async function callJenkinsJob({
46
+ jenkinsUrl,
47
+ jobName,
48
+ auth,
49
+ parameters,
50
+ }) {
51
+ try {
52
+ // Trigger the job
53
+ console.log('Triggering Jenkins job...');
54
+ const buildResponse = await axios.post(
55
+ `${jenkinsUrl}/job/${jobName}/buildWithParameters`,
56
+ new URLSearchParams(parameters),
57
+ {
58
+ auth,
59
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
60
+ validateStatus: (status) => status === 201 || status === 302, // Accept 201 and 302 as valid statuses
61
+ },
62
+ );
63
+
64
+ const queueUrl = buildResponse.headers.location;
65
+ console.log(`Job queued at: ${queueUrl}`);
66
+
67
+ // Poll queue for executable URL
68
+ let jobUrl = null;
69
+ while (!jobUrl) {
70
+ const queueResponse = await axios.get(`${queueUrl}api/json`, {
71
+ auth,
72
+ });
73
+ jobUrl = queueResponse.data.executable?.url || null;
74
+
75
+ if (!jobUrl) {
76
+ console.log('Waiting for job to start...');
77
+ await new Promise((resolve) => setTimeout(resolve, 5000));
78
+ }
79
+ }
80
+
81
+ jobUrl = jobUrl.replaceAll('https', 'http');
82
+
83
+ console.log(`Job started at: ${jobUrl}`);
84
+
85
+ // Stream console output
86
+ const consoleUrl = `${jobUrl}consoleText`;
87
+ let lastSize = 0;
88
+
89
+ console.log('Streaming job output...');
90
+ while (true) {
91
+ const consoleResponse = await axios.get(consoleUrl, {
92
+ auth,
93
+ responseType: 'text',
94
+ });
95
+
96
+ const consoleText = consoleResponse.data;
97
+ const newOutput = consoleText.slice(lastSize);
98
+ process.stdout.write(newOutput); // Write new output to console
99
+ lastSize = consoleText.length;
100
+
101
+ // Check job status
102
+ const jobResponse = await axios.get(`${jobUrl}api/json`, {
103
+ auth,
104
+ });
105
+ const result = jobResponse.data.result;
106
+
107
+ if (result) {
108
+ console.log(`\nJob completed with result: ${result}`);
109
+ if (result === 'SUCCESS') {
110
+ return;
111
+ } else {
112
+ throw new Error('Jenkins job failed');
113
+ }
114
+ }
115
+
116
+ // Wait before polling again
117
+ await new Promise((resolve) => setTimeout(resolve, 5000));
118
+ }
119
+ } catch (error) {
120
+ console.error('Error:', error.message);
121
+ throw error;
122
+ }
123
+ }
@@ -0,0 +1,84 @@
1
+ import axios from 'axios';
2
+
3
+ // Create preconfigured axios instance for Jira
4
+ const jiraClient = axios.create({
5
+ baseURL: 'https://docupacellc.atlassian.net',
6
+ auth: {
7
+ username: process.env.JIRA_API_EMAIL,
8
+ password: process.env.JIRA_API_KEY,
9
+ },
10
+ headers: {
11
+ 'Content-Type': 'application/json',
12
+ },
13
+ });
14
+
15
+ /**
16
+ * Updates the component version custom field in Jira
17
+ * @param {string} issueKey - The Jira issue key
18
+ * @param {string} packageName - The package name
19
+ * @param {string} version - The version to set
20
+ */
21
+ export async function updateComponentVersion(issueKey, packageName, version) {
22
+ const component = packageName.includes('/')
23
+ ? packageName.split('/')[1]
24
+ : packageName;
25
+ console.log(
26
+ `Updating ${issueKey} for package ${packageName} translated to component ${component} with version ${version}`,
27
+ );
28
+ try {
29
+ // First get current field value
30
+ const response = await jiraClient.get(
31
+ `/rest/api/2/issue/${issueKey}?fields=customfield_10458`,
32
+ );
33
+
34
+ let currentValue = response.data.fields.customfield_10458 || '';
35
+ let components = new Map();
36
+
37
+ try {
38
+ // Parse existing components
39
+ if (currentValue) {
40
+ currentValue.split(';').forEach((entry) => {
41
+ const [comp, ver] = entry.split(':');
42
+ components.set(comp, ver);
43
+ });
44
+ }
45
+ // Update or add new component version
46
+ } catch {
47
+ console.log('Patch version has incorrect data overriding');
48
+ }
49
+
50
+ components.set(component, version);
51
+
52
+ // Convert back to string format
53
+ const newValue = Array.from(components.entries())
54
+ .map(([comp, ver]) => `${comp}:${ver}`)
55
+ .join(';');
56
+
57
+ // Update the field
58
+ await jiraClient.put(`/rest/api/2/issue/${issueKey}`, {
59
+ fields: {
60
+ customfield_10458: newValue,
61
+ },
62
+ });
63
+
64
+ console.log(`Updated component versions for ${issueKey}: ${newValue}`);
65
+ } catch (error) {
66
+ console.error(`Error updating Jira component version: ${error.message}`);
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Makes a request to the Jira API using preconfigured client
72
+ * @param {string} endpoint - The API endpoint (starting with /)
73
+ * @param {Object} options - Request options (method, body, etc)
74
+ * @returns {Promise<any>} The response data
75
+ */
76
+ export async function makeJiraRequest(endpoint, options = {}) {
77
+ try {
78
+ const response = await jiraClient.get(endpoint, options);
79
+ return response.data;
80
+ } catch (error) {
81
+ console.error(`Error making Jira request: ${error.message}`);
82
+ throw error;
83
+ }
84
+ }
@@ -0,0 +1,17 @@
1
+ import { $ as original$ } from 'zx';
2
+
3
+ // Wrapped version of $
4
+ export const $ = async (strings, ...values) => {
5
+ const command = strings.reduce(
6
+ (acc, str, i) => acc + str + (values[i] ?? ''),
7
+ '',
8
+ );
9
+ console.log(`Executing: ${command}`);
10
+ try {
11
+ const result = await original$(strings, ...values);
12
+ return result;
13
+ } catch (error) {
14
+ console.error(`Error executing: ${command}`, error);
15
+ throw error;
16
+ }
17
+ };
@@ -0,0 +1,114 @@
1
+ import axios from 'axios';
2
+ import { getBitbucketInfo } from './config.js';
3
+
4
+ const BITBUCKET_BASE_URL = 'https://api.bitbucket.org/2.0';
5
+
6
+ const BITBUCKET_WS_URL = 'https://bitbucket.org/dp-cloud';
7
+
8
+ async function triggerPipelineByName(
9
+ repository,
10
+ pipelineName,
11
+ branch,
12
+ variables,
13
+ ) {
14
+ const { username, password, workspace } = getBitbucketInfo();
15
+ const url = `${BITBUCKET_BASE_URL}/repositories/${workspace}/${repository}/pipelines/`;
16
+ const auth = {
17
+ username: username,
18
+ password: password,
19
+ };
20
+
21
+ const payload = {
22
+ target: {
23
+ type: 'pipeline_ref_target',
24
+ ref_type: 'branch',
25
+ ref_name: branch,
26
+ selector: {
27
+ type: 'custom',
28
+ pattern: pipelineName,
29
+ },
30
+ },
31
+ variables: variables
32
+ ? Object.entries(variables).map(([key, value]) => ({
33
+ key,
34
+ value,
35
+ secured: false, // Set to true if the variable should be secure
36
+ }))
37
+ : undefined,
38
+ };
39
+
40
+ try {
41
+ const response = await axios.post(url, payload, { auth });
42
+ return response.data;
43
+ } catch (error) {
44
+ console.error(
45
+ 'Error triggering custom pipeline:',
46
+ error.response?.data || error.message,
47
+ );
48
+ throw error;
49
+ }
50
+ }
51
+
52
+ async function getPipelineStatus(repository, pipelineUUID) {
53
+ const { username, password, workspace } = getBitbucketInfo();
54
+ const url = `${BITBUCKET_BASE_URL}/repositories/${workspace}/${repository}/pipelines/${pipelineUUID}`;
55
+ const auth = {
56
+ username: username,
57
+ password: password,
58
+ };
59
+
60
+ try {
61
+ const response = await axios.get(url, { auth });
62
+ return response.data.state.name;
63
+ } catch (error) {
64
+ console.error(
65
+ 'Error fetching pipeline status:',
66
+ error.response?.data || error.message,
67
+ );
68
+ throw error;
69
+ }
70
+ }
71
+
72
+ export async function runPipeline(
73
+ repository,
74
+ pipelineName,
75
+ branch,
76
+ variables,
77
+ wait = false,
78
+ ) {
79
+ try {
80
+ console.log(
81
+ `Triggering custom pipeline: ${pipelineName} for branch: ${branch}...`,
82
+ );
83
+ const pipeline = await triggerPipelineByName(
84
+ repository,
85
+ pipelineName,
86
+ branch,
87
+ variables,
88
+ );
89
+
90
+ const pipelineUUID = pipeline.uuid;
91
+ const buildNumber = pipeline.build_number;
92
+
93
+ console.log(`Pipeline triggered with UUID: ${pipelineUUID}`);
94
+ console.log(
95
+ `See results here: ${BITBUCKET_WS_URL}/${repository}/piplines/results/${buildNumber}`,
96
+ );
97
+
98
+ if (!wait) {
99
+ return;
100
+ }
101
+
102
+ let status = '';
103
+ do {
104
+ console.log('Checking pipeline status...');
105
+ await new Promise((resolve) => setTimeout(resolve, 10000)); // Wait 10 seconds
106
+ status = await getPipelineStatus(repository, pipelineUUID);
107
+ console.log(`Pipeline status: ${status}`);
108
+ } while (['IN_PROGRESS', 'PENDING', 'RUNNING'].includes(status));
109
+
110
+ console.log(`Pipeline completed with status: ${status}`);
111
+ } catch (error) {
112
+ console.error('Error monitoring pipeline:', error.message);
113
+ }
114
+ }
@@ -1,46 +1,71 @@
1
- import { $ } from 'zx';
2
-
3
- export async function publish(GIT_SHA) {
4
- const BITBUCKET_REPO_SLUG = process.env.BITBUCKET_REPO_SLUG;
5
- const BUILD_IMAGE_NAME = `${BITBUCKET_REPO_SLUG}:build-${process.env.BITBUCKET_BUILD_NUMBER}`;
6
- const IMAGE_NAME = `${BITBUCKET_REPO_SLUG}:sha-${GIT_SHA}`;
7
- const JF_REGISTRY_FULL_URL = `${process.env.JF_REGISTRY_URL}/${process.env.JF_REGISTRY_REPO_NAME}`;
8
-
9
- console.log(`Publishing Docker image: ${IMAGE_NAME}`);
10
-
11
- // Docker login
12
- await $`echo ${process.env.JF_REGISTRY_PASSWORD_DEV} | docker login --username ${process.env.JF_REGISTRY_USER_DEV} --password-stdin ${process.env.JF_REGISTRY_URL}`;
13
-
14
- // Check if the image already exists
15
- try {
16
- await $`docker manifest inspect ${JF_REGISTRY_FULL_URL}/${IMAGE_NAME}`;
17
- console.log(`Tag sha-${GIT_SHA} already exists in the remote repository. Skipping push.`);
18
- } catch {
19
- await $`docker tag ${IMAGE_NAME} ${JF_REGISTRY_FULL_URL}/${IMAGE_NAME}`;
20
- await $`docker push ${JF_REGISTRY_FULL_URL}/${IMAGE_NAME}`;
21
- }
22
-
23
- // Tag and push build image
24
- await $`docker tag ${IMAGE_NAME} ${JF_REGISTRY_FULL_URL}/${BUILD_IMAGE_NAME}`;
25
- await $`docker push ${JF_REGISTRY_FULL_URL}/${BUILD_IMAGE_NAME}`;
26
-
27
- // Fetch Git tags pointing to the current HEAD
28
- const gitTagsOutput = (await $`git tag --points-at HEAD`).stdout.trim();
29
- const gitTags = gitTagsOutput.split('\n').filter((tag) => tag);
30
-
31
- // Tag and push each Git tag as a Docker tag
32
- if (gitTags.length > 0) {
33
- console.log(`Found Git tags pointing to HEAD: ${gitTags.join(', ')}`);
34
- for (const gitTag of gitTags) {
35
- const dockerTag = `${JF_REGISTRY_FULL_URL}/${BITBUCKET_REPO_SLUG}:${gitTag}`;
36
- console.log(`Tagging and pushing Docker image for Git tag: ${gitTag}`);
37
- await $`docker tag ${IMAGE_NAME} ${dockerTag}`;
38
- await $`docker push ${dockerTag}`;
39
- }
40
- } else {
41
- console.log('No Git tags found pointing to the current HEAD.');
42
- }
43
-
44
- // Docker logout
45
- await $`docker logout ${process.env.JF_REGISTRY_URL}`;
46
- }
1
+ import { $ } from './logger.js';
2
+
3
+ const {
4
+ JF_REGISTRY_URL,
5
+ JF_REGISTRY_REPO_NAME,
6
+ JF_REGISTRY_PASSWORD_DEV,
7
+ JF_REGISTRY_USER_DEV,
8
+ BITBUCKET_REPO_SLUG,
9
+ BITBUCKET_BUILD_NUMBER,
10
+ APP_NAME,
11
+ } = process.env;
12
+
13
+ const JF_REGISTRY_FULL_URL = `${JF_REGISTRY_URL}/${JF_REGISTRY_REPO_NAME}`;
14
+
15
+ async function loginRegistry() {
16
+ await $`echo ${JF_REGISTRY_PASSWORD_DEV} | docker login --username ${JF_REGISTRY_USER_DEV} --password-stdin ${JF_REGISTRY_URL}`;
17
+ }
18
+
19
+ async function logoutRegistry() {
20
+ await $`docker logout ${JF_REGISTRY_URL}`;
21
+ }
22
+
23
+ export async function publish(gitSha) {
24
+ process.env.DOCKER_HOST = '';
25
+
26
+ console.log(JSON.stringify(process.env));
27
+
28
+ const IMAGE_PREFIX = APP_NAME ?? BITBUCKET_REPO_SLUG;
29
+ const BUILD_IMAGE_NAME = `${IMAGE_PREFIX}:build-${BITBUCKET_BUILD_NUMBER}`;
30
+ const IMAGE_NAME = `${IMAGE_PREFIX}:sha-${gitSha}`;
31
+
32
+ console.log(`Publishing Docker image ${IMAGE_NAME}...`);
33
+
34
+ await loginRegistry();
35
+
36
+ let manifestExists = false;
37
+ try {
38
+ $.verbose = false; // Silence output temporarily
39
+ await $`docker manifest inspect ${JF_REGISTRY_FULL_URL}/${IMAGE_NAME}`;
40
+ manifestExists = true;
41
+ } catch {
42
+ manifestExists = false;
43
+ } finally {
44
+ $.verbose = true; // Restore normal output
45
+ }
46
+
47
+ if (manifestExists) {
48
+ console.log(
49
+ `Tag sha-${gitSha} already exists in the remote repository. Skipping push.`,
50
+ );
51
+ } else {
52
+ await $`docker tag ${IMAGE_NAME} ${JF_REGISTRY_FULL_URL}/${IMAGE_NAME}`;
53
+ await $`docker push ${JF_REGISTRY_FULL_URL}/${IMAGE_NAME}`;
54
+ }
55
+
56
+ await $`docker tag ${IMAGE_NAME} ${JF_REGISTRY_FULL_URL}/${BUILD_IMAGE_NAME}`;
57
+ await $`docker push ${JF_REGISTRY_FULL_URL}/${BUILD_IMAGE_NAME}`;
58
+
59
+ const gitTags = (await $`git tag --points-at HEAD`).stdout
60
+ .trim()
61
+ .split('\n')
62
+ .filter(Boolean);
63
+ for (const gitTag of gitTags) {
64
+ const version = gitTag.includes('@') ? 'v' + gitTag.split('@')[1] : gitTag;
65
+ const tagName = `${IMAGE_PREFIX}:${version}`;
66
+ await $`docker tag ${IMAGE_NAME} ${JF_REGISTRY_FULL_URL}/${tagName}`;
67
+ await $`docker push ${JF_REGISTRY_FULL_URL}/${tagName}`;
68
+ }
69
+
70
+ await logoutRegistry();
71
+ }
package/src/next/pull.js CHANGED
@@ -1,7 +1,7 @@
1
- import { $ } from 'zx';
2
-
3
- export async function pull(imageName) {
4
- const JF_REGISTRY_FULL_URL = `${process.env.JF_REGISTRY_URL}/${process.env.JF_REGISTRY_REPO_NAME}`;
5
- console.log(`Pulling Docker image: ${JF_REGISTRY_FULL_URL}/${imageName}`);
6
- await $`docker pull ${JF_REGISTRY_FULL_URL}/${imageName}`;
7
- }
1
+ import { $ } from 'zx';
2
+
3
+ export async function pull(imageName) {
4
+ const JF_REGISTRY_FULL_URL = `${process.env.JF_REGISTRY_URL}/${process.env.JF_REGISTRY_REPO_NAME}`;
5
+ console.log(`Pulling Docker image: ${JF_REGISTRY_FULL_URL}/${imageName}`);
6
+ await $`docker pull ${JF_REGISTRY_FULL_URL}/${imageName}`;
7
+ }
package/src/next/sonar.js CHANGED
@@ -1,23 +1,23 @@
1
- import { $ } from 'zx';
2
-
3
- export async function sonar(GIT_SHA) {
4
- const IMAGE_NAME = `${process.env.BITBUCKET_REPO_SLUG}:sha-${GIT_SHA}`;
5
- const SONAR_CONTAINER_NAME = `${process.env.BITBUCKET_REPO_SLUG}-${GIT_SHA}`;
6
-
7
- console.log(`Running Sonar analysis on Docker image: ${IMAGE_NAME}`);
8
-
9
- // Remove existing container
10
- const existingContainer = (
11
- await $`docker ps -a --filter "name=${SONAR_CONTAINER_NAME}" --format "{{.Names}}"`
12
- ).stdout.trim();
13
- if (existingContainer) {
14
- await $`docker rm -f ${SONAR_CONTAINER_NAME}`;
15
- }
16
-
17
- await $`docker run --rm \
18
- -e BRANCH=${process.env.BITBUCKET_BRANCH} \
19
- -e PR_ID=${process.env.BITBUCKET_PR_ID} \
20
- -e PR_DESTINATION_BRANCH=${process.env.BITBUCKET_PR_DESTINATION_BRANCH} \
21
- -e SONAR_TOKEN=${process.env.SONAR_TOKEN} \
22
- --name ${SONAR_CONTAINER_NAME} ${IMAGE_NAME} pnpm sonar`;
23
- }
1
+ import { $ } from 'zx';
2
+
3
+ export async function sonar(GIT_SHA) {
4
+ const IMAGE_NAME = `${process.env.BITBUCKET_REPO_SLUG}:sha-${GIT_SHA}`;
5
+ const SONAR_CONTAINER_NAME = `${process.env.BITBUCKET_REPO_SLUG}-${GIT_SHA}`;
6
+
7
+ console.log(`Running Sonar analysis on Docker image: ${IMAGE_NAME}`);
8
+
9
+ // Remove existing container
10
+ const existingContainer = (
11
+ await $`docker ps -a --filter "name=${SONAR_CONTAINER_NAME}" --format "{{.Names}}"`
12
+ ).stdout.trim();
13
+ if (existingContainer) {
14
+ await $`docker rm -f ${SONAR_CONTAINER_NAME}`;
15
+ }
16
+
17
+ await $`docker run --rm \
18
+ -e BRANCH=${process.env.BITBUCKET_BRANCH} \
19
+ -e PR_ID=${process.env.BITBUCKET_PR_ID} \
20
+ -e PR_DESTINATION_BRANCH=${process.env.BITBUCKET_PR_DESTINATION_BRANCH} \
21
+ -e SONAR_TOKEN=${process.env.SONAR_TOKEN} \
22
+ --name ${SONAR_CONTAINER_NAME} ${IMAGE_NAME} pnpm sonar`;
23
+ }
@@ -0,0 +1,93 @@
1
+ import { existsSync } from 'fs';
2
+ import path, { join } from 'path';
3
+ import { $ } from 'zx';
4
+
5
+ export function isMonoRepo() {
6
+ // Check for the presence of turbo.json which indicates a Turborepo monorepo
7
+ return existsSync(join(process.cwd(), 'turbo.json'));
8
+ }
9
+
10
+ /**
11
+ * Gets the packages that have changed between two git references (branches or tags)
12
+ * @param {string} fromRef - The starting git reference (branch or tag)
13
+ * @param {string} toRef - The ending git reference (branch or tag)
14
+ * @returns {Promise<Array<{name: string, path: string}>>} - Array of changed packages with name and path
15
+ */
16
+ export async function getChangedPackagesInternal(fromRef, toRef) {
17
+ try {
18
+ console.log(process.cwd());
19
+ // Run the turbo command to get changed packages between branches
20
+ const filterArg = `[${fromRef}...${toRef}]`;
21
+ const turboDryOutput =
22
+ await $`turbo run build --filter=${filterArg} --dry=json`;
23
+
24
+ const dryRunJson = turboDryOutput.stdout.trim();
25
+
26
+ const dryRun = JSON.parse(dryRunJson);
27
+
28
+ const packages = dryRun.tasks.map((task) => ({
29
+ name: task.package,
30
+ directory: task.directory,
31
+ path: path.resolve(process.cwd(), task.directory),
32
+ }));
33
+
34
+ return packages;
35
+ } catch (error) {
36
+ console.error('Error getting changed packages:', error);
37
+ return [];
38
+ }
39
+ }
40
+
41
+ export async function getAllPackages() {
42
+ try {
43
+ const turboDryOutput = await $`turbo run build --dry=json`;
44
+ const dryRunJson = turboDryOutput.stdout.trim();
45
+ const dryRun = JSON.parse(dryRunJson);
46
+
47
+ const packages = dryRun.tasks.map((task) => ({
48
+ name: task.package,
49
+ directory: task.directory,
50
+ path: path.resolve(process.cwd(), task.directory),
51
+ }));
52
+
53
+ return packages;
54
+ } catch (error) {
55
+ console.error('Error getting changed packages:', error);
56
+ return [];
57
+ }
58
+ }
59
+
60
+ export async function getAllAppPackages() {
61
+ const changedPackages = await getAllPackages();
62
+ const appPackages = changedPackages.filter((appPackage) =>
63
+ appPackage.directory.startsWith('apps'),
64
+ );
65
+ return appPackages;
66
+ }
67
+
68
+ export async function getChangedPackages() {
69
+ const branch = process.env.BITBUCKET_BRANCH;
70
+ const destBranch = process.env.BITBUCKET_PR_DESTINATION_BRANCH;
71
+
72
+ await $`git fetch origin ${destBranch}:${destBranch}`;
73
+ console.log(`Getting changed packages for [${destBranch}...${branch}]`);
74
+ const changedPackages = await getChangedPackagesInternal(destBranch, branch);
75
+ console.log('Changed Packages:\n', JSON.stringify(changedPackages, null, 2));
76
+ return changedPackages;
77
+ }
78
+
79
+ export async function getChangedAppPackages() {
80
+ const changedPackages = await getChangedPackages();
81
+ const appPackages = changedPackages.filter((appPackage) =>
82
+ appPackage.directory.startsWith('apps'),
83
+ );
84
+ return appPackages;
85
+ }
86
+
87
+ export async function setGitInfo() {
88
+ if (!process.env.IS_DEV) {
89
+ await $`git config --global user.email "gitops@docupace.com"`;
90
+ await $`git config --global user.name "Gitops"`;
91
+ await $`git remote set-url origin https://${process.env.THIS_REP_TOKEN}@bitbucket.org/${process.env.BITBUCKET_REPO_FULL_NAME}.git`;
92
+ }
93
+ }