Skip to content

Commit

Permalink
feat: add multiple issue generation
Browse files Browse the repository at this point in the history
  • Loading branch information
R3DRUN3 committed Jul 12, 2023
1 parent 0b0bf46 commit d23177d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 25 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ The script requires the following environment variables to be set:
- `TOKEN`: An access token with appropriate permissions to *read* and *open issues* on the target repo. [MANDATORY]
- `OPEN_ISSUE`: If set to `true`, this specify that the script needs to open a "*synchronization issue*" on the target repo, specifying the folder differences. [OPTIONAL]
The opened issues are structured like [this one](https://github.com/R3DRUN3/content-sync-tester/issues/8).
- `MULTIPLE_ISSUES`: If `OPEN_ISSUE` is set to `true` and this var is also set to `true`, the script will create multiple issues, one for every file difference. [OPTIONAL]
> **Warning**
> Be careful when setting the `MULTIPLE_ISSUES` var to true: if you execute this script against two folders with many files, it will create many issues on your target repo.

## How it works

The script performs the following steps:
Expand Down Expand Up @@ -124,7 +129,8 @@ In order to debug the script locally, you can create the `.vscode/launch.json` f
"REPO_FOLDER_1": "<path-of-the-reference-folder-inside-target-repo>",
"REPO_FOLDER_2": "<path-of-the-folder-to-compare-to-the-reference>",
"TOKEN": "<your-github-token-here>",
"OPEN_ISSUE": "false"
"OPEN_ISSUE": "false",
"MULTIPLE_ISSUES": "false"
}
}
]
Expand Down
87 changes: 63 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,37 +186,76 @@ func printFilesSorted(files []*github.RepositoryContent) {
}
}

// Open a synchronization issue on GitHub repository
// Open a synchronization issue on GitHub repository
func openSyncIssue(client *github.Client, repoURL, folder1, folder2 string, diffFiles, newerFiles []*github.RepositoryContent) error {
owner, repo := parseRepoURL(repoURL)
// Check if need to create multiple issues
if os.Getenv("MULTIPLE_ISSUES") == "true" {
for _, file := range diffFiles {
timestamp := time.Now().Format("2006-01-02 15:04:05")
issueTitle := "Synchronization Issue [" + timestamp + "]: " + folder1 + " vs " + folder2
issueBody := "## Synchronization Issue\n\n" +
"Folder1: " + folder1 + "\n\n" +
"Folder2: " + folder2 + "\n\n" +
"### Files present in " + folder1 + " but not in " + folder2 + "\n"
issueBody += "- " + *file.Name + "\n"
issueRequest := &github.IssueRequest{
Title: &issueTitle,
Body: &issueBody,
}
_, _, err := client.Issues.Create(context.Background(), owner, repo, issueRequest)
if err != nil {
return err
}
fmt.Println("\n[ SYNCHRONIZATION ISSUE OPENED ]")
}
for _, file := range newerFiles {
timestamp := time.Now().Format("2006-01-02 15:04:05")
issueTitle := "Synchronization Issue [" + timestamp + "]: " + folder1 + " vs " + folder2
issueBody := "## Synchronization Issue\n\n" +
"Folder1: " + folder1 + "\n\n" +
"Folder2: " + folder2 + "\n\n" +
"### Files present in both " + folder1 + " and " + folder2 + " with newer commits in " + folder1 + "\n"
issueBody += "- " + *file.Name + "\n"
issueRequest := &github.IssueRequest{
Title: &issueTitle,
Body: &issueBody,
}
_, _, err := client.Issues.Create(context.Background(), owner, repo, issueRequest)
if err != nil {
return err
}
fmt.Println("\n[ SYNCHRONIZATION ISSUE OPENED ]")
}
} else { // create a single issue
// Generate timestamp
timestamp := time.Now().Format("2006-01-02 15:04:05")
issueTitle := "Synchronization Issue [" + timestamp + "]: " + folder1 + " vs " + folder2
issueBody := "## Synchronization Issue\n\n" +
"Folder1: " + folder1 + "\n\n" +
"Folder2: " + folder2 + "\n\n" +
"### Files present in " + folder1 + " but not in " + folder2 + "\n"
for _, file := range diffFiles {
issueBody += "- " + *file.Name + "\n"
}

// Generate timestamp
timestamp := time.Now().Format("2006-01-02 15:04:05")
issueTitle := "Synchronization Issue [" + timestamp + "]: " + folder1 + " vs " + folder2
issueBody := "## Synchronization Issue\n\n" +
"Folder1: " + folder1 + "\n\n" +
"Folder2: " + folder2 + "\n\n" +
"### Files present in " + folder1 + " but not in " + folder2 + "\n"
for _, file := range diffFiles {
issueBody += "- " + *file.Name + "\n"
}
issueBody += "\n### Files present in both " + folder1 + " and " + folder2 + " with newer commits in " + folder1 + "\n"
for _, file := range newerFiles {
issueBody += "- " + *file.Name + "\n"
}

issueBody += "\n### Files present in both " + folder1 + " and " + folder2 + " with newer commits in " + folder1 + "\n"
for _, file := range newerFiles {
issueBody += "- " + *file.Name + "\n"
}
issueRequest := &github.IssueRequest{
Title: &issueTitle,
Body: &issueBody,
}

issueRequest := &github.IssueRequest{
Title: &issueTitle,
Body: &issueBody,
}
_, _, err := client.Issues.Create(context.Background(), owner, repo, issueRequest)
if err != nil {
return err
}

_, _, err := client.Issues.Create(context.Background(), owner, repo, issueRequest)
if err != nil {
return err
fmt.Println("\n[ SYNCHRONIZATION ISSUE OPENED ]")
return nil
}

fmt.Println("\n[ SYNCHRONIZATION ISSUE OPENED ]")
return nil
}

0 comments on commit d23177d

Please sign in to comment.