-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl10nAssigner.js
105 lines (83 loc) · 2.86 KB
/
l10nAssigner.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
const core = require('@actions/core');
const github = require('@actions/github');
const GITHUB_TOKEN_KEY = 'github-token'
function isPullRequest() {
return github.context.payload.pull_request != null
}
function hasExistingAssignees() {
return github.context.payload.pull_request.assignees.length > 0
}
async function getCommits(octokit) {
const since = getThirtyDaysAgo();
const msg = `Getting commits since ${since}`
console.log(msg)
const commits = await octokit.rest.repos.listCommits({
owner: github.context.payload.repository.owner.login,
repo: github.context.payload.repository.name,
sha: github.context.payload.pull_request.base.sha,
since: since
})
return commits
}
function getThirtyDaysAgo() {
const date = new Date();
date.setDate(date.getDate() - 30);
const since = date.toISOString();
return since;
}
function getAuthors(commits, l10nLogin) {
console.log('Getting authors.')
const authors = []
commits.data.forEach((commit) => {
if (commit.author.login != l10nLogin) {
authors.push(commit.author.login)
}
})
return authors
}
async function assignAuthors(octokit, authors) {
console.log('Assigning authors.')
octokit.rest.issues.addAssignees({
owner: github.context.payload.repository.owner.login,
repo: github.context.payload.repository.name,
issue_number: github.context.payload.pull_request.number,
assignees: authors[0]
});
}
const run = async () => {
try {
console.log('Running auto-assign action.')
const time = (new Date()).toTimeString();
core.setOutput("time", time);
const token = core.getInput(GITHUB_TOKEN_KEY);
if (token == null || token == '') {
throw new Error('No github token found. The auto-assign action requires a token.')
}
const octokit = github.getOctokit(token)
if (!isPullRequest()) {
throw new Error(
'No pull request found. The auto-assign action only works for pull requests.'
)
}
if (hasExistingAssignees()) {
console.log(`Pull request has existing assignees. Skipping.`)
return
}
const l10nLogin = core.getInput('l10n-github-login');
if (l10nLogin == null || l10nLogin == '') {
throw new Error('No l10n login found. The auto-assign action requires a l10n login.')
}
const commits = await getCommits(octokit)
if (commits.data.length == 0) {
throw new Error('No commits found. The auto-assign action only works for pull requests with commits.')
}
const authors = getAuthors(commits, l10nLogin)
if (authors.length == 0) {
throw new Error('No authors found. The auto-assign action only works for pull requests with authors.')
}
await assignAuthors(octokit, authors)
} catch (error) {
core.setFailed(error.message);
}
}
exports.run = run;