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 eb3dd888 authored by Andreas Wagner's avatar Andreas Wagner
Browse files

Finally, migrate to go module system.

parent a1046c5a
package conf
package main
import (
"fmt"
......@@ -6,11 +6,104 @@ import (
"strings"
"github.com/spf13/viper"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
)
// Data Types
type Config struct {
ListenSpec int64
HTTPS string
Verbose bool
APIRoot string
FileAPI string
WebhookAPI string
Secret string
WriteDOI WriteDOIConfig
Zenodo ZenodoConfig
Git GitConfig
Metadata MetadataConfig
Log LoggingConfig
T2ZCommitMessage string // This is not configurable in fact but stored here to be passed around among modules. Stores the commit message that this service creates and is set in conf.go
}
// WriteDOIConfig specifies if and how a new DOI value is written to the TEI file before processing
type WriteDOIConfig struct {
Mode string // 'add' or 'replace' (which actually also adds the element in question if none is present)
ParentPath string
ElementType string
Position string // 'firstChild' or 'lastChild'
AttributeName string // if the DOI goes into an attribute rather than a text node
OtherAttributes []AttributeSpec // if we need to add more attributes to an element that we're creating
AddChangeDesc bool
}
// AttributeSpec specifies an (xml) attribute and its value
type AttributeSpec struct {
AttName string
Value string
}
// LoggingConfig specifies all the parameters needed for logging.
type LoggingConfig struct {
File string
Level string
}
// ZenodoConfig specifies parameters for the zenodo repository connection.
type ZenodoConfig struct {
Host string
Token string
Prefix string
}
// GitConfig specifies git repositories from which webhooks are accepted.
type GitConfig struct {
Host string
Token string
Repo string
Branch string
HookUser string
TriggerPhrase string
DontPublishPhrase string
}
// MetadataConfig specifies metadata fields and xpaths to retrieve their values if the need to be different from the defaults.
type MetadataConfig struct {
Fields []metadataField
}
type metadataField struct {
Field string
XPath string
XExpression string
Subfields []metadataField
}
// Error messages
type Error struct {
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
}
// NewError returns a pointer to a Error struct
func NewError(typ string, msg string, status int, err error) *Error {
return &Error{
Typ: typ,
Message: msg,
ErrorVal: err,
HTTPCode: status,
}
}
// Error() method of interface
func (e *Error) Error() string {
return e.Message
}
// Configure loads configuration parameters into Config struct
func Configure(Config *tei2zenodo.Config) error {
func Configure(Config *Config) error {
/* Enable commandline switches:
err := viper.BindPFlags(cmd.Flags())
......
// Package github processes webhook posts, retrieves files and pipes them to zenodo processing
package github
package main
import (
"bytes"
......@@ -14,10 +13,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/google/go-github/github"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
)
// ====== Types =======
......@@ -320,7 +316,7 @@ type FileInfo struct {
// ProcessHook processes a Webhook's payload
// 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, *tei2zenodo.Error) {
func ProcessHook(hookType string, r io.ReadSeeker, conf *Config) (map[string]FileInfo, *Error) {
switch hookType {
case "push":
......@@ -331,21 +327,21 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (map
err := json.NewDecoder(r).Decode(&payload)
if err != nil {
log.Errorf("Error processing push hook: %+v", err)
return nil, tei2zenodo.NewError("errInternal", fmt.Sprintf("error processing push hook: %s", err.Error()), 500, err)
return nil, NewError("errInternal", fmt.Sprintf("error processing push hook: %s", err.Error()), 500, err)
}
// Filtering based on repo, pusher or branch
if conf.Git.Repo != "" && payload.Repository.FullName != conf.Git.Repo {
log.Warnf("Repo not applicable: %s", payload.Repository.FullName)
return nil, tei2zenodo.NewError("warnInapplicable", fmt.Sprintf("repo inapplicable: %s", payload.Repository.FullName), 204, nil)
return nil, 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 pusher not applicable: %s", payload.Pusher.Name)
return nil, tei2zenodo.NewError("warnInapplicable", fmt.Sprintf("pusher inapplicable: %s", payload.Pusher.Name), 204, nil)
return nil, 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 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)
return nil, NewError("warnInapplicable", fmt.Sprintf("branch inapplicable: %s", payload.Ref), 204, nil)
}
log.Tracef("Push payload: %+v", payload)
......@@ -372,7 +368,7 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (map
f, err := retrieveFiles(commitsURL, commitSHA, conf)
if err != nil {
log.Errorf("Error retrieving files from commit %s: %+v", commitSHA, err)
return nil, tei2zenodo.NewError("errGHProcessing", fmt.Sprintf("error retrieving files from commit %s: %s", commitSHA, err), 500, err)
return nil, NewError("errGHProcessing", fmt.Sprintf("error retrieving files from commit %s: %s", commitSHA, err), 500, err)
}
for k, v := range f {
......@@ -398,7 +394,7 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (map
}
if len(files) == 0 {
log.Warnf("No files to process")
return nil, tei2zenodo.NewError("warnInapplicable", fmt.Sprintf("no files to process"), 204, nil)
return nil, NewError("warnInapplicable", fmt.Sprintf("no files to process"), 204, nil)
}
return files, nil
}
......@@ -410,12 +406,12 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (map
default:
{
log.Warnf("Unknown hook type %s", hookType)
return nil, tei2zenodo.NewError("errWebhook", fmt.Sprintf("hook type %s not implemented", hookType), 501, nil)
return nil, NewError("errWebhook", fmt.Sprintf("hook type %s not implemented", hookType), 501, nil)
}
}
}
func retrieveFiles(commitsURL string, commitSHA string, conf *tei2zenodo.Config) (map[string]FileInfo, error) {
func retrieveFiles(commitsURL string, commitSHA string, conf *Config) (map[string]FileInfo, error) {
files := make(map[string]FileInfo)
// Compile GET request to retrieve commit information
......@@ -435,7 +431,7 @@ func retrieveFiles(commitsURL string, commitSHA string, conf *tei2zenodo.Config)
resp, err := client.Do(req)
if err != nil {
log.Errorf("Problem sending GET request: %v ...", err)
return nil, tei2zenodo.NewError("errNetComm", fmt.Sprintf("error sending GET request: %s", err.Error()), 500, err)
return nil, NewError("errNetComm", fmt.Sprintf("error sending GET request: %s", err.Error()), 500, err)
}
defer resp.Body.Close()
......@@ -443,11 +439,11 @@ func retrieveFiles(commitsURL string, commitSHA string, conf *tei2zenodo.Config)
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf("Problem reading GET response: %v ...", err)
return nil, tei2zenodo.NewError("errNetComm", fmt.Sprintf("error reading GET request: %s", err.Error()), 500, err)
return nil, NewError("errNetComm", fmt.Sprintf("error reading GET request: %s", err.Error()), 500, err)
}
if strconv.Itoa(resp.StatusCode)[:1] != "2" {
log.Errorf("Problem reported by github: %d %v. %+v ...", resp.StatusCode, err, content)
return nil, tei2zenodo.NewError("errGHProcessing", fmt.Sprintf("problem reported by github: %d %v", resp.StatusCode, err), 500, err)
return nil, NewError("errGHProcessing", fmt.Sprintf("problem reported by github: %d %v", resp.StatusCode, err), 500, err)
}
// Parse response
......@@ -455,7 +451,7 @@ func retrieveFiles(commitsURL string, commitSHA string, conf *tei2zenodo.Config)
err = json.Unmarshal(content, &commit)
if err != nil {
log.Errorf("Problem parsing github's response: %v ...", err)
return nil, tei2zenodo.NewError("errParse", fmt.Sprintf("problem parsing github's response: %s", err), 500, err)
return nil, NewError("errParse", fmt.Sprintf("problem parsing github's response: %s", err), 500, err)
}
// Publish or not?
......@@ -499,7 +495,7 @@ func retrieveFiles(commitsURL string, commitSHA string, conf *tei2zenodo.Config)
// DownloadFile takes a Deposit downloads the file from githubRawURL
// Returns a ReadSeeker and an error value
func DownloadFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config) (r io.ReadSeeker, err *tei2zenodo.Error) {
func DownloadFile(myDeposit *Deposit, conf *Config) (r io.ReadSeeker, err *Error) {
// Compile GET request
targetURI := myDeposit.GithubRawURL
......@@ -508,7 +504,7 @@ func DownloadFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config) (r io.
req, RQErr := http.NewRequest("GET", targetURI, nil)
if RQErr != nil {
log.Errorf("Problem creating GET request: %v ...", RQErr)
return nil, tei2zenodo.NewError("errInternal", fmt.Sprintf("error creating GET request: %s", RQErr.Error()), 500, RQErr)
return nil, NewError("errInternal", fmt.Sprintf("error creating GET request: %s", RQErr.Error()), 500, RQErr)
}
req.Header.Add("Authorization", "Bearer "+conf.Git.Token)
req.Header.Add("User-Agent", "tei2zenodo (https://gitlab.gwdg.de/rg-mpg-de/tei2zenodo/)")
......@@ -518,7 +514,7 @@ func DownloadFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config) (r io.
resp, RSPErr := client.Do(req)
if RSPErr != nil {
log.Errorf("Problem sending GET request: %v ...", RSPErr)
return nil, tei2zenodo.NewError("errNetComm", fmt.Sprintf("error sending GET request: %s", RSPErr.Error()), 500, RSPErr)
return nil, NewError("errNetComm", fmt.Sprintf("error sending GET request: %s", RSPErr.Error()), 500, RSPErr)
}
defer resp.Body.Close()
......@@ -533,7 +529,7 @@ func DownloadFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config) (r io.
// PutFile uploads a file to the github repository and commits
// Returns the commit sha and an error value
func PutFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config, c *gin.Context) (string, error) {
func PutFile(myDeposit *Deposit, conf *Config, c *gin.Context) (string, error) {
githubConf := conf.Git
......@@ -555,7 +551,7 @@ func PutFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config, c *gin.Cont
ref, _, GRErr := client.Git.GetRef(c, ownerName, repoName, "refs/heads/"+branch)
if GRErr != nil {
log.Errorf("Problem getting branch reference: %v ...", GRErr)
return "", tei2zenodo.NewError("errGHProcessing", fmt.Sprintf("problem getting branch reference: %s", GRErr.Error()), 500, GRErr)
return "", NewError("errGHProcessing", fmt.Sprintf("problem getting branch reference: %s", GRErr.Error()), 500, GRErr)
}
oldCommitSHA := ref.Object.SHA
log.Tracef(" Commit to base the new commit on (head of branch %s): %s", branch, oldCommitSHA)
......@@ -564,22 +560,22 @@ func PutFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config, c *gin.Cont
oldCommit, _, OCErr := client.Git.GetCommit(c, ownerName, repoName, *oldCommitSHA)
if OCErr != nil {
log.Errorf("Problem getting old commit: %v ...", OCErr)
return "", tei2zenodo.NewError("errGHProcessing", fmt.Sprintf("problem getting old commit: %s", OCErr.Error()), 500, OCErr)
return "", NewError("errGHProcessing", fmt.Sprintf("problem getting old commit: %s", OCErr.Error()), 500, OCErr)
}
baseTreeSHA := oldCommit.SHA
baseTree, _, GTErr := client.Git.GetTree(c, ownerName, repoName, *baseTreeSHA, true)
if GTErr != nil {
log.Errorf("Problem getting old tree: %v ...", GTErr)
return "", tei2zenodo.NewError("errGHProcessing", fmt.Sprintf("problem getting old tree: %s", GTErr.Error()), 500, GTErr)
return "", NewError("errGHProcessing", fmt.Sprintf("problem getting old tree: %s", GTErr.Error()), 500, GTErr)
}
if *baseTree.Truncated {
log.Errorf("Old tree is too large and has been truncated. This is not supported yet", nil)
return "", tei2zenodo.NewError("errGHProcessing", fmt.Sprintf("old tree is too large and has been truncated. This is not supported yet"), 500, nil)
return "", NewError("errGHProcessing", fmt.Sprintf("old tree is too large and has been truncated. This is not supported yet"), 500, nil)
}
log.Tracef(" Tree object of the old commit: %v", *baseTree)
// 3. create and post new tree object (get new treeSHA)
newEntry := &github.TreeEntry{
newEntry := github.TreeEntry{
Path: github.String(myDeposit.Filename),
Mode: github.String("100644"),
Type: github.String("blob"),
......@@ -588,12 +584,12 @@ func PutFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config, c *gin.Cont
// Size *int `json:"size,omitempty"`
// URL *string `json:"url,omitempty"`
}
entries := []*github.TreeEntry{}
entries := []github.TreeEntry{}
entries = append(entries, newEntry)
newTree, _, NTErr := client.Git.CreateTree(c, ownerName, repoName, *baseTree.SHA, entries)
if NTErr != nil {
log.Errorf("Problem creating new tree: %v ...", NTErr)
return "", tei2zenodo.NewError("errGHProcessing", fmt.Sprintf("problem creating new tree: %s", NTErr.Error()), 500, NTErr)
return "", NewError("errGHProcessing", fmt.Sprintf("problem creating new tree: %s", NTErr.Error()), 500, NTErr)
}
log.Tracef(" New tree object: %v", *newTree)
......@@ -609,11 +605,11 @@ func PutFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config, c *gin.Cont
// date := time.Now()
// author := &github.CommitAuthor{Date: &date, Name: &commitAuthor, Email: &commitAuthorEMail}
// commit := &github.Commit{Author: author, Message: &commitMessage, Tree: newTree, Parents: []*github.Commit{oldCommit}}
commit := &github.Commit{Message: &commitMessage, Tree: newTree, Parents: []*github.Commit{oldCommit}}
commit := &github.Commit{Message: &commitMessage, Tree: newTree, Parents: []github.Commit{*oldCommit}}
newCommit, _, NCErr := client.Git.CreateCommit(c, ownerName, repoName, commit)
if NCErr != nil {
log.Errorf("error creating commit object: %v ...", NCErr)
return "", tei2zenodo.NewError("errGHProcessing", fmt.Sprintf("error creating commit object: %s", NCErr.Error()), 500, NCErr)
return "", NewError("errGHProcessing", fmt.Sprintf("error creating commit object: %s", NCErr.Error()), 500, NCErr)
}
log.Tracef(" New commit object: %v", *newCommit)
......@@ -622,7 +618,7 @@ func PutFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config, c *gin.Cont
_, _, URErr := client.Git.UpdateRef(c, ownerName, repoName, ref, false)
if URErr != nil {
log.Errorf("Problem updating branch reference: %v ...", URErr)
return "", tei2zenodo.NewError("errGHProcessing", fmt.Sprintf("problem updating branch reference: %s", URErr.Error()), 500, URErr)
return "", NewError("errGHProcessing", fmt.Sprintf("problem updating branch reference: %s", URErr.Error()), 500, URErr)
}
log.Tracef(" Updated ref object: %v", *ref)
......
This diff is collapsed.
package logger
package main
import (
"io"
......@@ -6,11 +6,10 @@ import (
"strings"
"github.com/sirupsen/logrus"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
)
// ConfigureLogging will take the logging configuration and also adds a few default parameters.
func ConfigureLogging(config *tei2zenodo.LoggingConfig) (*logrus.Logger, error) {
func ConfigureLogging(config *LoggingConfig) (*logrus.Logger, error) {
/*
hostname, err := os.Hostname()
if err != nil {
......
package main
// tei2zenodo service parses TEI files for metadata and published them on zenodo.
// Files can be fed via REST POST or by calling a webhook that will retrieve the file(s).
import (
"bytes"
"fmt"
......@@ -7,27 +10,21 @@ import (
"strconv"
"strings"
// "github.com/davecgh/go-spew/spew"
"github.com/gin-gonic/autotls"
"github.com/sirupsen/logrus"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/conf"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/logger"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/routing"
)
// Config stores the application's configuration.
var Config tei2zenodo.Config
var log logrus.Logger
func main() {
var Config tei2zenodo.Config
Cerr := conf.Configure(&Config)
var Config Config
Cerr := Configure(&Config)
if Cerr != nil {
log.Fatal(fmt.Errorf("Error during config phase_ %+v", Cerr))
}
log, Lerr := logger.ConfigureLogging(&Config.Log)
log, Lerr := ConfigureLogging(&Config.Log)
if Lerr != nil {
log.Fatal(fmt.Errorf("Error during log setup phase: %+v", Lerr))
}
......@@ -40,7 +37,7 @@ func main() {
}
// Get routes
router := routing.SetupRoutes(Config)
router := SetupRoutes(Config)
// Run server, taking into account different SSL/TLS approaches
switch Config.HTTPS {
......
package routing
package main
import (
"bytes"
......@@ -11,20 +11,13 @@ import (
"net/http"
"strings"
log "github.com/sirupsen/logrus"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/stephenmuss/ginerus"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/github"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/t2zxml"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/zenodo"
)
// SetupRoutes establishes the various API endpoints.
func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
func SetupRoutes(conf Config) *gin.Engine {
// Switch gin to release mode
gin.SetMode(gin.ReleaseMode)
......@@ -57,7 +50,7 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
APIv1.POST(conf.FileAPI, func(c *gin.Context) {
var r io.ReadSeeker
var myDeposit tei2zenodo.Deposit
var myDeposit Deposit
var DOIs []string
if conf.Verbose {
......@@ -81,7 +74,7 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
}
if ztoken == "" {
log.Errorf("POST request without token submitted. Too insecure - ignoring...")
AbortMsg(401, tei2zenodo.NewError("errBadRequest", fmt.Sprintf("POST request without token submitted. Too insecure - ignoring..."), 401, nil), c)
AbortMsg(401, NewError("errBadRequest", fmt.Sprintf("POST request without token submitted. Too insecure - ignoring..."), 401, nil), c)
return
}
conf.Zenodo.Token = ztoken
......@@ -106,7 +99,7 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
file, err := ff.Open()
if err != nil {
log.Errorf("Problem opening form file: %+v", err)
AbortMsg(400, tei2zenodo.NewError("errParse", fmt.Sprintf("problem opening form file: %s", err), 400, err), c)
AbortMsg(400, NewError("errParse", fmt.Sprintf("problem opening form file: %s", err), 400, err), c)
return
}
r = file
......@@ -130,7 +123,7 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
} else {
log.Warnf("File upload request with invalid HMAC signature received, ignoring")
}
AbortMsg(403, tei2zenodo.NewError("errWebhook", fmt.Sprintf("File upload request with invalid HMAC signature received, ignoring"), 403, nil), c)
AbortMsg(403, NewError("errWebhook", fmt.Sprintf("File upload request with invalid HMAC signature received, ignoring"), 403, nil), c)
return
}
log.Debugf(" File upload request HMAC signature verified")
......@@ -140,10 +133,10 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
// If we still have no filename, parse one from the file's contents
if filename == "" {
log.Warnf(" No filename specified. Creating one from the file's content...")
f, GFErr := t2zxml.GetFilename(r)
f, GFErr := GetFilename(r)
if GFErr != nil {
log.Errorf("Problem reading filename: %+v", GFErr)
AbortMsg(500, tei2zenodo.NewError("errParse", fmt.Sprintf("error reading filename: %s", GFErr), 500, GFErr), c)
AbortMsg(500, NewError("errParse", fmt.Sprintf("error reading filename: %s", GFErr), 500, GFErr), c)
return
}
filename = f
......@@ -192,7 +185,7 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
writeDOIotherAttValue = c.Query("writeDOIotherAttValue")
}
if writeDOIotherAttName != "" {
otherAtt := tei2zenodo.AttributeSpec{AttName: writeDOIotherAttName, Value: writeDOIotherAttValue}
otherAtt := AttributeSpec{AttName: writeDOIotherAttName, Value: writeDOIotherAttValue}
conf.WriteDOI.OtherAttributes = append(conf.WriteDOI.OtherAttributes, otherAtt)
}
......@@ -232,20 +225,20 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
// === IV. Get file metadata / Parse TEI file ===
// Parse TEI file
var md tei2zenodo.ZMetadata
doi, PTErr := t2zxml.ParseTEI(r, &md, &conf)
var md ZMetadata
doi, PTErr := ParseTEI(r, &md, &conf)
if PTErr != nil {
switch PTErr.Typ {
case "errNoTEIXML":
{
log.Warnf("Problem with file %s: No TEI file.", myDeposit.Filename)
AbortMsg(400, tei2zenodo.NewError("errZProcessing", fmt.Sprintf("problem with file %s: No TEI file.", myDeposit.Filename), 400, PTErr), c)
AbortMsg(400, NewError("errZProcessing", fmt.Sprintf("problem with file %s: No TEI file.", myDeposit.Filename), 400, PTErr), c)
return
}
default:
{
log.Errorf("Error parsing TEI file: %v", PTErr)
AbortMsg(500, tei2zenodo.NewError("errParse", fmt.Sprintf("error parsing TEI file %s (%s): %s", myDeposit.Filename, myDeposit.GithubObjSHA, PTErr.Error()), 500, PTErr), c)
AbortMsg(500, NewError("errParse", fmt.Sprintf("error parsing TEI file %s (%s): %s", myDeposit.Filename, myDeposit.GithubObjSHA, PTErr.Error()), 500, PTErr), c)
return
}
}
......@@ -258,10 +251,10 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
// === V. Process file: claim/mixin doi, upload file ===
ZPFErr := zenodo.ProcessFile(r, doi, &md, &myDeposit, &conf)
ZPFErr := ProcessFile(r, doi, &md, &myDeposit, &conf)
if ZPFErr != nil {
log.Errorf("Problem processing file %s: %+v", myDeposit.Filename, ZPFErr)
AbortMsg(500, tei2zenodo.NewError("errZProcessing", fmt.Sprintf("problem processing file %s: %s", myDeposit.Filename, ZPFErr.Error()), 500, ZPFErr), c)
AbortMsg(500, NewError("errZProcessing", fmt.Sprintf("problem processing file %s: %s", myDeposit.Filename, ZPFErr.Error()), 500, ZPFErr), c)
return
}
......@@ -314,14 +307,14 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
} else {
log.Warnf("Webhook with invalid HMAC signature received, ignoring")
}
AbortMsg(403, tei2zenodo.NewError("errWebhook", fmt.Sprintf("Webhook with invalid HMAC signature received, ignoring"), 403, nil), c)
AbortMsg(403, NewError("errWebhook", fmt.Sprintf("Webhook with invalid HMAC signature received, ignoring"), 403, nil), c)
return
}
log.Debugf(" Webhook HMAC signature verified")
}
// Process Hook to extract files
files, PHErr := github.ProcessHook(hookType, r, &conf)
files, PHErr := ProcessHook(hookType, r, &conf)
if PHErr != nil {
switch PHErr.Typ {
case "warnInapplicable":
......@@ -333,7 +326,7 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
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)
AbortMsg(500, NewError("errWebhook", fmt.Sprintf("problem parsing webhook for %s event", hookType), 500, PHErr), c)
return
}
}
......@@ -346,7 +339,7 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
for f := range files {
log.Printf("--- Process file '%s' ---", f)
var myDeposit tei2zenodo.Deposit
var myDeposit Deposit
// myDeposit.Filename = strings.Replace(f, "/", "_", -1)
myDeposit.Filename = f
......@@ -357,28 +350,28 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
myDeposit.GithubRawURL = files[f].RawURL
// Get file
r, GDLErr := github.DownloadFile(&myDeposit, &conf)
r, GDLErr := DownloadFile(&myDeposit, &conf)
if GDLErr != nil {
log.Errorf("Problem downloading file %s (%s): %v ...", myDeposit.Filename, myDeposit.GithubObjSHA, GDLErr)
AbortMsg(500, tei2zenodo.NewError("errNetComm", fmt.Sprintf("error downloading file %s (%s): %s", myDeposit.Filename, myDeposit.GithubObjSHA, GDLErr.Error()), 500, GDLErr), c)
AbortMsg(500, NewError("errNetComm", fmt.Sprintf("error downloading file %s (%s): %s", myDeposit.Filename, myDeposit.GithubObjSHA, GDLErr.Error()), 500, GDLErr), c)
return
}
// Parse TEI file
var md tei2zenodo.ZMetadata
doi, PTErr := t2zxml.ParseTEI(r, &md, &conf)
var md ZMetadata
doi, PTErr := ParseTEI(r, &md, &conf)
if PTErr != nil {
switch PTErr.Typ {
case "errNoTEIXML":
{
log.Warnf("Problem with file %s (%s): No TEI file.", myDeposit.Filename, myDeposit.GithubObjSHA)
// AbortMsg(400, tei2zenodo.NewError("errZProcessing", fmt.Sprintf("problem with file %s (%s): No TEI file.", myDeposit.Filename, myDeposit.GithubObjSHA), 400, PTErr), c)
// AbortMsg(400, NewError("errZProcessing", fmt.Sprintf("problem with file %s (%s): No TEI file.", myDeposit.Filename, myDeposit.GithubObjSHA), 400, PTErr), c)
continue
}
default:
{
log.Errorf("Error parsing TEI file: %v", PTErr)
AbortMsg(500, tei2zenodo.NewError("errParse", fmt.Sprintf("error parsing TEI file %s (%s): %s", myDeposit.Filename, myDeposit.GithubObjSHA, PTErr.Error()), 500, PTErr), c)
AbortMsg(500, NewError("errParse", fmt.Sprintf("error parsing TEI file %s (%s): %s", myDeposit.Filename, myDeposit.GithubObjSHA, PTErr.Error()), 500, PTErr), c)
return
}
}
......@@ -390,18 +383,18 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
myDeposit.Changed = false