Skip to content

Commit

Permalink
[#944] Fix wrong credit information inherited by annotated author (#2060
Browse files Browse the repository at this point in the history
)

AnnotatorAnalyzer only overwrites the author but not the credit
information, when an author tag is found. If the analyze authorship
flag is enabled, credit information based on the blame author will be
wrongly inherited by the annotated author.

Lets assign partial credit if the annotated author is not the same as
the blame author, and keep the analyzed credit information if the 2
are the same.
  • Loading branch information
SkyBlaise99 committed Nov 12, 2023
1 parent a8b0b19 commit b827cc8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/reposense/authorship/FileInfoAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public FileResult analyzeTextFile(RepoConfiguration config, FileInfo fileInfo, b
aggregateBlameAuthorModifiedAndDateInfo(config, fileInfo, shouldAnalyzeAuthorship);
fileInfo.setFileType(config.getFileType(fileInfo.getPath()));

AnnotatorAnalyzer.aggregateAnnotationAuthorInfo(fileInfo, config.getAuthorConfig());
AnnotatorAnalyzer.aggregateAnnotationAuthorInfo(fileInfo, config.getAuthorConfig(), shouldAnalyzeAuthorship);

if (!config.getAuthorList().isEmpty() && fileInfo.isAllAuthorsIgnored(config.getAuthorList())) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ public class AnnotatorAnalyzer {
*
* @param fileInfo FileInfo to be further analyzed with author annotations.
* @param authorConfig AuthorConfiguration for current analysis.
* @param shouldAnalyzeAuthorship whether credit info needs to be overwritten.
*/
public static void aggregateAnnotationAuthorInfo(FileInfo fileInfo, AuthorConfiguration authorConfig) {
public static void aggregateAnnotationAuthorInfo(FileInfo fileInfo, AuthorConfiguration authorConfig,
boolean shouldAnalyzeAuthorship) {
Optional<Author> currentAnnotatedAuthor = Optional.empty();
Path filePath = Paths.get(fileInfo.getPath());
for (LineInfo lineInfo : fileInfo.getLines()) {
Expand All @@ -60,15 +62,15 @@ public static void aggregateAnnotationAuthorInfo(FileInfo fileInfo, AuthorConfig
boolean isUnknownAuthorSegment = !currentAnnotatedAuthor.isPresent() && !newAnnotatedAuthor.isPresent();

if (isEndOfAnnotatedSegment) {
lineInfo.setAuthor(currentAnnotatedAuthor.get());
lineInfo.updateAuthorAndCredit(currentAnnotatedAuthor.get(), shouldAnalyzeAuthorship);
currentAnnotatedAuthor = Optional.empty();
} else if (isUnknownAuthorSegment) {
currentAnnotatedAuthor = Optional.of(Author.UNKNOWN_AUTHOR);
} else {
currentAnnotatedAuthor = newAnnotatedAuthor.filter(author -> !author.isIgnoringFile(filePath));
}
}
currentAnnotatedAuthor.ifPresent(lineInfo::setAuthor);
currentAnnotatedAuthor.ifPresent(author -> lineInfo.updateAuthorAndCredit(author, shouldAnalyzeAuthorship));
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/reposense/authorship/model/LineInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public void setIsFullCredit(boolean isFullCredit) {
this.isFullCredit = isFullCredit;
}

/**
* If {@code newAuthor} is not the same as current author, then {@code newAuthor} will get partial credit.
* Else nothing happens since the 2 authors are the same, the credit info is retained.
* {@code isFullCredit} is only updated if {@code shouldAnalyzeAuthorship} is set to True.
*/
public void updateAuthorAndCredit(Author newAuthor, boolean shouldAnalyzeAuthorship) {
if (!author.equals(newAuthor)) {
author = newAuthor;

if (shouldAnalyzeAuthorship) {
isFullCredit = false;
}
}
}

@Override
public boolean equals(Object other) {
if (this == other) {
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/reposense/authorship/AuthorshipAnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

public class AuthorshipAnalyzerTest extends GitTestTemplate {
private static final LocalDateTime SINCE_DATE = TestUtil.getSinceDate(2018, Month.JANUARY.getValue(), 1);
private static final LocalDateTime UNTIL_DATE = TestUtil.getUntilDate(2019, Month.DECEMBER.getValue(), 1);
private static final LocalDateTime UNTIL_DATE = TestUtil.getUntilDate(2023, Month.DECEMBER.getValue(), 1);
private static final String TEST_FILENAME = "analyzeAuthorshipTest.java";
private static final String TEST1_FILENAME = "analyzeAuthorshipTest1.java";
private static final String TEST2_FILENAME = "analyzeAuthorshipTest2.java";
private static final String TEST3_FILENAME = "analyzeAuthorshipTest3.java";
private static final String BRANCH_NAME = "945-FileAnalyzerTest-analyzeAuthorship";
private static final CommitHash IGNORE_HASH = new CommitHash("f874c0992645bed626de2113659ce48d7a2233dd");
private static final Author MINGYI_AUTHOR = new Author(MINGYI_AUTHOR_NAME);
private static final Author SHICHEN_AUTHOR = new Author(SHICHEN_AUTHOR_NAME);

private RepoConfiguration config;

Expand All @@ -43,6 +45,7 @@ public void before() throws Exception {

config.addAuthorNamesToAuthorMapEntry(FAKE_AUTHOR, FAKE_AUTHOR_NAME);
config.addAuthorNamesToAuthorMapEntry(MINGYI_AUTHOR, MINGYI_AUTHOR_NAME);
config.addAuthorNamesToAuthorMapEntry(SHICHEN_AUTHOR, SHICHEN_AUTHOR_NAME);
}

@Test
Expand Down Expand Up @@ -147,6 +150,25 @@ public void analyzeAuthorship_sameAuthor_success() {
Assertions.assertTrue(fileInfo.getLine(3).isFullCredit());
}

@Test
public void analyzeAuthorship_annotatedAuthorOverride_success() {
FileInfo fileInfo = analyzeTextFile(TEST3_FILENAME);

// Line 1 - 4 is annotated to ming yi (myteo)
// Partial credit given since the blamed author is not the same as the annotated author
for (int i = 1; i <= 4; i++) {
Assertions.assertEquals(MINGYI_AUTHOR, fileInfo.getLine(i).getAuthor());
Assertions.assertFalse(fileInfo.getLine(i).isFullCredit());
}

// Line 5 - 8 is annotated to shi chen (SkyBlaise)
// Full credit is inherited since the blamed author is the same as the annotated author
for (int i = 5; i <= 8; i++) {
Assertions.assertEquals(SHICHEN_AUTHOR, fileInfo.getLine(i).getAuthor());
Assertions.assertTrue(fileInfo.getLine(i).isFullCredit());
}
}

private FileInfo analyzeTextFile(String relativePath) {
FileInfoExtractor fileInfoExtractor = new FileInfoExtractor();
FileInfo fileInfo = fileInfoExtractor.generateFileInfo(config, relativePath);
Expand Down
1 change: 1 addition & 0 deletions src/test/java/reposense/template/GitTestTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class GitTestTemplate {
protected static final String EUGENE_AUTHOR_NAME = "eugenepeh";
protected static final String YONG_AUTHOR_NAME = "Yong Hao TENG";
protected static final String MINGYI_AUTHOR_NAME = "myteo";
protected static final String SHICHEN_AUTHOR_NAME = "SkyBlaise";
protected static final String JAMES_AUTHOR_NAME = "jamessspanggg";
protected static final String JAMES_ALTERNATIVE_AUTHOR_NAME = "James Pang";
protected static final String JINYAO_AUTHOR_NAME = "jylee-git";
Expand Down

0 comments on commit b827cc8

Please sign in to comment.