Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ci): linear issues sync #2562

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,28 @@ jobs:
CHART_MUSEUM_PASSWORD: ${{ secrets.CHART_MUSEUM_PASSWORD }}
# The workflow will only trigger on non-draft releases
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#release
sync_linear:
needs:
- publish
- publish-chart
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When comparing this with loft-enterprise, why is this go.mod and that one is go.work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not using go workspaces in this repository. This is just a way for the action to retrieve the go version to be installed from the file.

- name: Update linear issues
run: go run -mod vendor . -release-tag="${{ needs.publish.outputs.release_version }}"
working-directory: hack/linear-sync
env:
GITHUB_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
LINEAR_TOKEN: ${{ secrets.LINEAR_TOKEN }}

notify_release:
needs:
- publish
- publish-chart
- sync_linear
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
Expand Down
15 changes: 15 additions & 0 deletions hack/changelog/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/loft-sh/changelog

go 1.22.5

require (
github.com/blang/semver v3.5.1+incompatible
github.com/google/go-github/v59 v59.0.0
github.com/shurcooL/githubv4 v0.0.0-20240120211514-18a1ae0e79dc
golang.org/x/oauth2 v0.25.0
)

require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect
)
16 changes: 16 additions & 0 deletions hack/changelog/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v59 v59.0.0 h1:7h6bgpF5as0YQLLkEiVqpgtJqjimMYhBkD4jT5aN3VA=
github.com/google/go-github/v59 v59.0.0/go.mod h1:rJU4R0rQHFVFDOkqGWxfLNo6vEk4dv40oDjhV/gH6wM=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/shurcooL/githubv4 v0.0.0-20240120211514-18a1ae0e79dc h1:vH0NQbIDk+mJLvBliNGfcQgUmhlniWBDXC79oRxfZA0=
github.com/shurcooL/githubv4 v0.0.0-20240120211514-18a1ae0e79dc/go.mod h1:zqMwyHmnN/eDOZOdiTohqIUKUrTFX62PNlu7IJdu0q8=
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 h1:17JxqqJY66GmZVHkmAsGEkcIu0oCe3AM420QDgGwZx0=
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466/go.mod h1:9dIRpgIY7hVhoqfe0/FcYp0bpInZaT7dc3BYOprrIUE=
golang.org/x/oauth2 v0.25.0 h1:CY4y7XT9v0cRI9oupztF8AgiIu99L/ksR/Xp/6jrZ70=
golang.org/x/oauth2 v0.25.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3 changes: 3 additions & 0 deletions hack/changelog/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package log

var LoggerKey struct{}
162 changes: 162 additions & 0 deletions hack/changelog/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package main

import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"sort"

"github.com/google/go-github/v59/github"
"github.com/loft-sh/changelog/log"
pullrequests "github.com/loft-sh/changelog/pull-requests"
"github.com/loft-sh/changelog/releases"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)

var ErrMissingToken = errors.New("github token must be set")

func main() {
if err := run(context.Background(), os.Stdout, os.Stderr, os.Args); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}

func run(
ctx context.Context,
stdout, stderr io.Writer,
args []string,
) error {
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
var (
owner = flagset.String("owner", "loft-sh", "The GitHub owner of the repository")
repo = flagset.String("repo", "vcluster", "The GitHub repository to generate the changelog for")
githubToken = flagset.String("token", "", "The GitHub token to use for authentication")
previousTag = flagset.String("previous-tag", "", "The previous tag to generate the changelog for (if not set, the last stable release will be used)")
releaseTag = flagset.String("release-tag", "", "The tag of the release to generate the changelog for")
updateNotes = flagset.Bool("update-notes", true, "Update the release notes of the release with the generated ones")
overwrite = flagset.Bool("overwrite", false, "Overwrite the release notes with the generated ones")
debug = flagset.Bool("debug", false, "Enable debug logging")
)
if err := flagset.Parse(args[1:]); err != nil {
return fmt.Errorf("parse flags: %w", err)
}

if *githubToken == "" {
*githubToken = os.Getenv("GITHUB_TOKEN")
}

if *githubToken == "" {
return ErrMissingToken
}

leveler := slog.LevelVar{}
leveler.Set(slog.LevelInfo)
if *debug {
leveler.Set(slog.LevelDebug)
}

logger := slog.New(slog.NewTextHandler(stderr, &slog.HandlerOptions{
Level: &leveler,
}))

ctx, stop := signal.NotifyContext(ctx, os.Interrupt, os.Kill)
defer stop()

ctx = context.WithValue(ctx, log.LoggerKey, logger)

httpClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: *githubToken,
},
))

client := github.NewClient(httpClient)
gqlClient := githubv4.NewClient(httpClient)

var stableTag string

