-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (55 loc) · 2.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const core = require("@actions/core");
const github = require("@actions/github");
const DEFAULT_SETTINGS = {
branchFork: "main",
branchUpstream: "main",
isDraft: false,
description: "",
title: null
};
const SETTINGS = {...DEFAULT_SETTINGS, ...JSON.parse(process.env.AUTO_PR_SETTINGS || "{}")};
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const [FORKED_REPO_OWNER, FORKED_REPO_NAME] = process.env.GITHUB_REPOSITORY.split("/");
const createBody = () => `
${SETTINGS.description}
Auto generated PR by [auto-fork-pr](https://github.com/UKnowWhoIm/auto-pr-fork) on ${new Date().toString()}
`;
const defaultTitle = () => `Catch up with ${FORKED_REPO_OWNER}/${FORKED_REPO_NAME}`
async function run () {
const octokit = github.getOctokit(GITHUB_TOKEN);
try {
const fokedRepoDetails = (await octokit.rest.repos.get({
owner: FORKED_REPO_OWNER,
repo: FORKED_REPO_NAME
})).data;
if (!fokedRepoDetails.fork) {
throw Error("Repository must be a fork");
}
const upstreamReposDetails = fokedRepoDetails.source;
await octokit.rest.pulls.create({
owner: upstreamReposDetails.owner.login,
repo: upstreamReposDetails.name,
head: `${FORKED_REPO_OWNER}:${SETTINGS.branchFork}`,
base: SETTINGS.branchUpstream,
title: SETTINGS.title || defaultTitle(),
body: createBody(),
draft: SETTINGS.isDraft
});
return "Pull request created successfully";
} catch (err) {
if (err.status === 404) {
throw Error("Repository not found, user may not have access to the repo");
} else if (err.status === 422) {
pattern = /Validation .+?: ({.+})/
const regexResult = err.message.match(pattern);
if (!regexResult) {
throw Error(err.message);
}
return JSON.parse(regexResult[1]).message;
}
throw err;
}
}
run()
.then(r => {console.log(r)})
.catch(err => {core.setFailed(err)});