-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
devops: Add ci warning for swizzled docusaurus components #9467
Merged
chris48s
merged 25 commits into
badges:master
from
jNullj:feat/9287/docusaurus-swizzled-warning-ci
Aug 18, 2023
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
9fe5c60
devops: Add ci warning for swizzled docusaurus components
jNullj afd5505
Fix table formating
jNullj 4e158d2
Fix order
jNullj 82f66ec
handle missing patch cases
jNullj 438a1e6
fix missing fetch in node16
jNullj 8677c52
fix typo
jNullj 6354ba2
fix typo
jNullj d16d987
changed from diff parse to json usage
jNullj 7f9f94e
fix keys issues
jNullj 2a0573a
fix parent dependency version check
jNullj a1c24dd
Merge branch 'badges:master' into feat/9287/docusaurus-swizzled-warni…
jNullj 5ccbb69
Merge branch 'master' into feat/9287/docusaurus-swizzled-warning-ci
jNullj 372d18f
Apply suggestions from code review - Improve description and text format
jNullj c4a41f6
Fix comment table format
jNullj 11fbd0b
Merge branch 'master' into feat/9287/docusaurus-swizzled-warning-ci
jNullj da3cf2e
fix type
jNullj c77af69
Add docusaurus-swizzled-warning to dependabot
jNullj db2e03c
refactor: remove node-fetch and use octokit insted
jNullj 81f020f
improve code readability
jNullj 9e1c2e4
fix type
jNullj 637d122
Merge branch 'master' into feat/9287/docusaurus-swizzled-warning-ci
jNullj 640567d
change pull_request_target to pull_request
jNullj 78551ff
Improve action log
jNullj 5311d03
Log old and new version
jNullj 6f867f7
Merge branch 'master' into feat/9287/docusaurus-swizzled-warning-ci
jNullj 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 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: 'docusaurus-theme-openapi swizzled component changes warning' | ||
description: 'Check for changes in docusaurus-theme-openapi components which are swizzled and prints out a warning' | ||
branding: | ||
icon: 'alert-triangle' | ||
color: 'yellow' | ||
inputs: | ||
github-token: | ||
description: 'The GITHUB_TOKEN secret' | ||
required: true | ||
runs: | ||
using: 'node16' | ||
main: 'index.js' |
This file contains 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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use strict' | ||
|
||
/** | ||
* Returns info about all files changed in a PR (max 3000 results) | ||
* | ||
* @param {object} client hydrated octokit ready to use for GitHub Actions | ||
* @param {string} owner repo owner | ||
* @param {string} repo repo name | ||
* @param {number} pullNumber pull request number | ||
* @returns {object[]} array of object that describe pr changed files - see https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests-files | ||
*/ | ||
async function getAllFilesForPullRequest(client, owner, repo, pullNumber) { | ||
const perPage = 100 // Max number of items per page | ||
let page = 1 // Start with the first page | ||
let allFiles = [] | ||
while (true) { | ||
const response = await client.rest.pulls.listFiles({ | ||
owner, | ||
repo, | ||
pull_number: pullNumber, | ||
per_page: perPage, | ||
page, | ||
}) | ||
|
||
if (response.data.length === 0) { | ||
// Break the loop if no more results | ||
break | ||
} | ||
|
||
allFiles = allFiles.concat(response.data) | ||
page++ // Move to the next page | ||
} | ||
return allFiles | ||
} | ||
|
||
/** | ||
* Get a list of files changed betwen two tags for a github repo | ||
* | ||
* @param {object} client hydrated octokit ready to use for GitHub Actions | ||
* @param {string} owner repo owner | ||
* @param {string} repo repo name | ||
* @param {string} baseTag base tag | ||
* @param {string} headTag head tag | ||
* @returns {string[]} Array listing all changed files betwen the base tag and the head tag | ||
*/ | ||
async function getChangedFilesBetweenTags( | ||
client, | ||
owner, | ||
repo, | ||
baseTag, | ||
headTag, | ||
) { | ||
const response = await client.rest.repos.compareCommits({ | ||
owner, | ||
repo, | ||
base: baseTag, | ||
head: headTag, | ||
}) | ||
|
||
return response.data.files.map(file => file.filename) | ||
} | ||
|
||
function findKeyEndingWith(obj, ending) { | ||
for (const key in obj) { | ||
if (key.endsWith(ending)) { | ||
return key | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get large (>1MB) JSON file from git repo on at ref as a json object | ||
* | ||
* @param {object} client Hydrated octokit ready to use for GitHub Actions | ||
* @param {string} owner Repo owner | ||
* @param {string} repo Repo name | ||
* @param {string} path Path of the file in repo relative to root directory | ||
* @param {string} ref Git refrence (commit, branch, tag) | ||
* @returns {string[]} Array listing all changed files betwen the base tag and the head tag | ||
*/ | ||
async function getLargeJsonAtRef(client, owner, repo, path, ref) { | ||
const fileSha = ( | ||
await client.rest.repos.getContent({ | ||
owner, | ||
repo, | ||
path, | ||
ref, | ||
}) | ||
).data.sha | ||
const fileBlob = ( | ||
await client.rest.git.getBlob({ | ||
owner, | ||
repo, | ||
file_sha: fileSha, | ||
}) | ||
).data.content | ||
return JSON.parse(Buffer.from(fileBlob, 'base64').toString()) | ||
} | ||
|
||
module.exports = { | ||
getAllFilesForPullRequest, | ||
getChangedFilesBetweenTags, | ||
findKeyEndingWith, | ||
getLargeJsonAtRef, | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,148 @@ | ||
'use strict' | ||
|
||
const core = require('@actions/core') | ||
const github = require('@actions/github') | ||
const { | ||
getAllFilesForPullRequest, | ||
getChangedFilesBetweenTags, | ||
findKeyEndingWith, | ||
getLargeJsonAtRef, | ||
} = require('./helpers') | ||
|
||
async function run() { | ||
try { | ||
const token = core.getInput('github-token', { required: true }) | ||
|
||
const { pull_request: pr } = github.context.payload | ||
if (!pr) { | ||
throw new Error('Event payload missing `pull_request`') | ||
} | ||
|
||
const client = github.getOctokit(token) | ||
const packageName = 'docusaurus-theme-openapi' | ||
const packageParentName = 'docusaurus-preset-openapi' | ||
const overideComponents = ['Curl', 'Response'] | ||
const messageTemplate = `<table><thead><tr><th colspan="2"> | ||
⚠️ This PR contains changes to components of ${packageName} we've overridden | ||
</th></tr> | ||
<tr><th colspan="2"> | ||
We need to watch out for changes to the ${overideComponents.join( | ||
', ', | ||
)} components | ||
</th></tr></thead> | ||
` | ||
|
||
if ( | ||
!['dependabot[bot]', 'dependabot-preview[bot]'].includes(pr.user.login) | ||
) { | ||
return | ||
} | ||
const files = await getAllFilesForPullRequest( | ||
client, | ||
github.context.repo.owner, | ||
github.context.repo.repo, | ||
pr.number, | ||
) | ||
|
||
const file = files.filter(f => f.filename === 'package-lock.json')[0] | ||
if (file === undefined) { | ||
return | ||
} | ||
|
||
const prCommitRefForFile = file.contents_url.split('ref=')[1] | ||
const pkgLockNewJson = await getLargeJsonAtRef( | ||
client, | ||
github.context.repo.owner, | ||
github.context.repo.repo, | ||
file.filename, | ||
prCommitRefForFile, | ||
) | ||
const pkgLockOldJson = await getLargeJsonAtRef( | ||
client, | ||
github.context.repo.owner, | ||
github.context.repo.repo, | ||
file.filename, | ||
'master', | ||
) | ||
|
||
const oldVesionModuleKey = findKeyEndingWith( | ||
pkgLockOldJson.packages, | ||
`node_modules/${packageName}`, | ||
) | ||
const newVesionModuleKey = findKeyEndingWith( | ||
pkgLockNewJson.packages, | ||
`node_modules/${packageName}`, | ||
) | ||
let oldVersion = pkgLockOldJson.packages[oldVesionModuleKey].version | ||
let newVersion = pkgLockNewJson.packages[newVesionModuleKey].version | ||
|
||
const oldVesionModuleKeyParent = findKeyEndingWith( | ||
pkgLockOldJson.packages, | ||
`node_modules/${packageParentName}`, | ||
) | ||
const newVesionModuleKeyParent = findKeyEndingWith( | ||
pkgLockNewJson.packages, | ||
`node_modules/${packageParentName}`, | ||
) | ||
const oldVersionParent = | ||
pkgLockOldJson.packages[oldVesionModuleKeyParent].dependencies[ | ||
packageName | ||
].substring(1) | ||
const newVersionParent = | ||
pkgLockNewJson.packages[newVesionModuleKeyParent].dependencies[ | ||
packageName | ||
].substring(1) | ||
|
||
// if parent dependency is higher version then existing | ||
// npm install will retrive the newer version from the parent dependency | ||
if (oldVersionParent > oldVersion) { | ||
oldVersion = oldVersionParent | ||
} | ||
if (newVersionParent > newVersion) { | ||
newVersion = newVersionParent | ||
} | ||
core.info(`oldVersion=${oldVersion}`) | ||
core.info(`newVersion=${newVersion}`) | ||
|
||
if (newVersion !== oldVersion) { | ||
const pkgChangedFiles = await getChangedFilesBetweenTags( | ||
client, | ||
'cloud-annotations', | ||
'docusaurus-openapi', | ||
`v${oldVersion}`, | ||
`v${newVersion}`, | ||
) | ||
const changedComponents = overideComponents.filter( | ||
componenet => | ||
pkgChangedFiles.filter( | ||
path => | ||
path.includes('docusaurus-theme-openapi/src/theme') && | ||
path.includes(componenet), | ||
).length > 0, | ||
) | ||
const versionReport = `<tbody><tr><td> Old version </td><td> ${oldVersion} </td></tr> | ||
<tr><td> New version </td><td> ${newVersion} </td></tr> | ||
` | ||
const changedComponentsReport = `<tr><td> Overide components changed </td><td> ${changedComponents.join( | ||
', ', | ||
)} </td></tr></tbody></table> | ||
` | ||
const body = messageTemplate + versionReport + changedComponentsReport | ||
await client.rest.issues.createComment({ | ||
owner: github.context.repo.owner, | ||
repo: github.context.repo.repo, | ||
issue_number: pr.number, | ||
body, | ||
}) | ||
|
||
core.info('Found changes and posted comment, done.') | ||
return | ||
} | ||
|
||
core.info('No changes found, done.') | ||
} catch (error) { | ||
core.setFailed(error.message) | ||
} | ||
} | ||
|
||
run() |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be useful here if we could write a log message here (again,
core.info()
) witholdVersion
andnewVersion
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added with 5311d03