-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
123 lines (99 loc) · 3.05 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
const core = require('@actions/core');
const github = require('@actions/github');
const yaml = require("yaml");
const _ = require("lodash");
const context = github.context;
const parseConfig = function (content) {
var r = yaml.parse(content);
console.log(r);
if(r == []) {
throw "config is empty or unparseable"
}
return r;
};
async function fetchContent(client) {
const response = await client.rest.repos.getContent({
owner: "dm-vdo",
repo: "vdo-auto-assign",
path: ".github/auto_assign.yml"
});
return Buffer.from(response.data.content, response.data.encoding).toString();
};
async function assignReviewer(octokit, reviewer) {
let key = "reviewers";
let target = reviewer;
if (_.isObject(reviewer) && _.has(reviewer, "team")) {
key = "team_reviewers";
target = reviewer.team;
}
await octokit.rest.pulls.requestReviewers({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
[key]: [target],
});
}
async function assignAssignee(octokit, assignee) {
let key = "assignees";
let target = assignee;
if (_.isObject(assignee) && _.has(assignee, "team")) {
return;
}
await octokit.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
[key]: [target],
});
}
const main = async () => {
try {
/**
* We need to fetch all the inputs that were provided to our action
* and store them in variables for us to use.
**/
const token = core.getInput('token', { required: true });
/**
* Now we need to create an instance of Octokit which will use to call
* GitHub's REST API endpoints.
* We will pass the token as an argument to the constructor. This token
* will be used to authenticate our requests.
* You can find all the information about how to use Octokit here:
* https://octokit.github.io/rest.js/v18
**/
const octokit = new github.getOctokit(token);
// Now we parse the config file.
const configContent = await fetchContent(octokit);
const config = parseConfig(configContent);
core.debug("config");
core.debug(JSON.stringify(config));
const { data: pullRequest } = await octokit.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
});
const author = pullRequest.user.login;
// Set a reviewer if there isn't one already.
if ((!pullRequest.requested_reviewers.length) && (!pullRequest.requested_teams.length)) {
// Authors cannot be reviewers
let reviewer = config["czar"];
if (config["czar"] == author) {
reviewer = config["backup"];
}
assignReviewer(octokit, reviewer).catch((error) => {
core.setFailed(error.message);
});
}
// Set an assignee if there isn't one already.
if ((!pullRequest.assignees.length)) {
assignAssignee(octokit, author).catch((error) => {
core.setFailed(error.message);
});
}
core.info("finished!");
} catch (error) {
core.setFailed(error.message);
}
}
// Call the main function to run the action
main();