Skip to content

Commit a0acaea

Browse files
committed
Added team name for better milestone pick
1 parent 46f4afb commit a0acaea

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ This action makes PR better for Zenhub integration.
99

1010
# Action input
1111

12-
| Name | Description | Example | Required |
13-
| ------------------ | -------------------------------------------------------------------------------- | ----------------------------------------------------------- | -------- |
14-
| `repo-token` | Repository Github token | `github-token` | yes |
15-
| `team-members` | List of Github usernames for members using toolkit (by default everybody included)| `username1,username2` | no |
12+
| Name | Description | Example | Required |
13+
| ------------------ | -------------------------------------------------------------------------------- | ----------------------------------------------------------- | -------- |
14+
| `repo-token` | Repository Github token | `github-token` | yes |
15+
| `team-members` | List of Github usernames for members using toolkit (by default everybody included) | `username1,username2` | no |
16+
| `team-name` | If name is provide, the milestone will be filter by regexp base on team name. | `platform` | no |
1617

1718
# Example usage
1819

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ inputs:
88
team-members:
99
required: false
1010
description: 'List of Github usernames'
11+
team-name:
12+
required: false
13+
description: 'Name of team'
1114
runs:
1215
using: 'node12'
1316
main: 'dist/index.js'

dist/index.js

Lines changed: 21 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function assignPrCreator(octokit: any, pullRequest: any): Promise<void> {
1212
console.log('Creator successfully assigned');
1313
}
1414

15-
async function fillCurrentMilestone(octokit: any, pullRequest: any): Promise<void> {
15+
async function fillCurrentMilestone(octokit: any, pullRequest: any, teamName: string): Promise<void> {
1616
// Assign PR to right sprint milestone
1717
const { data: milestones } = await octokit.request('GET /repos/{owner}/{repo}/milestones', {
1818
owner: github.context.repo.owner,
@@ -25,13 +25,27 @@ async function fillCurrentMilestone(octokit: any, pullRequest: any): Promise<voi
2525
}
2626

2727
const now = new Date();
28+
// All open milestones
2829
// @ts-ignore
29-
const openMilestone = milestones.find((milestone) => {
30+
const openMilestones = milestones.filter((milestone) => {
3031
return milestone.state === 'open'
3132
&& milestone.due_on && new Date(milestone.due_on) >= now;
3233
});
3334

34-
if (!openMilestone) {
35+
// Find milestone for the team, if team name was provided
36+
let foundMilestone;
37+
if (teamName) {
38+
const teamNameRegExp = new RegExp(teamName, 'i');
39+
// @ts-ignore
40+
foundMilestone = openMilestones.find(({ description, title }) => {
41+
return title.match(teamNameRegExp)
42+
|| description.match(teamNameRegExp);
43+
});
44+
} else if (openMilestones.length) {
45+
([foundMilestone] = openMilestones);
46+
}
47+
48+
if (!foundMilestone) {
3549
core.setFailed('Cannot find current sprint milestone!');
3650
return;
3751
}
@@ -40,15 +54,16 @@ async function fillCurrentMilestone(octokit: any, pullRequest: any): Promise<voi
4054
owner: github.context.repo.owner,
4155
repo: github.context.repo.repo,
4256
issue_number: pullRequest.number,
43-
milestone: openMilestone.number,
57+
milestone: foundMilestone.number,
4458
});
45-
console.log(`Milestone successfully filled with ${openMilestone.title}`);
59+
console.log(`Milestone successfully filled with ${foundMilestone.title}`);
4660
}
4761

4862
async function run(): Promise<void> {
4963
try {
5064
const repoToken = core.getInput('repo-token');
5165
const teamMembers = core.getInput('team-members');
66+
const teamName = core.getInput('team-members');
5267
const octokit = github.getOctokit(repoToken);
5368

5469
const teamMemberList = teamMembers ? teamMembers.split(',').map((member: string) => member.trim()) : [];
@@ -67,7 +82,7 @@ async function run(): Promise<void> {
6782
// Assign PR to creator of PR
6883
if (!isCreatorAssign) await assignPrCreator(octokit, pullRequest);
6984
// Fill milestone if there is any yet.
70-
if (!pullRequest.milestone) await fillCurrentMilestone(octokit, pullRequest);
85+
if (!pullRequest.milestone) await fillCurrentMilestone(octokit, pullRequest, teamName);
7186
} catch (error) {
7287
core.setFailed(error.message);
7388
}

0 commit comments

Comments
 (0)