|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const logger = require('./logger.js'); |
| 5 | + |
| 6 | +function replaceContentAndCommit() { |
| 7 | + // Replace all occurrences of opencepk/opencepk-module-ghactions-common with in .github/workflows/*.yml |
| 8 | + logger.info( |
| 9 | + 'Replacing opencepk/opencepk-module-ghactions-common in .github/workflows/*.yml', |
| 10 | + ); |
| 11 | + const workflowDir = path.join('.github', 'workflows'); |
| 12 | + const files = fs.readdirSync(workflowDir); |
| 13 | + files.forEach(file => { |
| 14 | + const filePath = path.join(workflowDir, file); |
| 15 | + if (filePath.endsWith('.yml') || filePath.endsWith('.yaml')) { |
| 16 | + let content = fs.readFileSync(filePath, 'utf8'); |
| 17 | + logger.info( |
| 18 | + `Replacing content from opencepk/opencepk-module-ghactions-common to {{internal repo owner}}/opencepk-module-ghactions-common in ${filePath}`, |
| 19 | + ); |
| 20 | + content = content.replace( |
| 21 | + /opencepk\/opencepk-module-ghactions-common/g, |
| 22 | + 'tucowsinc/opencepk-module-ghactions-common', |
| 23 | + ); |
| 24 | + logger.info( |
| 25 | + `Replacing content from opencepk/opencepk-projects-hub to {{internal repo owner}}/cepk-projects-hub in ${filePath}`, |
| 26 | + ); |
| 27 | + content = content.replace( |
| 28 | + /repo: 'opencepk\/opencepk-projects-hub'/g, |
| 29 | + "repo: 'tucowsinc/cepk-projects-hub'", |
| 30 | + ); |
| 31 | + fs.writeFileSync(filePath, content); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + // Commit the changes after replacement |
| 36 | + logger.info('Committing changes after replacement'); |
| 37 | + execSync('git add .github/workflows'); |
| 38 | + try { |
| 39 | + execSync( |
| 40 | + 'git commit -m "chores/cleanup: Replace opencepk with internal repo owner in workflow files"', |
| 41 | + ); |
| 42 | + } catch (error) { |
| 43 | + if (error.message.includes('nothing to commit')) { |
| 44 | + logger.info('No changes to commit in workflow files. Proceeding...'); |
| 45 | + } else { |
| 46 | + throw error; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Replace all occurrences of git@github.com:opencepk with git@github.com:{{internal repo owner}} in .pre-commit-config.yaml |
| 51 | + logger.info( |
| 52 | + 'Replacing git@github.com:opencepk in .pre-commit-config.yaml', |
| 53 | + ); |
| 54 | + const preCommitConfigPath = '.pre-commit-config.yaml'; |
| 55 | + if (fs.existsSync(preCommitConfigPath)) { |
| 56 | + let preCommitContent = fs.readFileSync(preCommitConfigPath, 'utf8'); |
| 57 | + preCommitContent = preCommitContent.replace( |
| 58 | + /git@github.com:opencepk/g, |
| 59 | + 'git@github.com:tucowsinc', |
| 60 | + ); |
| 61 | + fs.writeFileSync(preCommitConfigPath, preCommitContent); |
| 62 | + |
| 63 | + // Commit the changes after replacement |
| 64 | + logger.info( |
| 65 | + 'chores/cleanup: Committing changes to .pre-commit-config.yaml', |
| 66 | + ); |
| 67 | + execSync('git add .pre-commit-config.yaml'); |
| 68 | + try { |
| 69 | + logger.info('Committing changes to .pre-commit-config.yaml'); |
| 70 | + execSync( |
| 71 | + 'git commit -m "chores/update: Replace org opencepk in .pre-commit-config.yaml"', |
| 72 | + ); |
| 73 | + } catch (error) { |
| 74 | + logger.warn(`${JSON.stringify(error)}`); |
| 75 | + logger.info( |
| 76 | + 'No changes to commit in .pre-commit-config.yaml. Proceeding...', |
| 77 | + ); |
| 78 | + } |
| 79 | + } else { |
| 80 | + logger.info( |
| 81 | + '.pre-commit-config.yaml does not exist, skipping replacement.', |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +module.exports = { replaceContentAndCommit }; |
0 commit comments