Browse Source

Determine if repository is ready to reg in code

Don't use the template variables but call a separate function that
checks the latest tag against the HEAD commit.
Achilleas Koutsou 5 years ago
parent
commit
2b35acf970
1 changed files with 39 additions and 0 deletions
  1. 39 0
      internal/route/repo/repo_gin.go

+ 39 - 0
internal/route/repo/repo_gin.go

@@ -98,6 +98,45 @@ func readDataciteFile(entry *git.TreeEntry, c *context.Context) {
 	if doi := getRepoDOI(c); doi != "" && libgin.IsRegisteredDOI(doi) {
 		c.Data["DOI"] = doi
 	}
+
+	c.Data["IsDOIReady"] = isDOIReady(c)
+}
+
+// True if repository is not Private, is not registered, or is registered and
+// has changes.
+func isDOIReady(c *context.Context) bool {
+	dbrepo := c.Repo.Repository
+	gitrepo := c.Repo.GitRepo
+
+	headIsRegistered := func() bool {
+		currentDOI, ok := c.Data["DOI"]
+		if !ok {
+			return false
+		}
+
+		headBranch, err := gitrepo.GetHEADBranch()
+		if err != nil {
+			log.Error(2, "Failed to get HEAD branch for repo at %q: %v", gitrepo.Path, err)
+			return false
+		}
+
+		headCommit, err := gitrepo.GetBranchCommitID(headBranch.Name)
+		if err != nil {
+			log.Error(2, "Failed to get commit ID of branch %q for repo at %q: %v", headBranch.Name, gitrepo.Path, err)
+		}
+
+		// if current valid and registered DOI matches the HEAD commit, can't
+		// register again
+		doiCommit, err := gitrepo.GetTagCommitID(currentDOI.(string))
+		if err != nil {
+			log.Error(2, "Failed to get commit ID of tag %q for repo at %q: %v", currentDOI, gitrepo.Path, err)
+		}
+
+		log.Trace("%s ?= %s", headCommit, doiCommit)
+		return headCommit == doiCommit
+	}()
+
+	return !dbrepo.IsPrivate && !headIsRegistered
 }
 
 // getRepoDOI returns the DOI for the repository based on the following rules: