mirror of
https://github.com/community-scripts/ProxmoxVE
synced 2025-02-19 06:09:18 +00:00
Update changelog-pr.yml
This commit is contained in:
parent
d09b3875db
commit
920245bba1
140
.github/workflows/changelog-pr.yml
vendored
140
.github/workflows/changelog-pr.yml
vendored
@ -6,7 +6,7 @@ on:
|
|||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
update-changelog-pull-request:
|
update-changelog:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
env:
|
env:
|
||||||
CONFIG_PATH: .github/changelog-pr-config.json
|
CONFIG_PATH: .github/changelog-pr-config.json
|
||||||
@ -16,155 +16,47 @@ jobs:
|
|||||||
contents: write
|
contents: write
|
||||||
pull-requests: write
|
pull-requests: write
|
||||||
steps:
|
steps:
|
||||||
- name: Generate a token
|
|
||||||
id: generate-token
|
|
||||||
uses: actions/create-github-app-token@v1
|
|
||||||
with:
|
|
||||||
app-id: ${{ vars.APP_ID }}
|
|
||||||
private-key: ${{ secrets.APP_PRIVATE_KEY }}
|
|
||||||
|
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Get latest dates in changelog
|
- name: Get latest date in changelog
|
||||||
run: |
|
run: |
|
||||||
# Extract the latest and second latest dates from changelog
|
LATEST_DATE=$(grep '^## [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}' CHANGELOG.md | head -n 1 | awk '{print $2}')
|
||||||
DATES=$(grep '^## [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}' CHANGELOG.md | head -n 2 | awk '{print $2}')
|
|
||||||
|
|
||||||
LATEST_DATE=$(echo "$DATES" | sed -n '1p')
|
|
||||||
SECOND_LATEST_DATE=$(echo "$DATES" | sed -n '2p')
|
|
||||||
TODAY=$(date -u +%Y-%m-%d)
|
TODAY=$(date -u +%Y-%m-%d)
|
||||||
|
echo "LATEST_DATE=$LATEST_DATE" >> $GITHUB_ENV
|
||||||
echo "TODAY=$TODAY" >> $GITHUB_ENV
|
echo "TODAY=$TODAY" >> $GITHUB_ENV
|
||||||
if [ "$LATEST_DATE" == "$TODAY" ]; then
|
|
||||||
echo "LATEST_DATE=$SECOND_LATEST_DATE" >> $GITHUB_ENV
|
|
||||||
else
|
|
||||||
echo "LATEST_DATE=$LATEST_DATE" >> $GITHUB_ENV
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Get categorized pull requests
|
- name: Get merged PRs since last changelog
|
||||||
id: get-categorized-prs
|
id: merged_prs
|
||||||
uses: actions/github-script@v7
|
uses: actions/github-script@v7
|
||||||
with:
|
with:
|
||||||
script: |
|
script: |
|
||||||
const fs = require('fs').promises;
|
const latestDate = new Date(process.env.LATEST_DATE).toISOString();
|
||||||
const path = require('path');
|
const pulls = (await github.rest.pulls.list({
|
||||||
|
|
||||||
const configPath = path.resolve(process.env.CONFIG_PATH);
|
|
||||||
const fileContent = await fs.readFile(configPath, 'utf-8');
|
|
||||||
const changelogConfig = JSON.parse(fileContent);
|
|
||||||
const categorizedPRs = changelogConfig.map((obj) => ({ ...obj, notes: [] }));
|
|
||||||
|
|
||||||
const latestDateInChangelog = new Date(process.env.LATEST_DATE);
|
|
||||||
latestDateInChangelog.setUTCHours(23,59,59,999);
|
|
||||||
|
|
||||||
const { data: pulls } = await github.rest.pulls.list({
|
|
||||||
owner: context.repo.owner,
|
owner: context.repo.owner,
|
||||||
repo: context.repo.repo,
|
repo: context.repo.repo,
|
||||||
base: "main",
|
base: "main",
|
||||||
state: "closed",
|
state: "closed",
|
||||||
sort: "updated",
|
sort: "updated",
|
||||||
direction: "desc",
|
direction: "desc",
|
||||||
per_page: 100,
|
per_page: 100
|
||||||
});
|
})).data;
|
||||||
|
|
||||||
pulls.filter((pr) =>
|
const mergedPRs = pulls.filter(pr => pr.merged_at && new Date(pr.merged_at) > new Date(latestDate));
|
||||||
pr.merged_at &&
|
return mergedPRs.map(pr => `- ${pr.title} [@${pr.user.login}](https://github.com/${pr.user.login}) ([#${pr.number}](${pr.html_url}))`).join("\n");
|
||||||
new Date(pr.merged_at) > latestDateInChangelog &&
|
|
||||||
!pr.labels.some((label) => ["invalid", "wontdo", process.env.AUTOMATED_PR_LABEL].includes(label.name.toLowerCase()))
|
|
||||||
).forEach((pr) => {
|
|
||||||
const prLabels = pr.labels.map((label) => label.name.toLowerCase());
|
|
||||||
const prNote = `- ${pr.title} [@${pr.user.login}](https://github.com/${pr.user.login}) ([#${pr.number}](${pr.html_url}))`;
|
|
||||||
|
|
||||||
for (const { labels, notes } of categorizedPRs) {
|
|
||||||
const prHasCategoryLabel = labels.some((label) => prLabels.includes(label));
|
|
||||||
const isUnlabelledCategory = labels.length === 0;
|
|
||||||
if (prHasCategoryLabel || isUnlabelledCategory) {
|
|
||||||
notes.push(prNote);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return categorizedPRs;
|
|
||||||
|
|
||||||
- name: Update CHANGELOG.md
|
- name: Update CHANGELOG.md
|
||||||
uses: actions/github-script@v7
|
if: steps.merged_prs.outputs.result != ''
|
||||||
with:
|
|
||||||
script: |
|
|
||||||
const fs = require('fs').promises;
|
|
||||||
const path = require('path');
|
|
||||||
|
|
||||||
const today = process.env.TODAY;
|
|
||||||
const latestDateInChangelog = process.env.LATEST_DATE;
|
|
||||||
const changelogPath = path.resolve('CHANGELOG.md');
|
|
||||||
const categorizedPRs = ${{ steps.get-categorized-prs.outputs.result }};
|
|
||||||
|
|
||||||
let newReleaseNotes = `## ${today}\n\n### Changed\n\n`;
|
|
||||||
for (const { title, notes } of categorizedPRs) {
|
|
||||||
if (notes.length > 0) {
|
|
||||||
newReleaseNotes += `### ${title}\n\n${notes.join("\n")}\n\n`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const changelogContent = await fs.readFile(changelogPath, 'utf-8');
|
|
||||||
const changelogIncludesTodaysReleaseNotes = changelogContent.includes(`\n## ${today}`);
|
|
||||||
|
|
||||||
// Replace todays release notes or insert release notes above previous release notes
|
|
||||||
const regex = changelogIncludesTodaysReleaseNotes ?
|
|
||||||
new RegExp(`## ${today}.*(?=## ${latestDateInChangelog})`, "gs") :
|
|
||||||
new RegExp(`(?=## ${latestDateInChangelog})`, "gs");
|
|
||||||
|
|
||||||
const newChangelogContent = changelogContent.replace(regex, newReleaseNotes)
|
|
||||||
await fs.writeFile(changelogPath, newChangelogContent);
|
|
||||||
|
|
||||||
- name: Check if there are any changes
|
|
||||||
id: verify-diff
|
|
||||||
run: |
|
run: |
|
||||||
git diff --quiet . || echo "changed=true" >> $GITHUB_OUTPUT
|
echo -e "## $TODAY\n\n### Changes\n\n$(cat <<< "${{ steps.merged_prs.outputs.result }}")\n\n$(cat CHANGELOG.md)" > CHANGELOG.md
|
||||||
|
|
||||||
- name: Commit and push changes to separate branch
|
- name: Commit and push changes
|
||||||
if: steps.verify-diff.outputs.changed == 'true'
|
if: steps.merged_prs.outputs.result != ''
|
||||||
run: |
|
run: |
|
||||||
git config --global user.name "github-actions[bot]"
|
git config --global user.name "github-actions[bot]"
|
||||||
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
||||||
git add CHANGELOG.md
|
git add CHANGELOG.md
|
||||||
git commit -m "Update CHANGELOG.md"
|
git commit -m "Update CHANGELOG.md"
|
||||||
git checkout -b $BRANCH_NAME || git checkout $BRANCH_NAME
|
|
||||||
git push origin $BRANCH_NAME --force
|
git push origin $BRANCH_NAME --force
|
||||||
|
|
||||||
- name: Create pull request if not exists
|
|
||||||
if: steps.verify-diff.outputs.changed == 'true'
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
|
|
||||||
run: |
|
|
||||||
PR_EXISTS=$(gh pr list --head "${BRANCH_NAME}" --json number --jq '.[].number')
|
|
||||||
if [ -z "$PR_EXISTS" ]; then
|
|
||||||
gh pr create --title "[Github Action] Update CHANGELOG.md" \
|
|
||||||
--body "This PR is auto-generated by a Github Action to update the CHANGELOG.md file." \
|
|
||||||
--head $BRANCH_NAME \
|
|
||||||
--base main \
|
|
||||||
--label "$AUTOMATED_PR_LABEL"
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Approve pull request
|
|
||||||
if: steps.verify-diff.outputs.changed == 'true'
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
run: |
|
|
||||||
PR_NUMBER=$(gh pr list --head "${BRANCH_NAME}" --json number --jq '.[].number')
|
|
||||||
if [ -n "$PR_NUMBER" ]; then
|
|
||||||
gh pr review $PR_NUMBER --approve
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Re-approve pull request after update
|
|
||||||
if: steps.verify-diff.outputs.changed == 'true'
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
run: |
|
|
||||||
PR_NUMBER=$(gh pr list --head "${BRANCH_NAME}" --json number --jq '.[].number')
|
|
||||||
if [ -n "$PR_NUMBER" ]; then
|
|
||||||
gh pr review $PR_NUMBER --approve
|
|
||||||
fi
|
|
||||||
|
Loading…
Reference in New Issue
Block a user