Skip to content

Commit

Permalink
feat: manifest options to enable auto-merge on release pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dgellow committed Sep 14, 2023
1 parent 3d45465 commit 61d8757
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 6 deletions.
102 changes: 97 additions & 5 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
FileNotFoundError,
ConfigurationError,
isOctokitRequestError,
isOctokitGraphqlResponseError,

Check failure on line 31 in src/github.ts

View workflow job for this annotation

GitHub Actions / lint

'isOctokitGraphqlResponseError' is defined but never used. Allowed unused vars must match /^_$/u
} from './errors';

const MAX_ISSUE_BODY_SIZE = 65536;
Expand Down Expand Up @@ -213,10 +214,15 @@ interface FileDiff {
}
export type ChangeSet = Map<string, FileDiff>;

export type MergeMethod = 'MERGE' | 'SQUASH' | 'REBASE';

interface CreatePullRequestOptions {
fork?: boolean;
draft?: boolean;
reviewers?: [string];
autoMerge?: {
mergeMethod: MergeMethod;
};
/**
* If the number of an existing pull request is passed, its HEAD branch and attributes (title, labels, etc) will be
* updated instead of creating a new pull request.
Expand Down Expand Up @@ -1285,6 +1291,18 @@ export class GitHub {
pullRequest.labels
);

// attempt to enable auto-merge
if (options?.autoMerge) {
try {
await this.enablePullRequestAutoMerge(
pullRequestNumber,
options.autoMerge.mergeMethod
);
} catch (e: unknown) {
this.logger.warn(e as {}, `Failed to enable auto merge. Continuing.`);

Check warning on line 1302 in src/github.ts

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
}
}

// assign reviewers
if (options?.reviewers) {
try {
Expand All @@ -1294,11 +1312,10 @@ export class GitHub {
pull_number: pullRequestNumber,
reviewers: options.reviewers,
});
} catch (error) {
console.log(
`Failed to add reviewers. Continuing anyway: ${
isOctokitRequestError(error) ? error.message : error
}`
} catch (error: unknown) {
this.logger.warn(
isOctokitRequestError(error) ? error.message : (error as {}),
'Failed to add reviewers. Continuing.'
);
}
}
Expand Down Expand Up @@ -1353,6 +1370,9 @@ export class GitHub {
signoffUser?: string;
fork?: boolean;
pullRequestOverflowHandler?: PullRequestOverflowHandler;
autoMerge?: {
mergeMethod: MergeMethod;
};
}
): Promise<PullRequest> => {
// Update the files for the release if not already supplied
Expand Down Expand Up @@ -1388,6 +1408,7 @@ export class GitHub {
{
fork: options?.fork,
existingPrNumber: number,
autoMerge: options?.autoMerge,
}
);

Expand Down Expand Up @@ -2273,6 +2294,77 @@ export class GitHub {
invalidateFileCache() {
this.fileCache = new RepositoryFileCache(this.octokit, this.repository);
}

private async queryPullRequestId(
pullRequestNumber: number
): Promise<string | undefined> {
const query = `query pullRequestId($owner: String!, $repo: String!, $pullRequestNumber: Int!) {
repository(name: $repo, owner: $owner) {
pullRequest(number: $pullRequestNumber) {
id
}
}
}`;
const response = await this.graphqlRequest({
query,
owner: this.repository.owner,
repo: this.repository.repo,
pullRequestNumber,
});

this.logger.debug(response, 'queryPullrequestId');

const id = response?.repository?.pullRequest?.id;
if (id) {
return id as string;
}
return undefined;
}

private async mutatePullRequestEnableAutoMerge(
pullRequestId: string,
mergeMethod: MergeMethod
) {
const mutation = `mutation mutateEnableAutoMerge($pullRequestId: ID!, $mergeMethod: PullRequestMergeMethod) {
enablePullRequestAutoMerge(
input: {pullRequestId: $pullRequestId, mergeMethod: $mergeMethod}
) {
pullRequest {
autoMergeRequest{
authorEmail,
commitBody,
commitHeadline,
enabledAt,
enabledBy {
login
},
mergeMethod,
pullRequest{
id
}
}
}
}
}`;
const response = await this.graphqlRequest({
query: mutation,
pullRequestId,
mergeMethod,
});

this.logger.debug({response}, 'mutatePullrequestEnableAutoMerge');
}

private async enablePullRequestAutoMerge(
pullRequestNumber: number,
mergeMethod: MergeMethod
) {
const prId = await this.queryPullRequestId(pullRequestNumber);
if (!prId) {
throw new Error(`No id found for pull request ${pullRequestNumber}`);
}
await this.mutatePullRequestEnableAutoMerge(prId, mergeMethod);
}
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import {ChangelogSection} from './changelog-notes';
import {GitHub, GitHubRelease, GitHubTag} from './github';
import {GitHub, GitHubRelease, GitHubTag, MergeMethod} from './github';
import {Version, VersionsMap} from './version';
import {Commit, parseConventionalCommits} from './commit';
import {PullRequest} from './pull-request';
Expand Down Expand Up @@ -185,6 +185,7 @@ export interface ManifestOptions {
separatePullRequests?: boolean;
plugins?: PluginType[];
fork?: boolean;
autoMerge?: {mergeMethod: MergeMethod};
signoff?: string;
manifestPath?: string;
labels?: string[];
Expand Down Expand Up @@ -289,6 +290,7 @@ export class Manifest {
readonly changesBranch: string;
private separatePullRequests: boolean;
readonly fork: boolean;
private autoMerge?: {mergeMethod: MergeMethod};
private signoffUser?: string;
private labels: string[];
private skipLabeling?: boolean;
Expand Down Expand Up @@ -357,6 +359,7 @@ export class Manifest {
manifestOptions?.separatePullRequests ??
Object.keys(repositoryConfig).length === 1;
this.fork = manifestOptions?.fork || false;
this.autoMerge = manifestOptions?.autoMerge;
this.signoffUser = manifestOptions?.signoff;
this.releaseLabels =
manifestOptions?.releaseLabels || DEFAULT_RELEASE_LABELS;
Expand Down Expand Up @@ -1034,6 +1037,7 @@ export class Manifest {
{
fork: this.fork,
draft: pullRequest.draft,
autoMerge: this.autoMerge,
}
);

Expand Down Expand Up @@ -1065,6 +1069,7 @@ export class Manifest {
fork: this.fork,
signoffUser: this.signoffUser,
pullRequestOverflowHandler: this.pullRequestOverflowHandler,
autoMerge: this.autoMerge,
}
);

Expand Down Expand Up @@ -1092,6 +1097,7 @@ export class Manifest {
fork: this.fork,
signoffUser: this.signoffUser,
pullRequestOverflowHandler: this.pullRequestOverflowHandler,
autoMerge: this.autoMerge,
}
);
// TODO: consider leaving the snooze label
Expand Down

0 comments on commit 61d8757

Please sign in to comment.