-
-
Notifications
You must be signed in to change notification settings - Fork 5
changelog checker #54
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
Closed
Closed
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
7b25c0d
changelog checker
jake-perkins 5ab5c52
updated syntax
jake-perkins 8a89ebd
updated logic
jake-perkins c8d98fd
updates to ts approach
jake-perkins f94591b
remove pr number arg
jake-perkins 0e2c3dd
script updates
jake-perkins cc3acd8
fmt
jake-perkins a2325bf
lint
jake-perkins 8c3846a
remove un-used dependencies
jake-perkins 2c7bbae
yarn.lock
jake-perkins 8338f2c
dedup
jake-perkins f61d45a
add label check
jake-perkins 4281ed0
pr
jake-perkins 6bb2bb5
fix label logic
jake-perkins c71a795
syntax
jake-perkins b2e4abf
syntax
jake-perkins b32e2b9
fix
jake-perkins 778c758
syntax
jake-perkins 1df1e5d
changelog
jake-perkins 2f2ee48
globbing lint
jake-perkins 1d2a658
diff
jake-perkins 947179d
shellcheck
jake-perkins d49b71c
shellcheck
jake-perkins 39d6379
shellcheck
jake-perkins 7bdaf82
test
jake-perkins 1fcd744
lint
jake-perkins 70ca876
testing
jake-perkins 9e9ca3f
lint
jake-perkins b8d06f3
fmt
jake-perkins fe80e61
fmt
jake-perkins c25f415
fmt
jake-perkins 265ca44
lint
jake-perkins 3b79198
chore: remove comments
jake-perkins 4c8b7d4
Update src/changelog-check.ts
jake-perkins 0020d1a
Update src/changelog-check.ts
jake-perkins 03836c5
code-review-updates
jake-perkins 38b0053
updates
jake-perkins a94b63b
lock
jake-perkins 87a2684
code-review-fixes
jake-perkins 6331586
lint
jake-perkins a568305
Merge branch 'main' into changelog-checker
jake-perkins 160fa58
yarn dedup
jake-perkins 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
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| name: Changelog Check | ||
|
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| base-branch: | ||
| required: true | ||
| type: string | ||
| feature-branch: | ||
| required: true | ||
| type: string | ||
| pr-number: | ||
| required: false | ||
| type: string | ||
| secrets: | ||
| gh-token: | ||
| required: true | ||
|
|
||
| jobs: | ||
| check-changelog: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout github-tools repository | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| repository: MetaMask/github-tools | ||
| ref: changelog-checker | ||
| path: github-tools | ||
|
jake-perkins marked this conversation as resolved.
|
||
|
|
||
| - name: Set up Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version-file: ./github-tools/.nvmrc | ||
| cache-dependency-path: ./github-tools/yarn.lock | ||
| cache: yarn | ||
|
|
||
| - name: Enable Corepack | ||
| run: corepack enable | ||
| shell: bash | ||
| working-directory: ./github-tools | ||
|
|
||
| - name: Install dependencies | ||
| run: yarn --immutable | ||
| shell: bash | ||
| working-directory: ./github-tools | ||
|
jake-perkins marked this conversation as resolved.
|
||
|
|
||
| - name: Check PR Labels | ||
| id: label-check | ||
| run: | | ||
| # Fetch labels from the GitHub API | ||
| labels=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ | ||
| "https://api.github.com/repos/${{ github.repository }}/issues/${{ inputs.pr-number }}/labels") | ||
|
|
||
| # Proceed with checking for the 'no-changelog' label using jq | ||
| if echo "$labels" | jq -e '.[] | select(.name == "no-changelog")'; then | ||
| echo "No-changelog label found, skipping changelog check." | ||
| echo "SKIP_CHANGELOG=true" >> $GITHUB_ENV | ||
| else | ||
| echo "SKIP_CHANGELOG=false" >> $GITHUB_ENV | ||
| echo "No-changelog label not found, proceeding with changelog check." | ||
| fi | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.gh-token }} | ||
| shell: bash | ||
|
|
||
| - name: Check Changelog | ||
| if: env.SKIP_CHANGELOG != 'true' | ||
| id: changelog-check | ||
| shell: bash | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.gh-token }} | ||
| working-directory: ./github-tools | ||
| run: | | ||
| yarn run changelog-check ${{ github.repository }} ${{ inputs.base-branch }} ${{ inputs.feature-branch }} | ||
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
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 |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { parseChangelog } from '@metamask/auto-changelog'; | ||
| import nodeFetch from 'node-fetch'; | ||
|
|
||
| /** | ||
| * Asynchronously fetches the CHANGELOG.md file content from a specified GitHub repository and branch. | ||
| * The function constructs a URL to access the raw content of the file using GitHub's raw content service. | ||
| * It handles authorization using an optional GitHub token from environment variables. | ||
| * | ||
| * @param repo - The full name of the repository (e.g., "owner/repo"). | ||
| * @param branch - The branch from which to fetch the CHANGELOG.md file. | ||
| * @returns A promise that resolves to the content of the CHANGELOG.md file as a string. | ||
| * If the fetch operation fails, it logs an error and returns an empty string. | ||
| */ | ||
| async function fetchChangelogFromGitHub( | ||
| repo: string, | ||
| branch: string, | ||
| ): Promise<string> { | ||
| const url = `https://raw.githubusercontent.com/${repo}/${branch}/CHANGELOG.md`; | ||
| // eslint-disable-next-line n/no-process-env | ||
| const token = process.env.GITHUB_TOKEN ?? ''; | ||
|
|
||
| try { | ||
| const headers = token ? { Authorization: `Bearer ${token}` } : {}; | ||
| const response = await nodeFetch(url, { | ||
| headers, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`HTTP error! status: ${response.status}`); | ||
| } | ||
|
|
||
| return await response.text(); | ||
| } catch (error) { | ||
| console.error( | ||
| `❌ Error fetching CHANGELOG.md from ${branch} on ${repo}:`, | ||
| error, | ||
| ); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Compares the changelog entries between the base and feature branches to determine if there are differences. | ||
| * | ||
| * @param baseChanges - The content of the '[Unreleased]' section from the base branch's CHANGELOG.md. | ||
| * @param featureChanges - The content of the '[Unreleased]' section from the feature branch's CHANGELOG.md. | ||
| * @returns Returns true if there are new or differing number of entries in the feature branch compared to the base branch. | ||
| */ | ||
| function compareChangeLogs( | ||
| baseChanges: string[], | ||
| featureChanges: string[], | ||
| ): boolean { | ||
| const newEntries = featureChanges.filter( | ||
| (entry) => !baseChanges.includes(entry), | ||
| ); | ||
|
|
||
| // Log and return true if there are new entries | ||
| if (newEntries.length > 0) { | ||
| console.log('New entries in feature branch:', newEntries); | ||
| return true; | ||
| } | ||
|
|
||
| // Check if the number of entries has changed | ||
| if (baseChanges.length !== featureChanges.length) { | ||
| console.log( | ||
| 'The number of entries has changed. Base branch has', | ||
| baseChanges.length, | ||
| 'entries, while feature branch has', | ||
| featureChanges.length, | ||
| 'entries.', | ||
| ); | ||
| return true; | ||
| } | ||
|
|
||
| // If no new entries and the size has not changed, return false | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Validates that the CHANGELOG.md in a feature branch has been updated correctly by comparing it | ||
| * against the CHANGELOG.md in the base branch. | ||
| * @param repo - The GitHub repository from which to fetch the CHANGELOG.md file. | ||
| * @param baseBranch - The base branch (typically 'main' or 'master') to compare against. | ||
| * @param featureBranch - The feature branch that should contain the updated CHANGELOG.md. | ||
| */ | ||
| async function validateChangelog( | ||
| repo: string, | ||
| baseBranch: string, | ||
| featureBranch: string, | ||
| ) { | ||
| console.log(`🔍 Fetching CHANGELOG.md from GitHub repository: ${repo}`); | ||
|
|
||
| const [baseChangelogContent, featureChangelogContent] = await Promise.all([ | ||
| fetchChangelogFromGitHub(repo, baseBranch), | ||
| fetchChangelogFromGitHub(repo, featureBranch), | ||
| ]); | ||
|
|
||
| if (!featureChangelogContent) { | ||
| throw new Error('❌ CHANGELOG.md is missing in the feature branch.'); | ||
|
jake-perkins marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const baseUnreleasedChanges = parseChangelog({ | ||
| changelogContent: baseChangelogContent, | ||
| repoUrl: '', // Not needed as we're only parsing unreleased changes | ||
| }).getReleaseChanges('Unreleased'); | ||
|
|
||
| const featureUnreleasedChanges = parseChangelog({ | ||
| changelogContent: featureChangelogContent, | ||
| repoUrl: '', // Not needed as we're only parsing unreleased changes | ||
| }).getReleaseChanges('Unreleased'); | ||
|
|
||
| const baseChanges = Object.values(baseUnreleasedChanges).flat(); | ||
| const featureChanges = Object.values(featureUnreleasedChanges).flat(); | ||
|
|
||
| console.log('🔍 Comparing changelog entries...'); | ||
|
|
||
| console.log('Base unreleased section:', baseUnreleasedChanges); | ||
| console.log('Feature unreleased section:', featureUnreleasedChanges); | ||
|
|
||
| const hasChanges = compareChangeLogs(baseChanges, featureChanges); | ||
|
|
||
| if (!hasChanges) { | ||
| throw new Error( | ||
| "❌ No new entries detected under '## Unreleased'. Please update the changelog.", | ||
| ); | ||
| } | ||
|
|
||
| console.log('✅ CHANGELOG.md has been correctly updated.'); | ||
| } | ||
|
|
||
| // Parse command-line arguments | ||
| const args = process.argv.slice(2); | ||
| if (args.length < 3) { | ||
| console.error( | ||
| '❌ Usage: ts-node scripts/check-changelog.js <github-repo> <base-branch> <feature-branch>', | ||
| ); | ||
| throw new Error('❌ Missing required arguments.'); | ||
|
jake-perkins marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const [githubRepo, baseBranch, featureBranch] = args; | ||
|
|
||
| // Ensure all required arguments are provided | ||
| if (!githubRepo || !baseBranch || !featureBranch) { | ||
| console.error( | ||
| '❌ Usage: ts-node src/check-changelog.ts <github-repo> <base-branch> <feature-branch>', | ||
| ); | ||
| throw new Error('❌ Missing required arguments.'); | ||
| } | ||
|
|
||
| // Run the validation | ||
| validateChangelog(githubRepo, baseBranch, featureBranch).catch((error) => { | ||
| console.error('❌ Unexpected error:', error); | ||
| throw error; | ||
| }); | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.