Skip to content

Commit 2716e9b

Browse files
committed
Merge branch 'master' into live
* master: Syntax highlight plugin fix Old code cleanup: readNotice() Move repo DOI checks to contexter GIN Repo header button redesign Only show DOI buttons on repo home page [repo/header_gin] Fix registration link Replace "DOIfy" with more meaningful labels Determine if repository is ready to reg in code [repo/header] Revert to upstream and inject GIN changes [repo/header] Partially reverting to upstream [repo/home] Indentation fixes and div rebalancing [repo/home] Upstream cleanup Second row in header for DOI button and badge Check and log errors for new DOI retrieval function New DOI badge logic Remove unnecessary DOIReg flag in template data
2 parents cb95953 + 2722974 commit 2716e9b

File tree

10 files changed

+196
-157
lines changed

10 files changed

+196
-157
lines changed

internal/context/context_gin.go

Lines changed: 76 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,95 @@
11
package context
22

33
import (
4-
"os"
5-
"path"
64
"strings"
75

6+
"github.com/G-Node/git-module"
7+
"github.com/G-Node/gogs/internal/db"
88
"github.com/G-Node/gogs/internal/setting"
9-
"github.com/G-Node/gogs/internal/tool"
10-
"github.com/unknwon/com"
9+
"github.com/G-Node/libgin/libgin"
1110
log "gopkg.in/clog.v1"
1211
)
1312