if *previousTag != "" {
release, err := releases.FetchReleaseByTag(ctx, gqlClient, *owner, *repo, *previousTag)
if err != nil {
return fmt.Errorf("fetch release by tag: %w", err)
}

stableTag = release.TagName
} else {
if prevRelease, err := releases.LastStableReleaseBeforeTag(ctx, gqlClient, *owner, *repo, *releaseTag); err != nil {
return fmt.Errorf("get last stable release before tag: %w", err)
} else if prevRelease != "" {
stableTag = prevRelease
} else {
stableTag, _, err = releases.LastStableRelease(ctx, gqlClient, *owner, *repo)
if err != nil {
return fmt.Errorf("get last stable release: %w", err)
}
}
}

if stableTag == "" {
return errors.New("no stable release found")
}

logger.Info("Last stable release", "stableTag", stableTag)

var currentRelease releases.Release
if *releaseTag != "" {
var err error
currentRelease, err = releases.FetchReleaseByTag(ctx, gqlClient, *owner, *repo, *releaseTag)
if err != nil {
return fmt.Errorf("fetch release by tag: %w", err)
}

if currentRelease.TagName != *releaseTag {
return fmt.Errorf("release not found: %s", *releaseTag)
}
}

pullRequests, err := pullrequests.FetchAllPRsBetween(ctx, gqlClient, *owner, *repo, stableTag, *releaseTag)
if err != nil {
return fmt.Errorf("fetch all PRs until: %w", err)
}

logger.Info("Found merged pull requests between releases", "count", len(pullRequests), "previous", stableTag, "current", *releaseTag)

notes := []Note{}
for _, pr := range pullRequests {
notes = append(notes, NewNotesFromPullRequest(pr)...)
}
sort.Slice(notes, SortNotes(notes))

buffer := bytes.Buffer{}

for _, note := range notes {
if _, err := buffer.Write([]byte(note.String())); err != nil {
return fmt.Errorf("write note: %w", err)
}
}

if *releaseTag != "" && *updateNotes {
if currentRelease.Description == "" || *overwrite {
if err := releases.UpdateReleaseNotes(ctx, client, *owner, *repo, currentRelease.DatabaseId, buffer.String()); err != nil {
return fmt.Errorf("update release notes: %w", err)
}
logger.Info("Updated release notes", "releaseTag", *releaseTag)
} else {
logger.Warn("Release notes already exist for tag, skipping update", "releaseTag", *releaseTag)
}
}

if _, err := stdout.Write(buffer.Bytes()); err != nil {
return fmt.Errorf("write changelog: %w", err)
}

return nil
}
97 changes: 97 additions & 0 deletions hack/changelog/note.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"fmt"
"regexp"
"sort"
"strings"

pullrequests "github.com/loft-sh/changelog/pull-requests"
)

var notesInBodyREs = []*regexp.Regexp{
regexp.MustCompile("(?ms)^```release-note[s]?:(?P<type>[^\r\n]*)\r?\n?(?P<note>.*?)\r?\n?```"),
regexp.MustCompile("(?ms)^```release-note[s]?\r?\ntype:\\s?(?P<type>[^\r\n]*)\r?\nnote:\\s?(?P<note>.*?)\r?\n?```"),
}

type Note struct {
Type string
Author string
Body string
PR int
}

func (n Note) String() string {
return fmt.Sprintf("- %s: %s (by @%v in #%d)\n", n.Type, n.Body, n.Author, n.PR)
}

func NewNotesFromPullRequest(p pullrequests.PullRequest) []Note {
return NewNotes(p.Body, p.Author.Login, p.Number)
}

func SortNotes(res []Note) func(i, j int) bool {
return func(i, j int) bool {
if res[i].Type < res[j].Type {
return true
} else if res[j].Type < res[i].Type {
return false
} else if res[i].Body < res[j].Body {
return true
} else if res[j].Body < res[i].Body {
return false
} else if res[i].PR < res[j].PR {
return true
} else if res[j].PR < res[i].PR {
return false
}
return false
}
}

func NewNotes(body, author string, number int) []Note {
var res []Note
for _, re := range notesInBodyREs {
matches := re.FindAllStringSubmatch(body, -1)
if len(matches) == 0 {
continue
}

for _, match := range matches {
note := ""
typ := ""

for i, name := range re.SubexpNames() {
switch name {
case "note":
note = match[i]
case "type":
typ = strings.ToLower(match[i])
}
if note != "" && typ != "" {
break
}
}

note = strings.TrimSpace(note)
typ = strings.TrimSpace(typ)

if typ == "type" || typ == "none" || note == "none" {
note = ""
typ = ""
}
if note == "" && typ == "" {
continue
}

res = append(res, Note{
Type: typ,
Body: note,
PR: number,
Author: author,
})
}
}
sort.Slice(res, SortNotes(res))

return res
}
Loading
Loading