-
Notifications
You must be signed in to change notification settings - Fork 413
update-dependencies-github-action to avoid openig multiple PRs #2519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
anegg0
wants to merge
24
commits into
master
Choose a base branch
from
update-dependencies-github-action
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8c48d1b
update dependencies update-dependencies-github-action to avoid openin…
anegg0 6029d94
Update scripts/check-releases.ts
anegg0 b55acb3
Update scripts/check-releases.ts
anegg0 b826737
Update scripts/check-releases.ts
anegg0 0d15e84
Update scripts/check-releases.ts
anegg0 5833f99
Update scripts/check-releases.ts
anegg0 0f196d3
Update scripts/check-releases.ts
anegg0 cf7c37a
Update scripts/check-releases.ts
anegg0 57944dc
Update scripts/check-releases.ts
anegg0 d23d451
use force:true when branch is behind
anegg0 cfa7bd8
Update scripts/check-releases.ts
anegg0 c9981fd
Update scripts/check-releases.ts
anegg0 cbeeaba
Update scripts/check-releases.ts
anegg0 7591afa
Update scripts/check-releases.ts
anegg0 185aaed
Update scripts/check-releases.ts
anegg0 1518ff7
create a backup branch before force pushing in both "ahead" and "dive…
anegg0 e2b6d98
Update scripts/check-releases.ts
anegg0 c625262
solve duplicate functions and undefined functions
anegg0 0db7609
Update scripts/check-releases.ts
anegg0 dc5d8b6
fix: pr finding logic in check-releases script
anegg0 725868c
Merge branch 'master' into update-dependencies-github-action
anegg0 cad1422
Update scripts/check-releases.ts
anegg0 0ec2c7c
Merge branch 'master' into update-dependencies-github-action
pete-vielhaber 6b23f86
Merge branch 'master' into update-dependencies-github-action
anegg0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,21 @@ interface DependenciesConfig { | |
projects: Project[]; | ||
} | ||
|
||
async function createBackupBranch( | ||
octokit: ReturnType<typeof github.getOctokit>, | ||
context: typeof github.context, | ||
baseBranchName: string, | ||
sha: string, | ||
): Promise<string> { | ||
const backupBranchName = `${baseBranchName}-backup-${Date.now()}`; | ||
await octokit.rest.git.createRef({ | ||
...context.repo, | ||
ref: `refs/heads/${backupBranchName}`, | ||
sha: sha, | ||
}); | ||
return backupBranchName; | ||
} | ||
|
||
function extractRepoInfo(repoUrl: string): { owner: string; repo: string } | null { | ||
try { | ||
const url = new URL(repoUrl); | ||
|
@@ -89,7 +104,7 @@ function isNewerVersion(newVersion: string, currentVersion: string): boolean { | |
return semver.gt(cleanNew, cleanCurrent); | ||
} | ||
|
||
async function createPullRequest(updatedProjects: Project[]) { | ||
async function createOrUpdatePullRequest(updatedProjects: Project[]) { | ||
const token = process.env.GITHUB_TOKEN; | ||
if (!token) { | ||
throw new Error('GITHUB_TOKEN not found in environment'); | ||
|
@@ -98,27 +113,132 @@ async function createPullRequest(updatedProjects: Project[]) { | |
const octokit = github.getOctokit(token); | ||
const context = github.context; | ||
|
||
// Create a new branch | ||
const branchName = `docs/update-dependencies-${new Date().toISOString().split('T')[0]}`; | ||
const prTitle = 'chore: update dependencies to latest versions'; | ||
const branchName = 'docs/update-dependencies'; | ||
|
||
try { | ||
// Get the current commit SHA | ||
const { data: ref } = await octokit.rest.git.getRef({ | ||
// Check for existing open PRs with the same title | ||
const { data: existingPrs } = await octokit.rest.pulls.list({ | ||
...context.repo, | ||
ref: 'heads/master', | ||
state: 'open', | ||
}); | ||
let existingPr = existingPrs.find((pr) => pr.title === prTitle && pr.head.ref === branchName); // Ensure both title and branch match | ||
if (!existingPr) { | ||
console.log(`No existing pull request found with title "${prTitle}" on branch "${branchName}".`); | ||
} | ||
|
||
// Create a new branch | ||
await octokit.rest.git.createRef({ | ||
// Get the current commit SHA from master | ||
const { data: masterRef } = await octokit.rest.git.getRef({ | ||
...context.repo, | ||
ref: `refs/heads/${branchName}`, | ||
sha: ref.object.sha, | ||
ref: 'heads/master', | ||
}); | ||
|
||
// Update dependencies.json in the new branch | ||
let branchExists = false; | ||
try { | ||
// Check if branch already exists | ||
await octokit.rest.git.getRef({ | ||
...context.repo, | ||
ref: `heads/${branchName}`, | ||
}); | ||
branchExists = true; | ||
} catch (error: unknown) { | ||
if (isUnexpectedError(error)) { | ||
throw error; | ||
} | ||
} | ||
|
||
function isUnexpectedError(error: unknown): boolean { | ||
return ( | ||
error && | ||
typeof error === 'object' && | ||
'status' in error && | ||
(error as { status?: number }).status !== 404 | ||
); | ||
} | ||
anegg0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!branchExists) { | ||
// Create a new branch | ||
await octokit.rest.git.createRef({ | ||
...context.repo, | ||
ref: `refs/heads/${branchName}`, | ||
sha: masterRef.object.sha, | ||
}); | ||
console.log(`Created new branch: ${branchName}`); | ||
} else { | ||
// Update existing branch to latest master | ||
const { data: branchRef } = await octokit.rest.git.getRef({ | ||
...context.repo, | ||
ref: `heads/${branchName}`, | ||
}); | ||
|
||
if (branchRef.object.sha === masterRef.object.sha) { | ||
console.log(`Branch ${branchName} is already up-to-date.`); | ||
} else { | ||
const { data: comparison } = await octokit.rest.repos.compareCommits({ | ||
...context.repo, | ||
base: branchRef.object.sha, | ||
head: masterRef.object.sha, | ||
}); | ||
|
||
if (comparison.status === 'identical') { | ||
console.log(`Branch ${branchName} is already up-to-date.`); | ||
} else if (comparison.status === 'ahead') { | ||
// Create a backup branch to preserve commits | ||
const backupBranchName = await createBackupBranch( | ||
octokit, | ||
context, | ||
branchName, | ||
branchRef.object.sha, | ||
); | ||
console.log(`Created backup branch: ${backupBranchName} to preserve commits.`); | ||
|
||
// Update the branch to match master | ||
await octokit.rest.git.updateRef({ | ||
...context.repo, | ||
ref: `heads/${branchName}`, | ||
sha: masterRef.object.sha, | ||
force: true, | ||
}); | ||
console.log(`Updated branch ${branchName} to match master.`); | ||
} else if (comparison.status === 'diverged') { | ||
// Create a backup branch to preserve commits before force updating | ||
const backupBranchName = await createBackupBranch( | ||
octokit, | ||
context, | ||
branchName, | ||
branchRef.object.sha, | ||
); | ||
console.log(`Created backup branch: ${backupBranchName} to preserve diverged commits.`); | ||
|
||
await octokit.rest.git.updateRef({ | ||
...context.repo, | ||
ref: `heads/${branchName}`, | ||
sha: masterRef.object.sha, | ||
force: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Force pushing to update the branch in 'diverged' state will permanently lose any commits that were made to the branch. This could result in data loss if the branch contains important changes that haven't been merged. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
}); | ||
console.warn( | ||
`Forcefully updated branch ${branchName} to match master. Previous commits preserved in ${backupBranchName}.`, | ||
); | ||
} else if (comparison.status === 'behind') { | ||
await octokit.rest.git.updateRef({ | ||
...context.repo, | ||
ref: `heads/${branchName}`, | ||
sha: masterRef.object.sha, | ||
force: false, | ||
}); | ||
console.log(`Updated branch ${branchName} to match master.`); | ||
} else { | ||
const errorMessage = `Unexpected comparison status: ${comparison.status}. Manual intervention required.`; | ||
console.error(errorMessage); | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
} | ||
|
||
// Update dependencies.json in the branch | ||
const { data: content } = await octokit.rest.repos.getContent({ | ||
...context.repo, | ||
path: DEPENDENCIES_FILE, | ||
ref: branchName, | ||
anegg0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
if (!('content' in content)) { | ||
|
@@ -146,26 +266,41 @@ async function createPullRequest(updatedProjects: Project[]) { | |
sha: content.sha, | ||
}); | ||
|
||
// Create pull request | ||
const prBody = `This PR updates the following dependencies to their latest versions:\n\n${updatedProjects | ||
.map((p) => `- ${p.name}: ${p.latestRelease} (released on ${p.latestReleaseDate})`) | ||
.join('\n')}\n\nPlease review the changes and update the documentation accordingly.`; | ||
|
||
const { data: pr } = await octokit.rest.pulls.create({ | ||
...context.repo, | ||
title: 'chore: update dependencies to latest versions', | ||
head: branchName, | ||
base: 'master', | ||
body: prBody, | ||
}); | ||
if (existingPr) { | ||
// Update existing PR | ||
const { data: updatedPr } = await octokit.rest.pulls.update({ | ||
...context.repo, | ||
pull_number: existingPr.number, | ||
body: prBody, | ||
}); | ||
|
||
console.log(`Created PR #${pr.number}: ${pr.html_url}`); | ||
console.log(`Updated existing PR #${updatedPr.number}: ${updatedPr.html_url}`); | ||
|
||
// Set output for GitHub Actions | ||
core.setOutput('pr_number', pr.number); | ||
core.setOutput('pr_url', pr.html_url); | ||
// Set output for GitHub Actions | ||
core.setOutput('pr_number', updatedPr.number); | ||
core.setOutput('pr_url', updatedPr.html_url); | ||
} else { | ||
// Create new pull request | ||
const { data: pr } = await octokit.rest.pulls.create({ | ||
...context.repo, | ||
title: prTitle, | ||
head: branchName, | ||
base: 'master', | ||
body: prBody, | ||
}); | ||
|
||
console.log(`Created new PR #${pr.number}: ${pr.html_url}`); | ||
|
||
// Set output for GitHub Actions | ||
core.setOutput('pr_number', pr.number); | ||
core.setOutput('pr_url', pr.html_url); | ||
} | ||
} catch (error) { | ||
console.error('Error creating pull request:', error); | ||
console.error('Error creating/updating pull request:', error); | ||
throw error; | ||
} | ||
} | ||
|
@@ -206,8 +341,8 @@ async function main() { | |
} | ||
|
||
if (updatedProjects.length > 0) { | ||
console.log('Creating pull request with updates...'); | ||
await createPullRequest(updatedProjects); | ||
console.log('Creating or updating pull request with updates...'); | ||
await createOrUpdatePullRequest(updatedProjects); | ||
} else { | ||
console.log('All dependencies are up to date.'); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.