Notify of new Github repo stars

Script· trigger github Verified

by rossmccrann · 7/12/2022

The script

Submitted by adam186 Deno
Verified 367 days ago
1
import {
2
  setState,
3
  getState,
4
} from "https://deno.land/x/[email protected]/mod.ts";
5
import { Octokit } from "npm:@octokit/[email protected]";
6

7
const MAX_ITEMS = 500;
8

9
/**
10
 * @returns A list of starrers in descending order of when they starred.
11
 * The maximum number of returned items is 500.
12
 */
13
type Github = {
14
  token: string;
15
};
16
export async function main(auth: Github, owner: string, repo: string) {
17
  const octokit = new Octokit({
18
    auth: auth.token,
19
  });
20
  const lastUpdate = (await getState()) || 0;
21

22
  const starCount = (await octokit.repos.get({ owner, repo })).data
23
    .stargazers_count;
24
  let lastPage =
25
    +(starCount / 100).toFixed(0) + (starCount % 100 === 0 ? 0 : 1);
26
  const newStarrers: Starrer[] = [];
27

28
  await setState(Date.now());
29
  let runLoop = true;
30
  do {
31
    const response = await octokit.activity.listStargazersForRepo({
32
      owner,
33
      repo,
34
      per_page: 100,
35
      page: lastPage--,
36
      headers: {
37
        accept: "application/vnd.github.star+json",
38
      },
39
    });
40
    for (let i = response.data.length - 1; i >= 0; i--) {
41
      const entry = response.data[i];
42
      if (!entry || !entry.user) continue;
43
      const starredAt = new Date(entry.starred_at).getTime();
44
      if (starredAt < lastUpdate) {
45
        runLoop = false;
46
        break;
47
      }
48
      const { id, login, type, url, email, name } = entry.user;
49
      newStarrers.push({ id, starredAt, login, type, url, email, name });
50
      if (newStarrers.length >= MAX_ITEMS) {
51
        await setState(newStarrers.at(-1)?.starredAt);
52
        runLoop = false;
53
        break;
54
      }
55
    }
56
  } while (runLoop);
57
  console.log(newStarrers.length);
58
  return newStarrers;
59
}
60

61
interface Starrer {
62
  id: number;
63
  login: string;
64
  type: string;
65
  url: string;
66
  starredAt: number;
67
  email?: string | null;
68
  name?: string | null;
69
}
70

Other submissions
  • Submitted by rossmccrann Deno
    Created 1373 days ago
    1
    import * as wmill from "https://deno.land/x/[email protected]/index.ts";
    2
    import { Octokit } from "https://cdn.skypack.dev/octokit?dts";
    3
    import { WebClient } from "https://deno.land/x/[email protected]/mod.ts";
    4
    
    
    5
    export async function main(
    6
        gh_auth: wmill.Resource<"github">,
    7
        owner: string,
    8
        repo: string,
    9
        channel: string,
    10
        username: string
    11
    ) {
    12
        const octokit = new Octokit(gh_auth);
    13
        const response = await octokit.rest.repos.get({
    14
            owner,
    15
            repo,
    16
        });
    17
    
    
    18
        let repo_list = {};
    19
        repo_list[response.data.name] = response.data.stargazers_count;
    20
        const before = await wmill.getInternalState();
    21
        const key = Object.keys(repo_list)[0];
    22
    
    
    23
        await wmill.setInternalState({ [key]: repo_list[key] });
    24
        const after = await wmill.getInternalState();
    25
        const before_val: number = Object.values(before)[0];
    26
        const after_val: number = Object.values(after)[0];
    27
    
    
    28
        let notification_list = [];
    29
        let notification: string =
    30
            `No new Github stars on the following repo: \n ${key} : ${before_val} Stars`;
    31
        if (after_val > before_val) {
    32
            const difference: number = after_val - before_val;
    33
            notification =
    34
                `You have received ${difference} new Github stars on the following repo:\n${key} `;
    35
            notification_list.push(notification);
    36
        }
    37
    
    
    38
        return notification_list;
    39
    }