Edits history of script submission #22400 for ' List newly added issues (github)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { setState, getState } from "windmill-client@1";
    import { Octokit } from "@octokit/[email protected]";
    
    const MAX_ITEMS = 500;
    const PAGE_SIZE = 50;
    
    /**
     * @returns A list of issues in ascending order of creation date.
     *
     * The maximum number of returned items is 500 and therefore you'll miss
     * the issues that are older than the 500th one. Please choose your
     * scheduling accordingly.
     */
    type Github = {
      token: string;
    };
    export async function main(auth: Github, owner: string, repo: string) {
      const octokit = new Octokit({
        auth: auth.token,
      });
      const newIssues: Issue[] = [];
      const lastUpdate = await getState();
    
      let runLoop = true;
      let page = 1;
      do {
        const result = await octokit.issues.listForRepo({
          owner,
          repo,
          filter: "all",
          state: "open",
          sort: "created",
          direction: "desc",
          per_page: PAGE_SIZE,
          page: page++,
        });
        const issues = result.data.filter((i) => i && !i.pull_request);
    
        for (let i = 0; i < issues.length; i++) {
          const issue = issues[i];
          if (new Date(issue.created_at).getTime() <= lastUpdate) {
            runLoop = false;
            break;
          }
          newIssues.push({
            title: issue.title,
            url: issue.html_url,
            created_at: issue.created_at,
            user: {
              login: issue.user?.login,
              url: issue.user?.html_url,
            },
            labels: <string[]>(
              issue.labels
                .map((l) => (typeof l === "string" ? l : l.name))
                .filter(Boolean)
            ),
          });
          if (newIssues.length >= MAX_ITEMS) {
            runLoop = false;
            break;
          }
        }
    
        if (result.data.length < PAGE_SIZE) {
          break;
        }
      } while (runLoop);
    
      if (newIssues.length) {
        await setState(new Date(newIssues[0].created_at).getTime());
      }
      return newIssues.reverse();
    }
    
    interface Issue {
      title: string;
      url: string;
      created_at: string;
      user: {
        login?: string;
        url?: string;
      };
      labels: string[];
    }
    

    Submitted by hugo989 7 days ago