|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "jira-cloud-github-action/internal" |
| 6 | + "log" |
| 7 | + "regexp" |
| 8 | + "slices" |
| 9 | + "text/template" |
| 10 | + |
| 11 | + "github.com/alexflint/go-arg" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + var args struct { |
| 17 | + LogLevel string `arg:"env:INPUT_LOGLEVEL" help:"The loglevel to use" default:"INFO"` |
| 18 | + URL string `arg:"required,env:INPUT_URL" help:"The URL to your Jira Cloud instance"` |
| 19 | + User string `arg:"required,env:INPUT_USERNAME" help:"The user name for API requests"` |
| 20 | + Token string `arg:"required,env:INPUT_TOKEN" help:"The user's token for API requests"` |
| 21 | + CommitParseRegExp string `arg:"env:INPUT_COMMITPARSEREGEXP" help:"The Regexp string to use to parse Jira issue ids from commit messages. Requires a named group called id that catches the whole issue id and a group project that catches only the project key" default:"\\((?P<id>(?P<project>[A-Za-z]+)-[0-9]+)\\)"` |
| 22 | + CommentTemplate string `arg:"env:INPUT_COMMENTTEMPLATE" help:"The go template for the issue comment. Takes the CommentInfo struct as an input" default:"{{ .CommitsInfo.Type }}"` |
| 23 | + DryRun bool `arg:"env:INPUT_DRYRUN" help:"Don't actually do something, just write what would be done'" default:"false"` |
| 24 | + OnlyProjects []string `arg:"env:INPUT_ONLYPROJECTS" help:"Only allow commenting on these projects"` |
| 25 | + } |
| 26 | + arg.MustParse(&args) |
| 27 | + if l, err := logrus.ParseLevel(args.LogLevel); err != nil { |
| 28 | + log.Fatal(err) |
| 29 | + } else { |
| 30 | + logrus.SetLevel(l) |
| 31 | + } |
| 32 | + |
| 33 | + var matcher *regexp.Regexp |
| 34 | + if m, err := regexp.Compile(args.CommitParseRegExp); err != nil { |
| 35 | + logrus.Fatalf("Can not parse regular expression %s: %v", args.CommitParseRegExp, err) |
| 36 | + } else { |
| 37 | + matcher = m |
| 38 | + } |
| 39 | + |
| 40 | + api := internal.NewJiraAPI(args.URL, args.User, args.Token) |
| 41 | + |
| 42 | + var commentTemplate *template.Template |
| 43 | + if t, err := template.New("comment").Parse(args.CommentTemplate); err != nil { |
| 44 | + log.Fatalf("Can not parse template %s: %v", args.CommentTemplate, err) |
| 45 | + } else { |
| 46 | + commentTemplate = t |
| 47 | + } |
| 48 | + |
| 49 | + commitsFetchers := []internal.CommitsFetcher{&internal.PushCommitFetcher{}, &internal.PullRequestCommitFetcher{}} |
| 50 | + |
| 51 | + for _, fetcher := range commitsFetchers { |
| 52 | + if fetcher.Test() { |
| 53 | + logrus.Infof("Found %s", fetcher.GetInfo().Type) |
| 54 | + if commits, err := fetcher.GetCommits(); err != nil { |
| 55 | + log.Fatal(err) |
| 56 | + } else { |
| 57 | + for _, commit := range commits { |
| 58 | + matches := matcher.FindStringSubmatch(commit.Message) |
| 59 | + if len(matches) > 0 { |
| 60 | + project := matches[matcher.SubexpIndex("project")] |
| 61 | + if len(args.OnlyProjects) > 0 && !slices.Contains(args.OnlyProjects, project) { |
| 62 | + continue |
| 63 | + } |
| 64 | + issueID := matches[matcher.SubexpIndex("id")] |
| 65 | + commentInfo := internal.CommentInfo{ |
| 66 | + CommitsInfo: fetcher.GetInfo(), |
| 67 | + Commits: commits, |
| 68 | + } |
| 69 | + var commentWriter = bytes.Buffer{} |
| 70 | + if err := commentTemplate.Execute(&commentWriter, commentInfo); err != nil { |
| 71 | + log.Fatalf("Can not execute template %s with object %v: %v", args.CommentTemplate, commentInfo, err) |
| 72 | + } |
| 73 | + if err := api.CommentWorkItem(issueID, commentWriter.String()); err != nil { |
| 74 | + log.Fatal(err) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments