Skip to content

Commit c9e8145

Browse files
committed
Don't use index source file for URL duplication check
It's possible to accomplish the same thing in a more simple and secure manner by using the submission list.
1 parent 999ec32 commit c9e8145

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

main.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ type submissionType struct {
7878
var baseRef = flag.String("baseref", "", "")
7979
var repoPath = flag.String("repopath", "", "")
8080
var listName = flag.String("listname", "", "")
81-
var indexSourcePath = flag.String("indexsourcepath", "", "")
8281

8382
func main() {
8483
// Validate flag input.
@@ -105,19 +104,6 @@ func main() {
105104
errorExit(fmt.Sprintf("list file %s not found", listPath))
106105
}
107106

108-
if *indexSourcePath == "" {
109-
errorExit("--indexsourcepath flag is required")
110-
}
111-
112-
indexSourcePath := paths.New(*indexSourcePath)
113-
exist, err = indexSourcePath.ExistCheck()
114-
if err != nil {
115-
panic(err)
116-
}
117-
if !exist {
118-
errorExit(fmt.Sprintf("index source file %s not found", indexSourcePath))
119-
}
120-
121107
// Get the PR diff.
122108
err = os.Chdir(*repoPath)
123109
if err != nil {
@@ -137,7 +123,7 @@ func main() {
137123
// Process the submissions.
138124
var indexEntries []string
139125
for _, submissionURL := range submissionURLs {
140-
submission, indexEntry := populateSubmission(submissionURL, indexSourcePath)
126+
submission, indexEntry := populateSubmission(submissionURL, listPath)
141127
req.Submissions = append(req.Submissions, submission)
142128
indexEntries = append(indexEntries, indexEntry)
143129
}
@@ -216,7 +202,7 @@ func parseDiff(rawDiff []byte, listName string) (string, []string) {
216202
}
217203

218204
// populateSubmission does the checks on the submission that aren't provided by Arduino Lint and gathers the necessary data on it.
219-
func populateSubmission(submissionURL string, indexSourcePath *paths.Path) (submissionType, string) {
205+
func populateSubmission(submissionURL string, listPath *paths.Path) (submissionType, string) {
220206
indexSourceSeparator := "|"
221207
var submission submissionType
222208

@@ -256,11 +242,24 @@ func populateSubmission(submissionURL string, indexSourcePath *paths.Path) (subm
256242
}
257243

258244
// Check if the URL is already in the index.
259-
indexSourceLines, err := indexSourcePath.ReadFileAsLines()
260-
for _, indexSourceLine := range indexSourceLines {
261-
if strings.HasPrefix(indexSourceLine, fmt.Sprintf("%s%s", normalizedURLObject.String(), indexSourceSeparator)) {
262-
submission.Error = "Submission URL is already in the Library Manager index."
263-
return submission, ""
245+
listLines, err := listPath.ReadFileAsLines()
246+
occurrences := 0
247+
for _, listURL := range listLines {
248+
listURLObject, err := url.Parse(strings.TrimSpace(listURL))
249+
if err != nil {
250+
panic(err) // All list items have already passed parsing so something is broken if this happens.
251+
}
252+
normalizedListURLObject, err := normalizeURL(listURLObject)
253+
if err != nil {
254+
panic(fmt.Errorf("When processing list item %s: %s", listURL, err)) // All list items have already been through normalization without error so something is broken if this happens.
255+
}
256+
257+
if normalizedListURLObject.String() == normalizedURLObject.String() {
258+
occurrences++
259+
if occurrences > 1 {
260+
submission.Error = "Submission URL is already in the Library Manager index."
261+
return submission, ""
262+
}
264263
}
265264
}
266265

0 commit comments

Comments
 (0)