Skip to content

Commit

Permalink
Add Repo+Labels to teams
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Nov 9, 2024
1 parent 3eecc52 commit eaf08f2
Show file tree
Hide file tree
Showing 16 changed files with 489 additions and 65 deletions.
101 changes: 82 additions & 19 deletions server/application-server/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,59 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TeamInfo"
/admin/teams/{teamId}/repository/{repositoryOwner}/{repositoryName}:
post:
tags:
- admin
operationId: addRepositoryToTeam
parameters:
- name: teamId
in: path
required: true
schema:
type: integer
format: int64
- name: repositoryOwner
in: path
required: true
schema:
type: string
- name: repositoryName
in: path
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/TeamInfo"
/admin/teams/{teamId}/label/{label}:
post:
tags:
- admin
operationId: addLabelToTeam
parameters:
- name: teamId
in: path
required: true
schema:
type: integer
format: int64
- name: label
in: path
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/TeamInfo"
/admin/config/repositories:
post:
tags:
Expand Down Expand Up @@ -351,6 +404,25 @@ components:
type: array
items:
$ref: "#/components/schemas/PullRequestInfo"
RepositoryInfo:
required:
- htmlUrl
- id
- name
- nameWithOwner
type: object
properties:
id:
type: integer
format: int64
name:
type: string
nameWithOwner:
type: string
description:
type: string
htmlUrl:
type: string
PullRequestReviewInfo:
required:
- codeComments
Expand Down Expand Up @@ -384,25 +456,6 @@ components:
submittedAt:
type: string
format: date-time
RepositoryInfo:
required:
- htmlUrl
- id
- name
- nameWithOwner
type: object
properties:
id:
type: integer
format: int64
name:
type: string
nameWithOwner:
type: string
description:
type: string
htmlUrl:
type: string
UserTeams:
required:
- id
Expand Down Expand Up @@ -491,7 +544,9 @@ components:
required:
- color
- id
- labels
- name
- repositories
type: object
properties:
id:
Expand All @@ -501,6 +556,14 @@ components:
type: string
color:
type: string
repositories:
type: array
items:
$ref: "#/components/schemas/RepositoryInfo"
labels:
type: array
items:
$ref: "#/components/schemas/LabelInfo"
LeaderboardEntry:
required:
- numberOfApprovals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ public ResponseEntity<TeamInfoDTO> createTeam(@RequestBody TeamInfoDTO team) {
return ResponseEntity.ok(adminService.createTeam(team.name(), team.color()));
}

@PostMapping("/teams/{teamId}/repository/{repositoryOwner}/{repositoryName}")
public ResponseEntity<TeamInfoDTO> addRepositoryToTeam(@PathVariable Long teamId, @PathVariable String repositoryOwner, @PathVariable String repositoryName) {
return adminService.addRepositoryToTeam(teamId, repositoryOwner + '/' + repositoryName)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}

@PostMapping("/teams/{teamId}/label/{label}")
public ResponseEntity<TeamInfoDTO> addLabelToTeam(@PathVariable Long teamId, @PathVariable String label) {
return adminService.addLabelToTeam(teamId, label)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}