14-
// readNotice checks if a notice file exists and loads the message to display
15-
// on all pages.
16-
func readNotice(c *Context) {
17-
18-
fileloc := path.Join(setting.CustomPath, "notice")
19-
var maxlen int64 = 1024
20-
21-
if !com.IsExist(fileloc) {
22-
return
13+
// getRepoDOI returns the DOI for the repository based on the following rules:
14+
// - if the repository belongs to the DOI user and has a tag that matches the
15+
// DOI prefix, returns the tag.
16+
// - if the repo is forked by the DOI user, check the DOI fork for the tag as above.
17+
// - if the repo is forked by the DOI user and the fork doesn't have a tag,
18+
// returns the (old-style) calculated DOI, based on the hash of the repository
19+
// path.
20+
// - An empty string is returned if it is not not forked by the DOI user.
21+
// If an error occurs at any point, returns an empty string (the error is logged).
22+
// Tag retrieval is allowed to fail and falls back on the hashed DOI method.
23+
func getRepoDOI(c *Context) string {
24+
repo := c.Repo.Repository
25+
var doiFork *db.Repository
26+
if repo.Owner.Name == "doi" {
27+
doiFork = repo
28+
} else {
29+
if forks, err := repo.GetForks(); err == nil {
30+
for _, fork := range forks {
31+
if fork.MustOwner().Name == "doi" {
32+
doiFork = fork
33+
break
34+
}
35+
}
36+
} else {
37+
log.Error(2, "failed to get forks for repository %q (%d): %v", repo.FullName(), repo.ID, err)
38+
return ""
39+
}
2340
}
2441

25-
log.Trace("Found notice file")
26-
fp, err := os.Open(fileloc)
27-
if err != nil {
28-
log.Error(2, "Failed to open notice file %s: %v", fileloc, err)
29-
return
42+
if doiFork == nil {
43+
// not owned or forked by DOI, so not registered
44+
return ""
3045
}
31-
defer fp.Close()
3246

33-
finfo, err := fp.Stat()
34-
if err != nil {
35-
log.Error(2, "Failed to stat notice file %s: %v", fileloc, err)
36-
return
37-
}
47+
// check the DOI fork for a tag that matches our DOI prefix
48+
// if multiple exit, get the latest one
49+
doiBase := setting.DOI.Base
3850

39-
if finfo.Size() > maxlen { // Refuse to print very long messages
40-
log.Error(2, "Notice file %s size too large [%d > %d]: refusing to render", fileloc, finfo.Size(), maxlen)
41-
return
42-
}
43-
44-
buf := make([]byte, maxlen)
45-
n, err := fp.Read(buf)
51+
doiForkGit, err := git.OpenRepository(doiFork.RepoPath())
4652
if err != nil {
47-
log.Error(2, "Failed to read notice file: %v", err)
48-
return
53+
log.Error(2, "failed to open git repository at %q (%d): %v", doiFork.RepoPath(), doiFork.ID, err)
54+
return ""
4955
}
50-
buf = buf[:n]
51-
52-
if !tool.IsTextFile(buf) {
53-
log.Error(2, "Notice file %s does not appear to be a text file: aborting", fileloc)
54-
return
56+
if tags, err := doiForkGit.GetTags(); err == nil {
57+
var latestTime int64
58+
latestTag := ""
59+
for _, tagName := range tags {
60+
if strings.Contains(tagName, doiBase) {
61+
tag, err := doiForkGit.GetTag(tagName)
62+
if err != nil {
63+
// log the error and continue to the next tag
64+
log.Error(2, "failed to get information for tag %q for repository at %q: %v", tagName, doiForkGit.Path, err)
65+
continue
66+
}
67+
commit, err := tag.Commit()
68+
if err != nil {
69+
// log the error and continue to the next tag
70+
log.Error(2, "failed to get commit for tag %q for repository at %q: %v", tagName, doiForkGit.Path, err)
71+
continue
72+
}
73+
commitTime := commit.Committer.When.Unix()
74+
if commitTime > latestTime {
75+
latestTag = tagName
76+
latestTime = commitTime
77+
}
78+
return latestTag
79+
}
80+
}
81+
} else {
82+
// this shouldn't happen even if there are no tags
83+
// log the error, but fall back to the old method anyway
84+
log.Error(2, "failed to get tags for repository at %q: %v", doiForkGit.Path, err)
5585
}
5686

57-
noticetext := strings.SplitN(string(buf), "\n", 2)
58-
c.Data["HasNotice"] = true
59-
c.Data["NoticeTitle"] = noticetext[0]
60-
c.Data["NoticeMessage"] = noticetext[1]
87+
// Has DOI fork but isn't tagged: return old style has-based DOI
88+
repoPath := repo.FullName()
89+
// get base repo name if it's a DOI fork
90+
if c.Repo.Repository.IsFork && c.Repo.Owner.Name == "doi" {
91+
repoPath = c.Repo.Repository.BaseRepo.FullName()
92+
}
93+
uuid := libgin.RepoPathToUUID(repoPath)
94+
return doiBase + uuid[:6]
6195
}

internal/context/repo.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ package context
66

77
import (
88
"fmt"
9-
"io/ioutil"
10-
"strings"
11-
129
"gopkg.in/editorconfig/editorconfig-core-go.v1"
1310
"gopkg.in/macaron.v1"
11+
"io/ioutil"
12+
"strings"
1413

1514
"github.com/G-Node/git-module"
16-
1715
"github.com/G-Node/gogs/internal/db"
1816
"github.com/G-Node/gogs/internal/db/errors"
1917
"github.com/G-Node/gogs/internal/setting"
18+
"github.com/G-Node/libgin/libgin"
2019
)
2120

2221
type PullRequest struct {
@@ -270,6 +269,11 @@ func RepoAssignment(pages ...bool) macaron.Handler {
270269
c.Data["CommitID"] = c.Repo.CommitID
271270

272271
c.Data["IsGuest"] = !c.Repo.HasAccess()
272+
273+
if doi := getRepoDOI(c); doi != "" && libgin.IsRegisteredDOI(doi) {
274+
c.Data["DOI"] = doi
275+
}
276+
273277
}
274278
}
275279

internal/route/repo/repo_gin.go

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,47 @@ func readDataciteFile(entry *git.TreeEntry, c *context.Context) {
9393
return
9494
}
9595
c.Data["DOIInfo"] = &doiInfo
96-
97-
doi := calcRepoDOI(c, setting.DOI.Base)
98-
//ddata, err := ginDoi.GDoiMData(doi, "https://api.datacite.org/works/") //todo configure URL?
99-
100-
c.Data["DOIReg"] = libgin.IsRegisteredDOI(doi)
101-
c.Data["DOI"] = doi
96+
c.Data["IsDOIReady"] = isDOIReady(c)
10297
}
10398

104-
// calcRepoDOI calculates the theoretical DOI for a repository. If the repository
105-
// belongs to the DOI user (and is a fork) it uses the name for the base
106-
// repository.
107-
func calcRepoDOI(c *context.Context, doiBase string) string {
108-
repoN := c.Repo.Repository.FullName()
109-
// check whether this repo belongs to DOI and is a fork
110-
if c.Repo.Repository.IsFork && c.Repo.Owner.Name == "doi" {
111-
repoN = c.Repo.Repository.BaseRepo.FullName()
99+
// True if repository is not Private, is not registered, or is registered and
100+
// has changes.
101+
func isDOIReady(c *context.Context) bool {
102+
if hasDC, ok := c.Data["HasDatacite"]; !ok || !hasDC.(bool) {
103+
return false
112104
}
113-
uuid := libgin.RepoPathToUUID(repoN)
114-
return doiBase + uuid[:6]
105+
dbrepo := c.Repo.Repository
106+
gitrepo := c.Repo.GitRepo
107+
108+
headIsRegistered := func() bool {
109+
currentDOI, ok := c.Data["DOI"]
110+
if !ok {
111+
return false
112+
}
113+
114+
headBranch, err := gitrepo.GetHEADBranch()
115+
if err != nil {
116+
log.Error(2, "Failed to get HEAD branch for repo at %q: %v", gitrepo.Path, err)
117+
return false
118+
}
119+
120+
headCommit, err := gitrepo.GetBranchCommitID(headBranch.Name)
121+
if err != nil {
122+
log.Error(2, "Failed to get commit ID of branch %q for repo at %q: %v", headBranch.Name, gitrepo.Path, err)
123+
}
124+
125+
// if current valid and registered DOI matches the HEAD commit, can't
126+
// register again
127+
doiCommit, err := gitrepo.GetTagCommitID(currentDOI.(string))
128+
if err != nil {
129+
log.Error(2, "Failed to get commit ID of tag %q for repo at %q: %v", currentDOI, gitrepo.Path, err)
130+
}
131+
132+
log.Trace("%s ?= %s", headCommit, doiCommit)
133+
return headCommit == doiCommit
134+
}()
135+
136+
return !dbrepo.IsPrivate && !headIsRegistered
115137
}
116138

117139
// resolveAnnexedContent takes a buffer with the contents of a git-annex

public/css/custom.css

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,15 @@ figure figcaption {
237237
color: white;
238238
border-top-left-radius: 0.28571429rem;
239239
border-bottom-left-radius: 0.28571429rem;
240-
padding: 0.833em 1em;
240+
padding: 0.5em 1.5em;
241241
}
242242
.gin.doinr {
243243
background-color: #2185D0;
244244
float: right;
245245
color: white;
246246
border-top-right-radius: 0.28571429rem;
247247
border-bottom-right-radius: 0.28571429rem;
248-
padding: 0.833em 1em;
248+
padding: 0.5em 1.5em;
249249
}
250250
.ui.image.label.nobg {
251251
background: none;
@@ -375,3 +375,8 @@ textarea#description {
375375
font-size: 1rem;
376376
vertical-align: middle;
377377
}
378+
.ui.header .button {
379+
height: 2em;
380+
vertical-align: middle;
381+
padding: 0.5em 1.5em;
382+
}

public/img/gogs-hero.png

34.2 KB
Loading

public/js/gogs.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,21 +1281,7 @@ $(document).ready(function () {
12811281

12821282
// Highlight JS
12831283
if (typeof hljs != 'undefined') {
1284-
if (typeof(Worker) !== "undefined") {
1285-
addEventListener('load', function () {
1286-
var code = document.querySelector('code');
1287-
var worker = new Worker('/plugins/highlighter.js');
1288-
worker.onmessage = function (event) {
1289-
code.innerHTML = event.data;
1290-
}
1291-
if (code !== null) {
1292-
worker.postMessage(code.textContent);
1293-
}
1294-
})
1295-
} else {
1296-
hljs.initHighlightingOnLoad()
1297-
}
1298-
1284+
hljs.initHighlightingOnLoad();
12991285
}
13001286

13011287
// Dropzone

public/plugins/highlighter.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

templates/repo/header.tmpl

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
{{if not $.IsGuest}}
2222
<div class="ui right">
23+
{{template "repo/header_gin" $}}
2324
<form class="display inline" action="{{$.RepoLink}}/action/{{if $.IsWatchingRepo}}un{{end}}watch?redirect_to={{$.Link}}" method="POST">
2425
{{$.CSRFTokenHTML}}
2526
<div class="ui labeled button" tabindex="0">
@@ -31,9 +32,19 @@
3132
</a>
3233
</div>
3334
</form>
35+
<form class="display inline" action="{{$.RepoLink}}/action/{{if $.IsStaringRepo}}un{{end}}star?redirect_to={{$.Link}}" method="POST">
36+
{{$.CSRFTokenHTML}}
37+
<div class="ui labeled button" tabindex="0">
38+
<button class="ui basic button">
39+
<i class="star{{if not $.IsStaringRepo}} outline{{end}} icon"></i>{{if $.IsStaringRepo}}{{$.i18n.Tr "repo.unstar"}}{{else}}{{$.i18n.Tr "repo.star"}}{{end}}
40+
</button>
41+
<a class="ui basic label" href="{{.Link}}/stars">
42+
{{.NumStars}}
43+
</a>
44+
</div>
45+
</form>
3446
{{if .CanBeForked}}
35-
<div class="ui labeled button" tabindex="0"
36-
data-tooltip="Please note: annexed file content will not be forked" data-position="bottom center">
47+
<div class="ui labeled button" tabindex="0" data-tooltip="Please note: annexed file content will not be forked" data-position="bottom center">
3748
<a class="ui basic button {{if eq .OwnerID $.LoggedUserID}}poping up{{end}}" href="{{AppSubURL}}/repo/fork/{{.ID}}">
3849
<i class="octicon octicon-repo-forked"></i>{{$.i18n.Tr "repo.fork"}}
3950
</a>
@@ -42,39 +53,6 @@
4253
</a>
4354
</div>
4455
{{end}}
45-
{{if not $.DOIReg}}
46-
{{if and (and $.HasDatacite $.IsRepositoryAdmin) (not .IsPrivate)}}
47-
<div class="ui labeled button" tabindex="0">
48-
<a class="ui basic button"
49-
href="/{{.Owner.Name}}/{{.Name}}/doi">
50-
<i class="octicon octicon-squirrel"></i> DOIfy
51-
</a>
52-
</div>
53-
{{else}}
54-
{{if and $.IsRepositoryAdmin $.CommitsCount}}
55-
<div class="ui labeled button " tabindex="0">
56-
<a class="ui basic button"
57-
data-tooltip="Your repository does not fulfill all requirements for a doi yet. Click to get instructions."
58-
data-position="bottom center"
59-
href="/G-Node/Info/wiki/DOI">
60-
<i class="octicon octicon-squirrel"></i> DOIfy
61-
</a>
62-
<a class="ui basic label" href="{{$.RepoLink}}/_add/{{EscapePound $.BranchName}}/datacite.yml"
63-
data-tooltip="Add Doifile"
64-
data-position="bottom center">
65-
<i class="octicon octicon-file"></i>
66-
</a>
67-
</div>
68-
{{end}}
69-
{{end}}
70-
{{else}}
71-
<div class="nobg ui image label">
72-
<a href="https://doi.org/{{$.DOI}}">
73-
<div class="gin doi">DOI</div>
74-
</a>
75-
<div class="gin doinr">{{$.DOI}}</div>
76-
</div>
77-
{{end}}
7856
</div>
7957
{{end}}
8058
</div>

templates/repo/header_gin.tmpl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{{if or $.DOI $.IsRepositoryAdmin}} {{/* Show DOI buttons or badge */}}
2+
{{if $.IsRepositoryAdmin}}
3+
{{if $.IsDOIReady}} {{/* Ready to (re)register: Show button */}}
4+
<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>
5+
{{else if not $.DOI}} {{/*Link to registration instructions*/}}
6+
<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>
7+
<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>
8+
{{end}} {{/* End registration button */}}
9+
{{end}} {{/* Admin section */}}
10+
{{if $.DOI}} {{/* Registered repo: Show DOI badge */}}
11+
<div class="ui labeled button" tabindex="0">
12+
<a href="https://doi.org/{{$.DOI}}">
13+
<div class="gin doi">DOI</div>
14+
</a>
15+
<div class="gin doinr">{{$.DOI}}</div>
16+
</div>
17+
{{end}} {{/* End DOI badge */}}
18+
{{/* Close original header divs and create second row below for original buttons */}}
19+
</div>
20+
</div>
21+
</div><!--- end column -->
22+
</div><!--- end grid -->
23+
</div><!--- end container -->
24+
<div class="ui container"><!-- start container -->
25+
<div class="ui vertically padded grid head"><!-- start grid -->
26+
<div class="column"><!-- start column -->
27+
<div class="ui header">
28+
<div class="ui right">
29+
{{end}}

0 commit comments

Comments
 (0)