-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobation.go
169 lines (146 loc) · 5.89 KB
/
probation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"os"
"encoding/json"
"fmt"
"path/filepath"
"time"
"errors"
"net/http"
)
func baseProbationHandler(reqpath string, globals *globalConfiguration, approve bool) error {
incoming := struct {
Project *string `json:"project"`
Asset *string `json:"asset"`
Version *string `json:"version"`
}{}
{
handle, err := os.ReadFile(reqpath)
if err != nil {
return fmt.Errorf("failed to read %q; %w", reqpath, err)
}
err = json.Unmarshal(handle, &incoming)
if err != nil {
return newHttpError(http.StatusBadRequest, fmt.Errorf("failed to parse JSON from %q; %w", reqpath, err))
}
err = isMissingOrBadName(incoming.Project)
if err != nil {
return newHttpError(http.StatusBadRequest, fmt.Errorf("invalid 'project' property in %q; %w", reqpath, err))
}
err = isMissingOrBadName(incoming.Asset)
if err != nil {
return newHttpError(http.StatusBadRequest, fmt.Errorf("invalid 'asset' property in %q; %w", reqpath, err))
}
err = isMissingOrBadName(incoming.Version)
if err != nil {
return newHttpError(http.StatusBadRequest, fmt.Errorf("invalid 'version' property in %q; %w", reqpath, err))
}
}
username, err := identifyUser(reqpath)
if err != nil {
return fmt.Errorf("failed to find owner of %q; %w", reqpath, err)
}
project := *(incoming.Project)
project_dir := filepath.Join(globals.Registry, project)
err = globals.Locks.LockDirectory(project_dir, 10 * time.Second)
if err != nil {
return fmt.Errorf("failed to lock project directory %q; %w", project_dir, err)
}
defer globals.Locks.Unlock(project_dir)
existing, err := readPermissions(project_dir)
if err != nil {
return fmt.Errorf("failed to read permissions for %q; %w", project_dir, err)
}
if !isAuthorizedToMaintain(username, globals.Administrators, existing.Owners) {
return newHttpError(http.StatusForbidden, fmt.Errorf("user %q is not authorized to modify probation status in %q", username, project_dir))
}
asset_dir := filepath.Join(project_dir, *(incoming.Asset))
version_dir := filepath.Join(asset_dir, *(incoming.Version))
summ, err := readSummary(version_dir)
if err != nil {
return fmt.Errorf("failed to read the version summary at %q; %w", version_dir, err)
}
if !summ.IsProbational() {
return fmt.Errorf("version directory at %q is not on probation", version_dir)
}
if approve {
summ.OnProbation = nil
summary_path := filepath.Join(version_dir, summaryFileName)
err = dumpJson(summary_path, &summ)
if err != nil {
return fmt.Errorf("failed to update the version summary at %q; %w", summary_path, err)
}
latest, err := readLatest(asset_dir)
overwrite_latest := false
if err == nil {
latest_version := filepath.Join(asset_dir, latest.Version)
latest_summ, err := readSummary(latest_version)
if err != nil {
return fmt.Errorf("failed to read the latest version summary for %q; %w", latest_version, err)
}
finish_time, err := time.Parse(time.RFC3339, summ.UploadFinish)
if err != nil {
return fmt.Errorf("failed to parse the upload finish time at %q; %w", summary_path, err)
}
latest_time, err := time.Parse(time.RFC3339, latest_summ.UploadFinish)
if err != nil {
return fmt.Errorf("failed to read the latest version's upload finish time from %q; %w", latest_version, err)
}
overwrite_latest = finish_time.After(latest_time)
} else if errors.Is(err, os.ErrNotExist) {
overwrite_latest = true
latest = &latestMetadata{}
} else {
return fmt.Errorf("failed to read the latest version for %s; %w", asset_dir, err)
}
if overwrite_latest {
latest_path := filepath.Join(asset_dir, latestFileName)
latest.Version = *(incoming.Version)
err := dumpJson(latest_path, latest)
if err != nil {
return fmt.Errorf("failed to update the latest version at %q; %w", latest_path, err)
}
}
// Adding a log.
log_info := map[string]interface{} {
"type": "add-version",
"project": project,
"asset": *(incoming.Asset),
"version": *(incoming.Version),
"latest": overwrite_latest,
}
err = dumpLog(globals.Registry, &log_info)
if err != nil {
return fmt.Errorf("failed to save log file; %w", err)
}
} else {
freed, err := computeUsage(version_dir, true)
if err != nil {
return fmt.Errorf("failed to compute usage for %q; %w", version_dir, err)
}
err = os.RemoveAll(version_dir)
if err != nil {
return fmt.Errorf("failed to delete %q; %w", version_dir, err)
}
usage, err := readUsage(project_dir)
if err != nil {
return fmt.Errorf("failed to read the usage statistics for %q; %w", project_dir, err)
}
usage.Total -= freed
if usage.Total < 0 { // just in case.
usage.Total = 0
}
usage_path := filepath.Join(project_dir, usageFileName)
err = dumpJson(usage_path, &usage)
if err != nil {
return fmt.Errorf("failed to update project usage at %q; %w", usage_path, err)
}
}
return nil
}
func approveProbationHandler(reqpath string, globals *globalConfiguration) error {
return baseProbationHandler(reqpath, globals, true)
}
func rejectProbationHandler(reqpath string, globals *globalConfiguration) error {
return baseProbationHandler(reqpath, globals, false)
}