Skip to content

Commit ddec8f0

Browse files
committed
feat: respect PR template if found
1 parent 3e6ea7b commit ddec8f0

4 files changed

Lines changed: 59 additions & 2 deletions

File tree

packages/frontend-updates-runner/src/create-pull-request.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export async function createPullRequest({
1010
packageName,
1111
packageVersion,
1212
updateBody,
13+
template,
1314
}: {
1415
octokitWithAuth: Octokit;
1516
owner: string;
@@ -20,22 +21,31 @@ export async function createPullRequest({
2021
packageName: string;
2122
packageVersion: string;
2223
updateBody: string;
24+
template: string | null;
2325
}) {
24-
const { data } = await octokitWithAuth.rest.pulls.create({
26+
const response = await octokitWithAuth.rest.pulls.create({
2527
owner,
2628
repo,
2729
title,
2830
head,
2931
base,
3032
});
3133

34+
const { data } = response;
35+
3236
const previousBody = data.body ? `${data.body}\n\n` : "";
3337

38+
let body = `${previousBody}This PR was created by Simple Frontend (Jeremy) following an important release for ${packageName}.\n\n${updateBody}`;
39+
40+
if (template) {
41+
body = `${body}\n\n${template}`;
42+
}
43+
3444
await octokitWithAuth.rest.pulls.update({
3545
owner,
3646
repo,
3747
pull_number: data.number,
38-
body: `${previousBody}This PR was created by Simple Frontend (Jeremy) following an important release for ${packageName}.\n\n${updateBody}`,
48+
body,
3949
});
4050

4151
console.log(`Pull request created: ${owner}/${repo}#${data.number}`);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Octokit } from "octokit";
2+
3+
const PR_TEMPLATE_PATHS = [".github/pull_request_template.md", ".github/PULL_REQUEST_TEMPLATE.md"];
4+
5+
export async function getRepositoryPullRequestTemplate({
6+
octokitWithAuth,
7+
owner,
8+
repo,
9+
}: {
10+
octokitWithAuth: Octokit;
11+
owner: string;
12+
repo: string;
13+
}): Promise<string | null> {
14+
for (const path of PR_TEMPLATE_PATHS) {
15+
try {
16+
const res = await octokitWithAuth.rest.repos.getContent({
17+
owner,
18+
repo,
19+
path,
20+
});
21+
22+
// check that it's a file content
23+
if ("content" in res.data) {
24+
return Buffer.from(res.data.content, res.data.encoding as BufferEncoding).toString("utf-8");
25+
}
26+
} catch (error) {
27+
console.warn(`Unable to read PR template at ${path} for ${owner}/${repo}`);
28+
}
29+
}
30+
31+
return null;
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "dotenv/config";
2+
import { rmSync } from "node:fs";
3+
import { resolve } from "node:path";
4+
5+
rmSync(resolve(process.env.GITHUB_WORKSPACE!, "repositories"), { recursive: true, force: true });
6+
7+
import "./update-depency-in-repos.js";

packages/frontend-updates-runner/src/update-depency-in-repos.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { packageInRepo } from "./check-package-in-repo.js";
1010
import { execSync } from "child_process";
1111
import { createPullRequest } from "./create-pull-request.js";
1212
import { getUpdateBody } from "./get-update-body.js";
13+
import { getRepositoryPullRequestTemplate } from "./get-repository-pull-request-template.js";
1314

1415
const REPOSITORIES_PATH = resolve(process.env.GITHUB_WORKSPACE!, "repositories");
1516

@@ -36,6 +37,12 @@ export async function updateDependencyInRepos({
3637
installationId: installation.id,
3738
});
3839

40+
const template = await getRepositoryPullRequestTemplate({
41+
octokitWithAuth,
42+
owner: repository.owner.login,
43+
repo: repository.name,
44+
});
45+
3946
const git = simpleGit()
4047
.addConfig("user.email", "jeremypa.colin@gmail.com", false, "global")
4148
.addConfig("user.name", "Jeremy Colin (Simple Frontend)", false, "global");
@@ -106,6 +113,7 @@ export async function updateDependencyInRepos({
106113
packageName,
107114
packageVersion,
108115
updateBody,
116+
template,
109117
});
110118
} catch (error) {
111119
console.error(error);

0 commit comments

Comments
 (0)