fix: DIA-1403: Add total_cost, previous_version to refine output API … #19
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
name: "Update Draft Release" | |
on: | |
push: | |
branches: | |
- 'release/**' | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.event.pull_request.head.ref || github.ref }} | |
cancel-in-progress: true | |
env: | |
RELEASE_BRANCH_PREFIX: "release/" | |
jobs: | |
draft-release: | |
name: "Draft Release" | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
outputs: | |
id: ${{ steps.create-draft-release.outputs.id }} | |
rc-version: ${{ steps.create-draft-release.outputs.rc-version }} | |
steps: | |
- name: Create release draft | |
uses: actions/github-script@v7 | |
id: create-draft-release | |
env: | |
TARGET_COMMITISH: "${{ github.ref_name }}" | |
RELEASE_BRANCH_PREFIX: "${{ env.RELEASE_BRANCH_PREFIX }}" | |
DEFAULT_BRANCH: "${{ github.event.repository.default_branch }}" | |
with: | |
script: | | |
const { repo, owner } = context.repo; | |
const target_commitish = process.env.TARGET_COMMITISH; | |
const default_branch = process.env.DEFAULT_BRANCH; | |
let version = target_commitish.replace(process.env.RELEASE_BRANCH_PREFIX, '') | |
const regexp = '^[v]?([0-9]+)\.([0-9]+)\.([0-9]+)(\.post([0-9]+))?$'; | |
const {data: compare} = await github.rest.repos.compareCommits({ | |
owner, | |
repo, | |
base: default_branch, | |
head: target_commitish, | |
}); | |
const rc_version = `${version}rc${ compare.ahead_by }` | |
console.log(`rc-version: ${rc_version}`) | |
core.setOutput("rc-version", rc_version); | |
function compareVersions(a, b) { | |
if (a[1] === b[1]) | |
if (a[2] === b[2]) | |
if (a[3] === b[3]) | |
return (+a[5] || -1) - (+b[5] || -1) | |
else | |
return +a[3] - b[3] | |
else | |
return +a[2] - b[2] | |
else | |
return +a[1] - b[1] | |
} | |
const versionMatch = version.match(regexp) | |
if (!versionMatch) { | |
core.setFailed(`Version "${version}" from branch "${target_commitish}" does not match the regexp ${regexp}`) | |
process.exit() | |
} | |
const tags = await github.paginate( | |
github.rest.repos.listTags, | |
{ | |
owner, | |
repo, | |
per_page: 100 | |
}, | |
(response) => response.data | |
); | |
console.log(`Tags:`) | |
console.log(tags.map(e => e.name)) | |
const matchedTags = tags.filter(e => e.name.indexOf(version) !== -1) | |
console.log(`Tags for ${version}:`) | |
console.log(matchedTags.map(e => e.name)) | |
if (matchedTags.length !== 0) { | |
let newHotfixNumber = 0 | |
for (let matchedTag of matchedTags) { | |
const matchVersion = matchedTag.name.match('^[v]?([0-9]+)\.([0-9]+)\.([0-9]+)(.post([0-9]+))?$') | |
if (matchVersion && matchVersion[5]) { | |
const hotfixNumber = parseInt(matchVersion[5]) | |
if (newHotfixNumber <= hotfixNumber) { | |
newHotfixNumber = hotfixNumber + 1 | |
} | |
} | |
} | |
version = `${version}.post${newHotfixNumber}` | |
} | |
console.log(`New version: ${version}`) | |
const rawTags = tags.map(e => e.name) | |
const tagsWithNew = [...rawTags, version] | |
const sortedTags = tagsWithNew | |
.filter(e => e.match(regexp)) | |
.map((e => e.match(regexp))) | |
.sort(compareVersions) | |
.reverse() | |
.map(e => e[0]) | |
const previousTag = sortedTags[sortedTags.indexOf(version)+1] | |
console.log(`Previous version: ${previousTag}`) | |
console.log('Find or Create a Draft release') | |
const releases = await github.paginate( | |
github.rest.repos.listReleases, | |
{ | |
owner, | |
repo, | |
per_page: 100 | |
}, | |
(response) => response.data | |
); | |
let release = releases.find(e => target_commitish.endsWith(e.target_commitish) && e.draft) | |
if (release) { | |
console.log(`Draft release already exist ${release.html_url}`) | |
} else { | |
console.log(`Draft release is not found creating a new one`) | |
const {data: newDraftRelease} = await github.rest.repos.createRelease({ | |
owner, | |
repo, | |
draft: true, | |
prerelease: false, | |
name: version, | |
tag_name: version, | |
target_commitish: target_commitish, | |
}); | |
console.log(`Draft release is created ${newDraftRelease.html_url}`) | |
release = newDraftRelease; | |
core.setOutput("created", true); | |
} | |
console.log('Updating release with new release notes') | |
const {data: release_notes} = await github.rest.repos.generateReleaseNotes({ | |
owner, | |
repo, | |
tag_name: version, | |
previous_tag_name: previousTag, | |
target_commitish: target_commitish, | |
}); | |
const {data: updated_release} = await github.rest.repos.updateRelease({ | |
owner, | |
repo, | |
release_id: release.id, | |
draft: true, | |
prerelease: false, | |
name: version, | |
tag_name: version, | |
target_commitish: target_commitish, | |
body: release_notes.body | |
}); | |
console.log(`Draft release is updated ${updated_release.html_url}`) | |
core.setOutput("id", updated_release.id); | |
core.setOutput("tag_name", release.tag_name); | |
build-pypi: | |
name: "Build" | |
needs: | |
- draft-release | |
permissions: | |
contents: write | |
uses: ./.github/workflows/build_pypi.yml | |
with: | |
version: ${{ needs.draft-release.outputs.rc-version }} | |
ref: ${{ github.ref_name }} | |
release-id: ${{ needs.draft-release.outputs.id }} | |
secrets: inherit |