Skip to content

Instantly share code, notes, and snippets.

@OmkarKirpan
Last active March 17, 2023 12:27
Show Gist options
  • Select an option

  • Save OmkarKirpan/c7ca9618e3fe56846ffd8b153db62485 to your computer and use it in GitHub Desktop.

Select an option

Save OmkarKirpan/c7ca9618e3fe56846ffd8b153db62485 to your computer and use it in GitHub Desktop.

Revisions

  1. OmkarKirpan revised this gist Mar 17, 2023. 1 changed file with 35 additions and 0 deletions.
    35 changes: 35 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # Remove Ban-Sensitive-Files Module

    A script that removes the `ban-sensitive-files` module from the `devDependencies` of microservices hosted on GitHub Enterprise Server. The script performs the following actions for each repository:

    - Clone the repository to a local directory
    - Create a new branch named `feature/remove-ban-sensitive-files` from the given branch name
    - Remove the `ban-sensitive-files` module from the `devDependencies` of the `package.json` file
    - Rebuild the `package-lock.json` file
    - Commit the changes to the new branch and push them to the remote repository
    - Create a pull request with the title "Remove ban-sensitive-files module from dev dependencies" and a message detailing the changes made.

    ## Requirements

    - Node.js 14 or higher
    - A valid GitHub Enterprise Server account with appropriate permissions to the repositories

    ## Installation

    1. Clone this repository to your local machine.
    2. Navigate to the project directory and run `npm install` to install the necessary dependencies.

    ## Usage

    1. Modify the `repositories` and `branches` arrays in the `index.js` file to include the names of the repositories and branches you want to modify.
    2. Update the `githubCredentials` object in the `index.js` file to include your GitHub Enterprise Server credentials.
    3. Run the script by executing the command `npm start` in the project directory.
    4. The script will clone each repository, create a new branch, remove the `ban-sensitive-files` module, and create a pull request.

    ## Testing

    The script comes with a set of test cases that can be run using the `npm test` command. The tests verify the functionality of the script under different scenarios, such as invalid repository or branch names, missing `ban-sensitive-files` module in `package.json`, and invalid GitHub credentials.

    ## Contributions

    Contributions are welcome! If you have an idea or suggestion for improving the script, please open an issue or submit a pull request.
  2. OmkarKirpan revised this gist Mar 17, 2023. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -44,8 +44,11 @@ async function removeSensitiveFiles() {
    body: 'This pull request removes the ban-sensitive-files module from the dev dependencies of the package.json file.',
    });

    console.log(`Pull request created: ${pr.html_url}`);
    console.log(`Pull request created for ${name}: ${pr.html_url}`);
    }
    }

    removeSensitiveFiles();
    removeSensitiveFiles().catch((err) => {
    console.error(err);
    process.exit(1);
    });
  3. OmkarKirpan created this gist Mar 17, 2023.
    51 changes: 51 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    const { execSync } = require('child_process');
    const { Octokit } = require('@octokit/rest');

    const repos = [
    { name: 'repo1', branch: 'main' },
    { name: 'repo2', branch: 'develop' },
    // add more repositories and branches as needed
    ];

    const octokit = new Octokit({
    auth: 'PERSONAL_ACCESS_TOKEN',
    baseUrl: 'https://github.mycompany.com/api/v3',
    });

    async function removeSensitiveFiles() {
    for (const { name, branch } of repos) {
    const localDir = `/tmp/${name}`;

    // clone the repository to a local directory
    execSync(`git clone git@github.mycompany.com:${name}.git ${localDir}`);

    // create a new branch named "feature/remove-ban-sensitive-files" from given branch name
    execSync(`cd ${localDir} && git checkout ${branch} && git checkout -b feature/remove-ban-sensitive-files`);

    // remove the "ban-sensitive-files" module from the dev dependencies of the package.json file
    const packageJsonPath = `${localDir}/package.json`;
    const packageJson = require(packageJsonPath);
    delete packageJson.devDependencies['ban-sensitive-files'];
    require('fs').writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));

    // rebuild the package-lock.json file
    execSync(`cd ${localDir} && npm install`);

    // commit the changes to the new branch and push them to the remote repository
    execSync(`cd ${localDir} && git add package.json package-lock.json && git commit -m "Remove ban-sensitive-files module from dev dependencies" && git push -u origin feature/remove-ban-sensitive-files`);

    // create a pull request with the title "Remove ban-sensitive-files module from dev dependencies" and a message detailing the changes made.
    const { data: pr } = await octokit.pulls.create({
    owner: 'mycompany',
    repo: name,
    title: 'Remove ban-sensitive-files module from dev dependencies',
    head: 'feature/remove-ban-sensitive-files',
    base: branch,
    body: 'This pull request removes the ban-sensitive-files module from the dev dependencies of the package.json file.',
    });

    console.log(`Pull request created: ${pr.html_url}`);
    }
    }

    removeSensitiveFiles();
    12 changes: 12 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    {
    "name": "remove-ban-sensitive-files",
    "version": "1.0.0",
    "description": "A script that removes the ban-sensitive-files module from the dev dependencies of microservices hosted on GitHub Enterprise Server.",
    "main": "index.js",
    "scripts": {
    "start": "node index.js"
    },
    "dependencies": {
    "@octokit/rest": "^18.0.5"
    }
    }