Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Oct 6, 2024
1 parent 385d9d3 commit 1fa7ffd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 5 additions & 1 deletion changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ func (cl *ChangeLog) filterReleases(rels []*github.RepositoryRelease) []*github.
}

// Generate generates changelog text from given releases and outputs it to its writer
func (cl *ChangeLog) Generate(rels []*github.RepositoryRelease) error {
func (cl *ChangeLog) Generate(rels []*github.RepositoryRelease, links []*github.Autolink) error {
rels = cl.filterReleases(rels)

out := bufio.NewWriter(cl.out)
heading := strings.Repeat("#", cl.level)

linker := NewReflinker(cl.repoURL)
for _, l := range links {
linker.AddExtRef(*l.KeyPrefix, *l.URLTemplate, *l.IsAlphanumeric)
}

numRels := len(rels)
relLinks := make([]link, 0, numRels)
Expand Down
19 changes: 18 additions & 1 deletion github.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,30 @@ func (gh *GitHub) Releases() ([]*github.RepositoryRelease, error) {
}
}

func (gh *GitHub) CustomAutolinks() ([]*github.Autolink, error) {
links := []*github.Autolink{}
page := 1
for {
opts := github.ListOptions{Page: page}
ls, res, err := gh.api.Repositories.ListAutolinks(gh.apiCtx, gh.owner, gh.repoName, &opts)
if err != nil {
return nil, fmt.Errorf("cannot get custom autolinks from repository %s/%s via GitHub API: %w", gh.owner, gh.repoName, err)
}
links = append(links, ls...)
if res.NextPage == 0 {
return links, nil
}
page = res.NextPage
}
}

// NewGitHub creates GitHub instance from given repository URL
func NewGitHub(u *url.URL) (*GitHub, error) {
// '/owner/name'
path := strings.TrimSuffix(u.Path, ".git")
slug := strings.Split(path, "/")
if len(slug) != 3 {
return nil, fmt.Errorf("invalid slug of GitHub repo: %s", path)
return nil, fmt.Errorf("invalid slug in GitHub repository URL path: %s", path)
}

ctx := context.Background()
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,14 @@ func main() {
if err != nil {
fail(err)
}
ls, err := gh.CustomAutolinks()
if err != nil {
fail(err)
}

cl := NewChangeLog(os.Stdout, url, *heading, *drafts, reIgnore, reExtract)

if err := cl.Generate(rels); err != nil {
if err := cl.Generate(rels, ls); err != nil {
fail(err)
}
}

0 comments on commit 1fa7ffd

Please sign in to comment.