3

Notify of new Github repo stars

by
Published Jul 12, 2022
Scriptยท trigger github Verified

The script

Submitted by hugo989 Bun
Verified 15 days ago
1
import { setState, getState } from "windmill-client@1";
2
import { Octokit } from "@octokit/[email protected]";
3

4
const MAX_ITEMS = 500;
5

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

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

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

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

Other submissions
  • Submitted by rossmccrann Deno
    Created 1413 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
    }
  • Submitted by adam186 Deno
    Created 407 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