Skip to content

Commit

Permalink
fix: getTeamMembers (#2)
Browse files Browse the repository at this point in the history
* fix: teamLabel

* log

* log

* test: octokit

* try catch

* octokit.request

* fix: teamName

* 查看member是org级别的role

* fix: getPRCommitAuthors

* log: getPRCommitAuthors

* fix: use break instead of return

* add dist

* docs: update

* docs: update
  • Loading branch information
thinkasany authored Sep 11, 2023
1 parent 04696da commit 5dfb4ca
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 24 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ on:
types:
- opened
- synchronize
workflow_dispatch: # 添加手动触发事件
inputs:
pr_number:
description: 'PR Number'
required: true
jobs:
checkin:
runs-on: ubuntu-latest
Expand All @@ -28,11 +32,13 @@ jobs:
id: pr_number
run: echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
- name: Run pr-label Action
- name: Run Custom Action
uses: thinkasany/pr-label-action@master
with:
github_token: ${{ secrets.GH_TOKEN }}
github_token: ${{ secrets.ACTION_TOKEN }}
pr_number: ${{ env.PR_NUMBER }}
organize_name: 'actionv' # 组织的名字
team_name: 'action-team' # team的名字
```
### yml demo

Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ inputs:
pr_number:
description: "your pr number"
required: true

organize_name:
description: "your organize name"
required: true

team_name:
description: "your team name"
required: true

team_label:
description: "your team label"
Expand Down
64 changes: 51 additions & 13 deletions app/action.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const axios = require("axios");
const fs = require("fs");
const { Octokit } = require("@octokit/core");

// 使用 GitHub API 获取 PR 文件列表
const getFilesInPR = async payload => {
Expand Down Expand Up @@ -64,47 +62,87 @@ const addLabelToPR = async payload => {
});
};

// 获取仓库的所有有权限的成员
const getAllCollaborators = async payload => {
const { repo, owner, token } = payload;
// 获取指定团队下的成员列表
const getTeamMembers = async payload => {
const { orgName, teamName, token } = payload;

const apiUrl = `https://api.github.com/repos/${owner}/${repo}/collaborators`;
const apiUrl = `https://api.github.com/orgs/${orgName}/teams/${teamName}/members`;

console.log("getTeamMembers apiUrl", apiUrl);

const headers = {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github.v3+json",
"User-Agent": "GitHub-Collaborators-List"
"User-Agent": "GitHub-Team-Members"
};

try {
const response = await axios.get(apiUrl, { headers });

if (response.status === 200) {
const collaborators = response.data.map(
collaborator => collaborator.login
);
return collaborators;
const teamMembers = response.data.map(member => member.login);
return teamMembers;
} else {
console.error(`无法获取成员列表: ${response.data.message}`);
return null;
}
} catch (error) {
console.log(error);
console.error(`发生错误: ${error.message}`);
return null;
}
};

const getPRCommitAuthors = async payload => {
const { repo, owner, token, prNumber } = payload;

const apiUrl = `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/commits`;
const headers = {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github.v3+json",
"User-Agent": "GitHub-PR-Commits"
};
console.log('getPRCommitAuthors', apiUrl);

try {
const response = await axios.get(apiUrl, { headers });

if (response.status === 200) {
const commitAuthors = response.data.map(
commit => commit.commit.author.name
);
return commitAuthors;
} else {
throw new Error(`无法获取提交列表: ${response.data.message}`);
}
} catch (error) {
console.log('getPRCommitAuthors err', error);
throw new Error(`发生错误: ${error.message}`);
}
};

const Action = async payload => {
const { isEnableSuffix, teamLabel, isEnableTeamLabel } = payload;
console.log("payload", payload);
const files = await getFilesInPR(payload);
console.log("files", files);
const coreTeam = await getAllCollaborators(payload);
const coreTeam = await getTeamMembers(payload);
console.log("coreTeam", coreTeam);

if (isEnableTeamLabel) {
// 是否开启功能:添加 teamLabel 的label
await addLabelToPR({ ...payload, files: [teamLabel] });
try {
const commitAuthors = await getPRCommitAuthors(payload);
console.log("PR 中的提交者名字列表:", commitAuthors);
for (const author of commitAuthors) {
if (coreTeam.includes(author)) {
await addLabelToPR({ ...payload, files: [teamLabel] });
break;
}
}
} catch (error) {
console.error(error.message);
}
}

if (isEnableSuffix) {
Expand Down
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ const github = require("@actions/github");
const repoName = context.repo.repo;
const token = core.getInput("github_token", { required: true });
const prNumber = core.getInput("pr_number", { required: true });
const isEnableSuffix = core.getInput("enable_suffix") || true
const isEnableTeamLabel = core.getInput("enable_suffix") || "Core Team"
const teamLabel = core.getInput("team_label") || "Core Team"

log.info(`repoName: ${repoName}; owner:${owner}; prNumber: ${prNumber}; isEnableSuffix: ${isEnableSuffix}`);
const orgName = core.getInput("organize_name", { required: true });
const teamName = core.getInput("team_name", { required: true });
const isEnableSuffix = core.getInput("enable_suffix") || true;
const isEnableTeamLabel = core.getInput("enable_suffix") || true;
const teamLabel = core.getInput("team_label") || "Core Team";

log.info(
`repoName: ${repoName}; owner:${owner}; prNumber: ${prNumber}; isEnableSuffix: ${isEnableSuffix}`
);
const payload = {
repo: repoName,
owner,
token,
prNumber,
isEnableSuffix,
isEnableTeamLabel,
teamLabel
teamLabel,
orgName,
teamName
};
await Action(payload);
log.info("pr-label-action Action 成功结束运行!", repoName);
Expand Down

0 comments on commit 5dfb4ca

Please sign in to comment.