Code Scanning, Submission Method, and Code Scanning Server, Client, and Server
Differential scanning is performed through the pre-hook mechanism of the client and server side, and the problem of time-consuming and untimely scanning of existing codes is solved, and a fast and secure code submission process is realized to ensure the quality of the code base.
Patent Information
- Application Number
- CN202111435466.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-29
- Publication Date
- 2025-08-01
- Estimated Expiration
- 2041-11-29
AI Technical Summary
The existing code scanning method is time-consuming and not timely enough, and cannot effectively prevent problem code from entering the code base, and lacks a pre-prevention mechanism.
Through the pre-push hooks and pre-receive hooks on the client and server, obtain code submission information and change files, perform differential scans based on the baseline code, generate and compare scan results, determine new problems and prevent problem code submission.
Significantly reduce scanning time, quickly locate code problems, ensure code security, prevent problematic code from entering the code warehouse, and save labor costs.
Smart Images

Figure CN114371870B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a code scanning and submission method, a code scanning server, a client, and a server. Background Art
[0002] Existing mainstream code scanning methods are all based on scanning the entire code library or the diff between different branches (diff has a different meaning from difference, and here diff represents the difference between codes). By calling tools such as static code scanning tools for code scanning, and then generating a code scanning report.
[0003] For example, a common solution is to trigger a full-scale scan of the code regularly through a scheduled task or a similar method. This method is not timely enough and the full-scale scan is very time-consuming. Another common solution is to perform code checking after the code has been submitted to the system. This is a post-event strategy. After the code push is successful, it is then checked whether there are problems with the code. At this time, the problematic code has already been introduced into the code library. Another common solution is to obtain the incremental code and the target branch during the code push operation, merge them into the complete code, and perform a code scanning operation. This method is equivalent to scanning the entire code library, and is more timely compared to the first-mentioned solution, but still scans the full amount of code and is time-consuming. Summary of the Invention
[0004] In order to solve at least one of the above problems, this application is proposed. According to one aspect of this application, a code scanning method is provided. The method includes: receiving a first code submission message sent by a pre-push hook of a client, where the first code submission message is obtained by the pre-push hook after a user initiates a push operation on the locally submitted code on the client; receiving a second code submission message and a code change file sent by a pre-receive hook of a server, where the second code submission message and the code change file are obtained by the pre-receive hook after receiving the locally submitted code from the client; obtaining a baseline code associated with the code change file based on the first code submission message and the second code submission message, and determining a modified part in the baseline code based on the code change file; scanning the modified part in the baseline code to obtain a first scanning result; adding the code in the code change file to the baseline code to generate the locally submitted code, scanning the code in the code change file to obtain a second scanning result; comparing the first scanning result and the second scanning result to obtain a scanning result of the current push operation.
[0005] Among them, the client includes, but is not limited to, various media that can submit code, such as the linux operating system (centOS, ubuntu, etc.), the windows operating system, the macOS operating system, etc.
[0006] In an embodiment of the present application, the method further includes: after obtaining the scanning result, calling the interface opened in the pre-receive hook to actively notify the pre-receive hook that the scanning is completed, so that the pre-receive hook can obtain the scanning result of the current push operation for determining whether the current push operation is successful; or after obtaining the scanning result, the pre-receive hook obtains the scanning result of the current push operation by polling for determining whether the current push operation is successful.
[0007] In an embodiment of the present application, the comparing the first scanning result and the second scanning result to obtain the scanning result of the current push operation includes: for each program error in the second scanning result, determining whether the code line where the program error is located is an added line relative to the baseline code; if the code line where the program error is located is not an added line relative to the baseline code, determining that the program error is not a newly added program error; if the code line where the program error is located is an added line relative to the baseline code, determining that the program error is a newly added program error, and all the newly added program errors are used as the scanning result of the current push operation.
[0008] In an embodiment of the present application, when the code in the code change file is newly added code that does not exist in the baseline code, there is no modified part and the first scanning result, and the second scanning result is used as the scanning result of the current push operation.
[0009] In an embodiment of the present application, when the code change file indicates that the locally committed code is obtained by deleting some code from the baseline code, code scanning is not performed.
[0010] In an embodiment of the present application, the first code submission information includes a user name, a local submission identifier, a remote submission identifier, and a code repository address; the second code submission information includes a user name, a local submission identifier, a remote submission identifier, and a target code branch name.
[0011] In an embodiment of the present application, the code change file is a compressed file composed of code change files with relative paths.
[0012] In an embodiment of the present application, the baseline code is pre-downloaded from the server and stored.
[0013] In one embodiment of the present application, the method further includes: presenting a one - key push button for a failed push operation, and after the one - key push button is clicked by the user, the locally committed code will be forced to be submitted.
[0014] In one embodiment of the present application, the one - key push button is only available to users with a preset permission level.
[0015] In one embodiment of the present application, the client is a git client, and the server is a gitlab server, a github server, a Gitea code hosting server, a GitkKaken code hosting server, or a Beanstalk code hosting server.
[0016] According to another aspect of the present application, there is provided a code submission method, the method including: when a user initiates a push operation for locally committed code, intercepting information by a configured pre - push hook to obtain first code submission information; sending the first code submission information to a code scanning server, so that the code scanning server executes the above - mentioned code scanning method.
[0017] According to yet another aspect of the present application, there is provided a code submission method, the method including: after receiving locally committed code from a client, intercepting the submission by a configured pre - receive hook to obtain second code submission information and baseline code associated with the locally committed code; determining the code change part based on the locally committed code and the baseline code to generate a code change file; transmitting the second code submission information and the code change file to a code scanning server, so that the code scanning server executes the above - mentioned code scanning method.
[0018] According to still another aspect of the present application, there is provided a code scanning server, which is used to execute the above - mentioned code scanning method.
[0019] According to yet another aspect of the present application, there is provided a client, which is used to execute the above - mentioned code submission method.
[0020] According to still another aspect of the present application, there is provided a server, which is used to execute the above - mentioned code submission method.
[0021] According to another aspect of the present application, there is provided a code submission server, the server comprising: a receiving module, configured to receive first code submission information sent by a pre-push hook of a client, and receive second code submission information and code change files sent by a pre-receive hook of a server side, wherein the first code submission information is obtained by the pre-push hook after the user initiates a push operation on the locally committed code at the client, and the second code submission information and the code change files are obtained by the pre-receive hook after receiving the locally committed code from the client; a scanning module, configured to obtain baseline code associated with the code change files based on the first code submission information and the second code submission information, determine a modified portion in the baseline code based on the code change files, scan the modified portion in the baseline code to obtain a first scanning result, and add the code in the code change files to the baseline code to generate the locally committed code, and scan the code in the code change files to obtain a second scanning result; a comparison module, configured to compare the first scanning result and the second scanning result to obtain a scanning result of the current push operation.
[0022] According to yet another aspect of the present application, there is provided a client, the client comprising: an information intercepting module, configured to intercept information by a configured pre-push hook when the user initiates a push operation on the locally committed code, so as to obtain first code submission information; a sending module, configured to send the first code submission information to a code scanning server, so that the code scanning server executes the above code scanning method.
[0023] According to another aspect of the present application, there is provided a server side, the server side comprising: a submission intercepting module, configured to intercept a submission by a configured pre-receive hook after receiving the locally committed code from the client, so as to obtain second code submission information and baseline code associated with the locally committed code; a generating module, configured to determine a code change portion based on the locally committed code and the baseline code to generate a code change file; a transmitting module, configured to transmit the second code submission information and the code change file to a code scanning server, so that the code scanning server executes the above code scanning method.
[0024] According to yet another aspect of the present application, there is provided a storage medium, on which a computer program is stored, and when the computer program runs, it executes the above code scanning method or code submission method.
[0025] The solution of this application conducts code scanning based on code differences, which greatly reduces the scanning time compared to the full-scale code scanning method. In addition, the solution of this application scans the problems existing in the old and new versions of the differential files, and obtains new problems through algorithms; the new problems are strongly related to this submission, which facilitates the code submitter to quickly locate the problems existing in the modified part of the code, saving time and labor costs and creating value for the enterprise. Moreover, the solution of this application is a prior strategy (before the code is submitted to the server), which can effectively prevent the problem code from being incorporated into the code repository, thus ensuring the security of the code in the code repository. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] By describing the embodiments of the present application in more detail in conjunction with the accompanying drawings, the above and other objects, features, and advantages of the present application will become more apparent. The accompanying drawings are used to provide a further understanding of the embodiments of the present application, and constitute a part of the specification. Together with the embodiments of the present application, they are used to explain the present application and do not constitute a limitation to the present application. In the accompanying drawings, the same reference numerals generally represent the same components or steps.
[0027] Figure 1 FIG. shows a schematic flow chart of a code scanning method according to an embodiment of the present application.
[0028] Figure 2 FIG. shows a schematic flow chart of a code submission method according to an embodiment of the present application.
[0029] Figure 3 FIG. shows a schematic flow chart of a code submission method according to another embodiment of the present application.
[0030] Figure 4 FIG. shows a schematic flow block diagram of a code scanning and submission method according to an embodiment of the present application.
[0031] Figure 5 FIG. shows a schematic structural block diagram of a code scanning server according to an embodiment of the present application.
[0032] Figure 6 FIG. shows a schematic structural block diagram of a client according to an embodiment of the present application.
[0033] Figure 7 FIG. shows a schematic structural block diagram of a server according to an embodiment of the present application. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0034] To make the objectives, technical solutions, and advantages of the present application more apparent, exemplary embodiments according to the present application will be described in detail below with reference to the accompanying drawings. Apparently, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments of the present application. It should be understood that the present application is not limited by the exemplary embodiments described herein. Based on the embodiments of the present application described herein, all other embodiments obtained by those skilled in the art without creative efforts shall fall within the protection scope of the present application.
[0035] The push operation is an operation to push local code to a remote code version control system. When users manage code, they will perform a series of operations such as static scanning of the code. However, the full-scale code scanning results represent a huge workload and long-term improvement for a project. This technology provides a new code solution idea for the pain points of the existing operation modes in the current industry. By scanning the code during the push operation, large problems are precisely divided into small problems for each push, so as to break them down one by one. When programmers push code, they only need to do a relatively small amount of work to ensure that the submitted code is compliant, secure, and has low risks. Since the time to discover a program error (bug) is proportional to the cost of fixing the bug, the earlier a bug is discovered, the lower the cost of fixing it. Therefore, the technical solution of the present application can save a company a large amount of labor costs and improve the quality of products, thus being able to create huge commercial benefits.
[0036] The solution of the present application will be described below with reference to the accompanying drawings in combination with specific embodiments.
[0037] Figure 1 A schematic flowchart of a code scanning method 100 according to an embodiment of the present application. As Figure 1 shown, the code scanning method 100 may include the following steps:
[0038] In step S110, receive a first code submission message sent by a pre-push hook of a client. The first code submission message is obtained by the pre-push hook after a user initiates a push operation for locally submitted code on the client.
[0039] In step S120, receive a second code submission message and a code change file sent by a pre-receive hook of a server. The second code submission message and the code change file are obtained by the pre-receive hook after receiving the locally submitted code from the client.
[0040] In step S130, obtain baseline code associated with the code change file based on the first code submission message and the second code submission message, and determine the modified part in the baseline code based on the code change file.
[0041] In step S140, the modified part in the baseline code is scanned to obtain a first scanning result.
[0042] In step S150, the code in the code change file is added to the baseline code to generate the locally committed code, and the code in the code change file is scanned to obtain a second scanning result.
[0043] In step S160, the first scanning result and the second scanning result are compared to obtain the scanning result of this push operation.
[0044] In the embodiment of the present application, the code scanning method 100 can be executed by a code scanning server, which scans the code when pushing the code.
[0045] Specifically, the user initiates a push operation for the locally committed code on the client (such as a git client). After that, instead of directly transmitting the locally committed code to the server (such as a gitlab server), the pre-push hook on the client intercepts the information to obtain the first code submission information, and transmits the first code submission information to the code scanning server. Here, the first code submission information is information related to this code submission, such as the user name, local-commit_id, remote-commit_id, and code repository address, and is named in this way to distinguish it from the second code submission information described below. After receiving the first code submission information, the code scanning server can determine the baseline code associated with it based on the first code submission information for subsequent operations.
[0046] On the other hand, after the pre-push hook intercepts the information, the locally committed code is uploaded to the server. Instead of directly storing it in the code repository, the pre-receive hook on the server intercepts the submission to obtain the second code submission information. Here, the second code submission information is information related to this code submission, such as the user name, local-commit_id, remote-commit_id, and target code branch name, and is named in this way to distinguish it from the first code submission information described above. In addition, the pre-receive hook also obtains the baseline code associated with the locally committed code, compares the locally committed code with the baseline code to determine the code change part, and generates a code change file based on this (the code change file can be a compressed file with a path to reduce the data transmission volume), and transmits the code change file and the previous second code submission information to the code scanning server.
[0047] Based on the received first code submission information and second code submission information, the code scanning server can obtain the baseline code associated with the code change file. The baseline code can be obtained from a database of the code scanning server, and various baseline codes downloaded in advance using the access token of the project leader and remote-commit_id through the code repository address are stored in this database. This is a scheme of trading space for time, pulling the code library code to the code scanning server in advance, that is, the server retains a copy of the code repository. When performing code scanning, directly switch to the corresponding remote branch at the time of push to reduce the code pulling time. After the code scanning is completed, revert to the code of remote commit id. A relationship table of the repository name, the access token of the project leader, and the code repository address can be maintained for querying and obtaining the baseline code associated with the code change file.
[0048] After obtaining the baseline code associated with the code change file, the code scanning server can determine the modified parts in the baseline code according to the code in the code change file (that is, which parts of the baseline code are modified by the locally committed code relative to the baseline code). Then, the code scanning server can scan the modified parts to obtain a first scanning result, which is the bugs in the parts of the baseline code that will be modified in the future before this push operation. On the other hand, the code scanning server can add the code in the code change file to the baseline code to generate the locally committed code pushed by the user at the client, scan the added code (that is, the code in the code change file), and obtain a second scanning result, which is the bugs in the code submitted in this push operation. Therefore, by comparing the first scanning result and the second scanning result, new bugs can be detected as the scanning result of this push. According to a predetermined rule (such as according to the number and / or type of new bugs), it can be determined whether this push is successful.
[0049] In an embodiment of the present application, step S160 involves how to perform a diff of the scan results. The traditional diff method directly compares the line numbers of the scan results and their problem contents. The defect of this method is as follows: For example, when 10 bugs are scanned in file A, and then file A is modified to obtain file A*. A code segment with 5 bugs is added to file A* neither at the very front nor at the very end. At this time, the diff between files A and A* is compared. The industry usually compares by comparing line numbers and contents, which will cause the line numbers of the bugs after the inserted code segment to change during scanning. During the diff, it will be mistakenly considered that the bugs here and the original bugs are not the same bug, resulting in inaccurate diff results. The best case is that the new code is at the end of the original file A, and the diff result is the most accurate, which is 5 bugs. The worst case is that the new code is at the top of file A, causing the line numbers of all the original ten bugs to change, and all the bugs in the entire file are considered as diff. Based on this, the present application provides a solution: that is, by analyzing the original file A and A*, it is determined which line is the newly added line in A* and which line is the deleted line in A*. If the line where the diff bug is located is a newly added line, then the bug is a newly added bug; if the line where the bug is located is not a newly added line, then it is an original bug, and this original bug needs to be excluded from the diff statistical result.
[0050] Therefore, in an embodiment of the present application, in step S160, comparing the first scan result with the second scan result to obtain the scan result of the current push operation may include: for each program error (bug) in the second scan result, determining whether the code line where the bug is located is a newly added line relative to the baseline code; if the code line where the bug is located is not a newly added line relative to the baseline code, then determining that the bug is not a newly added bug; if the code line where the bug is located is a newly added line relative to the baseline code, then determining that the bug is a newly added bug, and all the newly added bugs are used as the scan result of the current push operation.
[0051] The above scanning process is applied to the following scenarios: The code in the code change file is the result of modifying a certain part of the baseline code, that is, the code in the code change file is the modified code. At this time, the first scanning result mentioned above is the scanning result of the part of the baseline code involved in the modification, and the second scanning result mentioned above is the scanning result of the code in the code change file. In another scenario, when the code in the code change file is code that does not exist in the baseline code, that is, the code in the code change file is newly added code, there is no part of the code in the baseline code that is modified. At this time, excluding the modified part in the baseline code, there is no first scanning result mentioned above. At this time, only the scanning result of this newly added code can be used, and this scanning result is directly used as the scanning result of this push. In yet another scenario, the code change file indicates that some code in the baseline code has been deleted to obtain the locally committed code. At this time, code scanning may not need to be performed. Generally speaking, the modification method (modification, addition, deletion) of the locally committed code relative to the baseline code can be determined according to the code change file. Depending on the different modification methods, the subsequent scanning performed will also be different.
[0052] In an embodiment of the present application, method 100 further includes (not shown): After obtaining the scanning result, call the interface opened in the pre-receive hook to actively notify the pre-receive hook that the scanning is completed, so that the pre-receive hook can obtain the scanning result of this push operation for determining whether this push operation is successful; or after obtaining the scanning result, the pre-receive hook obtains the scanning result of this push operation by polling for determining whether this push operation is successful. Generally speaking, the scanning result of this push operation can be obtained passively or actively by means of polling or interface callback.
[0053] For example, the pre-receive hook on the server side can obtain the scanning result of this push by polling for determining whether this push is successful. Generally, there are three situations: 1) The repository address is not found, then the client has not installed the hook, and the failure message returned is that the specified program is not installed on the client, and the push fails; 2) The repository address exists and the test passes, then continue to execute the subsequent process of the push; 3) The repository address exists but the test fails, then return the test result, the push fails, and an email is sent to the configured user to inform the reason for the push failure.
[0054] Therefore, the solution of this application is code scanning based on code differences, which greatly reduces the scanning time compared with the full-scale code scanning method. In addition, the solution of this application scans the problems existing in the old and new versions of the differential files, and obtains new problems through algorithms; the new problems are strongly related to this submission, which facilitates the code submitter to quickly locate the problems existing in the modified part of the code, saves time and labor costs, and creates value for the enterprise. Moreover, the solution of this application is a prior strategy (before the code is submitted to the server), which can effectively prevent the problem code from being incorporated into the code repository, thus ensuring the security of the code in the code repository.
[0055] In a further embodiment of the present application, method 100 may further include (not shown): presenting a one-key push button for a failed push, and after the one-key push button is clicked by the user, the code submitted locally will be forced to be committed. Among them, the one-key push button is only available to users with a preset permission level. In this embodiment, emergency code storage can be performed through permissions. Specifically, for a push failure caused by the code scan not passing the verification, a one-key push button will be displayed on the page of all pushes on the code scan server for project managers. The operation triggered by this button for this push is to obtain the code branch through the remote_commit_id by the code scan server account, pull the latest code, unzip the compressed package file of the code changes and copy the code in, and add, commit, and push to bypass the code scan server scan. The code conflict problem is the same as the conventional method. After one-key push, the push result is displayed on the page.
[0056] In an embodiment of the present application, the failed push can also be managed as follows: The user clicks "force merge" on the push record on the page and resubmits. On the page, the push records can be retrieved and filtered by user name and time, etc. The "force merge" button on the push is displayed through permission management. Ordinary users only display the code push record and the function of viewing details, and the code repository owner account can display the "force push" button. When the button is clicked, the database background sets the push value to 1. The user repushes on the client side, and the pre-receive calls the interface to query the push value, scans all tasks (jobs) with exactly the same local-commitId, remote-commit Id, and user name. If the flag bit push is null, continue scanning. If the flag bit push is 1, submit without code scanning, and the interface directly returns to skip scanning. The pre-receive operates according to the interface return. If all are null after traversal, then code scanning is performed. Push does not require a setting operation, and it is almost impossible to have two commits with the same commit id and user name. Since the number of push values of 1 is relatively small, in order to speed up the query, the job Id can be iterated to preferentially judge the push value. As the database content increases, the query becomes slower. The test results can be saved for a fixed period, such as one month, etc. If the user's push fails and there is a code conflict and needs to be resubmitted, at this time the commit id changes, and the code check will be performed again.
[0057] The above method 100 describes the code scanning method from the perspective of the code scanning server, and it also describes the cooperation operation between the client and the server. The following will be described with reference to Figure 2 and Figure 3 from the perspective of the client and the server respectively.
[0058] Figure 2 FIG. shows a schematic flowchart of a code submission method 200 according to an embodiment of the present application, which can be executed by a client (such as a git client). As Figure 2 shown, the code submission method 200 may include the following steps:
[0059] In step S210, when the user initiates a push operation for the locally committed code, the configured pre-push hook intercepts information to obtain the first code submission information.
[0060] In step S220, the first code submission information is sent to the code scanning server, so that the code scanning server executes the code scanning method described above.
[0061] The code submission method 200 mainly describes the operations performed by the client, especially the pre-push hook of the client, in the code scanning method of the present application. As described in detail above, it will not be elaborated here.
[0062] Figure 3 FIG. shows a schematic flowchart of a code submission method 300 according to another embodiment of the present application, which can be executed by a server (such as a GitLab server). As Figure 3 shown, the code submission method 300 may include the following steps:
[0063] In step S310, after receiving the locally committed code from the client, the configured pre-receive hook intercepts the submission to obtain the second code submission information and the baseline code associated with the locally committed code.
[0064] In step S320, the changed part of the code is determined based on the locally committed code and the baseline code to generate a code change file.
[0065] In step S330, the second code submission information and the code change file are transmitted to the code scanning server, so that the code scanning server executes the code scanning method described above.
[0066] The code submission method 300 mainly describes the operations performed by the server, especially the pre-receive hook of the server, in the code scanning method of the present application. As described in detail above, it will not be elaborated here.
[0067] Generally, the code scanning and submission methods of the present application involve the operations of the client, the server, and the code scanning server. The detailed process can be understood in combination with Figure 4 which has been described above and will not be elaborated in words here. According to another aspect of the present application, a code scanning server, a client, and a server are also provided, which are respectively used to execute the methods 100, 200, and 300 described above. As described in detail above, it will not be elaborated here. Only the main structure will be described in combination with Figures 5 to 7 here.
[0068] Figure 5 FIG. shows a schematic structural block diagram of a code scanning server 500 according to an embodiment of the present application. As Figure 5As shown in the figure, the server 500 includes: a receiving module 510, configured to receive first code submission information sent by a pre-push hook of a client, and receive second code submission information and code change files sent by a pre-receive hook of a server, where the first code submission information is obtained by the pre-push hook after a user initiates a push operation on locally committed code on the client, and the second code submission information and code change files are obtained by the pre-receive hook after receiving the locally committed code from the client; a scanning module 520, configured to obtain a baseline code associated with the code change files based on the first code submission information and the second code submission information, determine a modified part in the baseline code based on the code change files, scan the modified part in the baseline code to obtain a first scanning result, and add the code in the code change files to the baseline code to generate locally committed code, and scan the code in the code change files to obtain a second scanning result; a comparison module 530, configured to compare the first scanning result and the second scanning result to obtain a scanning result of the current push operation.
[0069] Figure 6 FIG. shows a schematic structural block diagram of a client 600 according to an embodiment of the present application. As Figure 6 shown, the client 600 includes: an information interception module 610, configured to intercept information by a configured pre-push hook when a user initiates a push operation on locally committed code, so as to obtain first code submission information; a sending module 620, configured to send the first code submission information to a code scanning server, so that the code scanning server executes the above code scanning method 100.
[0070] Figure 7 FIG. shows a schematic structural block diagram of a server 700 according to an embodiment of the present application. As Figure 7 shown, the server 700 includes: a submission interception module 710, configured to intercept a submission by a configured pre-receive hook after receiving locally committed code from a client, so as to obtain second code submission information and a baseline code associated with the locally committed code; a generation module 720, configured to determine a code change part based on the locally committed code and the baseline code to generate a code change file; a transmission module 730, configured to transmit the second code submission information and the code change file to a code scanning server, so that the code scanning server executes the above code scanning method 100.
[0071] According to another aspect of the present application, there is also provided a storage medium, on which a computer program is stored, and when the computer program runs, it executes the above code scanning method 100, 200 or 300.
[0072] Based on the above description, the solution of the present application performs code scanning based on code differences, which greatly reduces the scanning time compared to the full-scale code scanning method. In addition, the solution of the present application scans for problems in the old and new versions of the differential files and obtains new problems through algorithms; the new problems are strongly related to the current submission, which facilitates the code submitter to quickly locate the problems existing in the code of the modified part, saving time and labor costs and creating value for the enterprise. Moreover, the solution of the present application is a prior strategy (before the code is submitted to the server), which can effectively prevent problem code from being incorporated into the code repository, thereby ensuring the security of the code in the code repository.
[0073] Although example embodiments have been described herein with reference to the accompanying drawings, it should be understood that the above example embodiments are merely exemplary and are not intended to limit the scope of the present application thereto. Those of ordinary skill in the art can make various changes and modifications therein without departing from the scope and spirit of the present application. All such changes and modifications are intended to be included within the scope of the present application as claimed in the appended claims.
[0074] Those of ordinary skill in the art can realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed herein can be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the present application.
[0075] In several embodiments provided by the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of the units is only a logical function division, and there can be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed.
[0076] In the specification provided herein, a large number of specific details are set forth. However, it can be understood that the embodiments of the present application can be practiced without these specific details. In some instances, well-known methods, structures, and technologies have not been shown in detail so as not to obscure the understanding of this specification.
[0077] Similarly, it should be understood that, in order to streamline this application and assist in understanding one or more of the various inventive aspects, in the description of the exemplary embodiments of this application, the various features of this application are sometimes grouped together into a single embodiment, figure, or description thereof. However, the methods of this application should not be construed as reflecting an intention that the claimed application requires more features than are expressly recited in each claim. Rather, as reflected in the corresponding claims, the inventive point lies in that the corresponding technical problems can be solved with features that are less than all the features of a single disclosed embodiment. Thus, the claims following the detailed description are hereby expressly incorporated into the detailed description, where each claim itself serves as a separate embodiment of this application.
[0078] Those skilled in the art will appreciate that, except where features are mutually exclusive, any combination can be used to combine all the features disclosed in this specification (including the accompanying claims, abstract, and drawings), as well as all the processes or units of any method or apparatus so disclosed. Unless otherwise expressly stated, each feature disclosed in this specification (including the accompanying claims, abstract, and drawings) can be replaced by an alternative feature that serves the same, equivalent, or similar purpose.
[0079] In addition, those skilled in the art will be able to understand that, although some of the embodiments described herein include certain features included in other embodiments rather than other features, the combination of features of different embodiments means that it is within the scope of this application and forms different embodiments. For example, in the claims, any one of the claimed embodiments can be used in any combination.
[0080] The various component embodiments of this application can be implemented in hardware, or in software modules running on one or more processors, or in a combination thereof. Those skilled in the art should understand that a microprocessor or a digital signal processor (DSP) can be used in practice to implement some or all of the functions of some of the modules according to the embodiments of this application. This application can also be implemented as a device program (such as a computer program and a computer program product) for executing part or all of the methods described herein. Such a program implementing this application can be stored on a computer-readable medium, or can be in the form of one or more signals. Such signals can be downloaded from an Internet website, or provided on a carrier signal, or provided in any other form.
[0081] It should be noted that the above embodiments are illustrative of the present application rather than restrictive thereof, and those skilled in the art can design alternative embodiments without departing from the scope of the appended claims. In the claims, any reference signs placed between parentheses shall not be construed as limiting the claim. The word "comprising" does not exclude the presence of elements or steps not listed in the claim. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The present application can be implemented by means of hardware including several different elements and by means of a suitably programmed computer. In a unit claim listing several devices, several of these devices can be embodied by the same item of hardware. The use of the words first, second, and third, etc. does not denote any order. These words can be interpreted as names.
[0082] As described above, the specific embodiments or descriptions of the specific embodiments of the present application are only illustrative, and the protection scope of the present application is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed by the present application, and all such changes or substitutions should be covered within the protection scope of the present application. The protection scope of the present application shall be subject to the protection scope of the claims.
Claims
1. A code scanning method, characterized in that, The method includes: Receiving first code submission information sent by a pre - push hook of a client. The first code submission information is obtained by the pre - push hook intercepting information after the user initiates a push operation on the locally committed code in the client. After the pre - push hook intercepts the information, the locally committed code is uploaded to the server. Receiving second code submission information and code change files sent by a pre - receive hook of the server. The second code submission information and the code change files are obtained by the pre - receive hook of the server after receiving the locally committed code from the client, and the pre - receive hook of the server performs submission interception to obtain the second code submission information. Among them, both the first code submission information and the second code submission information are information related to the current code submission. Obtaining baseline code associated with the code change files based on the first code submission information and the second code submission information, and determining the modified part in the baseline code based on the code change files. The modified part is the part of the locally committed code modified relative to the baseline code. Scanning the modified part in the baseline code to obtain a first scan result. Adding the code in the code change files to the baseline code to generate the locally committed code, and scanning the code in the code change files to obtain a second scan result. Among them, the modification method of the locally committed code relative to the baseline code is determined according to the code change files. Depending on the different modification methods, different scans are performed. When the code in the code change files is new code that does not exist in the baseline code, there is no such modified part and the first scan result, and the second scan result is used as the scan result of this push operation. When the code change files indicate that the locally committed code is obtained by deleting some code from the baseline code, code scanning is not performed. Comparing the first scan result and the second scan result to obtain the scan result of this push operation, specifically including: For each program error in the second scan result, determining whether the code line where the program error is located is a new line relative to the baseline code. If the code line where the program error is located is not a new line relative to the baseline code, determining that the program error is not a newly added program error. If the code line where the program error is located is a new line relative to the baseline code, determining that the program error is a newly added program error, and all the newly added program errors are used as the scan result of this push operation.
2. The method according to claim 1, wherein The method further includes: After obtaining the scan result, calling an interface opened in the pre - receive hook to actively notify the pre - receive hook that the scan is completed, so that the pre - receive hook can obtain the scan result of this push operation for determining whether this push operation is successful; or After obtaining the scan result, the pre-receive hook obtains the scan result of the current push operation in a polling manner to determine whether the current push operation is successful.
3. The method according to claim 1, characterized in that The first code submission information includes a username, a local submission identifier, a remote submission identifier, and a code repository address; The second code submission information includes a username, a local submission identifier, a remote submission identifier, and a target code branch name.
4. The method according to claim 1, wherein The code change file is a compressed file composed of code change files with relative paths.
5. The method according to claim 1, wherein The baseline code is pre-downloaded from the server and stored.
6. The method according to any one of claims 1-5, characterized in that The method further includes: Presenting a one-click push button for the failed push operation, and after the one-click push button is clicked by the user, the code submitted locally will be forced to be committed.
7. The method according to claim 6, wherein The one-click push button is only available to users with a preset permission level.
8. The method according to any one of claims 1-3, characterized in that, The client is a git client, and the server is a gitlab server, a github server, a Gitea code hosting server, a GitkKaken code hosting server, or a Beanstalk code hosting server.
9. A code submission method, characterized in that, The method includes: When the user initiates a push operation for the locally submitted code, the pre-push hook configured intercepts the information to obtain the first code submission information; Sending the first code submission information to the code scanning server, so that the code scanning server executes the code scanning method according to any one of claims 1-8.
10. A code submission method, characterized in that, The method includes: After receiving the locally submitted code from the client, the pre-receive hook configured intercepts the submission to obtain the second code submission information and the baseline code associated with the locally submitted code; Determining the code change part based on the locally submitted code and the baseline code to generate a code change file; Transmitting the second code submission information and the code change file to the code scanning server, so that the code scanning server executes the code scanning method according to any one of claims 1-8.
11. A code scanning server, characterized in that, The code scanning server is used to execute the code scanning method according to any one of claims 1-8.
12. A client, characterized in that, The client is used to execute the code submission method according to claim 9.
13. A server, characterized in that, The server is used to execute the code submission method according to claim 10.
14. A code submission server, characterized in that, The server includes: A receiving module, configured to receive the first code submission information sent by the pre-push hook of the client, and receive the second code submission information and the code change file sent by the pre-receive hook of the server, where the first code submission information is obtained by the pre-push hook intercepting the information after the user initiates a push operation for the locally submitted code on the client, and after the pre-push hook intercepts the information, the locally submitted code is uploaded to the server, the second code submission information and the code change file are obtained by the pre-receive hook after receiving the locally submitted code from the client, and the pre-receive hook of the server intercepts the submission to obtain the second code submission information; A scanning module, configured to obtain a baseline code associated with a code change file based on the first code submission information and the second code submission information, determine a modified portion in the baseline code based on the code change file, where the modified portion is the portion of the locally submitted code modified relative to the baseline code, scan the modified portion in the baseline code to obtain a first scan result, add the code in the code change file to the baseline code to generate the locally submitted code, scan the code in the code change file to obtain a second scan result, where the modification method of the locally submitted code relative to the baseline code is determined according to the code change file, and different scans are performed according to different modification methods. When the code in the code change file is new code that does not exist in the baseline code, there is no such modified portion and the first scan result, and the second scan result is used as the scan result of this push operation. When the code change file indicates that the locally submitted code is obtained by deleting some code from the baseline code, no code scanning is performed; A comparison module, configured to compare the first scan result and the second scan result to obtain the scan result of this push operation, specifically including: For each program error in the second scan result, determine whether the line of code where the program error is located is a new line relative to the baseline code; If the line of code where the program error is located is not a new line relative to the baseline code, determine that the program error is not a newly added program error; If the line of code where the program error is located is a new line relative to the baseline code, determine that the program error is a newly added program error, and all the newly added program errors are used as the scan result of this push operation.
15. A client, characterized in that, The client includes: An information intercepting module, configured to intercept information by a configured pre-push hook when the user initiates a push operation for the locally submitted code to obtain the first code submission information; A sending module, configured to send the first code submission information to a code scanning server, so that the code scanning server executes the code scanning method according to any one of claims 1-8; 16. A server, characterized in that, The server includes: A submission interception module, configured to intercept a submission by a configured pre-receive hook after receiving the locally submitted code from the client to obtain the second code submission information and the baseline code associated with the locally submitted code; A generation module, configured to determine a code change portion based on the locally submitted code and the baseline code to generate a code change file; A transmission module, configured to transmit the second code submission information and the code change file to a code scanning server, so that the code scanning server executes the code scanning method according to any one of claims 1-8.
17. A storage medium, characterized in that, A computer program is stored on the storage medium, and when the computer program runs, it executes the code scanning method described in any one of claims 1-8 or the code submission method described in any one of claims 9-10.
Citation Information
Patent Citations
Static incremental code scanning method and equipment and computer readable storage medium
CN108319854A
Code submitting method and device and electronic equipment
CN111382049A