Implementation Plan:
a. Configuration:
- We need a config file that specifies:
- Which files to check (package.json, requirements.txt, etc.)
- How to update (command to run to update the lockfile)
- Owner rules per dependency.
- Maybe a list of dependencies to ignore.
b. Scheduled Job:
- Run periodically (e.g., daily) to check for updates.
c. For each dependency that has an update:
- Create a branch (e.g., update-<dependency>-<version>).
- Update the dependency (using the appropriate tool) and commit the changes.
- Push the branch and create a PR.
- Assign the PR to the owners (from config) and request reviews.
d. Monitoring:
- We need to monitor the main branch's CI status. We can use GitHub API to check the status of the latest commit on main.
- Alternatively, we can set up a webhook for when the main branch's CI status changes? But note: the PR might be merged and then the main branch's CI runs.
Steps for monitoring:
- After a PR is merged, note the commit SHA of the update (the one that was merged).
- Wait for the main branch's CI to run (or check periodically).
- If the main branch's CI fails, then we need to check: is the failure due to the update? We can try to see if the failure started after that commit? But note: other changes might have been merged at the same time.
Alternatively, we can have a separate test environment that runs the tests on the updated dependency before merging? But the bot doesn't control the CI of the PR? Actually, the PR's CI runs and if it passes, we merge. But the main branch might break because of the update even if the PR's CI passed? That's possible if the PR's CI didn't catch an issue that only appears when integrated with other changes? Or if the main branch has other changes that break with the update?
So, we need a way to isolate the update. We can do:
Option: When we create the update PR, we also create a test deployment (if applicable) and run integration tests? That might be heavy.
Alternatively, we can have a rule: only allow one update PR to be merged at a time? And then monitor the main branch after each merge.
Given the complexity, let's assume:
- The bot only creates PRs for one dependency at a time.
- We merge the PR only if the PR's CI passes and the main branch's CI is passing at the time of merge? But note: the main branch might be broken by the time we merge because someone else broke it.
So, maybe:
- We only merge the PR if:
a. The PR's CI passes.
b. The main branch's CI is passing at the moment we are about to merge (we check the status of the main branch's latest commit).
Then, after merging, we note the commit and then monitor the main branch for the next few commits? Actually, after we merge, the main branch will have a new commit (the merge commit). We can check the CI status of that new commit.
But note: if we merge and then someone else pushes a broken commit, the main branch's CI will fail, but it's not our update's fault.
How to attribute?
We can do:
- After we merge an update PR, we record the commit SHA of the merge (or the update commit) and the time.
- Then, we periodically check the main branch's CI status and if it fails, we check the commits since the last update. If the only change is our update (or the update is the most recent change and the failure started after that), then we rollback.
But this is complex.
Alternatively, we can have a staged rollout:
- Merge the update to a staging branch and run tests there, then merge to main only after staging passes.
However, the requirement is for a bot that handles safe rollbacks. So let's think of a simpler approach:
We will:
1. Create the update PR and let it be reviewed and merged by the owners (or the bot can auto-merge if the PR passes CI and has approvals?).
2. After merging, the bot will wait for the main branch's CI to complete (by polling the GitHub status of the latest commit on main).
3. If the main branch's CI fails, then the bot will create a rollback PR that reverts the update commit.
But note: the main branch might fail due to other reasons. We can try to see if the failure is due to the update by:
a. Checking the error logs: if the error is related to the updated dependency? (Hard to do automatically)
b. We can assume that if the main branch was passing before the update and fails after, then it's the update's fault? But that's not always true.
Given the complexity, maybe we rely on the owners to decide if they want to rollback? The bot can just create the rollback PR and let the owners decide.
So:
- When the main branch's CI fails after an update, the bot creates a rollback PR (reverting the update commit) and assigns the owners.
How to know which update caused the failure?
We can keep a log (in the bot's database or in a file in the repo) of the update commits and their corresponding PR numbers.
Steps:
- When we create an update PR, we record in a state file (or database) the dependency name, the PR number, the commit SHA (of the update branch, not the merge commit) and the time.
- After merging, we note the merge commit SHA (or the commit that was merged, which is the same as the update branch's commit if we did a squash?).
But note: the merge commit is a new commit that combines the update branch and main. The actual update change is in the merge commit? Actually, if we squash, then the merge commit is the update. If we do a merge commit, then the update is in one of the parents.
To simplify, let's assume we squash merge. Then the merge commit is the update.
Then, when the main branch's CI fails, we look at the latest commit on main. We check our state: do we have an update commit that is the parent of the current main? Actually, we can compare the commit that was the main before the update and after.
Alternatively, we can store:
- The commit SHA of the main branch before the update (the tip of main when we started the update).
- The update commit SHA (the one we merged).
- Then, after we merge, the main branch's new tip is the merge commit (if squash) or the merge commit. We can then check: if the main branch's CI fails on the new tip, we compare the changes between the old tip and the new tip. If the only change is the update, then we rollback.
But again, multiple updates might have been merged? We are doing one at a time, so it's safe.
So:
- We only allow one update PR to be open at a time? Or we process one at a time? We can queue them.
Let's design:
The bot will:
- Check for updates.
- For each update (one at a time, in order of severity? or alphabetical?) create a PR.
- Wait for the PR to be merged (or closed? if closed, skip).
- After merge, wait for the main branch's CI to run and check the status.
- If the main branch's CI fails, create a rollback PR that reverts the update commit.
How to revert:
- We have the update commit SHA (the one we merged). We can do:
`git revert --no-commit <update_commit_sha>`
Then commit and push.
But note: if there are multiple commits in the PR? We are squashing, so one commit.
If we don't squash, then the update might be multiple commits. Then we need to revert all the commits in the PR? We can get the list of commits in the PR and revert them in reverse order.
But to keep it simple, let's require squashing.
So, the plan:
Steps for the bot:
1. Read configuration (which files to check, how to update, owner rules).
2. For each dependency that needs update (and not ignored):
a. Create a branch: `update-<dependency>-<new_version>`.
b. Update the dependency (using the tool) and commit (with message: "Update <dependency> to <new_version>").
c. Push and create PR.
d. Assign the PR to the owners (from config) and request reviews.
e. Wait for the PR to be merged (or closed). We can poll the PR status.
f. If merged, note the commit SHA of the merge (which is the update commit, because we squashed) and the time.
g. Then, wait for the main branch's CI to run (poll the GitHub status of the main branch's latest commit until it has a status).
h. If the main branch's CI fails, then create a rollback branch and PR that reverts the update commit.
i. The rollback PR should be assigned to the same owners.
But note: what if the main branch's CI passes? Then we are done for that update.
What if the main branch's CI fails but then someone else fixes it before we create the rollback? We can check: when we see the main branch's CI fail, we check if the failure is still present after a few minutes? Or we just create the rollback immediately and let the owners decide.
Alternatively, we can have a Manual step for rollback? The requirement says "safe rollbacks", so automatic might be too aggressive.
Let's make it: when the main branch's CI fails after an update, the bot creates a rollback PR and then notifies the owners (via comments on the PR). The owners can then decide to merge the rollback or fix the issue.
But note: the main branch is broken, so we want to rollback quickly. Maybe we can auto-merge the rollback if the update PR was auto-merged? But that's risky.
We can have a rule: if the update was auto-merged (because it passed CI and had approvals) then the rollback is also auto-merged? But then if the update broke the main branch, we want to revert quickly.
Alternatively, we can have a timeout: if the main branch's CI fails within X minutes of the update, then we auto-merge the rollback? But that might be too aggressive.
Given the complexity, let's leave it as:
- The bot creates the rollback PR and assigns the owners. The owners must review and merge the rollback.
But note: the main branch is broken, so the owners might not be able to run tests because the main branch is broken? Actually, the rollback PR will be against the broken main branch? No, the rollback PR is based on the current main (which is broken) and we revert the update. Then when we merge the rollback, we are reverting the update and hopefully fixing the main branch.
However, if the main branch is broken because of the update, then the rollback PR will have conflicts? No, because we are reverting the update, so it should be a clean revert? Actually, if after the update, someone else changed the same files, then there might be conflicts. But since we are doing one update at a time and the update is typically only the lockfile and maybe the package file, conflicts are unlikely.
So, we can try:
- Create a branch from the current main (which is broken) and then revert the update commit. Then create a PR.
But note: the update commit is in the history of main. So we can do:
```
git checkout main
git pull
git checkout -b rollback-<dependency>
git revert <update_commit_sha>
git push -u origin rollback-<dependency>
```
Then create a PR.
However, if the update commit is not the latest? But we are processing one at a time, so it should be the latest.
But what if multiple updates were merged in quick succession? We are processing one at a time, so we wait for the first to be merged and then check the main branch. Then we process the next. So the update commit we are reverting is the one that was just merged.
But note: if we have multiple update PRs in the pipeline, we might have:
main: ... -> A (update1) -> B (update2) -> ...
We process update1, merge, then check main. If main breaks, we rollback update1. But then update2 might have been merged in between? We are processing sequentially, so we don't start update2 until update1 is done (merged or rollback). So no.
So, the design:
The bot has a queue of dependencies to update. It picks one, creates PR, waits for merge, then checks main. If main breaks, rollback. Then moves to next.
But what if the main branch breaks because of update1, then we rollback update1, and then update2 (which was merged after update1) might be the one that broke? But we haven't processed update2 yet because we are waiting for update1 to be done? Actually, we process one at a time. So if update1 breaks, we rollback and then we don't update update2 until we fix update1? That might not be ideal.
Alternatively, we can:
- Create PRs for all updates at once, but then we don't know which one broke.
So, we stick to one at a time.
Implementation details:
We need:
- A way to store the state (which update is being processed, the commit SHA, etc.). We can store in a file in the repo (e.g., `.dependency-bot-state.json`) or in an external database.
Let's use a file in the repo for simplicity.
The state file might look like:
```json
{
"current_update": {
"dependency": "lodash",
"pr_number": 123,
"update_commit_sha": "abc123",
"status": "merged", // or "rollback_created", etc.
},
"last_checked_main_commit": "def456",
"last_checked_main_status": "success" // or "failure"
}
```
But note: the bot might be restarted, so we need to persist state.
Alternatively, we can avoid state by:
- For each update, we create a PR and then we monitor that PR. After merge, we monitor the main branch for the next X minutes. If it fails, we rollback.
But we need to know which update commit to rollback. We can get the merge commit of the PR from the GitHub API.
Steps for one update:
1. Create PR.
2. Wait for PR to be merged (by polling the PR status). When merged, get the merge commit SHA (from the PR's merge commit).
3. Wait for the main branch's CI to run (poll the status of the main branch's latest commit until it has a status).
4. If the main branch's CI fails, then create a rollback PR that reverts the merge commit.
But note: the main branch's CI might fail because of something else. We can't be sure. But we assume that if the main branch's CI fails within, say, 10 minutes of the merge, then it's likely due to the update.
So, we can set a timeout: after the update is merged, we monitor the main branch for 10 minutes. If it fails during that time, we rollback.
How to monitor?
We can:
- Record the time when the update was merged.
- Then, every minute, check the main branch's CI status. If it fails and the failure started within the last 10 minutes, then rollback.
But note: the main branch might have been failing before? We can check the status of the main branch before the update? Actually, we can check the status of the main branch at the time we are about to merge? We want to merge only if the main branch is passing? Then after merge, if it fails, it's likely our update.
So:
- Before we merge (or after the PR is approved and we are about to merge), we check the main branch's CI status. If it's failing, we wait until it passes? But we can't control that.
Alternatively, we can have a rule: we only merge if the main branch's CI is passing at the moment. Then, after merge, if the main branch's CI fails, then it's because of our update (since the main branch was passing before).
So:
Step for merging:
- Wait for the PR to have approvals and CI passing.
- Then, check the main branch's CI status. If it's passing, then merge the PR.
- If it's failing, wait until it passes? But that might be indefinite. So we can have a timeout: wait for main branch to pass for up to 30 minutes? Then if it doesn't pass, we don't merge? But then the update is stuck.
Given the complexity, maybe we don't auto-merge. We let the owners merge. Then we don't have to worry about the main branch's status at merge time? But then we still want to rollback if the main branch breaks after the owners merge.
So, we can have two modes:
- Auto-merge: only if the main branch is passing at the time of merge.
- Manual merge: the owners merge, and then we monitor.
But the bot doesn't control the merge if it's manual.
So, we can:
- Create the PR and assign owners.
- The owners review and merge.
- The bot monitors the main branch after the merge (by checking the PR's merge event? or by polling the main branch?).
How does the bot know when the PR is merged? We can use GitHub webhooks? Or we can poll.
Let's assume we poll:
- Every 5 minutes, check for open PRs that are assigned to the bot? Or we have a list of PRs we created? We can store the PR numbers we created.
So, we have a list of PRs we created and their status. When a PR is merged, we note the merge time and the merge commit.
Then, we start monitoring the main branch for, say, 30 minutes after the merge.
If during that time the main branch's CI fails, we create a rollback.
But note: the main branch might fail because of something else that happened after the merge. We can check: the failure must be on the commit that is the merge commit or after? Actually, the main branch's CI runs on the latest commit. If the latest commit is the merge commit (or a commit after the merge), and it fails, we don't know if it's because of the update or something else.
We can try to see if the failure is due to the update by running the tests on the code without the update? That's complex.
Given the constraints, we'll do:
- After an update PR is merged, we record the merge commit SHA.
- We then check the main branch's CI status every minute for 30 minutes.
- If the main branch's CI fails at any point during that 30 minutes, we create a rollback PR that reverts the merge commit.
But note: the main branch might have other commits in between? We are only monitoring for 30 minutes, and we assume that the update is the only change in that window? Not necessarily.
We can try to narrow down: if the main branch's CI fails, we check the commits since the update. If there are no other commits, then it's likely the update. If there are other commits, then we don't rollback automatically? Or we still rollback and let the owners decide?
Given the requirement for safe rollbacks, we want to be cautious. So we can:
- If the main branch's CI fails within 30 minutes of the update, and there are no other commits in between (i.e., the update is the only change), then we auto-create a rollback PR.
- If there are other commits, we create a rollback PR but also notify the owners that there were other changes and they should investigate.
But how to know if there are other commits? We can get the commit history of main from the update commit to the current tip.
Steps:
Let:
update_commit = the merge commit of the update PR.
current_commit = the latest commit on main when we see the failure.
Then, `git log --oneline update_commit..current_commit` will show the commits after the update.
If the list is empty, then the update is the only change.
But note: the update commit is the one we merged. Then, if we see a failure on a commit that is after the update, then there are other changes. We don't know if the failure is due to the update or the other changes.
So, we can:
- If the failure occurs on a commit that is exactly the update commit (or the update commit is the only one since we started monitoring) then we rollback.
- If there are other commits, we don't auto-rollback, but we notify the owners.
But note: the main branch's CI might run on the update commit and fail, then we rollback immediately. If it runs on a later commit and fails, we don't.
So, we monitor the main branch's CI status. The status is associated with a commit. We can get the commit SHA for which the CI failed.
Then:
- If the commit SHA is the update commit, then we rollback.
- If the commit SHA is after the update, then we don't rollback automatically, but we notify.
How to get the commit SHA of the CI failure? The GitHub status API returns the commit SHA for which the status is set.
So:
- We store the update commit SHA.
- We poll the main branch's status (for the latest commit on main). We get the commit SHA and the status.
- If the status is failure and the commit SHA is the update commit (or is a descendant of the update commit and there are no other commits? Actually, the update commit is in the history, but the CI status is for the latest commit. So the latest commit might be the update commit or a commit after.
We can compare:
If the latest commit is the update commit -> rollback.
If the latest commit is after the update, then we check: are there any other commits between the update and the latest? We can get the list of commits in between. If the list is empty? That can't be because the latest is after. So there is at least one commit (the latest). But the update commit might be the parent of the latest? Then there is one commit (the latest) after the update.
Actually, the update commit is the one we merged. Then, if someone else pushed a commit after, the latest commit is that new commit. Then the CI failure is on that new commit, which might be due to the update or the new commit.
So, we only rollback if the CI failure is on the update commit itself.
How to know? We can check if the latest commit's SHA is the same as the update commit? Then it's the update commit.
But note: the update commit might not be the latest if someone else pushed after? Then we don't rollback.
So:
- We only rollback if the CI failure is on the update commit.
But what if the update commit passed CI (because the PR's CI passed) but then when merged to main, the main branch's CI runs on the merge commit and fails? That's the same as the update commit if we squashed. So yes.
So, we:
- After the update PR is merged, we note the merge commit SHA (update_commit).
- We then wait for the main branch's CI to run on that update_commit? Actually, the main branch's CI will run on the latest commit. If the update is the latest, then the CI status for the update_commit will be set (if we wait for the CI to complete). But note: the main branch might have moved on.
We can do:
- After the update is merged, we wait until the main branch's latest commit is the update_commit? That might not happen if someone pushes right after.
Alternatively, we can check the status of the update_commit on the main branch? But the status is per commit, and the update_commit is now in the main branch. We can check the status of that specific commit? GitHub status API allows getting status for a specific commit.
So:
- We can check the status of the update_commit (the merge commit) on the main branch. If it fails, then we rollback.
But note: the update_commit's status might be set by the PR's CI? Actually, the PR's CI runs on the PR's branch, not on the main branch. When we merge, the main branch's CI will run on the merge commit (which is the update_commit). So we can check the status of the update_commit on the main branch.
How? We can get the status of a commit by:
GET /repos/:owner/:repo/commits/:sha/status
So, we can:
- After the update is merged, we get the update_commit SHA.
- Then, we poll the status of that commit until we get a status (success, failure, pending).
- If it's failure, we rollback.
But what if the update_commit's status is success? Then we don't rollback.
However, the main branch might break later because of the update? For example, the update might cause a runtime error that isn't caught by tests. Then the CI passes but production fails. We are not handling that.
The requirement says "safe rollbacks", so we are handling CI failures. For production failures, we might need a different mechanism (like monitoring and then rollback). But that's more complex.
For now, we handle CI failures on the update_commit.
So, the plan:
For each update:
1. Create PR.
2. Wait for PR to be merged (by polling the PR until it's merged). When merged, get the merge commit SHA (update_commit).
3. Poll the status of update_commit on the main branch (using the commit status API) until it's not pending.
4. If the status is failure, create a rollback PR that reverts update_commit.
How to create the rollback PR?
Steps:
- Create a branch from the current main (which might have moved on, but we want to revert the update_commit). However, note: the update_commit is in the history of main. We can revert it even if there are commits after? Yes, but we might have conflicts.
But note: we are reverting the update_commit, which is a specific commit. If there are commits after that, then the revert might conflict because the changes of the update might have been modified by later commits.
Since we are doing one update at a time, and we are rolling back immediately after the update (and before other updates), then there should be no other commits? But there might be other commits from other developers.
We can try to revert and if there are conflicts, we let the owners resolve? But the main branch is broken, so we want to fix it quickly.
Alternatively, we can revert the update by:
- Checking out the commit before the update and then creating a new commit that is the same as before? That would lose any changes after the update? No, we want to keep the changes after the update but remove the update.
So, we do:
```
git checkout main
git pull
git checkout -b rollback-<dependency>
git revert --no-commit <update_commit_sha>
git commit -m "Rollback update of <dependency> due to CI failure"
git push -u origin rollback-<dependency>
```
Then create a PR.
If there are conflicts, the revert will fail and we can:
- Notify the owners that there are conflicts and they need to resolve manually.
But note: the main branch is broken, so we want to fix it. Maybe we can try to revert the update and then run tests? But the main branch is broken, so we can't rely on that.
Alternatively, we can:
- Create a rollback branch from the commit before the update? Then we lose the changes after the update? That's not acceptable.
So, we must revert the update commit on top of the current main.
We'll do:
- Try to revert. If it fails, then we create a PR with the conflicts and let the owners resolve.
But the PR will show conflicts and the owners will have to resolve.
Alternatively, we can automatically try to resolve conflicts by:
- Using `git revert -X ours` or `-X theirs`? But that might be dangerous.
Given the complexity, we'll just try to revert and if it fails, we create the PR with conflicts and notify the owners.
Steps for rollback PR:
- If revert succeeds, create PR.
- If revert fails, then we commit the partial revert? Or we don't commit? We can do:
```
git revert --no-commit <update_commit_sha>
# if there are conflicts, git will stop and we can't commit.
```
Then we can't create a PR because the branch is not ahead of main? Actually, we can still create a PR with conflicts? GitHub allows creating PRs with conflicts.
So, we push the branch even if the revert didn't complete? Actually, if there are conflicts, the revert will stop and we won't have a clean state. We can:
- Use `git revert --no-commit` and then if there are conflicts, we can commit the current state (which has conflicts) and then push? That would be a mess.
Alternatively, we can:
- Use `git revert -m 1 <merge_commit_sha>` for a merge commit? But we are squashing, so it's not a merge commit.
So, let's assume we squash. Then the update commit is a regular commit.
We can:
```
git checkout main
git pull
git checkout -b rollback-<dependency>
git revert --no-commit <update_commit_sha>
# if there are conflicts, git will stop and we have to resolve them manually? We can try:
git status
# Then we can try to automatically resolve by taking the version from the parent of the update? Not trivial.
```
Given the complexity, we might just:
- If the revert fails (non-zero exit code), then we don't create a PR? But then we have to notify the owners that the rollback failed.
Alternatively, we can create a PR that manually reverts the changes by:
- Checking out the state of the files from the commit before the update? But then we lose changes after the update.
We can do:
```
git checkout <commit_before_update> -- <files_changed_by_update>
git commit -m "Rollback update of <dependency> (manual)"
```
But we need to know which files were changed by the update. We can get that from the update commit.
Steps:
- Get the list of files changed in the update commit: `git diff-tree --no-commit-id --name-only -r <update_commit_sha>`
- Then, for each file, check out the version from the commit before the update (the parent of the update_commit).
But note: the update might have changed multiple files (package.json, lockfile, etc.). We want to revert only those files to the state before the update.
Then, we commit and push.
This avoids conflicts because we are only changing the files that the update changed, and we are setting them to the state before the update. But what if after the update, those files were changed by other commits? Then we are overwriting those changes? That's bad.
Example:
- Update commit changes package.json and lockfile.
- Then, another commit changes package.json (but not the same part? or same part?).
If we revert the update by setting the files to the state before the update, we lose the subsequent changes to those files.
So, we must use `git revert` to create a commit that undoes the changes of the update commit, but on top of the current state. That's what `git revert` does.
So, we stick to `git revert` and if it fails, we let the owners resolve the conflicts.
Summary of the bot:
We'll have:
- A configuration file (`.dependency-bot.yml`) with:
- `files`: list of files to check for dependencies (e.g., ['package.json', 'yarn.lock'])
- `update_commands`: a map from file type to command to update (e.g., for package.json: 'ncu -u' and then 'npm install')
- `owners`: a map from dependency name to list of owners (GitHub usernames or teams)
- `ignore`: list of dependencies to ignore.
- The bot will:
a. For each file in `files`, parse the dependencies (using a language-specific parser? or just run the update command and see what changes?).
b. For each dependency that has an update (and not ignored), create an update PR.
But note: the update command might update multiple dependencies at once. We want to update one at a time.
So, we can:
- Run the update command in a dry-run mode to see which dependencies have updates.
- Then, for each dependency, create a separate branch and update only that dependency? That might be tricky because the update command might update multiple.
Alternatively, we can:
- Update one dependency at a time by manually editing the version in the file? But then we have to update the lockfile too.
Let's assume we have a tool that can update a single dependency. For npm, we can do:
`npm install <package>@<version>`
That will update the package.json and the lockfile.
So, for each dependency:
- Check the current version in the file.
- Check the latest version (from the registry).
- If newer, then:
- Create branch.
- Run `npm install <package>@<version>` (or equivalent for other package managers).
- Commit.
But note: we have to update the lockfile too. The `npm install` command will update the lockfile.
So, we can do:
For each dependency that needs update:
branch_name = `update-${dependency}-${new_version}`
git checkout -b branch_name
run the install command for that dependency.
commit.
Then create PR.
This way, we update one dependency at a time.
But what if the dependency is a transitive dependency? We are only updating direct dependencies? The bot should update direct dependencies.
How to get the list of direct dependencies? From the package.json? Yes.
So, for npm, we parse package.json for dependencies and devDependencies.
Steps for the bot:
1. Parse the config file.
2. For each file in config['files']:
a. Parse the file to get the list of dependencies (with their current versions).
b. For each dependency:
i. Check if it's in the ignore list.
ii. Get the latest version from the registry (using the package manager's API).
iii. If the latest version is newer, then create an update PR for that dependency.
But note: we might have multiple files? For example, package.json and requirements.txt? Then we have to handle each package manager separately.
We can have a plugin system for each package manager.
Given the complexity, let's focus on one package manager (npm) for now.
Implementation:
We'll write the bot in Node.js (since we are dealing with npm).
We'll use:
- `octokit` for GitHub API.
- `npm-registry-fetch` to get the latest version from npm.
Steps:
1. Read `.dependency-bot.yml`.
2. Get the list of files (e.g., ['package.json']).
3. For each file:
a. Parse the file (for package.json, use `require` or `json`).
b. Get the dependencies (dependencies and devDependencies).
c. For each dependency:
i. If in ignore list, skip.
ii. Get the current version.
iii. Get the latest version from npm.
iv. If the latest version is greater (using semver), then create an update.
4. For each update:
a. Check if there's already an open PR for this dependency? We can search for existing PRs with the same branch name? Or we can track in state.
b. Create a branch: `update-${dependency}-${new_version}`.
c. Run `npm install ${dependency}@${new_version}`.
d. Commit the changes (package.json and lockfile).
e. Push the branch.
f. Create a PR:
title: `Update ${dependency} to ${new_version}`
body: |
This PR updates ${dependency} from ${current_version} to ${new_version}.
Release notes: (if we can get from npm? maybe not)
assignees: from config['owners'][dependency] (if exists, else default owners?)
reviewers: same as assignees? or request review.
g. Store the PR number and the branch name in state.
5. Then, monitor the PRs we created:
- For each PR in state that is not closed:
a. Check if the PR is merged (using the GitHub API).
b. If merged, get the merge commit SHA (from the PR data).
c. Then, check the status of that commit on the main branch (using the commit status API). Wait until the status is not pending.
d. If the status is failure, then create a rollback PR.
6. Rollback PR:
a. Create a branch: `rollback-${dependency}-${update_commit_sha}` (or just a timestamp).
b. Revert the update commit: `git revert --no-commit ${update_commit_sha}`
c. If the revert fails (non-zero exit), then we try to manually revert the files that were changed in the update commit? Or just fail and notify?
d. If the revert succeeds, commit and push.
e. Create a PR:
title: `Rollback update of ${dependency} due to CI failure`
body: |
This PR rolls back the update of ${dependency} to ${previous_version} because the main branch's CI failed after the update.
Original update PR: #${pr_number}
assignees: same as the original update PR.
reviewers: same.
f. Then, we can close the state for that update? Or mark it as rollback_created.
7. We also need to handle the case where the update PR is closed without merging (e.g., if the owners reject). Then we remove from state.
State management:
We can store state in a file in the repo (e.g., `.dependency-bot-state.json`). But note: the bot will be updating this state file? That might cause a loop.
Alternatively, we can store state in an external database? Or in the bot's local storage (but then if the bot restarts, we lose state).
Let's store in a file in the repo, but we have to be careful:
- The bot will commit the state file when it updates? That might cause a PR from the bot? We don't want that.
So, we can:
- Store the state in a separate branch (e.g., `dependency-bot-state`) and update that branch? That's complex.
Alternatively, we can store state in the bot's memory and also in a local file, and then when the bot restarts, it reads the local file. But if we have multiple instances, we need a shared storage.
Given that this is a simple bot, we can assume one instance and store state in a local file. But then if the bot restarts, it will re-check all PRs? That might be acceptable.
We can:
- On startup, read the state file (if exists) to get the list of PRs we are tracking.
- Then, for each PR in the state, check its status and act accordingly.
But note: the state file might become outdated if someone manually closes a PR? We can check the PR status and update the state.
So, the state file might look like:
```json
{
"updates": [
{
"dependency": "lodash",
"pr_number": 123,
"branch": "update-lodash-4.17.21",
"update_commit_sha": "abc123",
"status": "merged", // or "open", "closed", "rollback_created"
"rollback_pr_number": 456
}
]
}
```
We update this state file when:
- We create a new update PR: add a new entry.
- The PR is merged: update status to "merged", store the update_commit_sha.
- We create a rollback PR: update status to "rollback_created", store rollback_pr_number.
- The PR is closed without merge: remove the entry? or mark as "closed".
We can also have a cron job that runs every hour and:
- Checks for new updates (and creates PRs).
- Checks the state for open PRs and merged PRs that need rollback.
But note: we want to create the rollback soon after the main branch fails. So we might want to check more frequently.
We can have two processes:
- A scheduler that runs every 5 minutes to check for new updates and create PRs.
- A monitor that runs every minute to check the status of the main branch after an update? But we are storing the update_commit_sha and then we check the status of that commit.
Actually, we can combine:
Every 5 minutes:
1. Check for new updates and create PRs for any dependencies that need update and don't have an open PR (or state entry).
2. For each state entry that is in "merged" status and we haven't checked the CI for that update_commit_sha yet, check the CI status of that commit.
3. If the CI status is failure, create a rollback PR and update the state.
But note: we might create the rollback PR multiple times? We can check if we already created a rollback PR for that update.
So, we can:
- Only create a rollback if the status is "merged" and the rollback_pr_number is null.
Steps:
For each state entry with status "merged" and no rollback_pr_number:
- Get the update_commit_sha.
- Get the status of that commit on the main branch (using the GitHub API for that commit).
- If the status is failure, then create a rollback PR.
But note: the commit might not be on the main branch? It should be, because we merged the PR.
Also, the commit might have been pushed to main, but the CI might not have run yet? Then the status might be pending. We wait until it's not pending? But we are checking every 5 minutes, so we might miss the failure if it happens and then passes? We want to catch the failure.
We can:
- Check the status of the update_commit_sha. If it's failure, then rollback.
- If it's success, then we mark that we checked and don't rollback.
But what if the update_commit_sha's status is success, but then later the main branch's CI fails on a later commit? Then we don't rollback because the update_commit_sha passed? That's acceptable because the failure might be due to something else.
So, we only rollback if the update_commit_sha itself fails.
How to get the status of a specific commit?
GET /repos/:owner/:repo/commits/:sha/status
This returns a list of statuses. We look for the overall state (which is the combination of all statuses). Or we look for the status of the CI we care about? We might have multiple statuses.
We can define a list of required statuses (e.g., the main branch's CI). But note: the update_commit_sha might have multiple statuses (from the PR and from the main branch). We want the status on the main branch.
Actually, the status API for a commit returns all statuses for that commit, regardless of branch? But the statuses are associated with the commit, and the commit is the same in all branches. So the statuses are shared.
That's a problem: the PR's status and the main branch's status are the same commit? Not exactly: the PR's status is for the commit in the PR branch. The main branch's status is for the same commit (if we squashed and merged) but in the context of the main branch? Actually, the commit SHA is the same, but the statuses might be different because the commit is in different branches? No, the status is attached to the commit, not the branch.
So, if the PR's CI passed, then the commit's status is success. Then when we merge, the same commit is in main, and the status is still success? Unless we have a branch-specific CI? GitHub Actions can be set to run on pushes to main and on pull requests. So the same commit might have two statuses: one from the PR and one from the push to main? Actually, the statuses are separate because they are from different contexts (the PR and the push). But the commit SHA is the same.
So, when we check the status of the update_commit_sha, we might see multiple statuses. We need to know which one is from the main branch? We can filter by the context? Or we can check the status of the main branch's latest commit? But the update_commit_sha might not be the latest.
Given the complexity, maybe we don't rely on the commit status API for the update_commit_sha. Instead, we check the status of the main branch's latest commit, and if that latest commit is the update_commit_sha (i.e., the update is the latest) and the status is failure, then we rollback.
Steps:
- After the update is merged, we note the update_commit_sha.
- We then wait until the main branch's latest commit is the update_commit_sha? But if someone pushes after, it won't be.
- We can check: if the main branch's latest commit is the update_commit_sha and the status is failure, then rollback.
- If the main branch's latest commit is after the update, then we don't rollback automatically (because the failure might be due to the other commit).
So:
Let main_tip = the latest commit on main.
If main_tip == update_commit_sha and the status of main_tip is failure, then rollback.
How to get the latest commit on main?
GET /repos/:owner/:repo/commits/main?per_page=1
Then get the commit SHA.
So, in the monitor:
For each state entry with status "merged" and no rollback:
Get the latest commit on main.
If the latest commit on main is the update_commit_sha:
Get the status of that commit (or the status from the main branch? Actually, the status we get from the commit API is for that commit, and it includes all statuses. We can look for the overall state (the `state` field in the status API response) which is the combination of all statuses.
If the overall state is failure, then create rollback.
But note: the overall state might be failure because of one failing status. We want to rollback if any of the required statuses fail? We can define a list of required status contexts (e.g., the CI for the main branch). But the overall state is the combination.
Alternatively, we can check the status of the main branch's CI specifically? But we don't know the context name.
We can assume that the main branch's CI is the one that runs on push to main. And we want to rollback if that fails.
But note: the update_commit_sha might have multiple statuses: one from the PR (which passed) and one from the push to main (which might fail). We want the one from the push to main.
How to distinguish? The status API returns a list of status objects, each with a `context` (the name of the status) and a `state`. We can look for the status context that corresponds to the main branch's CI? But we don't know the context.
Alternatively, we can check the status of the main branch's latest commit (which is the update_commit_sha) and if the overall state is failure, then we rollback.
But the overall state is the combination of all statuses. If one status fails, the overall state is failure.
So, if the update_commit_sha has any failing status, we rollback? That might be too broad because the PR's status might have failed? But we only merged if the PR passed, so the PR's status should be success. But the main branch's CI might have a different set of tests? Possibly.
Given the complexity, we'll do:
- After the update is merged, we wait for the main branch's CI to run on the update_commit_sha (i.e., the latest commit on main is the update_commit_sha and the CI status is set).
- Then, we check the overall state of the update_commit_sha. If it's failure, we rollback.
But note: the update_commit_sha might have a success status from the PR, and then when pushed to main, the main branch's CI runs and fails. Then the overall state will be failure? Actually, the overall state is the combination of all statuses. If one fails, the overall state is failure.
So, if the main branch's CI fails, the overall state will be failure.
So, we can rely on the overall state.
But what if the update_commit_sha has multiple statuses and one fails? Then we rollback. That's acceptable because we want to rollback if any of the main branch's CI fails.
However, the PR's status might have been success, but then the main branch's CI fails. That's exactly what we want to catch.
So, we:
- Check the overall state of the update_commit_sha. If it's failure, rollback.
But note: the update_commit_sha might not have any status yet? Then we wait.
So, in the monitor:
For each state entry with status "merged" and no rollback:
Get the update_commit_sha.
Get the status of that commit (using the commit status API).
If the status is pending, skip (we'll check next time).
If the status is failure, create rollback.
If the status is success, then we mark that we checked and don't need to rollback.
But what if the status becomes success after we checked and it was failure? That's unlikely because once it fails, it stays failed until fixed? But if someone fixes the main branch (by reverting the update manually or by fixing the issue), then the status might become success. But we already created a rollback? We don't want to create two rollbacks.
So, we only create a rollback if we see failure and we haven't created a rollback yet.
We can store in the state:
"update_commit_sha": "...",
"update_commit_status": "pending" | "success" | "failure",
"rollback_created": false
Then, we update the status when we check.
But note: the status might change after we check? We are polling every 5 minutes. We might miss the failure if it passes quickly? But if the main branch's CI fails, it usually stays failed until fixed.
We can:
- When we see the update_commit_sha's status is failure, we create the rollback and set rollback_created to true.
- If we see success, we set update_commit_status to success and don't create rollback.
But what if the status is failure, we create rollback, and then the main branch's CI passes? Then the rollback PR might be unnecessary? But we already created it. We can leave it to the owners.
Alternatively, we can check the status again before creating the rollback? We are already checking.
So, the algorithm:
For each state entry with status "merged" and rollback_created == false:
Get the status of update_commit_sha.
If status == "failure":
Create rollback PR.
Set rollback_created = true.
Else if status == "success":
Mark update_commit_status = "success" (so we don't keep checking).
Else (pending): do nothing.
But note: the update_commit_sha might not have any status? Then we skip.
We can also set a timeout: if we haven't seen a status for the update_commit_sha after, say, 1 hour, then we assume it's broken? But we don't know.
Given the complexity, we'll stick to checking the status.
Now, the code structure:
We'll have:
- index.js: main file.
- config.js: read the config file.
- github.js: wrapper for GitHub API.
- npm.js: for npm specific operations (get latest version, update package.json and lockfile).
Steps in index.js:
async function main() {
const config = await loadConfig();
const state = await loadState();
// Step 1: Check for new updates and create PRs.
await checkForUpdates(config, state);
// Step 2: Monitor existing updates for rollback.
await monitorForRollbacks(config, state);
// Save state.
await saveState(state);
}
We run this every 5 minutes (via cron).
checkForUpdates:
For each file in config.files:
Parse the file and get dependencies.
For each dependency:
if in config.ignore, skip.
if there's already an open PR for this dependency (in state), skip.
get latest version from npm.
if latest > current, then createUpdatePR.
createUpdatePR:
- Create branch.
- Run npm install <dep>@<version>.
- Commit and push.
- Create PR via GitHub API.
- Add to state.
monitorForRollbacks:
For each state entry with status "merged" and !rollback_created:
Get the update_commit_sha from the state (we stored when the PR was merged).
Get the status of that commit.
If status is failure, createRollbackPR.
createRollbackPR:
- Create branch from main.
- Run `git revert --no-commit <update_commit_sha>`.
- If revert fails, then we try to manually revert? Or just fail and notify? Let's try to revert and if it fails, we don't create a PR? But then we need to notify.
- Actually, we can create a PR with the revert conflict? We can push the branch even if the revert didn't complete? No, because the revert will stop and we won't have a commit.
We can:
try {
git revert --no-commit <update_commit_sha>
git commit -m "Rollback update of <dependency>"
git push
create PR.
} catch (error) {
// Revert failed due to conflicts.
// We can try to resolve by taking the version from the parent of the update? Not trivial.
// Instead, we can create a PR that manually reverts the files? But we don't want to lose other changes.
// Alternatively, we can:
// git reset --hard // to discard the failed revert?
// Then, for each file changed in the update_commit_sha, check out the version from the parent of the update_commit_sha?
// But that would overwrite any changes to those files after the update.
// Let's not do that.
// Instead, we can create a PR that reverts the update by:
// git show <update_commit_sha> -- <file> | git apply -R
// That applies the reverse of the patch. But that might also have conflicts.
// Given the complexity, if the revert fails, we do:
// Notify the owners that the rollback failed due to conflicts and they need to manually rollback.
// We can create an issue instead of a PR? Or we can create a PR with the conflicts? GitHub allows creating a PR with conflicts if we push a branch that has conflicts with the base.
// But we don't have a commit because the revert didn't complete.
// So, we can:
// git revert --no-commit <update_commit_sha> || true // ignore error
// Then, we commit the current state (which has conflicts) and push? That would be a mess.
// Alternatively, we can use `git revert -X ours` to automatically resolve conflicts by favoring our changes (the main branch). But that might not be what we want.
// We want to undo the changes of the update, but keep the changes after. So we want to apply the reverse of the update on top of the current main. That's exactly what `git revert` does. If there are conflicts, it means that the changes after the update conflict with the revert. We need human intervention.
// So, we can:
// - Try `git revert -X ours` or `-X theirs`? But which one? We want to keep the changes after the update, so we want to keep "ours" (the current main) and discard the update. So `-X ours` will keep our changes when there's a conflict. That might be acceptable.
// Let's do:
// git revert -X ours --no-commit <update_commit_sha>
// Then commit and push.
// This will resolve conflicts by keeping the current main's version.
// But note: the update might have changed a file, and then after the update, someone else changed the same file. The conflict is because the update and the subsequent change touch the same lines. With `-X ours`, we keep the subsequent change and discard the update's change. That's what we want: we want to remove the update's change but keep the subsequent change.
// So, that might be safe.
// We'll try:
// git revert -X ours --no-commit <update_commit_sha>
// if it fails (non-zero), then we try `git revert -X theirs`? No, that would keep the update's change and discard the subsequent change, which is not what we want.
// So, we only try `-X ours`.
// If that fails (maybe because of binary files?), then we give up and notify.
}
So, in code:
try {
await exec(`git revert -X ours --no-commit ${update_commit_sha}`);
await exec(`git commit -m "Rollback update of ${dependency} due to CI failure"`);
await exec(`git push -u origin rollback-${dependency}-${update_commit_sha}`);
// create PR
} catch (e) {
// Notify owners that rollback failed due to conflicts.
// We can create an issue or comment on the original update PR?
}
But note: the branch might already exist? We are creating a new branch name each time.
We'll create a branch name: `rollback-${dependency}-${update_commit_sha}` (truncated if necessary).
Now, the code.
This is a high-level design. We'll write the code in Node.js.
We'll use:
- `fs` for reading files.
- `child_process` for running git and npm commands.
- `octokit` for GitHub.
- `semver` for version comparison.
We'll assume the bot has write access to the repo (so it can push branches and create PRs).
We'll also assume the bot runs in the repo directory.
Let's code the main functions.
Due to the complexity, we'll write a simplified version.
Steps:
1. Load config and state.
2. Check for updates.
3. Monitor for rollbacks.
4. Save state.
We'll run this in a loop with a delay (or via cron).
Let's code.</think>