@DeleteMapping("/teams/{teamId}")
public ResponseEntity<TeamInfoDTO> deleteTeam(@PathVariable Long teamId) {
return adminService.deleteTeam(teamId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package de.tum.in.www1.hephaestus.admin;

import de.tum.in.www1.hephaestus.gitprovider.label.Label;
import de.tum.in.www1.hephaestus.gitprovider.label.LabelRepository;
import de.tum.in.www1.hephaestus.gitprovider.repository.Repository;
import de.tum.in.www1.hephaestus.gitprovider.repository.RepositoryRepository;
import de.tum.in.www1.hephaestus.gitprovider.team.Team;
import de.tum.in.www1.hephaestus.gitprovider.team.TeamInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.team.TeamService;
import de.tum.in.www1.hephaestus.gitprovider.user.User;
import de.tum.in.www1.hephaestus.gitprovider.user.UserInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.user.UserRepository;
import de.tum.in.www1.hephaestus.gitprovider.user.UserTeamsDTO;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -14,25 +24,26 @@
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import de.tum.in.www1.hephaestus.gitprovider.team.Team;
import de.tum.in.www1.hephaestus.gitprovider.team.TeamInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.team.TeamService;
import de.tum.in.www1.hephaestus.gitprovider.user.User;
import de.tum.in.www1.hephaestus.gitprovider.user.UserInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.user.UserRepository;
import de.tum.in.www1.hephaestus.gitprovider.user.UserTeamsDTO;

@Service
public class AdminService {

private static final Logger logger = LoggerFactory.getLogger(AdminService.class);

@Autowired
private AdminRepository adminRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private TeamService teamService;

@Autowired
private RepositoryRepository repositoryRepository;

@Autowired
private LabelRepository labelRepository;

@Value("${monitoring.repositories}")
private String[] repositoriesToMonitor;

Expand Down Expand Up @@ -122,6 +133,38 @@ public TeamInfoDTO createTeam(String name, String color) {
return TeamInfoDTO.fromTeam(teamService.createTeam(name, color));
}

public Optional<TeamInfoDTO> addRepositoryToTeam(Long teamId, String repositoryName) {
logger.info("Adding repository with name: " + repositoryName + " to team with ID: " + teamId);
Optional<Team> optionalTeam = teamService.getTeam(teamId);
if (optionalTeam.isEmpty()) {
return Optional.empty();
}
Team team = optionalTeam.get();
Repository repository = repositoryRepository.findByNameWithOwner(repositoryName);
if (repository == null) {
return Optional.empty();
}
team.addRepository(repository);
teamService.saveTeam(team);
return Optional.of(TeamInfoDTO.fromTeam(team));
}

public Optional<TeamInfoDTO> addLabelToTeam(Long teamId, String label) {
logger.info("Adding label '" + label + "' to team with ID: " + teamId);
Optional<Team> optionalTeam = teamService.getTeam(teamId);
if (optionalTeam.isEmpty()) {
return Optional.empty();
}
Team team = optionalTeam.get();
Optional<Label> labelEntity = labelRepository.findByName(label);
if (labelEntity.isEmpty()) {
return Optional.empty();
}
team.addLabel(labelEntity.get());
teamService.saveTeam(team);
return Optional.of(TeamInfoDTO.fromTeam(team));
}

public Optional<TeamInfoDTO> deleteTeam(Long teamId) {
logger.info("Deleting team with ID: " + teamId);
Optional<Team> optionalTeam = teamService.getTeam(teamId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import de.tum.in.www1.hephaestus.gitprovider.issue.Issue;
import de.tum.in.www1.hephaestus.gitprovider.repository.Repository;
import de.tum.in.www1.hephaestus.gitprovider.team.Team;

@Entity
@Table(name = "label")
Expand Down Expand Up @@ -47,6 +48,10 @@ public class Label {
@JoinColumn(name = "repository_id")
@ToString.Exclude
private Repository repository;

@ManyToMany(mappedBy = "labels")
@ToString.Exclude
private Set<Team> teams = new HashSet<>();

// Ignored GitHub properties:
// - default
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package de.tum.in.www1.hephaestus.gitprovider.label;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LabelRepository extends JpaRepository<Label, Long> {
Optional<Label> findByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.Getter;
Expand All @@ -21,6 +22,7 @@
import de.tum.in.www1.hephaestus.gitprovider.issue.Issue;
import de.tum.in.www1.hephaestus.gitprovider.label.Label;
import de.tum.in.www1.hephaestus.gitprovider.milestone.Milestone;
import de.tum.in.www1.hephaestus.gitprovider.team.Team;

@Entity
@Table(name = "repository")
Expand Down Expand Up @@ -82,6 +84,10 @@ public class Repository extends BaseGitServiceEntity {
@OneToMany(mappedBy = "repository", cascade = CascadeType.REMOVE, orphanRemoval = true)
@ToString.Exclude
private Set<Milestone> milestones = new HashSet<>();

@ManyToMany(mappedBy = "repositories")
@ToString.Exclude
private Set<Team> teams = new HashSet<>();

public enum Visibility {
PUBLIC, PRIVATE, INTERNAL, UNKNOWN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import java.util.Set;

import org.springframework.lang.NonNull;

import de.tum.in.www1.hephaestus.gitprovider.label.Label;
import de.tum.in.www1.hephaestus.gitprovider.repository.Repository;
import de.tum.in.www1.hephaestus.gitprovider.user.User;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -38,6 +39,16 @@ public class Team {
@NonNull
private String color;

@ManyToMany
@JoinTable(name = "team_repositories", joinColumns = @JoinColumn(name = "team_id"), inverseJoinColumns = @JoinColumn(name = "repository_id"))
@ToString.Exclude
private Set<Repository> repositories = new HashSet<>();

@ManyToMany
@JoinTable(name = "team_labels", joinColumns = @JoinColumn(name = "team_id"), inverseJoinColumns = @JoinColumn(name = "label_id"))
@ToString.Exclude
private Set<Label> labels = new HashSet<>();

@ManyToMany
@JoinTable(name = "team_members", joinColumns = @JoinColumn(name = "team_id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
@ToString.Exclude
Expand All @@ -52,4 +63,20 @@ public void removeMember(User user) {
members.remove(user);
user.removeTeam(this);
}

public void addRepository(Repository repository) {
repositories.add(repository);
}

public void removeRepository(Repository repository) {
repositories.remove(repository);
}

public void addLabel(Label label) {
labels.add(label);
}

public void removeLabel(Label label) {
labels.remove(label);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package de.tum.in.www1.hephaestus.gitprovider.team;

import de.tum.in.www1.hephaestus.gitprovider.label.LabelInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.repository.RepositoryInfoDTO;
import java.util.List;
import org.springframework.lang.NonNull;

public record TeamInfoDTO(@NonNull Long id, @NonNull String name, @NonNull String color) {
public record TeamInfoDTO(
@NonNull Long id,
@NonNull String name,
@NonNull String color,
@NonNull List<RepositoryInfoDTO> repositories,
@NonNull List<LabelInfoDTO> labels
) {
public static TeamInfoDTO fromTeam(Team team) {
return new TeamInfoDTO(team.getId(), team.getName(), team.getColor());
return new TeamInfoDTO(
team.getId(),
team.getName(),
team.getColor(),
team.getRepositories().stream().map(RepositoryInfoDTO::fromRepository).toList(),
team.getLabels().stream().map(LabelInfoDTO::fromLabel).toList()
);
}
}
Loading

0 comments on commit eaf08f2

Please sign in to comment.