Dear Gitlab users, due to maintenance reasons, Gitlab will not be available on Thursday 30.09.2021 from 5:00 pm to approximately 5:30 pm.

Commit c0e81c35 authored by Andreas Wagner's avatar Andreas Wagner
Browse files

Fix http status codes.

parent 9e315b40
......@@ -302,6 +302,7 @@ type FileInfo struct {
Change string
CommitSHA string
Date time.Time
DOIURL string
DoPublish bool
Filename string
ObjectSHA string
......@@ -319,7 +320,7 @@ type FileInfo struct {
// ProcessHook processes a Webhook's payload
// it returns a (boolean) doPublish value, a map of filenames/urls with all concerned files, and an error value
func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (map[string]FileInfo, error) {
func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (map[string]FileInfo, *tei2zenodo.Error) {
switch hookType {
case "push":
......@@ -335,16 +336,16 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (map
// Filtering based on repo, pusher or branch
if conf.Git.Repo != "" && payload.Repository.FullName != conf.Git.Repo {
log.Warnf("Repo not allowed: %s", payload.Repository.FullName)
return nil, tei2zenodo.NewError("errBadRequest", fmt.Sprintf("repo not allowed: %s", payload.Repository.FullName), 500, nil)
log.Warnf("Repo not applicable: %s", payload.Repository.FullName)
return nil, tei2zenodo.NewError("warnInapplicable", fmt.Sprintf("repo inapplicable: %s", payload.Repository.FullName), 204, nil)
}
if conf.Git.HookUser != "" && payload.Pusher.Name != conf.Git.HookUser {
log.Warnf("Git user not allowed as pusher: %s", payload.Pusher.Name)
return nil, tei2zenodo.NewError("errBadRequest", fmt.Sprintf("git user not allowed as pusher: %s", payload.Pusher.Name), 500, nil)
log.Warnf("Git pusher not applicable: %s", payload.Pusher.Name)
return nil, tei2zenodo.NewError("warnInapplicable", fmt.Sprintf("pusher inapplicable: %s", payload.Pusher.Name), 204, nil)
}
if conf.Git.Branch != "" && payload.Ref != "refs/heads/"+conf.Git.Branch {
log.Warnf("Git push to the wrong branch: %s instead of %s", payload.Ref, conf.Git.Branch)
return nil, tei2zenodo.NewError("errBadRequest", fmt.Sprintf("git push to the wrong branch: %s instead of %s", payload.Ref, conf.Git.Branch), 500, nil)
log.Warnf("Git push to inapplicable branch: %s instead of %s", payload.Ref, conf.Git.Branch)
return nil, tei2zenodo.NewError("warnInapplicable", fmt.Sprintf("branch inapplicable: %s", payload.Ref), 204, nil)
}
log.Tracef("Push payload: %+v", payload)
......@@ -628,7 +629,7 @@ func PutFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config, c *gin.Cont
log.Printf(" Success.")
}
return "", nil
return myDeposit.CommitSHA, nil
// For testing: uri := "https://postman-echo.com/post"
// https://api.github.com/repos/:owner/:repo/contents/:path
......
......@@ -165,20 +165,35 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
// Process Hook to extract files
files, PHErr := github.ProcessHook(hookType, r, &conf)
if PHErr != nil {
log.Errorf("Error parsing webhook event: %v", PHErr)
AbortMsg(500, tei2zenodo.NewError("errWebhook", fmt.Sprintf("problem parsing webhook for %s event", hookType), 500, PHErr), c)
return
switch PHErr.Typ {
case "warnInapplicable":
{
log.Errorf("webhook inapplicable: %v", PHErr)
c.Status(http.StatusNoContent) // "processed hook successfully (nothing to do)"
return
}
default:
{
log.Errorf("Error parsing webhook event: %v", PHErr)
AbortMsg(500, tei2zenodo.NewError("errWebhook", fmt.Sprintf("problem parsing webhook for %s event", hookType), 500, PHErr), c)
return
}
}
}
var DOIs []string
var SHAs []string
// Send each file to processing
for f := range files {
log.Printf("--- Process file '%s' ---", f)
var myDeposit tei2zenodo.Deposit
// myDeposit.Filename = strings.Replace(f, "/", "_", -1)
myDeposit.Filename = f
myDeposit.CommitSHA = files[f].CommitSHA
myDeposit.DoPublish = files[f].DoPublish
myDeposit.Filename = f
myDeposit.GithubBlobURL = files[f].BlobURL
myDeposit.GithubObjSHA = files[f].ObjectSHA
myDeposit.GithubRawURL = files[f].RawURL
......@@ -190,7 +205,6 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
AbortMsg(500, tei2zenodo.NewError("errNetComm", fmt.Sprintf("error downloading file %s (%s): %s", myDeposit.Filename, myDeposit.GithubObjSHA, GDLErr.Error()), 500, GDLErr), c)
return
}
// log.Tracef("Beginning of submitted file %s: %s ...", filename, r[:100])
// Parse TEI file
var md tei2zenodo.ZMetadata
......@@ -214,6 +228,7 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
r.Seek(0, 0)
md.DOI = ""
// Upload (and publish?) file to zenodo
ZPFErr := zenodo.ProcessFile(r, doi, &md, &myDeposit, &conf)
if ZPFErr != nil {
log.Warnf("Problem processing file %s: %v ...", myDeposit.Filename, ZPFErr)
......@@ -221,6 +236,7 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
return
}
// Upload file back to github
_, GPFErr := github.PutFile(&myDeposit, &conf, c)
if GPFErr != nil {
log.Errorf("Error putting file to github: %v", GPFErr)
......@@ -229,23 +245,13 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
}
log.Printf(" Successfully processed file %s (DOI %s, github SHA %s).", myDeposit.Filename, myDeposit.DepositDOI, myDeposit.DepositDOI)
/*
// The upload to github requires more than just a token:
// you need to authenticate an app to act in lieu of the user in github...
// See https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/
commitSHA, GHErr := github.PutFile(&myDeposit, &conf)
if GHErr != nil {
log.Printf("Problem uploading file %s to github: %+v", f, GHErr)
AbortMsg(500, tei2zenodo.NewError("errGHProcessing", fmt.Sprintf("problem uploading file %s to github", myDeposit.Filename), 500, GHErr), c)
return
}
log.Printf(" New file %s committed to github (%s).", f, commitSHA)
*/
SHAs = append(SHAs, myDeposit.CommitSHA)
DOIs = append(DOIs, myDeposit.DOIURL)
}
log.Printf("====== All done ======")
c.JSON(200, "processed hook successfully")
c.Header("Location", strings.Join(DOIs, ";"))
c.JSON(http.StatusCreated, gin.H{"doi": DOIs, "sha": SHAs})
})
}
}
......
......@@ -238,7 +238,7 @@ type ZDate struct {
// Error messages
type Error struct {
Typ string // errNoTEIXML, errParse, errWebhook, errZProcessing, errGHProcessing, errSerializing, errInternal, errBadConfig, errNetComm, errBadRequest
Typ string // errNoTEIXML, errParse, errWebhook, errZProcessing, errGHProcessing, errSerializing, errInternal, errBadConfig, errNetComm, errBadRequest, warnInapplicable
Message string
HTTPCode int
ErrorVal error // what may have been raised by previous code
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment