Skip to content

Commit

Permalink
Merge pull request CleverRaven#72504 from CleverRaven/truncate-genera…
Browse files Browse the repository at this point in the history
…ted-release-notes

Truncate generated release notes for experimental releases
  • Loading branch information
kevingranade authored Mar 20, 2024
2 parents d57e526 + c55b548 commit 8d5e2e5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ on:

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPOSITORY_NAME: ${{ github.event.repository.name }}

jobs:
release:
Expand All @@ -39,8 +40,13 @@ jobs:
echo "tag_name=cdda-experimental-${{ steps.get-timestamp.outputs.time }}" >> $GITHUB_OUTPUT
echo "release_name=Cataclysm-DDA experimental build ${{ steps.get-timestamp.outputs.time }}" >> $GITHUB_OUTPUT
- uses: actions/checkout@v4
- name: Generate Release Notes
id: generate-release-notes
run: |
npm install @actions/github
node build-scripts/generate-release-notes.js '${{ secrets.GITHUB_TOKEN }}' '${{ steps.generate_env_vars.outputs.tag_name }}' "$(git log -1 --format='%H')" > notes.txt
- run: |
gh release create ${{ steps.generate_env_vars.outputs.tag_name }} --generate-notes --prerelease --title "${{ steps.generate_env_vars.outputs.release_name }}" --target "$(git log -1 --format='%H')"
gh release create ${{ steps.generate_env_vars.outputs.tag_name }} --notes-file notes.txt --prerelease --title "${{ steps.generate_env_vars.outputs.release_name }}" --target "$(git log -1 --format='%H')"
builds:
needs: release
Expand Down
77 changes: 77 additions & 0 deletions build-scripts/generate-release-notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// ./tools/scripts/generate-release-notes.js

const github = require('@actions/github');

/**
* Generates the release notes for a github release.
*
* Arguments:
* 1 - github_token
* 2 - new version
* 3 - commit SHA of new release
*/
const token = process.argv[2];
const version = process.argv[3];
const comittish = process.argv[4];
const repo = process.env.REPOSITORY_NAME
const owner = process.env.GITHUB_REPOSITORY_OWNER

async function main() {
const client = github.getOctokit(token);

const latestReleaseResponse = await client.request(
'GET /repos/{owner}/{repo}/releases/latest',
{
owner: owner,
repo: repo,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
}
);
const previousTag = latestReleaseResponse.data?.tag_name;

const response = await client.request(
'POST /repos/{owner}/{repo}/releases/generate-notes',
{
owner: owner,
repo: repo,
tag_name: version,
previous_tag_name: previousTag,
target_commitish: comittish,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
}
);

const noteSections = response.data.body?.split('\n\n');
const trimmedSections = [];
const githubNotesMaxCharLength = 125000;
const maxSectionLength = githubNotesMaxCharLength / noteSections.length;
for (let i = 0; i < noteSections.length; i++) {
if (noteSections[i].length > githubNotesMaxCharLength) {
const lastLineIndex =
noteSections[i].substring(0, maxSectionLength).split('\n').length - 1;
const trimmed =
noteSections[i]
.split('\n')
.slice(0, lastLineIndex - 1)
.join('\n') +
`\n... (+${
noteSections[i].split('\n').length - (lastLineIndex + 1)
} others)`;
trimmedSections.push(trimmed);
continue;
}

trimmedSections.push(noteSections[i]);
}

console.log(trimmedSections.join('\n\n'));
}

main().catch((e) => {
console.error(`Failed generating release notes with error: ${e}`);
process.exit(0);
});

0 comments on commit 8d5e2e5

Please sign in to comment.