Skip to content

Commit c0dc83b

Browse files
Merge pull request #1378 from Devils-Knight/fix-dependabot
Create or update dependabot config based on input
2 parents ed7272f + 7ca8084 commit c0dc83b

11 files changed

+116
-49
lines changed

dependabotconfig.go

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bufio"
5+
"encoding/json"
56
"strings"
67

78
dependabot "github.com/paulvollmer/dependabot-config-go"
@@ -15,40 +16,69 @@ type UpdateDependabotConfigResponse struct {
1516
ConfigfileFetchError bool
1617
}
1718

19+
type Ecosystem struct {
20+
PackageEcosystem string
21+
Directory string
22+
Interval string
23+
}
24+
25+
type UpdateDependabotConfigRequest struct {
26+
Ecosystems []Ecosystem
27+
Content string
28+
}
29+
1830
func UpdateDependabotConfig(dependabotConfig string) (*UpdateDependabotConfigResponse, error) {
19-
inputConfigFile := []byte(dependabotConfig)
31+
var updateDependabotConfigRequest UpdateDependabotConfigRequest
32+
json.Unmarshal([]byte(dependabotConfig), &updateDependabotConfigRequest)
33+
inputConfigFile := []byte(updateDependabotConfigRequest.Content)
2034
configMetadata := dependabot.New()
2135
err := configMetadata.Unmarshal(inputConfigFile)
2236
if err != nil {
2337
return nil, err
2438
}
2539

2640
response := new(UpdateDependabotConfigResponse)
27-
response.FinalOutput = dependabotConfig
28-
response.OriginalInput = dependabotConfig
41+
response.FinalOutput = updateDependabotConfigRequest.Content
42+
response.OriginalInput = updateDependabotConfigRequest.Content
2943
response.IsChanged = false
3044

31-
if !configMetadata.HasPackageEcosystem("github-actions") {
32-
item := dependabot.Update{}
33-
item.PackageEcosystem = "github-actions"
34-
item.Directory = "/"
45+
if updateDependabotConfigRequest.Content == "" {
46+
if len(updateDependabotConfigRequest.Ecosystems) == 0 {
47+
return response, nil
48+
}
49+
response.FinalOutput = "version: 2\nupdates:"
50+
} else {
51+
response.FinalOutput += "\n"
52+
}
53+
for _, Update := range updateDependabotConfigRequest.Ecosystems {
54+
updateAlreadyExist := false
55+
for _, update := range configMetadata.Updates {
56+
if update.PackageEcosystem == Update.PackageEcosystem && update.Directory == Update.Directory {
57+
updateAlreadyExist = true
58+
break
59+
}
60+
}
61+
if !updateAlreadyExist {
62+
item := dependabot.Update{}
63+
item.PackageEcosystem = Update.PackageEcosystem
64+
item.Directory = Update.Directory
3565

36-
schedule := dependabot.Schedule{}
37-
schedule.Interval = "daily"
66+
schedule := dependabot.Schedule{}
67+
schedule.Interval = Update.Interval
3868

39-
item.Schedule = schedule
40-
items := []dependabot.Update{}
41-
items = append(items, item)
42-
addedItem, err := yaml.Marshal(items)
43-
data := string(addedItem)
69+
item.Schedule = schedule
70+
items := []dependabot.Update{}
71+
items = append(items, item)
72+
addedItem, err := yaml.Marshal(items)
73+
data := string(addedItem)
4474

45-
data = addIndentation(data)
46-
if err != nil {
47-
return nil, err
75+
data = addIndentation(data)
76+
if err != nil {
77+
return nil, err
78+
}
79+
response.FinalOutput = response.FinalOutput + data
80+
response.IsChanged = true
4881
}
49-
50-
response.FinalOutput = response.FinalOutput + "\n" + data
51-
response.IsChanged = true
5282
}
5383

5484
return response, nil

dependabotconfig_test.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"io/ioutil"
56
"log"
67
"path"
@@ -13,27 +14,51 @@ func TestConfigDependabotFile(t *testing.T) {
1314
const outputDirectory = "./testfiles/dependabotfiles/output"
1415

1516
tests := []struct {
16-
fileName string
17-
isChanged bool
17+
fileName string
18+
Ecosystems []Ecosystem
19+
isChanged bool
1820
}{
19-
{fileName: "DependabotFile-without-github-action.yml", isChanged: true},
20-
{fileName: "DependabotFile-with-github-action.yml", isChanged: false},
21+
{
22+
fileName: "Without-github-action.yml",
23+
Ecosystems: []Ecosystem{{"github-actions", "/", "daily"}, {"npm", "/app", "daily"}},
24+
isChanged: true,
25+
},
26+
{
27+
fileName: "With-github-action.yml",
28+
Ecosystems: []Ecosystem{{"github-actions", "/", "daily"}},
29+
isChanged: false,
30+
},
31+
{
32+
fileName: "File-not-exit.yml",
33+
Ecosystems: []Ecosystem{{"github-actions", "/", "daily"}},
34+
isChanged: true,
35+
},
36+
{
37+
fileName: "Same-ecosystem-different-directory.yml",
38+
Ecosystems: []Ecosystem{{"github-actions", "/", "daily"}, {"npm", "/sample", "daily"}},
39+
isChanged: true,
40+
},
2141
}
2242

2343
for _, test := range tests {
24-
44+
var updateDependabotConfigRequest UpdateDependabotConfigRequest
2545
input, err := ioutil.ReadFile(path.Join(inputDirectory, test.fileName))
2646
if err != nil {
2747
log.Fatal(err)
2848
}
49+
updateDependabotConfigRequest.Content = string(input)
50+
updateDependabotConfigRequest.Ecosystems = test.Ecosystems
51+
inputRequest, err := json.Marshal(updateDependabotConfigRequest)
52+
if err != nil {
53+
log.Fatal(err)
54+
}
2955

30-
output, err := UpdateDependabotConfig(string(input))
56+
output, err := UpdateDependabotConfig(string(inputRequest))
3157
if err != nil {
3258
t.Fatalf("Error not expected: %s", err)
3359
}
3460

3561
expectedOutput, err := ioutil.ReadFile(path.Join(outputDirectory, test.fileName))
36-
3762
if err != nil {
3863
log.Fatal(err)
3964
}

main.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,28 +183,10 @@ func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
183183

184184
if strings.Contains(httpRequest.RawPath, "/update-dependabot-config") {
185185

186-
configFile := ""
187-
queryStringParams := httpRequest.QueryStringParameters
188-
// if owner is set, assuming that repo, path are also set
189-
// get the dockerfile using API
190-
if _, ok := queryStringParams["owner"]; ok {
191-
configFile, err = GetGitHubWorkflowContents(httpRequest.QueryStringParameters)
192-
if err != nil {
193-
fixResponse := &UpdateDependabotConfigResponse{ConfigfileFetchError: true}
194-
output, _ := json.Marshal(fixResponse)
195-
response = events.APIGatewayProxyResponse{
196-
StatusCode: http.StatusOK,
197-
Body: string(output),
198-
}
199-
returnValue, _ := json.Marshal(&response)
200-
return returnValue, nil
201-
}
202-
} else {
203-
// if owner is not set, then dockerfile should be sent in the body
204-
configFile = httpRequest.Body
205-
}
186+
updateDependabotConfigRequest := ""
187+
updateDependabotConfigRequest = httpRequest.Body
206188

207-
fixResponse, err := UpdateDependabotConfig(configFile)
189+
fixResponse, err := UpdateDependabotConfig(updateDependabotConfigRequest)
208190
if err != nil {
209191
response = events.APIGatewayProxyResponse{
210192
StatusCode: http.StatusInternalServerError,

testfiles/dependabotfiles/input/File-not-exit.yml

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "npm"
4+
# Files stored in `app` directory
5+
directory: "/app"
6+
schedule:
7+
interval: "daily"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: github-actions
4+
directory: /
5+
schedule:
6+
interval: daily
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "npm"
4+
# Files stored in `app` directory
5+
directory: "/app"
6+
schedule:
7+
interval: "daily"
8+
9+
- package-ecosystem: github-actions
10+
directory: /
11+
schedule:
12+
interval: daily
13+
14+
- package-ecosystem: npm
15+
directory: /sample
16+
schedule:
17+
interval: daily

testfiles/dependabotfiles/output/DependabotFile-with-github-action.yml renamed to testfiles/dependabotfiles/output/With-github-action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ updates:
1111
# default location of `.github/workflows`
1212
directory: "/"
1313
schedule:
14-
interval: "daily"
14+
interval: "daily"

0 commit comments

Comments
 (0)