-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
118 lines (104 loc) · 3.21 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
const core = require('@actions/core');
const github = require('@actions/github');
var octokit;
const orgName = core.getInput('org-name');
const authToken = process.env.GITHUB_TOKEN;
const path = core.getInput('file-path');
const name = core.getInput('commit-user-name');
const email = core.getInput('commit-user-email');
const message = core.getInput('commit-msg');
const [repoOwner, repoName] = process.env.GITHUB_REPOSITORY.split('/');
// For Testing Locally
/*
const fs = require('fs');
function get_test_values(){
return JSON.parse(fs.readFileSync('test_data.json', { encoding: 'utf8' }));
}
var test_data = get_test_values();
const orgName = test_data['org_name'];
const authToken = test_data['token'];
const path = test_data['path'];
const name = test_data['name'];
const email = test_data['email'];
const message = test_data['message']
const repoOwner = orgName;
const repoName = test_data['repo'];
*/
async function getMemberData(team){
let resp = await octokit.teams.listMembersInOrg({
'org': orgName,
'team_slug': team,
}).catch(
err => {
core.error(err);
process.exit(1);
}
);
return resp.data;
}
async function run(){
try{
if (!authToken) {
throw new Error('Token not found');
}
octokit = github.getOctokit(authToken);
let teamsResponse = await octokit.teams.list({
'org': orgName,
}).catch(
err => {
core.error(err);
process.exit(1);
}
);
let teams = teamsResponse.data;
let memberData = {};
for(let i=0; i < teams.length; i++){
memberData[teams[i].name] = await getMemberData(teams[i].name);
}
let jsonFileResponse = await octokit.repos.getContent({
'owner': repoOwner,
'repo': repoName,
'path': path,
}).catch(
err => {
core.error(err);
process.exit(1);
}
);
// github transfers files in base64
let prevContent = Buffer.from(jsonFileResponse.data.content, 'base64').toString('ascii');
let content = JSON.stringify(memberData);
let base64String = Buffer.from(content).toString('base64');
if(prevContent !== content){
await octokit.repos.createOrUpdateFileContents({
'owner': repoOwner,
'repo': repoName,
'message': message,
'content': base64String,
'path': path,
'sha': jsonFileResponse.data.sha,
'committer': {
'name': name,
'email': email
}
})
.then(
core.setOutput('message', 'File Updated'), console.log('file updated')
)
.catch(
err =>{
core.error(err);
process.exit(1);
}
);
}
else{
core.setOutput('message', 'No Changes Detected');
console.log('no changes');
}
}
catch (error) {
core.setFailed(error.message);
}
}
run();