Skip to content

Commit

Permalink
dependabot adds e2e label and action to add changesets to dependabot PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpascual committed Dec 20, 2024
1 parent 072eb0b commit d826612
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ updates:
interval: 'weekly'
# Update package.json files if new version is outside of version range specified there. Otherwise lock file only.
versioning-strategy: increase-if-necessary
labels:
- 'dependency'
- 'run-e2e'
21 changes: 21 additions & 0 deletions .github/workflows/health_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,24 @@ jobs:
uses: actions/dependency-review-action@0c155c5e8556a497adf53f2c18edabf945ed8e70 # version 4.3.2
with:
config-file: ./.github/dependency_review_config.yml
generate_changeset:
if: github.event_name == 'pull_request' && github.event.pull_request.user.login == 'dependabot[bot]'
runs_on: ubuntu-latest
needs:
- install
- resolve_inputs
steps:
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # version 4.1.4
fetch-depth: 0
- uses: ./.github/actions/setup_node
with:
node-version: 18
- uses: ./.github/actions/restore_install_cache
with:
node-version: 18
cdk-version: ${{ needs.resolve_inputs.outputs.cdk_version }}
- name: Generate changesets for packages with version updates
run: npx tsx scripts/dependabot_generate_changesets.ts "$BASE_SHA" "$HEAD_SHA"
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
8 changes: 8 additions & 0 deletions scripts/components/git_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ export class GitClient {
return filenameDiffOutput.toString().split(EOL);
};

/**
* Get changes made in a specific modified file from getChangedFiles
*/
getFileChanges = async (file: string) => {
const { stdout: changes } = await this.exec`git show ${file}`;
return changes;
};

/**
* Switches to branchName. Creates the branch if it does not exist.
*/
Expand Down
93 changes: 93 additions & 0 deletions scripts/dependabot_generate_changesets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import fsp from 'fs/promises';
import { EOL } from 'os';
import { GitClient } from './components/git_client.js';
import { readPackageJson } from './components/package-json/package_json.js';

const createChangesetFile = async (
versionUpdates: Map<string, string>,
packageNames: string[]
) => {
let message = '';

for (const [dep, version] of versionUpdates) {
message += `Update dependency ${dep} to ${version}.${EOL}`;
}

const frontmatterContent = packageNames
.map((name) => `'${name}': patch`)
.join(EOL);
const body = `---${EOL}${frontmatterContent}${EOL}---${EOL}${EOL}${message.trim()}${EOL}`;
await fsp.writeFile(fileName, body);
};

const getVersionUpdates = async (files: string[]) => {
const updates = new Map<string, string>();
for (const file of files) {
const changes = await gitClient.getFileChanges(file);
for (const change of changes.split(EOL)) {
if (!change.startsWith('+ ')) {
continue;
}
// This will capture lines in git diff like:
// + "<dependency>": "^<version>"
const match = change.match(/"(.*)": "(.*)"/);

if (!match) {
continue;
}
// Add dependency name (match[1]) and new version (match[2]) to Map
updates.set(match[1].replace(/"/g, ''), match[2].replace(/"/g, ''));
}
}

return updates;
};

const gitClient = new GitClient();

const branch = await gitClient.getCurrentBranch();
if (!branch.startsWith('dependabot/')) {
// if branch is not a dependabot branch, return early
process.exit();
}

const baseRef = process.argv[2];
if (baseRef === undefined) {
throw new Error('No base ref specified for generate changeset check');
}

const headRef = process.argv[3].trim();
if (headRef === undefined) {
throw new Error('No head ref specified for generate changeset check');
}

const changedFiles = await gitClient.getChangedFiles(baseRef);
const modifiedPackageDirs = new Set<string>();

// Get modified package.json files that we care about for changesets
const packageJsonFiles = changedFiles.filter(
(changedFile) =>
changedFile.startsWith('packages/') &&
!['packages/integration-tests', 'packages/eslint-rules'].some(
(packageName) => changedFile.startsWith(packageName)
) &&
changedFile.endsWith('package.json')
);

packageJsonFiles.forEach((changedPackageFile) => {
modifiedPackageDirs.add(changedPackageFile.split('/').slice(0, 2).join('/'));
});

const packageNames = [];
for (const modifiedPackageDir of modifiedPackageDirs) {
const { name: modifiedPackageName } = await readPackageJson(
modifiedPackageDir
);
packageNames.push(modifiedPackageName);
}

const fileName = `.changeset/dependabot-${headRef}.md`;
const versionUpdates = await getVersionUpdates(packageJsonFiles);
await createChangesetFile(versionUpdates, packageNames);
await gitClient.commitAllChanges('add changeset');
await gitClient.push({ force: true });

0 comments on commit d826612

Please sign in to comment.