summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-04-17 02:51:23 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-04-17 03:18:53 -0600
commitacd9ee5f9bad8dff524d54a78d074f64aee3d0a5 (patch)
treed7b92d63577155dc5e5a29bb69560426cf899fbc /cmd
parent001168238aadb4eac052dd17bc5e0c800a64698b (diff)
imworkingon: Check if things have been released
Diffstat (limited to 'cmd')
-rw-r--r--cmd/generate/gitcache.go66
-rw-r--r--cmd/generate/src_contribs.go59
2 files changed, 120 insertions, 5 deletions
diff --git a/cmd/generate/gitcache.go b/cmd/generate/gitcache.go
new file mode 100644
index 0000000..7caf024
--- /dev/null
+++ b/cmd/generate/gitcache.go
@@ -0,0 +1,66 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "os/exec"
+ "strings"
+
+ "git.mothstuff.lol/lukeshu/eclipse/lib/gitcache"
+)
+
+var gitFetched = map[string]struct{}{}
+
+var gitCache = &gitcache.Cache{
+ Dir: ".git-cache",
+}
+
+func withGit(u string, fn func(dir string) error) error {
+ if _, ok := gitFetched[u]; !ok {
+ if err := gitCache.Fetch(os.Stderr, u); err != nil {
+ return err
+ }
+ }
+ return gitCache.WithFastClone(os.Stderr, u, fn)
+}
+
+func getGitTagThatContainsAll(gitURL string, gitHashes ...string) (string, error) {
+ if len(gitHashes) == 0 {
+ return "", nil
+ }
+ var tag string
+ err := withGit(gitURL, func(dir string) error {
+ gitHash := gitHashes[0]
+ if len(gitHashes) > 1 {
+ cmdline := append([]string{"git", "merge-base", "--independent", "--"}, gitHashes...)
+ cmd := exec.Command(cmdline[0], cmdline[1:]...)
+ cmd.Dir = dir
+ var stdout strings.Builder
+ cmd.Stdout = &stdout
+ cmd.Stderr = os.Stderr
+ if err := cmd.Run(); err != nil {
+ return err
+ }
+ gitHash = strings.TrimSpace(stdout.String())
+ }
+ cmd := exec.Command("git", "for-each-ref",
+ "--count=1",
+ "--format=%(refname:lstrip=2)",
+ "--contains="+gitHash,
+ "refs/tags/",
+ )
+ cmd.Dir = dir
+ var stdout strings.Builder
+ cmd.Stdout = &stdout
+ cmd.Stderr = os.Stderr
+ if err := cmd.Run(); err != nil {
+ return err
+ }
+ tag = strings.TrimSpace(stdout.String())
+ return nil
+ })
+ if err != nil {
+ return "", fmt.Errorf("%q: %w", gitURL, err)
+ }
+ return tag, nil
+}
diff --git a/cmd/generate/src_contribs.go b/cmd/generate/src_contribs.go
index 760b56a..f201ab5 100644
--- a/cmd/generate/src_contribs.go
+++ b/cmd/generate/src_contribs.go
@@ -102,8 +102,9 @@ func (c Contribution) fetchStatus() (string, error) {
var obj struct {
// State values are "open" and "closed".
- State string `json:"state"`
- Merged bool `json:"merged"`
+ State string `json:"state"`
+ Merged bool `json:"merged"`
+ MergeCommitSha string `json:"merge_commit_sha"`
}
if err := httpGetJSON(urlStr, &obj); err != nil {
return "", err
@@ -111,7 +112,15 @@ func (c Contribution) fetchStatus() (string, error) {
ret := obj.State
if obj.Merged {
ret = "merged"
+ tag, err := getGitTagThatContainsAll("https://github.com/"+user+"/"+repo, obj.MergeCommitSha)
+ if err != nil {
+ return "", err
+ }
+ if tag != "" {
+ ret = "released in " + tag
+ }
}
+
return ret, nil
}
if m := reGitLabMR.FindStringSubmatch(c.URLs[0]); m != nil {
@@ -123,7 +132,9 @@ func (c Contribution) fetchStatus() (string, error) {
var obj struct {
// State values are "opened", "closed", "locked", and "merged".
- State string `json:"state"`
+ State string `json:"state"`
+ MergeCommitSha string `json:"merge_commit_sha"`
+ SquashCommitSha string `json:"squash_commit_sha"`
}
if err := httpGetJSON(urlStr, &obj); err != nil {
return "", err
@@ -133,13 +144,51 @@ func (c Contribution) fetchStatus() (string, error) {
if ret == "opened" {
ret = "open"
}
+
+ if ret == "merged" {
+ var mergeCommit string
+ if obj.MergeCommitSha != "" {
+ mergeCommit = obj.MergeCommitSha
+ }
+ if obj.SquashCommitSha != "" {
+ mergeCommit = obj.SquashCommitSha
+ }
+ if mergeCommit != "" {
+ tag, err := getGitTagThatContainsAll("https://"+authority+"/"+projectID+".git", mergeCommit)
+ if err != nil {
+ return "", err
+ }
+ if tag != "" {
+ ret = "released in " + tag
+ }
+ }
+ }
+
return ret, nil
}
if len(c.URLs) > 1 {
+ var gitURL string
+ var gitCommits []string
for _, u := range c.URLs[1:] {
- if reGitHubCommit.MatchString(u) {
- return "merged", nil
+ 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 {
+ ret := "merged"
+ tag, err := getGitTagThatContainsAll(gitURL, gitCommits...)
+ if err != nil {
+ return "", err
+ }
+ if tag != "" {
+ ret = "released in " + tag
}
+ return ret, nil
}
}
return "", fmt.Errorf("idk how to get status for %q", c.URLs[0])