|
| 1 | +name: Shared ChangeLog Check |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + feature-branch: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + pr-number: |
| 10 | + required: false |
| 11 | + type: string |
| 12 | + event-type: |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + is-monorepo: |
| 16 | + description: 'Set to true for monorepo projects (only yarn workspaces are supported)' |
| 17 | + required: false |
| 18 | + type: boolean |
| 19 | + default: false |
| 20 | + secrets: |
| 21 | + gh-token: |
| 22 | + required: true |
| 23 | + |
| 24 | +jobs: |
| 25 | + shared_changelog_check: |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - name: Conditional Skip or Execute |
| 29 | + run: | |
| 30 | + if [[ "${{ inputs.event-type }}" == "merge_group" ]]; then |
| 31 | + echo "Merge group event detected, auto-succeeding." |
| 32 | + exit 0 |
| 33 | + fi |
| 34 | + echo "Proceeding with pull request checks." |
| 35 | +
|
| 36 | + - name: Checkout github-tools repository |
| 37 | + if: ${{ inputs.event-type == 'pull_request' }} |
| 38 | + uses: actions/checkout@v4 |
| 39 | + with: |
| 40 | + repository: MetaMask/core |
| 41 | + ref: changelog-checker |
| 42 | + path: github-tools |
| 43 | + |
| 44 | + - name: Enable Corepack |
| 45 | + if: ${{ inputs.event-type == 'pull_request' }} |
| 46 | + run: corepack enable |
| 47 | + shell: bash |
| 48 | + |
| 49 | + - name: Set up Node.js |
| 50 | + if: ${{ inputs.event-type == 'pull_request' }} |
| 51 | + uses: actions/setup-node@v4 |
| 52 | + with: |
| 53 | + node-version-file: ./github-tools/.nvmrc |
| 54 | + cache-dependency-path: ./github-tools/yarn.lock |
| 55 | + cache: yarn |
| 56 | + |
| 57 | + - name: Install dependencies |
| 58 | + if: ${{ inputs.event-type == 'pull_request' }} |
| 59 | + run: yarn --immutable |
| 60 | + shell: bash |
| 61 | + working-directory: ./github-tools |
| 62 | + |
| 63 | + - name: Check PR Labels |
| 64 | + if: ${{ inputs.event-type == 'pull_request' }} |
| 65 | + id: label-check |
| 66 | + env: |
| 67 | + GITHUB_TOKEN: ${{ secrets.gh-token }} |
| 68 | + GITHUB_REPO: ${{ github.repository }} |
| 69 | + PR_NUMBER: ${{ inputs.pr-number }} |
| 70 | + run: | |
| 71 | + # Fetch labels from the GitHub API |
| 72 | + if ! labels=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 73 | + "https://api.github.com/repos/${GITHUB_REPO}/issues/${PR_NUMBER}/labels"); then |
| 74 | + echo "::error::Failed to fetch labels from GitHub API." |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | +
|
| 78 | + # Proceed with checking for the 'no-changelog' label using jq |
| 79 | + if echo "$labels" | jq -e '.[] | select(.name == "no-changelog")'; then |
| 80 | + echo "No-changelog label found, skipping changelog check." |
| 81 | + echo "SKIP_CHANGELOG=true" >> "$GITHUB_ENV" |
| 82 | + else |
| 83 | + echo "SKIP_CHANGELOG=false" >> "$GITHUB_ENV" |
| 84 | + echo "No-changelog label not found, proceeding with changelog check." |
| 85 | + fi |
| 86 | + shell: bash |
| 87 | + |
| 88 | + - name: Check Changelog |
| 89 | + if: ${{ inputs.event-type == 'pull_request' && env.SKIP_CHANGELOG != 'true' }} |
| 90 | + id: changelog-check |
| 91 | + shell: bash |
| 92 | + env: |
| 93 | + GITHUB_TOKEN: ${{ secrets.gh-token }} |
| 94 | + IS_MONOREPO: ${{ inputs.is-monorepo || 'false' }} |
| 95 | + GITHUB_REPO: ${{ github.repository }} |
| 96 | + HEAD_REF: ${{ github.head_ref }} |
| 97 | + PR_NUMBER: ${{ inputs.pr-number }} |
| 98 | + working-directory: ./github-tools |
| 99 | + run: | |
| 100 | + if [[ "$IS_MONOREPO" == "true" ]]; then |
| 101 | + echo "Running in monorepo mode - checking changelogs for changed packages..." |
| 102 | +
|
| 103 | + # Get all changed files with pagination support |
| 104 | + CHANGED_FILES="" |
| 105 | + PAGE=1 |
| 106 | + while true; do |
| 107 | + echo "Fetching changed files (page $PAGE)..." |
| 108 | +
|
| 109 | + if ! PAGE_RESPONSE=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 110 | + "https://api.github.com/repos/${GITHUB_REPO}/pulls/${PR_NUMBER}/files?page=$PAGE&per_page=100"); then |
| 111 | + echo "::error::Failed to fetch changed files from GitHub API." |
| 112 | + exit 1 |
| 113 | + fi |
| 114 | +
|
| 115 | + # Check if the response is a valid JSON array |
| 116 | + if ! echo "$PAGE_RESPONSE" | jq -e 'if type=="array" then true else false end' > /dev/null; then |
| 117 | + echo "::error::Invalid response from GitHub API." |
| 118 | + echo "Response:" |
| 119 | + echo "$PAGE_RESPONSE" | jq '.' |
| 120 | + exit 1 |
| 121 | + fi |
| 122 | +
|
| 123 | + PAGE_FILES=$(echo "$PAGE_RESPONSE" | jq -r '.[].filename') |
| 124 | + ITEMS_COUNT=$(echo "$PAGE_RESPONSE" | jq 'length') |
| 125 | +
|
| 126 | + # If no files returned or empty array, we've reached the end of pagination |
| 127 | + if [ -z "$PAGE_FILES" ] || [ "$ITEMS_COUNT" == "0" ]; then |
| 128 | + break |
| 129 | + fi |
| 130 | +
|
| 131 | + # Append files from this page |
| 132 | + if [ -n "$CHANGED_FILES" ]; then |
| 133 | + CHANGED_FILES="$CHANGED_FILES"$'\n'"$PAGE_FILES" |
| 134 | + else |
| 135 | + CHANGED_FILES="$PAGE_FILES" |
| 136 | + fi |
| 137 | +
|
| 138 | + # Break if returned items fewer than per_page (indicating last page) |
| 139 | + if [ "$ITEMS_COUNT" -lt 100 ]; then |
| 140 | + break |
| 141 | + fi |
| 142 | +
|
| 143 | + # Go to next page |
| 144 | + PAGE=$((PAGE+1)) |
| 145 | + done |
| 146 | +
|
| 147 | + # Clean up empty lines |
| 148 | + CHANGED_FILES=$(echo "$CHANGED_FILES" | grep -v '^$' || echo "") |
| 149 | +
|
| 150 | + # Check if we got any files after pagination and cleanup - exit early if there are no changed files to process |
| 151 | + if [ -z "$CHANGED_FILES" ]; then |
| 152 | + echo "No changed files found. Exiting successfully." |
| 153 | + exit 0 |
| 154 | + fi |
| 155 | +
|
| 156 | + # Debug output |
| 157 | + echo "Files changed in this PR:" |
| 158 | + echo "$CHANGED_FILES" |
| 159 | + echo "Total: $(echo "$CHANGED_FILES" | wc -l) files" |
| 160 | +
|
| 161 | + # Identify changed packages |
| 162 | + declare -a CHANGED_PACKAGES |
| 163 | +
|
| 164 | + # Extract package names from file paths |
| 165 | + while IFS= read -r FILE; do |
| 166 | + if [[ "$FILE" =~ ^packages/([^/]+)/ ]]; then |
| 167 | + PACKAGE="${BASH_REMATCH[1]}" |
| 168 | +
|
| 169 | + # Skip test files, docs, yaml configs, and changelog files |
| 170 | + if [[ ! "$FILE" =~ \.(test|spec)\. ]] && \ |
| 171 | + [[ ! "$FILE" =~ __tests__/ ]] && \ |
| 172 | + [[ ! "$FILE" =~ ^packages/$PACKAGE/docs/ ]] && \ |
| 173 | + [[ ! "$FILE" =~ \.(ya?ml)$ ]] && \ |
| 174 | + [[ ! "$FILE" =~ ^packages/$PACKAGE/CHANGELOG\.md$ ]]; then |
| 175 | +
|
| 176 | + # Add package to our tracking array if not already there |
| 177 | + if [[ ! " ${CHANGED_PACKAGES[*]} " =~ \ ${PACKAGE}\ ]]; then |
| 178 | + CHANGED_PACKAGES+=("$PACKAGE") |
| 179 | + echo "Code changes detected in package: $PACKAGE" |
| 180 | + fi |
| 181 | + fi |
| 182 | + fi |
| 183 | + done <<< "$CHANGED_FILES" |
| 184 | +
|
| 185 | + # Skip if no packages with code changes were found |
| 186 | + if [ ${#CHANGED_PACKAGES[@]} -eq 0 ]; then |
| 187 | + echo "No package code changes detected that would require changelog updates." |
| 188 | + exit 0 |
| 189 | + fi |
| 190 | +
|
| 191 | + # Check changelogs for each changed package |
| 192 | + OVERALL_STATUS=0 |
| 193 | + for PACKAGE in "${CHANGED_PACKAGES[@]}"; do |
| 194 | + echo "Checking changelog for package: $PACKAGE" |
| 195 | + CHANGELOG_PATH="packages/${PACKAGE}/CHANGELOG.md" |
| 196 | +
|
| 197 | + # Call the existing changelog:check script for each package |
| 198 | + if ! yarn run changelog:check "$GITHUB_REPO" "$HEAD_REF" "$CHANGELOG_PATH"; then |
| 199 | + OVERALL_STATUS=1 |
| 200 | + echo "::error::Changelog check failed for package: $PACKAGE" |
| 201 | + fi |
| 202 | + done |
| 203 | +
|
| 204 | + # Exit with the overall status |
| 205 | + exit $OVERALL_STATUS |
| 206 | + else |
| 207 | + echo "Running in single-repo mode - checking changelog for the entire repository..." |
| 208 | + # Original single-repo check |
| 209 | + yarn run changelog:check "$GITHUB_REPO" "$HEAD_REF" "CHANGELOG.md" |
| 210 | + fi |
0 commit comments