-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
150 lines (142 loc) · 5.24 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const { getInput, setOutput, setFailed } = require('@actions/core');
const GitHub = require('@actions/github');
const request = require('request');
const rp = require('request-promise');
const getInputBool = (name, defaultValue = false) => {
const param = getInput(name);
if (param === 'true' || param === '1') {
return true
} else if (param === 'false' || param === '0') {
return false
} else return defaultValue
};
async function main() {
// retrieve input data
const token = getInput('github-token', { required: true });
const sha = getInput('sha');
const repo = getInput('repo');
const username = getInput('username');
const password = getInput('password');
const octokit = GitHub.getOctokit(token);
// get current pull request number
const getPRnumber = await octokit.repos.listPullRequestsAssociatedWithCommit({
owner: repo.split("/")[0],
repo: repo.split("/")[1],
commit_sha: sha,
});
const pr_number = getPRnumber.data[0].number;
// get pull request data
const { data: pullRequest } = await octokit.pulls.get({
owner: repo.split("/")[0],
repo: repo.split("/")[1],
pull_number: pr_number
});
const githubUsername = pullRequest.user.login;
console.log("GitHub Username: ", githubUsername);
const labelsArray = new Array();
for (i in pullRequest.labels) {
labelsArray.push(pullRequest.labels[i].name);
}
// get user data
const { data: userData } = await octokit.users.getByUsername({
username: githubUsername,
});
const fullName = userData.name;
const userEmail = userData.email;
const pr_url = pullRequest.html_url;
console.log("Full Name: ", fullName);
console.log("Email ", userEmail);
console.log("Evidence URL: ", pr_url);
var options = {
method: 'POST',
uri: 'https://api.eu.badgr.io/o/token',
form: {
username: username,
password: password
},
headers: {
'Content-Type': 'application/json'
}
};
accessToken = await rp(options)
.then(function (body) {
var response = JSON.parse(body);
accessToken = response.access_token;
return accessToken;
})
.catch(function (err) {
console.log(err)
});
var options = {
method: 'GET',
uri: 'https://api.eu.badgr.io/v2/badgeclasses',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken
}
};
const badgeDetailsArray = new Array();
await rp(options)
.then(function (body) {
var responseAllBadges = JSON.parse(body);
for (var i = 0; i < labelsArray.length; i++) {
for (var j = 0; j < responseAllBadges.result.length; j++) {
var badge = responseAllBadges.result[j].name.toLowerCase();
var label = labelsArray[i].replace('"', '').replace('"', '').toLowerCase();
if (badge.includes(label)) {
console.log(responseAllBadges.result[j]);
badgeDetailsArray.push(responseAllBadges.result[j]);
}
}
}
});
if (Array.isArray(badgeDetailsArray) && badgeDetailsArray.length > 0) {
for (i in badgeDetailsArray) {
var options = {
method: 'POST',
uri: 'https://api.eu.badgr.io/v2/badgeclasses/' + badgeDetailsArray[i].entityId.toString() + '/assertions',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken
},
json: true,
body: {
recipient: {
identity: userEmail,
hashed: true,
type: "email",
plaintextIdentity: fullName
},
issuedOn: new Date().toISOString(),
evidence: [
{
url: pr_url,
narrative: badgeDetailsArray[i].criteriaNarrative
}
],
notify: true,
extensions: {
"extensions:recipientProfile": {
"@context": "https://openbadgespec.org/extensions/recipientProfile/context.json",
"type": [
"Extension",
"extensions:RecipientProfile"
],
name: fullName
}
}
}
};
await rp(options)
.then(function (body) {
console.log("The following badge was generated for the specified recipient:\n ", body);
})
.catch(function (err) {
console.log(err)
});
}
} else {
console.log("The badge(s) generation failed for the specified recipient");
}
}
main().catch(error => setFailed(error.message));