Skip to content

Commit

Permalink
change dependabot daily checks to monthly
Browse files Browse the repository at this point in the history
  • Loading branch information
ArloL committed Sep 14, 2024
1 parent 0836290 commit 43f64bd
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 124 deletions.
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ updates:
- package-ecosystem: "maven"
directory: "/"
schedule:
interval: "daily"
interval: "monthly"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
interval: "monthly"
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ public ChoreContext doit(ChoreContext context) {
template.removeEnv();
}

template.setKeyToSequence(
"jobs.analyze.strategy.matrix.language",
languages
);
template.setJobMatrixKey("analyze", "language", languages);

FilesSilent.writeString(codeqlWorkflow, template.asString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public ChoreContext doit(ChoreContext context) {
addEcosystemIfFileExists(".terraform.lock.hcl", "terraform", context);
addCompositeGitHubActions(context);

dependabotConfigFile.changeDailyScheduleToMonthly();

FilesSilent.writeString(dependabotYml, dependabotConfigFile.asString());

return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import static io.github.arlol.chorito.tools.Yamls.getKeyAsNode;
import static io.github.arlol.chorito.tools.Yamls.getKeyAsSequence;
import static io.github.arlol.chorito.tools.Yamls.getYamlPath;
import static io.github.arlol.chorito.tools.Yamls.newMap;
import static io.github.arlol.chorito.tools.Yamls.newScalar;
import static io.github.arlol.chorito.tools.Yamls.newSequence;
import static io.github.arlol.chorito.tools.Yamls.newTuple;
import static io.github.arlol.chorito.tools.Yamls.nodeAsMap;
import static io.github.arlol.chorito.tools.Yamls.scalarValue;
import static io.github.arlol.chorito.tools.Yamls.setKey;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import org.snakeyaml.engine.v2.nodes.MappingNode;
import org.snakeyaml.engine.v2.nodes.Node;
import org.snakeyaml.engine.v2.nodes.SequenceNode;

Expand All @@ -21,7 +26,7 @@ public class DependabotConfigFile {
public DependabotConfigFile() {
root = Optional.of(
newMap(
newTuple("version", 2),
newTuple("version", newScalar(2)),
newTuple("updates", newSequence())
)
);
Expand All @@ -35,8 +40,14 @@ public String asString() {
return Yamls.asString(root);
}

private Optional<SequenceNode> getUpdates() {
return getKeyAsSequence(nodeAsMap(root), "updates");
private List<Node> getUpdates() {
return getKeyAsSequence(nodeAsMap(root), "updates")
.map(SequenceNode::getValue)
.orElseThrow();
}

private Stream<MappingNode> getUpdatesAsMappingNode() {
return getUpdates().stream().map(node -> nodeAsMap(node));
}

public boolean hasEcosystemWithDirectory(
Expand All @@ -45,22 +56,18 @@ public boolean hasEcosystemWithDirectory(
) {
var directoryWithoutTrailingSlash = directory
.substring(0, directory.length() - 1);
return getUpdates().map(SequenceNode::getValue)
.orElse(List.of())
.stream()
.map(node -> nodeAsMap(node))
.anyMatch(step -> {
return scalarValue(getKeyAsNode(step, "package-ecosystem"))
.filter(value -> packageEcosystem.equals(value))
.isPresent()
&& scalarValue(
getKeyAsNode(step, "directory")
).filter(
return getUpdatesAsMappingNode().anyMatch(step -> {
return scalarValue(getKeyAsNode(step, "package-ecosystem"))
.filter(value -> packageEcosystem.equals(value))
.isPresent()
&& scalarValue(getKeyAsNode(step, "directory"))
.filter(
value -> directory.equals(value)
|| directoryWithoutTrailingSlash
.equals(value)
).isPresent();
});
)
.isPresent();
});
}

public void addEcosystemInDirectory(String ecosystem, String directory) {
Expand All @@ -69,14 +76,21 @@ public void addEcosystemInDirectory(String ecosystem, String directory) {
}

var nodes = List.of(
newTuple("package-ecosystem", ecosystem),
newTuple("directory", directory),
newTuple("schedule", newMap(newTuple("interval", "daily")))
newTuple("package-ecosystem", newScalar(ecosystem)),
newTuple("directory", newScalar(directory)),
newTuple(
"schedule",
newMap(newTuple("interval", newScalar("monthly")))
)
);
var updates = getUpdates();
updates.map(SequenceNode::getValue)
.ifPresent(list -> list.add(newMap(nodes)));
getUpdates().add(newMap(nodes));
}

public void changeDailyScheduleToMonthly() {
getYamlPath(root.orElseThrow(), "/updates/schedule[interval=daily]")
.forEach(node -> {
setKey(nodeAsMap(node), "interval", newScalar("monthly"));
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
import static io.github.arlol.chorito.tools.Yamls.getKeyAsMap;
import static io.github.arlol.chorito.tools.Yamls.getKeyAsNode;
import static io.github.arlol.chorito.tools.Yamls.getKeyAsSequence;
import static io.github.arlol.chorito.tools.Yamls.getKeyAsTuple;
import static io.github.arlol.chorito.tools.Yamls.getYamlPath;
import static io.github.arlol.chorito.tools.Yamls.newScalar;
import static io.github.arlol.chorito.tools.Yamls.newSequence;
import static io.github.arlol.chorito.tools.Yamls.nodeAsMap;
import static io.github.arlol.chorito.tools.Yamls.removeKey;
import static io.github.arlol.chorito.tools.Yamls.scalarValue;
import static io.github.arlol.chorito.tools.Yamls.setKey;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.snakeyaml.engine.v2.api.DumpSettings;
import org.snakeyaml.engine.v2.api.LoadSettings;
import org.snakeyaml.engine.v2.common.FlowStyle;
import org.snakeyaml.engine.v2.common.ScalarStyle;
import org.snakeyaml.engine.v2.composer.Composer;
import org.snakeyaml.engine.v2.emitter.Emitter;
Expand Down Expand Up @@ -100,18 +101,12 @@ public Optional<SequenceNode> getOnSchedule() {
}

public void setOnScheduleCron(String newCron) {
var onSchedule = getOnSchedule();
var firstScheduleNode = nodeAsMap(
onSchedule.map(SequenceNode::getValue).map(l -> l.get(0))
);
var tuple = getKeyAsTuple(firstScheduleNode, "cron").orElseThrow();
var scalarNode = new ScalarNode(
Tag.STR,
newCron,
ScalarStyle.SINGLE_QUOTED
setKey(
nodeAsMap(getYamlPath(root.orElseThrow(), "/on/schedule/0"))
.getFirst(),
"cron",
newScalar(newCron, ScalarStyle.SINGLE_QUOTED)
);
NodeTuple nodeTuple = new NodeTuple(tuple.getKeyNode(), scalarNode);
firstScheduleNode.orElseThrow().setValue(List.of(nodeTuple));
}

public Optional<MappingNode> getEnv() {
Expand All @@ -127,7 +122,7 @@ public void updatePermissionsFromTemplate(
) {
for (NodeTuple jobTuple : getJobs().map(MappingNode::getValue)
.orElse(List.of())) {
String jobName = scalarValue(jobTuple.getKeyNode());
String jobName = scalarValue(jobTuple.getKeyNode()).orElseThrow();
var job = nodeAsMap(jobTuple.getValueNode());
if (template.hasJob(jobName)) {
var templatePermissions = getKeyAsMap(
Expand Down Expand Up @@ -158,7 +153,7 @@ public void updatePermissionsFromTemplate(
.get(index);
String detailKey = scalarValue(
jobDetailsTuple.getKeyNode()
);
).orElseThrow();
if ("runs-on".equals(detailKey)) {
continue;
}
Expand Down Expand Up @@ -193,27 +188,23 @@ public void removeActionFromJob(String jobName, String actionName) {
.isEmpty();
})
.toList();
setKey(
jobNode,
"steps",
new SequenceNode(Tag.SEQ, nodes, FlowStyle.BLOCK)
);
setKey(jobNode.orElseThrow(), "steps", newSequence(nodes));
}

public void setKeyToSequence(String yamlpath, List<String> sequence) {
List<String> split = Arrays.asList(yamlpath.split("\\."));
Optional<MappingNode> parentNode = nodeAsMap(root);
for (int i = 0; i < split.size() - 1; i++) {
String key = split.get(i);
parentNode = getKeyAsMap(parentNode, key);
}
List<Node> nodes = sequence.stream().map(value -> {
return (Node) new ScalarNode(Tag.STR, value, ScalarStyle.PLAIN);
}).toList();
public void setJobMatrixKey(String job, String key, List<String> values) {
List<Node> nodes = new ArrayList<>();
values.stream()
.map(value -> newScalar(value, ScalarStyle.PLAIN))
.forEach(scalar -> nodes.add(scalar));
setKey(
parentNode,
split.get(split.size() - 1),
new SequenceNode(Tag.SEQ, nodes, FlowStyle.BLOCK)
nodeAsMap(
Yamls.getYamlPath(
getJob(job).orElseThrow(),
"/strategy/matrix"
)
).getFirst(),
key,
newSequence(nodes)
);
}

Expand Down
Loading

0 comments on commit 43f64bd

Please sign in to comment.