-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
159 lines (154 loc) · 7.26 KB
/
action.yml
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
151
152
153
154
155
156
157
158
159
name: 'Team member table README updater'
description: 'This Action opens a PR to update the README.md file with a table of team members'
branding:
icon: 'users'
color: 'green'
inputs:
org:
description: 'The organization to get the team members from'
required: true
default: 'my-org'
team:
description: 'The team to get the members from'
required: true
default: 'my-team'
my-pat:
description: 'The personal access token to use to authenticate with GitHub'
required: true
runs:
using: "composite"
steps:
- uses: actions/github-script@v7
with:
github-token: ${{ inputs.my-pat }}
result-encoding: string
script: |
// Get the list of members on the team
const members = await github.rest.teams.listMembersInOrg({
org: "${{ inputs.org }}",
team_slug: "${{ inputs.team }}",
});
// Log the list of members to the console
console.log("Members output:");
console.log(members);
console.log(members.data);
// For each member returned, use the user api to get their login, avatar, location, and bio
const memberData = await Promise.all(members.data.map(async (member) => {
const user = await github.rest.users.getByUsername({
username: member.login
});
// Log the user data to the console
console.log("User output:");
console.log(user);
console.log(user.data);
return user.data;
}));
// Log the member data to the console
console.log("MemberData output:");
console.log(memberData);
// Create a markdown table with the member data, removing any newlines and carriage-return from the bio if the bio exists
const table = memberData.map((member) => {
return `| <img src="${member.avatar_url}" width="30" /> | @${member.login} | ${member.location} | ${member.bio ? member.bio.replace(/(\r\n|\n|\r)/gm, "") : ""} |`;
}).join('\n');
// Log the table to the console
console.log("Table output:");
console.log(table);
// Add headers to the table
const tableWithHeaders = `| 👤 | Username | Location | Bio |\n| --- | --- | --- | --- |\n${table}`;
// Log the table with headers to the console
console.log("Table with headers output:");
console.log(tableWithHeaders);
// Get the README file of the current repo context and its contents
// Add the table to the README following the "<!--auto-team-table-->" comment if the table content has changed
const readme = await github.rest.repos.getReadme({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.ref
});
const readmeContent = Buffer.from(readme.data.content, "base64").toString();
const tableComment = "<!--auto-team-table-->";
const tableStart = readmeContent.indexOf(tableComment) + tableComment.length;
const tableEnd = readmeContent.indexOf("<!--/auto-team-table-->");
const readmeContentBeforeTable = readmeContent.substring(0, tableStart);
const readmeContentAfterTable = readmeContent.substring(tableEnd);
const newReadmeContent = `${readmeContentBeforeTable}\n${tableWithHeaders}\n${readmeContentAfterTable}`;
// Log the newReadmeContent
console.log("New README content:");
console.log(newReadmeContent);
// If the newReadmeContent is different from the current README content, commit the new README content to the new branch
// Create a blob with the updated README
if (newReadmeContent !== readmeContent) {
console.log("Updating README");
// Create a new branch new branch with a unique name based on the first 7 characters of the commit SHA for the README update and commit the updated README
const branch = await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/heads/update-team-readme-${context.sha.slice(0, 7)}`,
sha: context.sha
});
// Log the branch to the console
console.log("Branch output:");
console.log(branch);
const blob = await github.rest.git.createBlob({
owner: context.repo.owner,
repo: context.repo.repo,
content: newReadmeContent,
encoding: "utf-8"
});
// Log the blob to the console
console.log("Blob output:");
console.log(blob);
// Create a tree with the updated README
const tree = await github.rest.git.createTree({
owner: context.repo.owner,
repo: context.repo.repo,
tree: [
{
path: "README.md",
mode: "100644",
type: "blob",
sha: blob.data.sha
}
],
base_tree: context.sha
});
// Log the tree to the console
console.log("Tree output:");
console.log(tree);
// Create a commit with the updated README
const commit = await github.rest.git.createCommit({
owner: context.repo.owner,
repo: context.repo.repo,
message: "Update `@${{ inputs.org }}/${{ inputs.team }}` team members in README",
tree: tree.data.sha,
parents: [context.sha]
});
// Log the commit to the console
console.log("Commit output:");
console.log(commit);
// Push the commit to the new branch with a unique name based on the first 7 characters of the commit SHA
const push = await github.rest.git.updateRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `heads/update-team-readme-${context.sha.slice(0, 7)}`,
sha: commit.data.sha
});
// Log the push to the console
console.log("Push output:");
console.log(push);
// Create a pull request with the new branch
const pullRequest = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "Update `@${{ inputs.org }}/${{ inputs.team }}` team README",
// Use the table as the description
body: "Auto-generated table of `@${{ inputs.org }}/${{ inputs.team }}` team members:\n\n" + tableWithHeaders,
head: `update-team-readme-${context.sha.slice(0, 7)}`,
base: "main"
});
// Log the pull request to the console
console.log("Pull request output:");
console.log(pullRequest);
// Return the pull request number
return pullRequest.data.number;
}