diff --git a/.github/actions/testing_report_generation/action.yml b/.github/actions/testing_report_generation/action.yml index d88aafe..f50bf4c 100644 --- a/.github/actions/testing_report_generation/action.yml +++ b/.github/actions/testing_report_generation/action.yml @@ -5,15 +5,19 @@ inputs: description: 'access token' required: true issue-labels: - description: 'Testing labels(delimited by comma or space)' + description: 'Testing labels(delimited by comma)' required: true default: 'nightly-testing' + issue-title: + description: 'Title of a Github Issue' + required: true + default: 'Nightly Testing Report' assignee: description: 'Assignee of this issue' required: false default: '' exclude-workflow-files: - description: 'Excluded yml files(delimited by comma or space)' + description: 'Excluded yml files(delimited by comma)' required: false runs: using: 'docker' diff --git a/.github/actions/testing_report_generation/app.rb b/.github/actions/testing_report_generation/app.rb index ac1889e..deea6fd 100644 --- a/.github/actions/testing_report_generation/app.rb +++ b/.github/actions/testing_report_generation/app.rb @@ -4,25 +4,31 @@ require 'optparse' require "json" -REPO_NAME_WITH_OWNER = 'firebase/firebase-ios-sdk'.freeze -REPORT_TESTING_REPO = 'granluo/issue_generations'.freeze -excluded_workflows = [] -issue_labels = [] +pp ENV +REPO_NAME_WITH_OWNER = ENV['GITHUB_REPOSITORY'] +NO_WORKFLOW_RUNNING_INFO = 'All nightly cron job were not run. Please make sure there at least exists one cron job running.'.freeze +EXCLUDED_WORKFLOWS = [] +ISSUE_LABELS = "" +ISSUE_TITLE = "Auto-Generated Testing Report" + if not ENV['INPUT_EXCLUDE-WORKFLOW-FILES'].nil? - excluded_workflows = ENV['INPUT_EXCLUDE-WORKFLOW-FILES'].split(/[ ,]/) + EXCLUDED_WORKFLOWS = ENV['INPUT_EXCLUDE-WORKFLOW-FILES'].split(/[ ,]/) +end +if not ENV['INPUT_ISSUE-LABELS'].nil? + ISSUE_LABELS = ENV['INPUT_ISSUE-LABELS'] end -if not ENV['INPUT_ISSUE_LABELS'].nil? - issue_labels = ENV['INPUT_ISSUE_LABELS'].split(/[ ,]/) +if not ENV['INPUT_ISSUE-TITLE'].nil? + ISSUE_TITLE = ENV['INPUT_ISSUE-TITLE'] end -assignee = ENV['INPUT_ASSIGNEES'] +ASSIGNEE = ENV['INPUT_ASSIGNEES'] class Table def initialize(title) - cur_time = Time.now.utc.localtime("-07:00") + cur_time = Time.now.utc.localtime("-08:00") + @is_empty_table = true @text = String.new "" @text << "# %s\n" % [title] - @text << "Failures are detected in workflow(s)\n" - @text << "This issue is generated at %s\n" % [cur_time.strftime('%m/%d/%Y %H:%M %p') ] + @text << "This issue is generated at %s\n" % [cur_time.strftime('%m/%d/%Y %H:%M %p %:z') ] @text << "| Workflow |" @text << (cur_time - 86400).strftime('%m/%d') + "|" @text << "\n| -------- |" @@ -31,22 +37,31 @@ def initialize(title) end def add_workflow_run_and_result(workflow, result) + @is_empty_table = false record = "| %s | %s |\n" % [workflow, result] @text << record end def get_report() + if @is_empty_table + return nil + end return @text end end -report = Table.new("Nightly Testing Report") +failure_report = Table.new(ISSUE_TITLE) +success_report = Table.new(ISSUE_TITLE) client = Octokit::Client.new(access_token: ENV["INPUT_ACCESS-TOKEN"]) -last_issue = client.list_issues(REPORT_TESTING_REPO, :labels => ENV['INPUT_ISSUE-LABEL'])[0] +last_issue = client.list_issues(REPO_NAME_WITH_OWNER, :labels => ISSUE_LABELS, :state => "all")[0] workflows = client.workflows(REPO_NAME_WITH_OWNER) -puts "Excluded workflow files: " + excluded_workflows.join(",") +puts "Excluded workflow files: " + EXCLUDED_WORKFLOWS.join(",") for wf in workflows.workflows do + # skip if it is the issue generation workflow. + if wf.name == ENV["GITHUB_WORKFLOW"] + next + end workflow_file = File.basename(wf.path) puts workflow_file workflow_text = "[%s](%s)" % [wf.name, wf.html_url] @@ -55,17 +70,34 @@ def get_report() latest_run = runs[0] if latest_run.nil? puts "no schedule runs found." - elsif excluded_workflows.include?(workflow_file) + elsif EXCLUDED_WORKFLOWS.include?(workflow_file) puts workflow_file + " is excluded in the report." elsif Time.now.utc - latest_run.created_at < 86400 - puts latest_run.event + latest_run.html_url + " " + latest_run.created_at.to_s + " " + latest_run.conclusion - result_text = "[%s](%s)" % [latest_run.conclusion, latest_run.html_url] - report.add_workflow_run_and_result(workflow_text, result_text) unless latest_run.conclusion == "success" + result_text = "[%s](%s)" % [latest_run.conclusion.nil? ? "in_process" : latest_run.conclusion, latest_run.html_url] + if latest_run.conclusion == "success" + success_report.add_workflow_run_and_result(workflow_text, result_text) + else + failure_report.add_workflow_run_and_result(workflow_text, result_text) + end end end -if not last_issue.nil? && last_issue.state == "open" - client.add_comment(REPORT_TESTING_REPO, last_issue.number,report.get_report) +# Check if there exists any cron jobs. +if failure_report.get_report.nil? && success_report.get_report.nil? + if last_issue.state == "open" + client.add_comment(REPO_NAME_WITH_OWNER, last_issue.number, NO_WORKFLOW_RUNNING_INFO) + else + client.create_issue(REPO_NAME_WITH_OWNER, ISSUE_TITLE, NO_WORKFLOW_RUNNING_INFO, labels: ISSUE_LABELS, assignee: ASSIGNEE) + end +# Close an issue if all workflows succeed. +elsif failure_report.get_report.nil? and last_issue.state == "open" + client.add_comment(REPO_NAME_WITH_OWNER, last_issue.number, success_report.get_report) + client.close_issue(REPO_NAME_WITH_OWNER, last_issue.number) +# If the last issue is open, then failed report will be commented to the issue. +elsif !last_issue.nil? and last_issue.state == "open" + client.add_comment(REPO_NAME_WITH_OWNER, last_issue.number,failure_report.get_report) + last_issue.add_comment(REPO_NAME_WITH_OWNER, last_issue.number,failure_report.get_report) +# Creat an new issue otherwise. else - new_issue = client.create_issue(REPORT_TESTING_REPO, 'Nightly Testing Report' + Time.now.utc.localtime("-07:00").strftime('%m/%d/%Y %H:%M %p'), report.get_report, labels: issue_labels, assignee: assignee) + client.create_issue(REPO_NAME_WITH_OWNER, ISSUE_TITLE, failure_report.get_report, labels: ISSUE_LABELS, assignee: ASSIGNEE) end diff --git a/.github/workflows/test_prod.yml b/.github/workflows/generate_issues.yml similarity index 83% rename from .github/workflows/test_prod.yml rename to .github/workflows/generate_issues.yml index df20b9b..dba12a0 100644 --- a/.github/workflows/test_prod.yml +++ b/.github/workflows/generate_issues.yml @@ -1,5 +1,7 @@ -name: generate_issue +name: generate a report + on: + workflow_dispatch: pull_request: paths: - '.github/workflows/generate_issues.yml' @@ -7,7 +9,7 @@ on: # Run every day at 4am (PST) - cron uses UTC times - cron: '0 12 * * *' jobs: - hello_world_job: + generate_an_issue: runs-on: ubuntu-latest name: Generate a nightly testing report issue steps: @@ -17,3 +19,4 @@ jobs: with: access-token: '${{ secrets.ISSUE_TEST_TOKEN }}' exclude-workflow-files: 'performance.yml performance-integration-tests.yml' + issue-labels: 'nightly-testing' diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index eac26fa..0000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,18 +0,0 @@ -on: [push] - -jobs: - hello_world_job: - runs-on: ubuntu-latest - name: A job to say hello - steps: - - uses: actions/checkout@v2 - - name: Hello world action step - id: hello - uses: ./.github/actions/hello-docker - with: - who-to-greet: 'Mona the Octocat' - access-token: '${{ secrets.ISSUE_TEST_TOKEN }}' - exclude-workflow-files: 'performance.yml performance-integration-tests.yml' - # Use the output from the `hello` step - - name: Get the output time - run: echo "The time was ${{ steps.hello.outputs.time }}" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index fe403a3..0000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,36 +0,0 @@ -name: test_issue_generation - -on: - pull_request: - paths: - - '.github/workflows/test.yml' -jobs: - get_link: - runs-on: macOS-latest - steps: - - uses: actions/checkout@v2 - - name: Get Job Link - run: | - echo "https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" - echo "https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" >> id_artifact.txt - - uses: actions/upload-artifact@v2 - with: - name: job_log_link - path: id_artifact.txt - dummy_job: - runs-on: macOS-latest - steps: - - uses: actions/checkout@v2 - - name: Get Job Link - run: | - echo "https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" - - uses: actions/download-artifact@v2 - with: - name: job_log_link - - name: Add Link - run: | - echo -e "\nhttps://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" >> id_artifact.txt - - uses: actions/upload-artifact@v2 - with: - name: job_log_link - path: id_artifact.txt