summaryrefslogtreecommitdiff
path: root/cmd/generate/forge_part_git.go
blob: 52882866d95bb00a2aadafaeca264b9ec7eb2625 (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
package main

import (
	"fmt"
	"regexp"
	"time"
)

var reGitHubCommit = regexp.MustCompile(`^https://github\.com/([^/?#]+)/([^/?#]+)/commit/([0-9a-f]+)(?:\?[^#]*)?(?:#.*)?$`)

type PartGit struct{}

var _ Forge = PartGit{}

func (PartGit) FetchStatus(urls []string) (string, error) {
	var gitURL string
	var gitCommits []string
	for _, u := range urls {
		if m := reGitHubCommit.FindStringSubmatch(u); m != nil {
			user := m[1]
			repo := m[2]
			hash := m[3]

			gitURL = "https://github.com/" + user + "/" + repo
			gitCommits = append(gitCommits, hash)
		}
	}
	if len(gitCommits) == 0 {
		return "", nil
	}
	ret := statusMerged
	tag, err := getGitTagThatContainsAll(gitURL, gitCommits...)
	if err != nil {
		return "", err
	}
	if tag != "" {
		ret = fmt.Sprintf(statusReleasedFmt, tag)
	}
	return ret, nil
}

func (PartGit) FetchSubmittedAt(urls []string) (time.Time, error) {
	return time.Time{}, nil
}

func (PartGit) FetchLastUpdated(urls []string) (time.Time, User, error) {
	var ret time.Time
	for _, u := range urls {
		if m := reGitHubCommit.FindStringSubmatch(u); m != nil {
			user := m[1]
			repo := m[2]
			hash := m[3]

			urlStr := "https://api.github.com/repos/" + user + "/" + repo + "/commits/" + hash
			var obj struct {
				Commit struct {
					Author struct {
						Date time.Time `json:"date"`
					} `json:"author"`
					Committer struct {
						Date time.Time `json:"date"`
					} `json:"committer"`
				} `json:"commit"`
			}
			if err := httpGetJSON(urlStr, nil, &obj); err != nil {
				return time.Time{}, User{}, err
			}
			if obj.Commit.Author.Date.After(ret) {
				ret = obj.Commit.Author.Date
			}
			if obj.Commit.Committer.Date.After(ret) {
				ret = obj.Commit.Committer.Date
			}
		}
	}
	if ret.IsZero() {
		return time.Time{}, User{}, nil
	}
	return time.Time{}, User{}, nil
}