-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
143 lines (130 loc) · 5.41 KB
/
index.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as fetch from 'node-fetch';
import * as core from '@actions/core';
// RegExp Expressions of correct repo and project URL
let projectUrlRegex = /https:\/\/github.com\/(orgs|users)\/([^/]+)\/projects\/([\d]+)\/?/;
let repoUrlRegex = /https:\/\/github.com\/([^/]+)\/([^/]+)\/?/;
// Function to iterate the pullRequests and update the Project Column
export async function updateProjectColumn(pullReqListEndpoint: string, cardEndpoint: string, authToken: string, COLUMN_NAME: string, currentHours: number, HOURS_FLAG: number) {
try {
let res = await fetch(pullReqListEndpoint, {})
let json: Array<any> = await res.json();
for (let pullReq of json) {
let createdAtHours = new Date(pullReq["created_at"]).getHours();
let hoursDiff = currentHours - createdAtHours;
if (hoursDiff <= HOURS_FLAG) {
// Adding PR Card To Column
let res = await addPRCardToColumn(cardEndpoint, pullReq["id"], authToken);
if (!res.error) {
if (!res.already_added) {
// Successfully added
core.info(`Added [PR Title: ${pullReq['title']}]`);
}
} else {
core.info(`Failed to Add [PR Title: ${pullReq['title']}]`);
core.error(res.message);
}
}
}
} catch (err) {
core.info(err);
}
}
// Function verifies the projectEndpoint and returns the columnEndpoint of desired project
export async function getColumnEndpoint(projectEndpoint: string, PROJECT_URL: string) {
let res = await fetch(projectEndpoint, {
'headers': {
'Accept': 'application/vnd.github.inertia-preview+json'
}
});
let json: Array<any> = await res.json();
if (json["message"] === 'Not Found') {
// Invalid Project Associated With The User
throw new Error("No Projects Associated With The User");
}
let project = json.find((e) => {
return e.html_url == PROJECT_URL;
})
if (project) {
return project["columns_url"];
} else {
// Invalid Project URL
throw new Error("Project URL doesn't exist");
}
}
// Helper Function to iterate the pullRequests and update the Project Column
export async function addPRCardToColumn(cardsEndpoint: string, pullRequestId: number, authToken: string): Promise<{ 'error': boolean, 'message'?: string, 'already_added'?: boolean }> {
var options = {
method: 'POST',
headers: {
'Accept': 'application/vnd.github.inertia-preview+json',
'Authorization': `Basic ${authToken}`
},
body: JSON.stringify({
"content_type": "PullRequest",
"content_id": pullRequestId
})
};
let res = await fetch(cardsEndpoint, options)
let json = await res.json();
if (json['errors']) {
if (json['errors'][0]['message'] == 'Project already has the associated issue') {
// PR is already linked to the project
return { 'error': false, 'already_added': true };
} else {
return { 'error': true, 'message': json['errors'][0]['message'], 'already_added': false };
}
} else {
return { 'error': false };
}
}
// Function to construct Pull Request List Endpoint [GitHub APIs]
export function constructPullReqListEndpoint(REPO_URL: string) {
let isValid = repoUrlRegex.test(REPO_URL);
if (!isValid) {
throw Error('Not A Valid Repo URL');
}
let projectMeta = REPO_URL.replace('https://github.com/', '').split('/');
let type = projectMeta[0];
let uName = projectMeta[1];
return { pullReqListEndpoint: `https://api.github.com/repos/${type}/${uName}/pulls`, uName }
}
// Function to construct Project Endpoint [GitHub APIs]
export function constructProjectEndpoint(PROJECT_URL) {
let isValid = projectUrlRegex.test(PROJECT_URL)
if (!isValid) {
throw Error("Invalid Project URL");
}
let projectMeta = PROJECT_URL.replace('https://github.com/', '').split('/');
let type = projectMeta[0];
let projectName = projectMeta[1];
return { projectEndpoint: `https://api.github.com/${type}/${projectName}/projects` }
}
// Helper Function to Construct Auth Token
export function constructAuthToken(uName: string, ACCESS_TOKEN: string) {
return Buffer.from(`${uName}:${ACCESS_TOKEN}`).toString('base64');
}
// Function returns the desired Card Endpoint after verifing its name with COLUMN_NAME
export async function getCardEndpoint(columnEndpoint: string, authToken: string, COLUMN_NAME: string) {
var options = {
method: 'GET',
headers: {
'Accept': 'application/vnd.github.inertia-preview+json',
'Authorization': `Basic ${authToken}`
},
};
let res = await fetch(columnEndpoint, options)
let json: Array<any> = await res.json();
if (json.length == 0) {
// No Column In The Project
throw new Error('No Column In The Project')
}
let column = json.find((e) => {
return e.name == COLUMN_NAME;
})
if (column) {
return column["cards_url"];
} else {
// No column exist with the given COLUMN_NAME
throw new Error('No column exist with the given COLUMN_NAME')
}
}