2022-12-27 15:37:40 +01:00
|
|
|
import { Octokit } from "@octokit/rest"
|
|
|
|
|
import { Option, program } from "commander"
|
|
|
|
|
import { fileURLToPath } from "url"
|
2021-06-18 11:10:33 +02:00
|
|
|
import path from "path"
|
|
|
|
|
import fs from "fs"
|
2022-08-04 18:18:57 +02:00
|
|
|
import crypto from "crypto"
|
2021-06-18 11:10:33 +02:00
|
|
|
|
|
|
|
|
const wasRunFromCli = fileURLToPath(import.meta.url).startsWith(process.argv[1])
|
|
|
|
|
|
2022-08-04 18:18:57 +02:00
|
|
|
function hashFileSha256(filePath) {
|
|
|
|
|
const input = fs.readFileSync(filePath)
|
2022-12-27 15:37:40 +01:00
|
|
|
return crypto.createHash("sha256").update(input).digest("hex")
|
2022-08-04 18:18:57 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-18 11:10:33 +02:00
|
|
|
if (wasRunFromCli) {
|
2022-04-28 14:36:24 +02:00
|
|
|
program
|
2022-12-27 15:37:40 +01:00
|
|
|
.requiredOption("--releaseName <releaseName>", "Name of the release")
|
|
|
|
|
.requiredOption("--milestone <milestone>", "Milestone to reference")
|
|
|
|
|
.requiredOption("--tag <tag>", "The commit tag to reference")
|
2022-04-28 14:36:24 +02:00
|
|
|
.addOption(
|
2022-12-27 15:37:40 +01:00
|
|
|
new Option("--platform <platform>", "label filter for the issues to include in the notes")
|
2022-12-09 16:50:39 +01:00
|
|
|
.choices(["android", "ios", "desktop", "web"])
|
2022-12-27 15:37:40 +01:00
|
|
|
.default("web"),
|
2022-04-28 14:36:24 +02:00
|
|
|
)
|
2022-12-27 15:37:40 +01:00
|
|
|
.addOption(
|
|
|
|
|
new Option("--uploadFile <filePath>", "path to a file to upload. can be passed multiple times.")
|
|
|
|
|
.argParser((cur, prev) => (prev ? prev.concat(cur) : [cur]))
|
|
|
|
|
.default([]),
|
2022-08-04 18:18:57 +02:00
|
|
|
)
|
2022-12-27 15:37:40 +01:00
|
|
|
.option("--toFile <toFile>", "If provided, the release notes will be written to the given file path. Implies `--dryRun`")
|
|
|
|
|
.option("--dryRun", "Don't make any changes to github")
|
|
|
|
|
.option("--format <format>", "Format to generate notes in", "github")
|
2022-04-26 18:09:04 +02:00
|
|
|
.action(async (options) => {
|
|
|
|
|
await createReleaseNotes(options)
|
|
|
|
|
})
|
|
|
|
|
.parseAsync(process.argv)
|
|
|
|
|
}
|
2021-06-18 11:10:33 +02:00
|
|
|
|
2022-12-27 15:37:40 +01:00
|
|
|
async function createReleaseNotes({ releaseName, milestone, tag, platform, uploadFile, toFile, dryRun, format }) {
|
2021-06-18 11:10:33 +02:00
|
|
|
const releaseToken = process.env.GITHUB_TOKEN
|
|
|
|
|
|
|
|
|
|
if (!releaseToken) {
|
|
|
|
|
throw new Error("No GITHUB_TOKEN set!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const octokit = new Octokit({
|
|
|
|
|
auth: releaseToken,
|
2022-12-27 15:37:40 +01:00
|
|
|
userAgent: "tuta-github-release-v0.0.1",
|
2021-06-18 11:10:33 +02:00
|
|
|
})
|
|
|
|
|
|
2022-04-26 18:09:04 +02:00
|
|
|
let releaseNotes
|
|
|
|
|
|
|
|
|
|
const githubMilestone = await getMilestone(octokit, milestone)
|
|
|
|
|
const issues = await getIssuesForMilestone(octokit, githubMilestone)
|
2022-12-27 15:37:40 +01:00
|
|
|
const { bugs, other } = sortIssues(filterIssues(issues, platform))
|
2022-04-26 18:09:04 +02:00
|
|
|
|
|
|
|
|
if (format === "ios") {
|
|
|
|
|
releaseNotes = renderIosReleaseNotes(bugs, other)
|
|
|
|
|
} else {
|
|
|
|
|
releaseNotes = renderGithubReleaseNotes({
|
|
|
|
|
milestoneUrl: githubMilestone.html_url,
|
|
|
|
|
bugIssues: bugs,
|
|
|
|
|
otherIssues: other,
|
2022-12-27 15:37:40 +01:00
|
|
|
files: uploadFile,
|
2021-06-18 11:10:33 +02:00
|
|
|
})
|
2022-04-26 18:09:04 +02:00
|
|
|
}
|
2021-06-18 11:10:33 +02:00
|
|
|
|
2022-04-26 18:09:04 +02:00
|
|
|
console.log("Release notes:")
|
|
|
|
|
console.log(releaseNotes)
|
2021-06-18 11:10:33 +02:00
|
|
|
|
2022-04-26 18:09:04 +02:00
|
|
|
if (!dryRun && !toFile) {
|
|
|
|
|
const draftResponse = await createReleaseDraft(octokit, releaseName, tag, releaseNotes)
|
2021-06-18 11:10:33 +02:00
|
|
|
|
2022-12-27 15:37:40 +01:00
|
|
|
const { upload_url, id } = draftResponse.data
|
2022-08-04 18:18:57 +02:00
|
|
|
for (const filePath of uploadFile) {
|
|
|
|
|
console.log(`Uploading asset "${filePath}"`)
|
|
|
|
|
await uploadAsset(octokit, upload_url, id, filePath)
|
2022-04-26 18:09:04 +02:00
|
|
|
}
|
2021-06-18 11:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-26 18:09:04 +02:00
|
|
|
if (toFile) {
|
2022-05-11 10:55:59 +02:00
|
|
|
console.log(`writing release notes to ${toFile}`)
|
2022-05-11 10:48:15 +02:00
|
|
|
await fs.promises.writeFile(toFile, releaseNotes, "utf-8")
|
2022-04-26 18:09:04 +02:00
|
|
|
}
|
2021-06-18 11:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getMilestone(octokit, milestoneName) {
|
2022-12-27 15:37:40 +01:00
|
|
|
const { data } = await octokit.issues.listMilestones({
|
2022-04-26 18:09:04 +02:00
|
|
|
owner: "tutao",
|
|
|
|
|
repo: "tutanota",
|
|
|
|
|
direction: "desc",
|
2022-12-27 15:37:40 +01:00
|
|
|
state: "all",
|
2022-04-26 18:09:04 +02:00
|
|
|
})
|
2021-06-18 11:10:33 +02:00
|
|
|
|
2022-12-27 15:37:40 +01:00
|
|
|
const milestone = data.find((m) => m.title === milestoneName)
|
2021-06-18 11:10:33 +02:00
|
|
|
|
|
|
|
|
if (milestone) {
|
|
|
|
|
return milestone
|
|
|
|
|
} else {
|
2022-12-27 15:37:40 +01:00
|
|
|
const titles = data.map((m) => m.title)
|
2021-06-18 11:10:33 +02:00
|
|
|
throw new Error(`No milestone named ${milestoneName} found. Milestones: ${titles.join(", ")}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-26 18:09:04 +02:00
|
|
|
async function getIssuesForMilestone(octokit, milestone) {
|
2021-06-18 11:10:33 +02:00
|
|
|
const response = await octokit.issues.listForRepo({
|
|
|
|
|
owner: "tutao",
|
|
|
|
|
repo: "tutanota",
|
|
|
|
|
milestone: milestone.number,
|
2022-12-27 15:37:40 +01:00
|
|
|
state: "all",
|
2021-06-18 11:10:33 +02:00
|
|
|
})
|
|
|
|
|
return response.data
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 14:02:45 +01:00
|
|
|
/**
|
|
|
|
|
* Filter the issues for the given platform.
|
2022-06-27 14:45:40 +02:00
|
|
|
* If an issue has no platform label, then it will be included
|
2022-02-23 14:02:45 +01:00
|
|
|
* If an issue has a label for a different platform, it won't be included,
|
2022-06-27 14:45:40 +02:00
|
|
|
* _unless_ it also has the label for the specified platform.
|
2022-02-23 14:02:45 +01:00
|
|
|
*/
|
2021-06-18 11:10:33 +02:00
|
|
|
function filterIssues(issues, platform) {
|
2022-12-09 16:50:39 +01:00
|
|
|
const allPlatforms = new Set(["android", "ios", "desktop"])
|
|
|
|
|
// issues that have any of these labels will not be included in any release notes
|
|
|
|
|
const excludedLabels = new Set(["dev bug", "usage test"])
|
2022-12-27 15:37:40 +01:00
|
|
|
issues = issues.filter((issue) => !issue.labels.some((label) => excludedLabels.has(label.name)))
|
2022-12-09 16:50:39 +01:00
|
|
|
|
|
|
|
|
if (platform === "web") {
|
|
|
|
|
// for the web app, we only want to include issues that don't have a platform label
|
2022-12-27 15:37:40 +01:00
|
|
|
return issues.filter((i) => areDisjoint(labelSet(i), allPlatforms))
|
2022-12-09 16:50:39 +01:00
|
|
|
} else if (allPlatforms.has(platform)) {
|
|
|
|
|
const otherPlatforms = new Set(allPlatforms)
|
|
|
|
|
otherPlatforms.delete(platform)
|
2022-12-27 15:37:40 +01:00
|
|
|
return issues.filter((issue) => issue.labels.some((label) => label.name === platform) || !issue.labels.some((label) => otherPlatforms.has(label.name)))
|
2021-06-18 11:10:33 +02:00
|
|
|
} else {
|
|
|
|
|
throw new Error(`Invalid value "${platform}" for "platform"`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-26 18:09:04 +02:00
|
|
|
/**
|
|
|
|
|
* Sort issues into bug issues and other issues
|
|
|
|
|
*/
|
2021-06-18 11:10:33 +02:00
|
|
|
function sortIssues(issues) {
|
|
|
|
|
const bugs = []
|
2022-04-26 18:09:04 +02:00
|
|
|
const other = []
|
2021-06-18 11:10:33 +02:00
|
|
|
for (const issue of issues) {
|
2022-12-27 15:37:40 +01:00
|
|
|
const isBug = issue.labels.find((l) => l.name === "bug" || l.name === "dev bug")
|
2021-06-18 11:10:33 +02:00
|
|
|
if (isBug) {
|
|
|
|
|
bugs.push(issue)
|
|
|
|
|
} else {
|
2022-04-26 18:09:04 +02:00
|
|
|
other.push(issue)
|
2021-06-18 11:10:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-12-27 15:37:40 +01:00
|
|
|
return { bugs, other }
|
2021-06-18 11:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
2022-12-27 15:37:40 +01:00
|
|
|
function renderGithubReleaseNotes({ milestoneUrl, bugIssues, otherIssues, files }) {
|
|
|
|
|
const whatsNewListRendered = otherIssues.length > 0 ? "# What's new\n" + otherIssues.map((issue) => ` - ${issue.title} #${issue.number}`).join("\n") : ""
|
2022-04-26 18:09:04 +02:00
|
|
|
|
2022-12-27 15:37:40 +01:00
|
|
|
const bugsListRendered = bugIssues.length > 0 ? "# Bugfixes\n" + bugIssues.map((issue) => ` - ${issue.title} #${issue.number}`).join("\n") : ""
|
2021-06-18 11:10:33 +02:00
|
|
|
|
|
|
|
|
const milestoneUrlObject = new URL(milestoneUrl)
|
|
|
|
|
milestoneUrlObject.searchParams.append("closed", "1")
|
2022-04-26 18:09:04 +02:00
|
|
|
|
2022-08-10 13:41:58 +02:00
|
|
|
let assetSection = ""
|
2022-08-04 18:18:57 +02:00
|
|
|
if (files.length > 0) {
|
2022-08-10 13:41:58 +02:00
|
|
|
assetSection += "# Asset Checksums (SHA256)\n"
|
2022-08-04 18:18:57 +02:00
|
|
|
for (const f of files) {
|
|
|
|
|
const hash = hashFileSha256(f)
|
|
|
|
|
const filename = path.basename(f)
|
|
|
|
|
console.log(`hash of ${filename}: `, hash)
|
2022-08-10 13:41:58 +02:00
|
|
|
assetSection += `**${filename}:**\n${hash}\n\n`
|
2022-08-04 18:18:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-04-26 18:09:04 +02:00
|
|
|
|
|
|
|
|
return `
|
2021-06-18 11:10:33 +02:00
|
|
|
${whatsNewListRendered}
|
|
|
|
|
|
|
|
|
|
${bugsListRendered}
|
|
|
|
|
|
|
|
|
|
# Milestone
|
|
|
|
|
${milestoneUrlObject.toString()}
|
|
|
|
|
|
2022-08-10 13:41:58 +02:00
|
|
|
${assetSection}
|
2022-04-26 18:09:04 +02:00
|
|
|
`.trim()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderIosReleaseNotes(bugs, rest) {
|
2022-12-27 15:37:40 +01:00
|
|
|
const whatsNewSection = rest.length > 0 ? "what's new:\n" + rest.map((issue) => issue.title).join("\n") : ""
|
2022-08-10 13:41:58 +02:00
|
|
|
|
2022-12-27 15:37:40 +01:00
|
|
|
const bugfixSection = bugs.length > 0 ? "bugfixes:\n" + bugs.map((issue) => "fixed " + issue.title).join("\n") : ""
|
2022-04-26 18:09:04 +02:00
|
|
|
|
2022-08-10 13:41:58 +02:00
|
|
|
return `${whatsNewSection}\n${bugfixSection}`.trim()
|
2021-06-18 11:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function createReleaseDraft(octokit, name, tag, body) {
|
|
|
|
|
return octokit.repos.createRelease({
|
|
|
|
|
owner: "tutao",
|
|
|
|
|
repo: "tutanota",
|
|
|
|
|
draft: true,
|
|
|
|
|
name,
|
|
|
|
|
tag_name: tag,
|
|
|
|
|
body,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function uploadAsset(octokit, uploadUrl, releaseId, assetPath) {
|
|
|
|
|
const response = octokit.rest.repos.uploadReleaseAsset({
|
|
|
|
|
owner: "tutao",
|
|
|
|
|
repo: "tutanota",
|
|
|
|
|
release_id: releaseId,
|
|
|
|
|
data: await fs.promises.readFile(assetPath),
|
|
|
|
|
name: path.basename(assetPath),
|
2022-12-27 15:37:40 +01:00
|
|
|
upload_url: uploadUrl,
|
|
|
|
|
})
|
2021-06-18 11:10:33 +02:00
|
|
|
|
|
|
|
|
if (response.status < 200 || response.status > 299) {
|
|
|
|
|
console.error(`Asset upload failed "${assetPath}. Response:"`, response)
|
|
|
|
|
}
|
2022-12-09 16:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test whether two js sets have no elements in common
|
|
|
|
|
*/
|
|
|
|
|
function areDisjoint(setA, setB) {
|
2022-12-27 15:37:40 +01:00
|
|
|
return [...setA].filter((el) => setB.has(el)).length === 0
|
2022-12-09 16:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function labelSet(issue) {
|
2022-12-27 15:37:40 +01:00
|
|
|
return new Set(issue.labels.map((l) => l.name))
|
|
|
|
|
}
|