-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchiveAll.js
More file actions
110 lines (91 loc) · 3.27 KB
/
archiveAll.js
File metadata and controls
110 lines (91 loc) · 3.27 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const fetch = require("node-fetch");
const fs = require("fs");
const { exec } = require('@actions/exec');
const { Octokit } = require("@octokit/rest");
const { customAlphabet } = require("nanoid");
const nanoid = customAlphabet(
"ModuleSymbhasOwnPrABCDEFGHNRVfgctiUvzKqYTJkLxpZXIjQW",
5
);
const { wait, createAsigneeList } = require('./utils');
const archiveJobs = async (data, octokit, owner, repo, workingDirectory, pathToContentFolder, archiveBranchPrefix, archiveCommitMessage, asigneeUsernames, startingBranch) => {
const markdownsToArchive = data.map((t) => {
return {
fileName: `${workingDirectory}/${pathToContentFolder}/${t.jobPostFilename}`,
url: t.url,
};
});
const asignees = createAsigneeList(asigneeUsernames.split(','), 1);
const branch = `${archiveBranchPrefix}/${nanoid()}`;
const fullCommitMessage = `${archiveCommitMessage}`;
await exec('git', ['-C', workingDirectory, 'branch', branch]);
await wait(200);
await exec('git', ['-C', workingDirectory, 'checkout', branch]);
await wait(200);
const archivedMarkdownUrls = [];
markdownsToArchive.forEach(({ fileName, url }) => {
try {
const content = fs.readFileSync(fileName, "utf8");
const match = content.match(/archived: "true"/g);
if (!match) {
const [, firstPart, secondPart] = content.match(/(---.*)(---.*)/s);
fs.writeFileSync(
fileName,
`${firstPart}archived: "true"\n${secondPart}`,
{ encoding: "utf8", flag: "w" }
);
archivedMarkdownUrls.push(url);
}
} catch (err) { }
});
await wait(200);
await exec('git', ['-C', workingDirectory, 'add', '-A']);
await wait(200);
await exec('git', ['-C', workingDirectory, 'commit', '--no-verify', '-m', fullCommitMessage]);
await wait(200);
await exec('git', ['-C', workingDirectory, 'push', '--set-upstream', 'origin', branch]);
await wait(200);
const prResponse = await octokit.pulls.create({
owner,
repo,
title: `${archiveCommitMessage} ${new Date().toLocaleDateString()}`,
head: branch,
base: startingBranch,
body: `
# ${archiveCommitMessage} ${new Date().toLocaleDateString()}
Dear CroCoder devs please merge this to archive jobs.
`,
draft: true,
maintainer_can_modify: true,
});
await wait(200);
const { number } = prResponse.data;
await octokit.pulls.requestReviewers({
owner,
repo,
pull_number: number,
reviewers: [asignees[0]]
});
await wait(200);
}
const archiveAllJobs = async (owner, repo, jobBoardApiUrl, jobBoardApiToken, workingDirectory, pathToContentFolder, archiveBranchPrefix, archiveCommitMessage, asigneeUsernames, startingBranch, githubToken) => {
const octokit = new Octokit({
auth: githubToken,
});
await fetch(`${jobBoardApiUrl}/archive`, {
method: "POST",
headers: { Authorization: jobBoardApiToken },
});
let url = `${jobBoardApiUrl}/archive`;
let options = {
method: "GET",
headers: { Authorization: jobBoardApiToken },
};
const response = await fetch(url, options);
const data = await response.json();
await archiveJobs(data, octokit, owner, repo, workingDirectory, pathToContentFolder, archiveBranchPrefix, archiveCommitMessage, asigneeUsernames, startingBranch);
}
module.exports = {
archiveAllJobs,
archiveJobs,
};