aboutsummaryrefslogtreecommitdiff
path: root/cmd/priorworkgen/main.go
blob: d6cea6a8131b528f40dfd785559e5ac8066a6fc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Command priorworkgen generates the list of prior work (read: GitHub repositories) for contractual stuff.
package main

import (
	"context"
	"flag"
	"log"
	"fmt"
	"os"

	"github.com/google/go-github/github"
	_ "github.com/joho/godotenv/autoload"
	"golang.org/x/oauth2"
	"within.website/x/internal"
)

var (
	ghToken = flag.String("gh-token", "", "github personal access token")
)

func main() {
	internal.HandleStartup()
	ctx := context.Background()
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: *ghToken},
	)
	tc := oauth2.NewClient(ctx, ts)

	client := github.NewClient(tc)

	var repos []*github.Repository
	var np int

	for {
		// list all repositories for the authenticated user

		options := &github.RepositoryListOptions{
			ListOptions: github.ListOptions{Page: np},
			Type:        "owner",
		}
		mrepos, resp, err := client.Repositories.List(ctx, "", options)
		if err != nil {
			log.Printf("can't get next page: %v", err)
			break
		}
		np = resp.NextPage

		repos = append(repos, mrepos...)

		log.Printf("got info on %d repos", len(repos))

		if len(repos) > 150 {
			break
		}
	}

	for _, repo := range repos {
		if repo.GetFork() {
			continue
		}
		if repo.GetPrivate() {
			continue
		}

		if repo.Description == nil {
			desc := ""
			repo.Description = &desc
		}

		name := repo.GetName()
		desc := repo.GetDescription()
		refn := repo.GetGitURL()
		creat := repo.GetCreatedAt()
		lastm := repo.GetUpdatedAt()

		if name == "ircbot" {
			continue
		}

		const blurb = `name: ${name}
description: ${desc}
reference number: ${refn}
date of creation: ${creat}
date of last modification: ${lastm}
other owners: none
`

		mapping := func(inp string) string {
			switch inp {
			case "name":
				return name
			case "desc":
				if desc == "" {
					return "no description available"
				}

				return desc
			case "refn":
				return refn
			case "creat":
				return creat.String()
			case "lastm":
				return lastm.String()
			}

			return "<unknown input " + inp + ">"
		}

		fmt.Println(os.Expand(blurb, mapping))
	}
}