|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "code.gitea.io/sdk/gitea" |
| 11 | + "github.com/davecgh/go-spew/spew" |
| 12 | + "github.com/go-semantic-release/semantic-release/v2/pkg/semrel" |
| 13 | +) |
| 14 | + |
| 15 | +type GiteaRepository struct { |
| 16 | + client *gitea.Client |
| 17 | + repo string |
| 18 | + owner string |
| 19 | + stripVTagPrefix bool |
| 20 | + baseURL string |
| 21 | +} |
| 22 | + |
| 23 | +//gocyclo:ignore |
| 24 | +func (repo *GiteaRepository) Init() error { |
| 25 | + giteaHost := "https://hub.cybercinch.nz" |
| 26 | + repo.baseURL = giteaHost |
| 27 | + slug := "cybercinch/ansible-role-common" |
| 28 | + token := "dff97800f1f8238e3c0f6a45f14f6ba3b477a4f5" |
| 29 | + if !strings.Contains(slug, "/") { |
| 30 | + return errors.New("invalid slug") |
| 31 | + } |
| 32 | + split := strings.Split(slug, "/") |
| 33 | + // This could be due to act locally |
| 34 | + // We'll work backwards to get the values |
| 35 | + repo.owner = split[len(split)-2] |
| 36 | + repo.repo = split[len(split)-1] |
| 37 | + |
| 38 | + // Ensure no .git suffix remains |
| 39 | + repo.repo = strings.TrimSuffix(repo.repo, ".git") |
| 40 | + |
| 41 | + ctx := context.Background() |
| 42 | + if giteaHost != "" { |
| 43 | + client, err := gitea.NewClient(giteaHost, |
| 44 | + gitea.SetToken(token), |
| 45 | + gitea.SetContext(ctx)) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + repo.client = client |
| 50 | + } |
| 51 | + |
| 52 | + var err error |
| 53 | + stripVTagPrefix := "false" |
| 54 | + repo.stripVTagPrefix, err = strconv.ParseBool(stripVTagPrefix) |
| 55 | + |
| 56 | + if stripVTagPrefix != "" && err != nil { |
| 57 | + return fmt.Errorf("failed to set property strip_v_tag_prefix: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func (repo *GiteaRepository) GetCommits(_, toSha string) ([]*semrel.RawCommit, error) { |
| 64 | + allCommits := make([]*semrel.RawCommit, 0) |
| 65 | + opts := &gitea.ListOptions{PageSize: 100} |
| 66 | + done := false |
| 67 | + for { |
| 68 | + commits, resp, err := repo.client.ListRepoCommits(repo.owner, repo.repo, gitea.ListCommitOptions{ |
| 69 | + SHA: toSha, |
| 70 | + ListOptions: *opts, |
| 71 | + }) |
| 72 | + |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + for _, commit := range commits { |
| 77 | + sha := commit.SHA |
| 78 | + fmt.Println(spew.Sdump(commit)) |
| 79 | + allCommits = append(allCommits, &semrel.RawCommit{ |
| 80 | + SHA: sha, |
| 81 | + RawMessage: commit.RepoCommit.Message, |
| 82 | + Annotations: map[string]string{ |
| 83 | + "author_login": commit.Author.UserName, |
| 84 | + "author_name": commit.Author.FullName, |
| 85 | + "author_email": commit.Author.Email, |
| 86 | + "author_date": commit.RepoCommit.Author.Date, |
| 87 | + "committer_login": commit.Committer.UserName, |
| 88 | + "committer_name": commit.Committer.FullName, |
| 89 | + "committer_email": commit.Committer.Email, |
| 90 | + "committer_date": commit.RepoCommit.Committer.Date, |
| 91 | + }, |
| 92 | + }) |
| 93 | + } |
| 94 | + if done || resp.NextPage == 0 { |
| 95 | + break |
| 96 | + } |
| 97 | + opts.Page = resp.NextPage |
| 98 | + } |
| 99 | + return allCommits, nil |
| 100 | +} |
| 101 | + |
| 102 | +func main() { |
| 103 | + c := &GiteaRepository{} |
| 104 | + c.Init() |
| 105 | + |
| 106 | + commits, _ := c.GetCommits("0902ffb7682f11a22d85a5532231e68497f53afd", "") |
| 107 | + |
| 108 | + println(spew.Sdump(commits)) |
| 109 | + |
| 110 | +} |
0 commit comments