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 10, 2022
1 parent 257fa0e commit 88a0793
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 93 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -129,8 +129,8 @@ You can configure Release Drafter using the following key in your `.github/relea
| `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. |
| `commitish` | Optional | The release target, i.e. branch/tag/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
45 changes: 24 additions & 21 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 ${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,11 @@ const generateReleaseInfo = ({
name = template(name, versionInfo)
}

if (commitish === undefined) {
commitish = config['commitish'] || ''
}

return {
name,
tag,
body,
commitish,
targetCommitish,
prerelease: isPreRelease,
draft: shouldDraft,
}
Expand All @@ -129375,7 +129378,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 +129392,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 ${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
21 changes: 10 additions & 11 deletions lib/releases.js
Expand Up @@ -17,7 +17,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 @@ -28,9 +32,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 @@ -269,7 +272,7 @@ const generateReleaseInfo = ({
name,
isPreRelease,
shouldDraft,
commitish,
targetCommitish,
}) => {
const { owner, repo } = context.repo()

Expand Down Expand Up @@ -319,15 +322,11 @@ const generateReleaseInfo = ({
name = template(name, versionInfo)
}

if (commitish === undefined) {
commitish = config['commitish'] || ''
}

return {
name,
tag,
body,
commitish,
targetCommitish,
prerelease: isPreRelease,
draft: shouldDraft,
}
Expand All @@ -336,7 +335,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 @@ -350,7 +349,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

0 comments on commit 88a0793

Please sign in to comment.