TIPS之 github action 自动统计组织/个人 活跃信息

github action 自动统计组织/个人 活跃信息

Posted by 董江 on Friday, July 7, 2023

Github Action 自动统计组织/个人 活跃信息

最近用 Github Actions 玩出了花样。这里记录一下通过 Github Api 和 Github Actions 统计组织内活跃信息统计。

希望做到统计:

  • 统计 org 下所有仓库的数据,而不是单个仓库(目前所在组织里有 30+ 个仓库)
  • 可以到个人维度,也可以到组织纬度

通过 github 早已经发布了 GraphQLAPI explorer 去测试和使用其 API 的体验是相当好,决定自己做这个统计。

依赖底层工具

GraphQL API 主要用到了以下几个接口:

1. 获取组织内的 repositories

query Repositories($username: String!, $cursor: String) {
  organization(login: $username) {
    repositories(
      first: 100
      isLocked: false
      orderBy: { field: PUSHED_AT, direction: DESC }
      after: $cursor
    ) {
      pageInfo {
        hasNextPage
        endCursor
      }
      edges {
        node {
          pushedAt
          name
        }
      }
    }
  }
}

2. 获取 repository 下的 commits

query Commits($owner: String!, $name: String!, $cursor: String) {
  repository(owner: $owner, name: $name) {
    defaultBranchRef {
      target {
        ... on Commit {
          history(first: 30, after: $cursor) {
            pageInfo {
              hasNextPage
              endCursor
            }
            edges {
              node {
                author {
                  user {
                    name
                    login
                  }
                }
                commitUrl
                abbreviatedOid
                pushedDate
                additions
                deletions
                message
              }
            }
          }
        }
      }
    }
  }
}

使用的 Client 就是 Apollo GraphQL Client。

用 github actions 去跑任务做周期性统计

name: Log @kubeservice organization statistics

on:
  schedule:
    - cron: '0 1 * * *'
  workflow_dispatch:

jobs:
  Stats:
    runs-on: ubuntu-latest
    steps:
      - uses: gr2m/org-stats-action@v1.0.3
        id: stats
        with:
          org: kubeservice-stack
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - run: |
          cat << EOF
          result: ${{ steps.stats.outputs.data }}
          EOF          
      - name: Modify README.md
        uses: jaywcjlove/github-action-modify-file-content@v1.4.4
        with:
          path: profile/README.md
          token: ${{ secrets.GITHUB_TOKEN }}
          body: |
            本周社区更新情况: 
            | 条目 | 活跃情况 |
            | :-- | :--: |
            |🐱‍open issues| ${{ fromJson(steps.stats.outputs.data).openIssues }} |
            |💻closed issues| ${{ fromJson(steps.stats.outputs.data).closedIssues }} |
            |💬open pull requests| ${{ fromJson(steps.stats.outputs.data).openPullRequests }} |
            |🕑︎closed pull requests| ${{ fromJson(steps.stats.outputs.data).closedPullRequests }}|
            |🔥merged pull requests| ${{ fromJson(steps.stats.outputs.data).mergedPullRequests }}|
                         

首先,这里使用了两个 github workflow 的 trigger:

  • schedule 也就是 cron 用于周期性跑任务
  • workflow_dispatch 可以提供个简单的表单,用来主动去创建一个 workflow

然后,这里在完成统计后将数据写入profile/README.md中。 并替换其中模板。

...
<!--GAMFC--><!--GAMFC-END-->
...

「如果这篇文章对你有用,请随意打赏」

Kubeservice博客

如果这篇文章对你有用,请随意打赏

使用微信扫描二维码完成支付