Skip to content

Commit

Permalink
Unify commit search and release targeting around commitish/ref (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Bulash committed Feb 13, 2022
1 parent 257fa0e commit 86d84ec
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 111 deletions.
50 changes: 25 additions & 25 deletions README.md
Expand Up @@ -106,31 +106,31 @@ template: |

You can configure Release Drafter using the following key in your `.github/release-drafter.yml` file:

| Key | Required | Description |
| -------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `template` | Required | The template for the body of the draft release. Use [template variables](#template-variables) to insert values. |
| `header` | Optional | Will be prepended to `template`. Use [template variables](#template-variables) to insert values. |
| `footer` | Optional | Will be appended to `template`. Use [template variables](#template-variables) to insert values. |
| `category-template` | Optional | The template to use for each category. Use [category template variables](#category-template-variables) to insert values. Default: `"## $TITLE"`. |
| `name-template` | Optional | The template for the name of the draft release. For example: `"v$NEXT_PATCH_VERSION"`. |
| `tag-template` | Optional | The template for the tag of the draft release. For example: `"v$NEXT_PATCH_VERSION"`. |
| `version-template` | Optional | The template to use when calculating the next version number for the release. Useful for projects that don't use semantic versioning. Default: `"$MAJOR.$MINOR.$PATCH"` |
| `change-template` | Optional | The template to use for each merged pull request. Use [change template variables](#change-template-variables) to insert values. Default: `"* $TITLE (#$NUMBER) @$AUTHOR"`. |
| `change-title-escapes` | Optional | Characters to escape in `$TITLE` when inserting into `change-template` so that they are not interpreted as Markdown format characters. Default: `""` |
| `no-changes-template` | Optional | The template to use for when there’s no changes. Default: `"* No changes"`. |
| `references` | Optional | The references to listen for configuration updates to `.github/release-drafter.yml`. Refer to [References](#references) to learn more about this |
| `categories` | Optional | Categorize pull requests using labels. Refer to [Categorize Pull Requests](#categorize-pull-requests) to learn more about this option. |
| `exclude-labels` | Optional | Exclude pull requests using labels. Refer to [Exclude Pull Requests](#exclude-pull-requests) to learn more about this option. |
| `include-labels` | Optional | Include only the specified pull requests using labels. Refer to [Include Pull Requests](#include-pull-requests) to learn more about this option. |
| `exclude-contributors` | Optional | Exclude specific usernames from the generated `$CONTRIBUTORS` variable. Refer to [Exclude Contributors](#exclude-contributors) to learn more about this option. |
| `no-contributors-template` | Optional | The template to use for `$CONTRIBUTORS` when there's no contributors to list. Default: `"No contributors"`. |
| `replacers` | Optional | Search and replace content in the generated changelog body. Refer to [Replacers](#replacers) to learn more about this option. |
| `sort-by` | Optional | Sort changelog by merged_at or title. Can be one of: `merged_at`, `title`. Default: `merged_at`. |
| `sort-direction` | Optional | Sort changelog in ascending or descending order. Can be one of: `ascending`, `descending`. Default: `descending`. |
| `prerelease` | Optional | Mark the draft release as pre-release. Default `false`. |
| `version-resolver` | Optional | Adjust the `$RESOLVED_VERSION` variable using labels. Refer to [Version Resolver](#version-resolver) to learn more about this |
| `filter-by-commitish` | Optional | Filter previous releases to consider only the target branch of the release. Default: `false`. |
| `commitish` | Optional | Specify the target branch of the release. Default: the default branch of the repo. |
| Key | Required | Description |
| -------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `template` | Required | The template for the body of the draft release. Use [template variables](#template-variables) to insert values. |
| `header` | Optional | Will be prepended to `template`. Use [template variables](#template-variables) to insert values. |
| `footer` | Optional | Will be appended to `template`. Use [template variables](#template-variables) to insert values. |
| `category-template` | Optional | The template to use for each category. Use [category template variables](#category-template-variables) to insert values. Default: `"## $TITLE"`. |
| `name-template` | Optional | The template for the name of the draft release. For example: `"v$NEXT_PATCH_VERSION"`. |
| `tag-template` | Optional | The template for the tag of the draft release. For example: `"v$NEXT_PATCH_VERSION"`. |
| `version-template` | Optional | The template to use when calculating the next version number for the release. Useful for projects that don't use semantic versioning. Default: `"$MAJOR.$MINOR.$PATCH"` |
| `change-template` | Optional | The template to use for each merged pull request. Use [change template variables](#change-template-variables) to insert values. Default: `"* $TITLE (#$NUMBER) @$AUTHOR"`. |
| `change-title-escapes` | Optional | Characters to escape in `$TITLE` when inserting into `change-template` so that they are not interpreted as Markdown format characters. Default: `""` |
| `no-changes-template` | Optional | The template to use for when there’s no changes. Default: `"* No changes"`. |
| `references` | Optional | The references to listen for configuration updates to `.github/release-drafter.yml`. Refer to [References](#references) to learn more about this |
| `categories` | Optional | Categorize pull requests using labels. Refer to [Categorize Pull Requests](#categorize-pull-requests) to learn more about this option. |
| `exclude-labels` | Optional | Exclude pull requests using labels. Refer to [Exclude Pull Requests](#exclude-pull-requests) to learn more about this option. |
| `include-labels` | Optional | Include only the specified pull requests using labels. Refer to [Include Pull Requests](#include-pull-requests) to learn more about this option. |
| `exclude-contributors` | Optional | Exclude specific usernames from the generated `$CONTRIBUTORS` variable. Refer to [Exclude Contributors](#exclude-contributors) to learn more about this option. |
| `no-contributors-template` | Optional | The template to use for `$CONTRIBUTORS` when there's no contributors to list. Default: `"No contributors"`. |
| `replacers` | Optional | Search and replace content in the generated changelog body. Refer to [Replacers](#replacers) to learn more about this option. |
| `sort-by` | Optional | Sort changelog by merged_at or title. Can be one of: `merged_at`, `title`. Default: `merged_at`. |
| `sort-direction` | Optional | Sort changelog in ascending or descending order. Can be one of: `ascending`, `descending`. Default: `descending`. |
| `prerelease` | Optional | Mark the draft release as pre-release. Default `false`. |
| `version-resolver` | Optional | Adjust the `$RESOLVED_VERSION` variable using labels. Refer to [Version Resolver](#version-resolver) to learn more about this |
| `commitish` | Optional | The release target, i.e. branch or commit it should point to. Default: the ref that release-drafter runs for, e.g. `refs/heads/master` if configured to run on pushes to `master`. |
| `filter-by-commitish` | Optional | Filter previous releases to consider only those with the target matching `commitish`. Default: `false`. |

Release Drafter also supports [Probot Config](https://github.com/probot/probot-config), if you want to store your configuration files in a central repository. This allows you to share configurations between projects, and create a organization-wide configuration file by creating a repository named `.github` with the file `.github/release-drafter.yml`.

Expand Down
53 changes: 34 additions & 19 deletions dist/index.js
Expand Up @@ -128604,15 +128604,19 @@ module.exports = (app, { getRouter }) => {
return
}

const targetCommitish = commitish || config['commitish'] || ref
const filterByCommitish = config['filter-by-commitish']

const { draftRelease, lastRelease } = await findReleases({
ref,
context,
config,
targetCommitish,
filterByCommitish,
})

const { commits, pullRequests: mergedPullRequests } =
await findCommitsWithAssociatedPullRequests({
context,
ref,
targetCommitish,
lastRelease,
config,
})
Expand All @@ -128634,7 +128638,7 @@ module.exports = (app, { getRouter }) => {
name,
isPreRelease,
shouldDraft,
commitish,
targetCommitish,
})

let createOrUpdateReleaseResponse
Expand Down Expand Up @@ -128725,7 +128729,7 @@ const findCommitsWithAssociatedPullRequestsQuery = /* GraphQL */ `
query findCommitsWithAssociatedPullRequests(
$name: String!
$owner: String!
$ref: String!
$targetCommitish: String!
$withPullRequestBody: Boolean!
$withPullRequestURL: Boolean!
$since: GitTimestamp
Expand All @@ -128734,7 +128738,7 @@ const findCommitsWithAssociatedPullRequestsQuery = /* GraphQL */ `
$withHeadRefName: Boolean!
) {
repository(name: $name, owner: $owner) {
object(expression: $ref) {
object(expression: $targetCommitish) {
... on Commit {
history(first: 100, since: $since, after: $after) {
totalCount
Expand Down Expand Up @@ -128786,15 +128790,15 @@ const findCommitsWithAssociatedPullRequestsQuery = /* GraphQL */ `

const findCommitsWithAssociatedPullRequests = async ({
context,
ref,
targetCommitish,
lastRelease,
config,
}) => {
const { owner, repo } = context.repo()
const variables = {
name: repo,
owner,
ref,
targetCommitish,
withPullRequestBody: config['change-template'].includes('$BODY'),
withPullRequestURL: config['change-template'].includes('$URL'),
withBaseRefName: config['change-template'].includes('$BASE_REF_NAME'),
Expand All @@ -128807,7 +128811,7 @@ const findCommitsWithAssociatedPullRequests = async ({
if (lastRelease) {
log({
context,
message: `Fetching all commits for reference ${ref} since ${lastRelease.created_at}`,
message: `Fetching parent commits of ${targetCommitish} since ${lastRelease.created_at}`,
})

data = await paginate(
Expand All @@ -128822,7 +128826,7 @@ const findCommitsWithAssociatedPullRequests = async ({
(commit) => commit.committedDate != lastRelease.created_at
)
} else {
log({ context, message: `Fetching all commits for reference ${ref}` })
log({ context, message: `Fetching parent commits of ${targetCommitish}` })

data = await paginate(
context.octokit.graphql,
Expand Down Expand Up @@ -129056,7 +129060,11 @@ const sortReleases = (releases) => {
}
}

const findReleases = async ({ ref, context, config }) => {
const findReleases = async ({
context,
targetCommitish,
filterByCommitish,
}) => {
let releases = await context.octokit.paginate(
context.octokit.repos.listReleases.endpoint.merge(
context.repo({
Expand All @@ -129067,9 +129075,8 @@ const findReleases = async ({ ref, context, config }) => {

log({ context, message: `Found ${releases.length} releases` })

const { 'filter-by-commitish': filterByCommitish } = config
const filteredReleases = filterByCommitish
? releases.filter((r) => ref.match(`/${r.target_commitish}$`))
? releases.filter((r) => targetCommitish.match(`/${r.target_commitish}$`))
: releases
const sortedPublishedReleases = sortReleases(
filteredReleases.filter((r) => !r.draft)
Expand Down Expand Up @@ -129308,7 +129315,7 @@ const generateReleaseInfo = ({
name,
isPreRelease,
shouldDraft,
commitish,
targetCommitish,
}) => {
const { owner, repo } = context.repo()

Expand Down Expand Up @@ -129358,15 +129365,23 @@ const generateReleaseInfo = ({
name = template(name, versionInfo)
}

if (commitish === undefined) {
commitish = config['commitish'] || ''
// Tags are not supported as `target_commitish` by Github API.
// GITHUB_REF or the ref from webhook start with `refs/tags/`, so we handle
// those here. If it doesn't but is still a tag - it must have been set
// explicitly by the user, so it's fair to just let the API respond with an error.
if (targetCommitish.startsWith('refs/tags/')) {
log({
context,
message: `${targetCommitish} is not supported as release target, falling back to default branch`,
})
targetCommitish = ''
}

return {
name,
tag,
body,
commitish,
targetCommitish,
prerelease: isPreRelease,
draft: shouldDraft,
}
Expand All @@ -129375,7 +129390,7 @@ const generateReleaseInfo = ({
const createRelease = ({ context, releaseInfo }) => {
return context.octokit.repos.createRelease(
context.repo({
target_commitish: releaseInfo.commitish,
target_commitish: releaseInfo.targetCommitish,
name: releaseInfo.name,
tag_name: releaseInfo.tag,
body: releaseInfo.body,
Expand All @@ -129389,7 +129404,7 @@ const updateRelease = ({ context, draftRelease, releaseInfo }) => {
const updateReleaseParameters = updateDraftReleaseParameters({
name: releaseInfo.name || draftRelease.name,
tag_name: releaseInfo.tag || draftRelease.tag_name,
target_commitish: releaseInfo.commitish,
target_commitish: releaseInfo.targetCommitish,
})

return context.octokit.repos.updateRelease(
Expand Down
12 changes: 8 additions & 4 deletions index.js
Expand Up @@ -158,15 +158,19 @@ module.exports = (app, { getRouter }) => {
return
}

const targetCommitish = commitish || config['commitish'] || ref
const filterByCommitish = config['filter-by-commitish']

const { draftRelease, lastRelease } = await findReleases({
ref,
context,
config,
targetCommitish,
filterByCommitish,
})

const { commits, pullRequests: mergedPullRequests } =
await findCommitsWithAssociatedPullRequests({
context,
ref,
targetCommitish,
lastRelease,
config,
})
Expand All @@ -188,7 +192,7 @@ module.exports = (app, { getRouter }) => {
name,
isPreRelease,
shouldDraft,
commitish,
targetCommitish,
})

let createOrUpdateReleaseResponse
Expand Down
12 changes: 6 additions & 6 deletions lib/commits.js
Expand Up @@ -6,7 +6,7 @@ const findCommitsWithAssociatedPullRequestsQuery = /* GraphQL */ `
query findCommitsWithAssociatedPullRequests(
$name: String!
$owner: String!
$ref: String!
$targetCommitish: String!
$withPullRequestBody: Boolean!
$withPullRequestURL: Boolean!
$since: GitTimestamp
Expand All @@ -15,7 +15,7 @@ const findCommitsWithAssociatedPullRequestsQuery = /* GraphQL */ `
$withHeadRefName: Boolean!
) {
repository(name: $name, owner: $owner) {
object(expression: $ref) {
object(expression: $targetCommitish) {
... on Commit {
history(first: 100, since: $since, after: $after) {
totalCount
Expand Down Expand Up @@ -67,15 +67,15 @@ const findCommitsWithAssociatedPullRequestsQuery = /* GraphQL */ `

const findCommitsWithAssociatedPullRequests = async ({
context,
ref,
targetCommitish,
lastRelease,
config,
}) => {
const { owner, repo } = context.repo()
const variables = {
name: repo,
owner,
ref,
targetCommitish,
withPullRequestBody: config['change-template'].includes('$BODY'),
withPullRequestURL: config['change-template'].includes('$URL'),
withBaseRefName: config['change-template'].includes('$BASE_REF_NAME'),
Expand All @@ -88,7 +88,7 @@ const findCommitsWithAssociatedPullRequests = async ({
if (lastRelease) {
log({
context,
message: `Fetching all commits for reference ${ref} since ${lastRelease.created_at}`,
message: `Fetching parent commits of ${targetCommitish} since ${lastRelease.created_at}`,
})

data = await paginate(
Expand All @@ -103,7 +103,7 @@ const findCommitsWithAssociatedPullRequests = async ({
(commit) => commit.committedDate != lastRelease.created_at
)
} else {
log({ context, message: `Fetching all commits for reference ${ref}` })
log({ context, message: `Fetching parent commits of ${targetCommitish}` })

data = await paginate(
context.octokit.graphql,
Expand Down

0 comments on commit 86d84ec

Please sign in to comment.