Kaynağa Gözat

Edit DataCite when it already exists

Achilleas Koutsou 5 yıl önce
ebeveyn
işleme
c8a75524ed

+ 13 - 0
internal/context/context_gin.go

@@ -93,3 +93,16 @@ func getRepoDOI(c *Context) string {
 	uuid := libgin.RepoPathToUUID(repoPath)
 	return doiBase + uuid[:6]
 }
+
+// hasDataCite returns 'true' if a repository includes a file called
+// 'datacite.yml' in its root.  No checks are made to determine if the file is
+// valid.  If any error occurs, for example due to an uninitialised repository
+// or missing repository root, it returns 'false' without error.
+func hasDataCite(c *Context) bool {
+	commit, err := c.Repo.GitRepo.GetBranchCommit(c.Repo.Repository.DefaultBranch)
+	if err != nil {
+		return false
+	}
+	_, err = commit.GetBlobByPath("./datacite.yml")
+	return err == nil
+}

+ 2 - 0
internal/context/repo.go

@@ -270,6 +270,8 @@ func RepoAssignment(pages ...bool) macaron.Handler {
 
 		c.Data["IsGuest"] = !c.Repo.HasAccess()
 
+		c.Data["HasDataCite"] = hasDataCite(c)
+
 		if doi := getRepoDOI(c); doi != "" && libgin.IsRegisteredDOI(doi) {
 			c.Data["DOI"] = doi
 		}

+ 4 - 4
internal/route/repo/repo_gin.go

@@ -73,10 +73,10 @@ func serveAnnexedKey(ctx *context.Context, name string, contentPath string) erro
 	return err
 }
 
-func readDataciteFile(entry *git.TreeEntry, c *context.Context) {
+func readDataciteFile(entry *git.Blob, c *context.Context) {
 	log.Trace("Found datacite.yml file")
-	c.Data["HasDatacite"] = true
-	doiData, err := entry.Blob().Data()
+	c.Data["HasDataCite"] = true
+	doiData, err := entry.Data()
 	if err != nil {
 		log.Error(2, "datacite.yml blob could not be read: %v", err)
 		return
@@ -99,7 +99,7 @@ func readDataciteFile(entry *git.TreeEntry, c *context.Context) {
 // True if repository is not Private, is not registered, or is registered and
 // has changes.
 func isDOIReady(c *context.Context) bool {
-	if hasDC, ok := c.Data["HasDatacite"]; !ok || !hasDC.(bool) {
+	if hasDC, ok := c.Data["HasDataCite"]; !ok || !hasDC.(bool) {
 		return false
 	}
 	dbrepo := c.Repo.Repository

+ 3 - 5
internal/route/repo/view.go

@@ -54,14 +54,12 @@ func renderDirectory(c *context.Context, treeLink string) {
 		c.ServerError("GetCommitsInfoWithCustomConcurrency", err)
 		return
 	}
-	c.Data["HasDatacite"] = false
-	var readmeFile *git.Blob
-	for _, entry := range entries {
-		if !entry.IsDir() && entry.Name() == "datacite.yml" {
+	if c.Data["HasDataCite"].(bool) {
+		if entry, err := tree.GetBlobByPath("datacite.yml"); err != nil {
 			readDataciteFile(entry, c)
-			break
 		}
 	}
+	var readmeFile *git.Blob
 	for _, entry := range entries {
 		if entry.IsDir() || !markup.IsReadmeFile(entry.Name()) {
 			continue

+ 5 - 1
templates/repo/header_gin.tmpl

@@ -4,7 +4,11 @@
 							<a class="ui basic button" href="/{{.Repository.Owner.Name}}/{{.Repository.Name}}/doi"><i class="octicon octicon-squirrel"></i> Request {{if $.DOI}}New{{end}} DOI</a>
 						{{else if not $.DOI}} {{/*Link to registration instructions*/}}
 							<a class="ui basic button" data-tooltip="Your repository does not fulfill all requirements for a DOI yet. Click to get instructions." data-position="bottom center" href="/G-Node/Info/wiki/DOI"><i class="octicon octicon-squirrel"></i> How to publish</a>
-							<a class="ui basic button" href="{{$.RepoLink}}/_add/{{EscapePound $.BranchName}}/datacite.yml" data-tooltip="Add datacite file" data-position="bottom center"><i class="octicon octicon-file">Add DataCite file</i></a>
+							{{if $.HasDataCite}}
+								<a class="ui basic button" href="{{$.RepoLink}}/_edit/{{EscapePound $.BranchName}}/datacite.yml" data-tooltip="Add datacite file" data-position="bottom center"><i class="octicon octicon-file">Edit DataCite file</i></a>
+							{{else}}
+								<a class="ui basic button" href="{{$.RepoLink}}/_add/{{EscapePound $.BranchName}}/datacite.yml" data-tooltip="Add datacite file" data-position="bottom center"><i class="octicon octicon-file">Add DataCite file</i></a>
+							{{end}}
 						{{end}} {{/* End registration button */}}
 					{{end}} {{/* Admin section */}}
 					{{if $.DOI}} {{/* Registered repo: Show DOI badge */}}

+ 1 - 1
templates/repo/view_list.tmpl

@@ -59,6 +59,6 @@
 {{if and .ReadmeExist .IsTextFile}}
 	{{template "repo/view_file" .}}
 {{end}}
-{{if and .HasDatacite .IsTextFile}}
+{{if and .HasDataCite .IsTextFile}}
 	{{template "repo/doifile" .}}
 {{end}}