GitHub Action:如何检查公关创建者是否是特定团队的一部分?

发布于 2025-02-01 13:14:22 字数 1592 浏览 1 评论 0原文

我想运行一个github动作,该动作检查公关创建者是否是Github团队“我的酷团队”的成员,如果是这样,请做某事。

我很难理解如何利用octokit团队端点“获得用户的团队成员资格” https://octokit.github.io/rest.js/v18#teams

文档解释了 https://docs.github.com/en/rest/rest/teams/members#get-team -Membership-for-a-user

如果

{
  "url": "https://api.github.com/teams/1/memberships/octocat",
  "role": "maintainer",
  "state": "active"
}

用户在团队中,否则它将返回404,我也不知道该如何处理?

这就是我写的:

on: pull_request_target

jobs:
  my_job:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v6
        with:
          script: |
            // If user is member of my_team team do nothing else do something
            // See: https://octokit.github.io/rest.js/v18#teams
            const creator = context.payload.sender.login
            const opts = github.rest.teams.getMembershipForUserInOrg.endpoint.merge({
              org: 'my_org',
              team_slug: 'My cool Team',
              username: ???? #How do I get the username of who opened the PR?
            })
            const response = await github.paginate(opts)

           
            if (response.state <> "active") {
                return
              }

            // User is not in the team continue to do something... 

有什么想法我该怎么做?

I want to run a Github action that checks if the PR creator is a member of GitHub team "My cool Team" and if so do something.

I'm having trouble understanding how I can leverage the octokit team endpoint "Get team membership for a user" https://octokit.github.io/rest.js/v18#teams

The docs explain that the response for https://docs.github.com/en/rest/teams/members#get-team-membership-for-a-user

is

{
  "url": "https://api.github.com/teams/1/memberships/octocat",
  "role": "maintainer",
  "state": "active"
}

if the user is in the team otherwise it returns 404 which i also don't know how to handle?

This is what I wrote:

on: pull_request_target

jobs:
  my_job:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v6
        with:
          script: |
            // If user is member of my_team team do nothing else do something
            // See: https://octokit.github.io/rest.js/v18#teams
            const creator = context.payload.sender.login
            const opts = github.rest.teams.getMembershipForUserInOrg.endpoint.merge({
              org: 'my_org',
              team_slug: 'My cool Team',
              username: ???? #How do I get the username of who opened the PR?
            })
            const response = await github.paginate(opts)

           
            if (response.state <> "active") {
                return
              }

            // User is not in the team continue to do something... 

Any thoughts how I can do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2025-02-08 13:14:22

从您的代码段中,

const creator = context.payload.sender.login

创建者变量是打开PR的人的用户名。另外,等待GetMembershipforuserinorg()可能足以满足您的需求。像:

            const creator = context.payload.sender.login
            const result = await github.rest.teams.getMembershipForUserInOrg({
              org: context.repo.owner,
              team_slug: 'my-cool-team',
              username: creator
            })
            console.log(creator, result) // do whatever you want

From your code snippet

const creator = context.payload.sender.login

The creator variable is the username of who opened the PR. Also, await for the result of getMembershipForUserInOrg() could be enough for your need. Something like:

            const creator = context.payload.sender.login
            const result = await github.rest.teams.getMembershipForUserInOrg({
              org: context.repo.owner,
              team_slug: 'my-cool-team',
              username: creator
            })
            console.log(creator, result) // do whatever you want
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文