forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-devpool-pr.ts
More file actions
82 lines (63 loc) · 2.28 KB
/
Copy pathcreate-devpool-pr.ts
File metadata and controls
82 lines (63 loc) · 2.28 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Context } from "../../types/context";
import { GithubContent, GitHubPayload } from "../../types/payload";
export async function createDevPoolPR(context: Context) {
const logger = context.logger;
const payload = context.event.payload as GitHubPayload;
const devPoolOwner = "ubiquity";
const devPoolRepo = "devpool-directory";
if (!payload.repositories_added) {
return logger.info("No repositories added");
}
const repository = payload.repositories_added[0];
logger.info("New Install: ", { repository: repository.full_name });
const [owner, repo] = repository.full_name.split("/");
// create a new branch
const branchName = `add-${owner}-${repo}`;
const baseRef = "development";
const path = "projects.json";
const { data: branch } = await context.event.octokit.repos.getBranch({
owner: devPoolOwner,
repo: devPoolRepo,
branch: "development",
});
// Get the current projects json file
const { data: file } = await context.event.octokit.repos.getContent({
owner: devPoolOwner,
repo: devPoolRepo,
path,
ref: "development",
});
const contentFile = Object.assign({} as GithubContent, file);
const curContent = Buffer.from(contentFile.content, "base64").toString();
const curContentParsed = JSON.parse(curContent);
// Edit the urls in content
curContentParsed["urls"].push(`https://github.com/${repository.full_name}`);
// current hash of main branch
const mainSha = branch.commit.sha;
// create branch from sha
await context.event.octokit.git.createRef({
owner: devPoolOwner,
repo: devPoolRepo,
ref: `refs/heads/add-${owner}-${repo}`,
sha: mainSha,
});
logger.info("Branch created on DevPool Directory");
await context.event.octokit.repos.createOrUpdateFileContents({
owner: devPoolOwner,
repo: devPoolRepo,
path,
message: `feat: add ${repository.full_name}`,
content: Buffer.from(JSON.stringify(curContentParsed, null, 2)).toString("base64"),
branch: branchName,
sha: contentFile.sha,
});
// create the pull request
await context.event.octokit.pulls.create({
owner: devPoolOwner,
repo: devPoolRepo,
title: `Add ${repository.full_name} to repo`,
head: branchName,
base: baseRef,
});
return logger.info("Pull request created on DevPool Directory");
}